Update handler.py
Browse files- handler.py +17 -7
handler.py
CHANGED
|
@@ -69,19 +69,29 @@ class EndpointHandler:
|
|
| 69 |
|
| 70 |
# Decode base64 to image
|
| 71 |
image_data = base64.b64decode(base64_string)
|
| 72 |
-
image = Image.open(BytesIO(image_data))
|
| 73 |
-
|
| 74 |
-
# Convert to RGB if necessary (handles PNG, JPEG, etc.)
|
| 75 |
-
if image.mode != 'RGB':
|
| 76 |
-
image = image.convert('RGB')
|
| 77 |
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
result = self.model.infer(
|
| 86 |
self.tokenizer,
|
| 87 |
prompt=prompt,
|
|
|
|
| 69 |
|
| 70 |
# Decode base64 to image
|
| 71 |
image_data = base64.b64decode(base64_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Define the prompt for Markdown conversion
|
| 74 |
prompt = "<image>\n<|grounding|>Convert the document to markdown."
|
| 75 |
|
| 76 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 77 |
|
| 78 |
+
image_path = os.path.join(temp_dir, "input_image.png")
|
| 79 |
+
with open(image_path, "wb") as f:
|
| 80 |
+
f.write(image_data)
|
| 81 |
+
|
| 82 |
+
print(f"Image saved to: {image_path}")
|
| 83 |
+
|
| 84 |
+
# Verify the image can be opened
|
| 85 |
+
try:
|
| 86 |
+
test_image = Image.open(image_path)
|
| 87 |
+
if test_image.mode != 'RGB':
|
| 88 |
+
test_image = test_image.convert('RGB')
|
| 89 |
+
test_image.save(image_path) # Save converted version
|
| 90 |
+
print(f"Image verified: {test_image.size}, mode: {test_image.mode}")
|
| 91 |
+
except Exception as img_error:
|
| 92 |
+
return {"error": f"Invalid image: {str(img_error)}"}
|
| 93 |
+
|
| 94 |
+
# Run OCR inference
|
| 95 |
result = self.model.infer(
|
| 96 |
self.tokenizer,
|
| 97 |
prompt=prompt,
|