Spaces:
Running
Running
Delete improvements.md
Browse files- improvements.md +0 -587
improvements.md
DELETED
@@ -1,587 +0,0 @@
|
|
1 |
-
# Historical OCR Application Improvements
|
2 |
-
|
3 |
-
Based on a thorough code review of the Historical OCR application, I've identified several areas for improvement to reduce technical debt and enhance the application's functionality, maintainability, and performance.
|
4 |
-
|
5 |
-
## 1. Code Organization and Structure
|
6 |
-
|
7 |
-
### 1.1 Modularize Large Functions
|
8 |
-
Several functions in the codebase are excessively long and handle multiple responsibilities:
|
9 |
-
|
10 |
-
- **Issue**: `process_file()` in ocr_processing.py is over 400 lines and handles file validation, preprocessing, OCR processing, and result formatting.
|
11 |
-
- **Solution**: Break down into smaller, focused functions:
|
12 |
-
```python
|
13 |
-
def process_file(uploaded_file, options):
|
14 |
-
# Validate and prepare file
|
15 |
-
file_info = validate_and_prepare_file(uploaded_file)
|
16 |
-
|
17 |
-
# Apply preprocessing based on document type
|
18 |
-
preprocessed_file = preprocess_document(file_info, options)
|
19 |
-
|
20 |
-
# Perform OCR processing
|
21 |
-
ocr_result = perform_ocr(preprocessed_file, options)
|
22 |
-
|
23 |
-
# Format and enhance results
|
24 |
-
return format_and_enhance_results(ocr_result, file_info)
|
25 |
-
```
|
26 |
-
|
27 |
-
### 1.2 Consistent Error Handling
|
28 |
-
Error handling approaches vary across modules:
|
29 |
-
|
30 |
-
- **Issue**: Some functions use try/except blocks with detailed logging, while others return error dictionaries or raise exceptions.
|
31 |
-
- **Solution**: Implement a consistent error handling strategy:
|
32 |
-
```python
|
33 |
-
class OCRError(Exception):
|
34 |
-
def __init__(self, message, error_code=None, details=None):
|
35 |
-
self.message = message
|
36 |
-
self.error_code = error_code
|
37 |
-
self.details = details
|
38 |
-
super().__init__(self.message)
|
39 |
-
|
40 |
-
def handle_error(func):
|
41 |
-
@functools.wraps(func)
|
42 |
-
def wrapper(*args, **kwargs):
|
43 |
-
try:
|
44 |
-
return func(*args, **kwargs)
|
45 |
-
except OCRError as e:
|
46 |
-
logger.error(f"OCR Error: {e.message} (Code: {e.error_code})")
|
47 |
-
return {"error": e.message, "error_code": e.error_code, "details": e.details}
|
48 |
-
except Exception as e:
|
49 |
-
logger.error(f"Unexpected error: {str(e)}")
|
50 |
-
return {"error": "An unexpected error occurred", "details": str(e)}
|
51 |
-
return wrapper
|
52 |
-
```
|
53 |
-
|
54 |
-
## 2. API Integration and Performance
|
55 |
-
|
56 |
-
### 2.1 API Client Optimization
|
57 |
-
The Mistral API client initialization and usage can be improved:
|
58 |
-
|
59 |
-
- **Issue**: The client is initialized for each request and error handling is duplicated.
|
60 |
-
- **Solution**: Create a singleton API client with centralized error handling:
|
61 |
-
```python
|
62 |
-
class MistralClient:
|
63 |
-
_instance = None
|
64 |
-
|
65 |
-
@classmethod
|
66 |
-
def get_instance(cls, api_key=None):
|
67 |
-
if cls._instance is None:
|
68 |
-
cls._instance = cls(api_key)
|
69 |
-
return cls._instance
|
70 |
-
|
71 |
-
def __init__(self, api_key=None):
|
72 |
-
self.api_key = api_key or os.environ.get("MISTRAL_API_KEY", "")
|
73 |
-
self.client = Mistral(api_key=self.api_key)
|
74 |
-
|
75 |
-
def process_ocr(self, document, **kwargs):
|
76 |
-
try:
|
77 |
-
return self.client.ocr.process(document=document, **kwargs)
|
78 |
-
except Exception as e:
|
79 |
-
# Centralized error handling
|
80 |
-
return self._handle_api_error(e)
|
81 |
-
```
|
82 |
-
|
83 |
-
### 2.2 Caching Strategy
|
84 |
-
The current caching approach can be improved:
|
85 |
-
|
86 |
-
- **Issue**: Cache keys don't always account for all relevant parameters, and TTL is fixed at 24 hours.
|
87 |
-
- **Solution**: Implement a more sophisticated caching strategy:
|
88 |
-
```python
|
89 |
-
def generate_cache_key(file_content, options):
|
90 |
-
# Create a comprehensive hash of all relevant parameters
|
91 |
-
options_str = json.dumps(options, sort_keys=True)
|
92 |
-
content_hash = hashlib.md5(file_content).hexdigest()
|
93 |
-
return f"{content_hash}_{hashlib.md5(options_str.encode()).hexdigest()}"
|
94 |
-
|
95 |
-
# Adaptive TTL based on document type
|
96 |
-
def get_cache_ttl(document_type):
|
97 |
-
ttl_map = {
|
98 |
-
"handwritten": 48 * 3600, # 48 hours for handwritten docs
|
99 |
-
"newspaper": 24 * 3600, # 24 hours for newspapers
|
100 |
-
"standard": 12 * 3600 # 12 hours for standard docs
|
101 |
-
}
|
102 |
-
return ttl_map.get(document_type, 24 * 3600)
|
103 |
-
```
|
104 |
-
|
105 |
-
## 3. State Management
|
106 |
-
|
107 |
-
### 3.1 Streamlit Session State
|
108 |
-
The application uses a complex state management approach:
|
109 |
-
|
110 |
-
- **Issue**: Many session state variables with unclear relationships and reset logic.
|
111 |
-
- **Solution**: Implement a more structured state management approach:
|
112 |
-
```python
|
113 |
-
class DocumentState:
|
114 |
-
def __init__(self):
|
115 |
-
self.document = None
|
116 |
-
self.original_bytes = None
|
117 |
-
self.name = None
|
118 |
-
self.mime_type = None
|
119 |
-
self.is_sample = False
|
120 |
-
self.processed = False
|
121 |
-
self.temp_files = []
|
122 |
-
|
123 |
-
def reset(self):
|
124 |
-
# Clean up temp files
|
125 |
-
for temp_file in self.temp_files:
|
126 |
-
if os.path.exists(temp_file):
|
127 |
-
os.unlink(temp_file)
|
128 |
-
|
129 |
-
# Reset state
|
130 |
-
self.__init__()
|
131 |
-
|
132 |
-
# Initialize in session state
|
133 |
-
if 'document_state' not in st.session_state:
|
134 |
-
st.session_state.document_state = DocumentState()
|
135 |
-
```
|
136 |
-
|
137 |
-
### 3.2 Result History Management
|
138 |
-
The current approach to managing result history can be improved:
|
139 |
-
|
140 |
-
- **Issue**: Results are stored directly in session state with limited management.
|
141 |
-
- **Solution**: Create a dedicated class for result history:
|
142 |
-
```python
|
143 |
-
class ResultHistory:
|
144 |
-
def __init__(self, max_results=20):
|
145 |
-
self.results = []
|
146 |
-
self.max_results = max_results
|
147 |
-
|
148 |
-
def add_result(self, result):
|
149 |
-
# Add timestamp and ensure result is serializable
|
150 |
-
result = self._prepare_result(result)
|
151 |
-
self.results.insert(0, result)
|
152 |
-
|
153 |
-
# Trim to max size
|
154 |
-
if len(self.results) > self.max_results:
|
155 |
-
self.results = self.results[:self.max_results]
|
156 |
-
|
157 |
-
def _prepare_result(self, result):
|
158 |
-
# Add timestamp and ensure result is serializable
|
159 |
-
result = result.copy()
|
160 |
-
result['timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
161 |
-
|
162 |
-
# Ensure result is serializable
|
163 |
-
return json.loads(json.dumps(result, default=str))
|
164 |
-
```
|
165 |
-
|
166 |
-
## 4. Image Processing Pipeline
|
167 |
-
|
168 |
-
### 4.1 Preprocessing Configuration
|
169 |
-
The preprocessing configuration can be improved:
|
170 |
-
|
171 |
-
- **Issue**: Preprocessing options are scattered across different parts of the code.
|
172 |
-
- **Solution**: Create a centralized preprocessing configuration:
|
173 |
-
```python
|
174 |
-
PREPROCESSING_CONFIGS = {
|
175 |
-
"standard": {
|
176 |
-
"grayscale": True,
|
177 |
-
"denoise": True,
|
178 |
-
"contrast": 5,
|
179 |
-
"deskew": True
|
180 |
-
},
|
181 |
-
"handwritten": {
|
182 |
-
"grayscale": True,
|
183 |
-
"denoise": True,
|
184 |
-
"contrast": 10,
|
185 |
-
"deskew": True,
|
186 |
-
"adaptive_threshold": {
|
187 |
-
"block_size": 21,
|
188 |
-
"constant": 5
|
189 |
-
}
|
190 |
-
},
|
191 |
-
"newspaper": {
|
192 |
-
"grayscale": True,
|
193 |
-
"denoise": True,
|
194 |
-
"contrast": 5,
|
195 |
-
"deskew": True,
|
196 |
-
"column_detection": True
|
197 |
-
}
|
198 |
-
}
|
199 |
-
```
|
200 |
-
|
201 |
-
### 4.2 Image Segmentation
|
202 |
-
The image segmentation approach can be improved:
|
203 |
-
|
204 |
-
- **Issue**: Segmentation is optional and not well-integrated with the preprocessing pipeline.
|
205 |
-
- **Solution**: Make segmentation a standard part of the preprocessing pipeline for certain document types:
|
206 |
-
```python
|
207 |
-
def preprocess_document(file_info, options):
|
208 |
-
# Apply basic preprocessing
|
209 |
-
preprocessed_file = apply_basic_preprocessing(file_info, options)
|
210 |
-
|
211 |
-
# Apply segmentation for specific document types
|
212 |
-
if options["document_type"] in ["newspaper", "book", "multi_column"]:
|
213 |
-
return apply_segmentation(preprocessed_file, options)
|
214 |
-
|
215 |
-
return preprocessed_file
|
216 |
-
```
|
217 |
-
|
218 |
-
## 5. User Experience Enhancements
|
219 |
-
|
220 |
-
### 5.1 Progressive Loading
|
221 |
-
Improve the user experience during processing:
|
222 |
-
|
223 |
-
- **Issue**: The UI can appear frozen during long-running operations.
|
224 |
-
- **Solution**: Implement progressive loading and feedback:
|
225 |
-
```python
|
226 |
-
def process_with_feedback(file, options, progress_callback):
|
227 |
-
# Update progress at each step
|
228 |
-
progress_callback(10, "Validating document...")
|
229 |
-
file_info = validate_and_prepare_file(file)
|
230 |
-
|
231 |
-
progress_callback(30, "Preprocessing document...")
|
232 |
-
preprocessed_file = preprocess_document(file_info, options)
|
233 |
-
|
234 |
-
progress_callback(50, "Performing OCR...")
|
235 |
-
ocr_result = perform_ocr(preprocessed_file, options)
|
236 |
-
|
237 |
-
progress_callback(80, "Enhancing results...")
|
238 |
-
final_result = format_and_enhance_results(ocr_result, file_info)
|
239 |
-
|
240 |
-
progress_callback(100, "Complete!")
|
241 |
-
return final_result
|
242 |
-
```
|
243 |
-
|
244 |
-
### 5.2 Result Visualization
|
245 |
-
Enhance the visualization of OCR results:
|
246 |
-
|
247 |
-
- **Issue**: Results are displayed in a basic format with limited visualization.
|
248 |
-
- **Solution**: Implement enhanced visualization options:
|
249 |
-
```python
|
250 |
-
def display_enhanced_results(result):
|
251 |
-
# Create tabs for different views
|
252 |
-
tabs = st.tabs(["Text", "Annotated", "Side-by-Side", "JSON"])
|
253 |
-
|
254 |
-
with tabs[0]:
|
255 |
-
# Display formatted text
|
256 |
-
st.markdown(format_ocr_text(result["ocr_contents"]["raw_text"]))
|
257 |
-
|
258 |
-
with tabs[1]:
|
259 |
-
# Display annotated image with bounding boxes
|
260 |
-
display_annotated_image(result)
|
261 |
-
|
262 |
-
with tabs[2]:
|
263 |
-
# Display side-by-side comparison
|
264 |
-
col1, col2 = st.columns(2)
|
265 |
-
with col1:
|
266 |
-
st.image(result["original_image"])
|
267 |
-
with col2:
|
268 |
-
st.markdown(format_ocr_text(result["ocr_contents"]["raw_text"]))
|
269 |
-
|
270 |
-
with tabs[3]:
|
271 |
-
# Display raw JSON
|
272 |
-
st.json(result)
|
273 |
-
```
|
274 |
-
|
275 |
-
## 6. Testing and Reliability
|
276 |
-
|
277 |
-
### 6.1 Automated Testing
|
278 |
-
Implement comprehensive testing:
|
279 |
-
|
280 |
-
- **Issue**: Limited or no automated testing.
|
281 |
-
- **Solution**: Implement unit and integration tests:
|
282 |
-
```python
|
283 |
-
# Unit test for preprocessing
|
284 |
-
def test_preprocess_image():
|
285 |
-
# Test with various document types
|
286 |
-
for doc_type in ["standard", "handwritten", "newspaper"]:
|
287 |
-
# Load test image
|
288 |
-
with open(f"test_data/{doc_type}_sample.jpg", "rb") as f:
|
289 |
-
image_bytes = f.read()
|
290 |
-
|
291 |
-
# Apply preprocessing
|
292 |
-
options = {"document_type": doc_type, "grayscale": True, "denoise": True}
|
293 |
-
result = preprocess_image(image_bytes, options)
|
294 |
-
|
295 |
-
# Assert result is not None and different from original
|
296 |
-
assert result is not None
|
297 |
-
assert result != image_bytes
|
298 |
-
```
|
299 |
-
|
300 |
-
### 6.2 Error Recovery
|
301 |
-
Implement better error recovery mechanisms:
|
302 |
-
|
303 |
-
- **Issue**: Errors in one part of the pipeline can cause the entire process to fail.
|
304 |
-
- **Solution**: Implement graceful degradation:
|
305 |
-
```python
|
306 |
-
def process_with_fallbacks(file, options):
|
307 |
-
try:
|
308 |
-
# Try full processing pipeline
|
309 |
-
return full_processing_pipeline(file, options)
|
310 |
-
except OCRError as e:
|
311 |
-
logger.warning(f"Full pipeline failed: {e.message}. Trying simplified pipeline.")
|
312 |
-
try:
|
313 |
-
# Try simplified pipeline
|
314 |
-
return simplified_processing_pipeline(file, options)
|
315 |
-
except Exception as e2:
|
316 |
-
logger.error(f"Simplified pipeline failed: {str(e2)}. Falling back to basic OCR.")
|
317 |
-
# Fall back to basic OCR
|
318 |
-
return basic_ocr_only(file)
|
319 |
-
```
|
320 |
-
|
321 |
-
## 7. Documentation and Maintainability
|
322 |
-
|
323 |
-
### 7.1 Code Documentation
|
324 |
-
Improve code documentation:
|
325 |
-
|
326 |
-
- **Issue**: Inconsistent documentation across modules.
|
327 |
-
- **Solution**: Implement consistent docstring format and add module-level documentation:
|
328 |
-
```python
|
329 |
-
"""
|
330 |
-
OCR Processing Module
|
331 |
-
|
332 |
-
This module handles the core OCR processing functionality, including:
|
333 |
-
- File validation and preparation
|
334 |
-
- Image preprocessing
|
335 |
-
- OCR processing with Mistral AI
|
336 |
-
- Result formatting and enhancement
|
337 |
-
|
338 |
-
The main entry point is the `process_file` function.
|
339 |
-
"""
|
340 |
-
|
341 |
-
def process_file(file, options):
|
342 |
-
"""
|
343 |
-
Process a file with OCR.
|
344 |
-
|
345 |
-
Args:
|
346 |
-
file: The file to process (UploadedFile or bytes)
|
347 |
-
options: Dictionary of processing options
|
348 |
-
- document_type: Type of document (standard, handwritten, etc.)
|
349 |
-
- preprocessing: Dictionary of preprocessing options
|
350 |
-
- use_vision: Whether to use vision model
|
351 |
-
|
352 |
-
Returns:
|
353 |
-
Dictionary containing OCR results and metadata
|
354 |
-
|
355 |
-
Raises:
|
356 |
-
OCRError: If OCR processing fails
|
357 |
-
"""
|
358 |
-
# Implementation
|
359 |
-
```
|
360 |
-
|
361 |
-
### 7.2 Configuration Management
|
362 |
-
Improve configuration management:
|
363 |
-
|
364 |
-
- **Issue**: Configuration is scattered across multiple files.
|
365 |
-
- **Solution**: Implement a centralized configuration system:
|
366 |
-
```python
|
367 |
-
"""
|
368 |
-
Configuration Module
|
369 |
-
|
370 |
-
This module provides a centralized configuration system for the application.
|
371 |
-
"""
|
372 |
-
|
373 |
-
import os
|
374 |
-
import yaml
|
375 |
-
from pathlib import Path
|
376 |
-
|
377 |
-
class Config:
|
378 |
-
_instance = None
|
379 |
-
|
380 |
-
@classmethod
|
381 |
-
def get_instance(cls):
|
382 |
-
if cls._instance is None:
|
383 |
-
cls._instance = cls()
|
384 |
-
return cls._instance
|
385 |
-
|
386 |
-
def __init__(self):
|
387 |
-
self.config = {}
|
388 |
-
self.load_config()
|
389 |
-
|
390 |
-
def load_config(self):
|
391 |
-
# Load from config file
|
392 |
-
config_path = Path(__file__).parent / "config.yaml"
|
393 |
-
if config_path.exists():
|
394 |
-
with open(config_path, "r") as f:
|
395 |
-
self.config = yaml.safe_load(f)
|
396 |
-
|
397 |
-
# Override with environment variables
|
398 |
-
for key, value in os.environ.items():
|
399 |
-
if key.startswith("OCR_"):
|
400 |
-
config_key = key[4:].lower()
|
401 |
-
self.config[config_key] = value
|
402 |
-
|
403 |
-
def get(self, key, default=None):
|
404 |
-
return self.config.get(key, default)
|
405 |
-
```
|
406 |
-
|
407 |
-
## 8. Security Enhancements
|
408 |
-
|
409 |
-
### 8.1 API Key Management
|
410 |
-
Improve API key management:
|
411 |
-
|
412 |
-
- **Issue**: API keys are stored in environment variables with limited validation.
|
413 |
-
- **Solution**: Implement secure API key management:
|
414 |
-
```python
|
415 |
-
def get_api_key():
|
416 |
-
# Try to get from secure storage first
|
417 |
-
api_key = get_from_secure_storage("mistral_api_key")
|
418 |
-
|
419 |
-
# Fall back to environment variable
|
420 |
-
if not api_key:
|
421 |
-
api_key = os.environ.get("MISTRAL_API_KEY", "")
|
422 |
-
|
423 |
-
# Validate key format
|
424 |
-
if api_key and not re.match(r'^[A-Za-z0-9_-]{30,}$', api_key):
|
425 |
-
logger.warning("API key format appears invalid")
|
426 |
-
|
427 |
-
return api_key
|
428 |
-
```
|
429 |
-
|
430 |
-
### 8.2 Input Validation
|
431 |
-
Improve input validation:
|
432 |
-
|
433 |
-
- **Issue**: Limited validation of user inputs.
|
434 |
-
- **Solution**: Implement comprehensive input validation:
|
435 |
-
```python
|
436 |
-
def validate_file(file):
|
437 |
-
# Check file size
|
438 |
-
if len(file.getvalue()) > MAX_FILE_SIZE:
|
439 |
-
raise OCRError("File too large", "FILE_TOO_LARGE")
|
440 |
-
|
441 |
-
# Check file type
|
442 |
-
file_type = get_file_type(file)
|
443 |
-
if file_type not in ALLOWED_FILE_TYPES:
|
444 |
-
raise OCRError(f"Unsupported file type: {file_type}", "UNSUPPORTED_FILE_TYPE")
|
445 |
-
|
446 |
-
# Check for malicious content
|
447 |
-
if is_potentially_malicious(file):
|
448 |
-
raise OCRError("File appears to be malicious", "SECURITY_RISK")
|
449 |
-
|
450 |
-
return file_type
|
451 |
-
```
|
452 |
-
|
453 |
-
## 9. Performance Optimizations
|
454 |
-
|
455 |
-
### 9.1 Parallel Processing
|
456 |
-
Implement parallel processing for multi-page documents:
|
457 |
-
|
458 |
-
- **Issue**: Pages are processed sequentially, which can be slow for large documents.
|
459 |
-
- **Solution**: Implement parallel processing:
|
460 |
-
```python
|
461 |
-
def process_pdf_pages(pdf_path, options):
|
462 |
-
# Extract pages
|
463 |
-
pages = extract_pdf_pages(pdf_path)
|
464 |
-
|
465 |
-
# Process pages in parallel
|
466 |
-
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
|
467 |
-
future_to_page = {executor.submit(process_page, page, options): i
|
468 |
-
for i, page in enumerate(pages)}
|
469 |
-
|
470 |
-
results = []
|
471 |
-
for future in concurrent.futures.as_completed(future_to_page):
|
472 |
-
page_idx = future_to_page[future]
|
473 |
-
try:
|
474 |
-
result = future.result()
|
475 |
-
results.append((page_idx, result))
|
476 |
-
except Exception as e:
|
477 |
-
logger.error(f"Error processing page {page_idx}: {str(e)}")
|
478 |
-
|
479 |
-
# Sort results by page index
|
480 |
-
results.sort(key=lambda x: x[0])
|
481 |
-
|
482 |
-
# Combine results
|
483 |
-
return combine_page_results([r[1] for r in results])
|
484 |
-
```
|
485 |
-
|
486 |
-
### 9.2 Resource Management
|
487 |
-
Improve resource management:
|
488 |
-
|
489 |
-
- **Issue**: Temporary files are not always cleaned up properly.
|
490 |
-
- **Solution**: Implement better resource management:
|
491 |
-
```python
|
492 |
-
class TempFileManager:
|
493 |
-
def __init__(self):
|
494 |
-
self.temp_files = []
|
495 |
-
|
496 |
-
def create_temp_file(self, content, suffix=".tmp"):
|
497 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
498 |
-
tmp.write(content)
|
499 |
-
self.temp_files.append(tmp.name)
|
500 |
-
return tmp.name
|
501 |
-
|
502 |
-
def cleanup(self):
|
503 |
-
for temp_file in self.temp_files:
|
504 |
-
try:
|
505 |
-
if os.path.exists(temp_file):
|
506 |
-
os.unlink(temp_file)
|
507 |
-
except Exception as e:
|
508 |
-
logger.warning(f"Failed to remove temp file {temp_file}: {str(e)}")
|
509 |
-
self.temp_files = []
|
510 |
-
|
511 |
-
def __enter__(self):
|
512 |
-
return self
|
513 |
-
|
514 |
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
515 |
-
self.cleanup()
|
516 |
-
```
|
517 |
-
|
518 |
-
## 10. Extensibility
|
519 |
-
|
520 |
-
### 10.1 Plugin System
|
521 |
-
Implement a plugin system for extensibility:
|
522 |
-
|
523 |
-
- **Issue**: Adding new document types or processing methods requires code changes.
|
524 |
-
- **Solution**: Implement a plugin system:
|
525 |
-
```python
|
526 |
-
class OCRPlugin:
|
527 |
-
def __init__(self, name, description):
|
528 |
-
self.name = name
|
529 |
-
self.description = description
|
530 |
-
|
531 |
-
def can_handle(self, file_info):
|
532 |
-
"""Return True if this plugin can handle the file"""
|
533 |
-
raise NotImplementedError
|
534 |
-
|
535 |
-
def process(self, file_info, options):
|
536 |
-
"""Process the file and return results"""
|
537 |
-
raise NotImplementedError
|
538 |
-
|
539 |
-
# Example plugin
|
540 |
-
class HandwrittenDocumentPlugin(OCRPlugin):
|
541 |
-
def __init__(self):
|
542 |
-
super().__init__("handwritten", "Handwritten document processor")
|
543 |
-
|
544 |
-
def can_handle(self, file_info):
|
545 |
-
# Check if this is a handwritten document
|
546 |
-
return file_info.get("document_type") == "handwritten"
|
547 |
-
|
548 |
-
def process(self, file_info, options):
|
549 |
-
# Specialized processing for handwritten documents
|
550 |
-
# ...
|
551 |
-
```
|
552 |
-
|
553 |
-
### 10.2 API Abstraction
|
554 |
-
Create an abstraction layer for the OCR API:
|
555 |
-
|
556 |
-
- **Issue**: The application is tightly coupled to the Mistral AI API.
|
557 |
-
- **Solution**: Implement an abstraction layer:
|
558 |
-
```python
|
559 |
-
class OCRProvider:
|
560 |
-
def process_image(self, image_path, options):
|
561 |
-
"""Process an image and return OCR results"""
|
562 |
-
raise NotImplementedError
|
563 |
-
|
564 |
-
def process_pdf(self, pdf_path, options):
|
565 |
-
"""Process a PDF and return OCR results"""
|
566 |
-
raise NotImplementedError
|
567 |
-
|
568 |
-
class MistralOCRProvider(OCRProvider):
|
569 |
-
def __init__(self, api_key=None):
|
570 |
-
self.client = MistralClient.get_instance(api_key)
|
571 |
-
|
572 |
-
def process_image(self, image_path, options):
|
573 |
-
# Implementation using Mistral API
|
574 |
-
|
575 |
-
def process_pdf(self, pdf_path, options):
|
576 |
-
# Implementation using Mistral API
|
577 |
-
|
578 |
-
# Factory function to get the appropriate provider
|
579 |
-
def get_ocr_provider(provider_name="mistral"):
|
580 |
-
if provider_name == "mistral":
|
581 |
-
return MistralOCRProvider()
|
582 |
-
# Add more providers as needed
|
583 |
-
raise ValueError(f"Unknown OCR provider: {provider_name}")
|
584 |
-
```
|
585 |
-
|
586 |
-
## Implementation Priority
|
587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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