Update handler.py
Browse files- handler.py +17 -13
handler.py
CHANGED
|
@@ -5,6 +5,7 @@ import base64
|
|
| 5 |
from io import BytesIO
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
class EndpointHandler:
|
| 10 |
def __init__(self, model_dir = 'deepseek-ai/DeepSeek-OCR'):
|
|
@@ -77,19 +78,22 @@ class EndpointHandler:
|
|
| 77 |
# Define the prompt for Markdown conversion
|
| 78 |
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error processing image: {e}")
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
class EndpointHandler:
|
| 11 |
def __init__(self, model_dir = 'deepseek-ai/DeepSeek-OCR'):
|
|
|
|
| 78 |
# Define the prompt for Markdown conversion
|
| 79 |
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 80 |
|
| 81 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 82 |
+
print(f"Using temporary directory: {temp_dir}")
|
| 83 |
+
|
| 84 |
+
# Run OCR inference
|
| 85 |
+
result = self.model.infer(
|
| 86 |
+
self.tokenizer,
|
| 87 |
+
prompt=prompt,
|
| 88 |
+
image_file=image, # Pass the PIL Image object directly
|
| 89 |
+
output_path=temp_dir,
|
| 90 |
+
base_size=1024,
|
| 91 |
+
image_size=640,
|
| 92 |
+
crop_mode=True,
|
| 93 |
+
save_results=False
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return result
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error processing image: {e}")
|