Update handler.py
Browse files- handler.py +22 -15
handler.py
CHANGED
|
@@ -51,26 +51,33 @@ class EndpointHandler:
|
|
| 51 |
image_data = base64.b64decode(base64_string)
|
| 52 |
image = Image.open(BytesIO(image_data))
|
| 53 |
|
| 54 |
-
|
| 55 |
if image.mode != 'RGB':
|
| 56 |
image = image.convert('RGB')
|
| 57 |
|
| 58 |
-
|
| 59 |
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error processing image: {e}")
|
|
|
|
| 51 |
image_data = base64.b64decode(base64_string)
|
| 52 |
image = Image.open(BytesIO(image_data))
|
| 53 |
|
| 54 |
+
# Convert to RGB if necessary (handles PNG, JPEG, etc.)
|
| 55 |
if image.mode != 'RGB':
|
| 56 |
image = image.convert('RGB')
|
| 57 |
|
| 58 |
+
# Define the prompt for Markdown conversion
|
| 59 |
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 60 |
|
| 61 |
+
# Run OCR inference
|
| 62 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 63 |
+
output_path = temp_dir # Now output_path is defined
|
| 64 |
+
|
| 65 |
+
# Define the prompt for Markdown conversion
|
| 66 |
+
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 67 |
+
|
| 68 |
+
# Run OCR inference
|
| 69 |
+
result = self.model.infer(
|
| 70 |
+
self.tokenizer,
|
| 71 |
+
prompt=prompt,
|
| 72 |
+
image_file=image,
|
| 73 |
+
output_path=output_path, # Use temporary directory
|
| 74 |
+
base_size=1024,
|
| 75 |
+
image_size=640,
|
| 76 |
+
crop_mode=True,
|
| 77 |
+
save_results=True # Save to temporary directory
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return result
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
print(f"Error processing image: {e}")
|