Update handler.py
Browse files- handler.py +16 -25
handler.py
CHANGED
|
@@ -6,9 +6,6 @@ from io import BytesIO
|
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
import tempfile
|
| 9 |
-
import sys
|
| 10 |
-
from io import StringIO
|
| 11 |
-
from contextlib import redirect_stdout
|
| 12 |
|
| 13 |
class EndpointHandler:
|
| 14 |
def __init__(self, model_dir = 'deepseek-ai/DeepSeek-OCR'):
|
|
@@ -82,7 +79,7 @@ class EndpointHandler:
|
|
| 82 |
print(f"Image saved to: {image_path}")
|
| 83 |
|
| 84 |
# Verify the image can be opened
|
| 85 |
-
output_file = os.path.join(temp_dir, "output.md")
|
| 86 |
try:
|
| 87 |
test_image = Image.open(image_path)
|
| 88 |
if test_image.mode != 'RGB':
|
|
@@ -92,28 +89,22 @@ class EndpointHandler:
|
|
| 92 |
except Exception as img_error:
|
| 93 |
return {"error": f"Invalid image: {str(img_error)}"}
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
self.tokenizer,
|
| 102 |
-
prompt=prompt,
|
| 103 |
-
image_file=image_path, # Pass the PIL Image object directly
|
| 104 |
-
output_path=output_file,
|
| 105 |
-
base_size=1024,
|
| 106 |
-
image_size=640,
|
| 107 |
-
crop_mode=True,
|
| 108 |
-
save_results=True
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
captured_output = output_buffer.getvalue()
|
| 112 |
-
|
| 113 |
-
print(f"Captured output length: {len(captured_output)}")
|
| 114 |
-
print(f"First 500 chars of captured output: {captured_output[:500]}")
|
| 115 |
-
|
| 116 |
-
return captured_output
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
print(f"Error processing image: {e}")
|
|
|
|
| 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'):
|
|
|
|
| 79 |
print(f"Image saved to: {image_path}")
|
| 80 |
|
| 81 |
# Verify the image can be opened
|
| 82 |
+
output_file = os.path.join(temp_dir, "output-1.md")
|
| 83 |
try:
|
| 84 |
test_image = Image.open(image_path)
|
| 85 |
if test_image.mode != 'RGB':
|
|
|
|
| 89 |
except Exception as img_error:
|
| 90 |
return {"error": f"Invalid image: {str(img_error)}"}
|
| 91 |
|
| 92 |
+
# Run OCR inference
|
| 93 |
+
result = self.model.infer(
|
| 94 |
+
self.tokenizer,
|
| 95 |
+
prompt=prompt,
|
| 96 |
+
image_file=image_path, # Pass the PIL Image object directly
|
| 97 |
+
output_path=output_file,
|
| 98 |
+
base_size=1024,
|
| 99 |
+
image_size=640,
|
| 100 |
+
crop_mode=True,
|
| 101 |
+
save_results=True
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
if os.path.exists(output_file):
|
| 105 |
+
with open(output_file, 'r', encoding='utf-8') as f:
|
| 106 |
+
content = f.read()
|
| 107 |
+
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Error processing image: {e}")
|