wealthcoders commited on
Commit
40c2ea5
·
verified ·
1 Parent(s): e9703d0

Update handler.py

Browse files
Files changed (1) hide show
  1. 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
- # 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
- result = self.model.infer(
63
- self.tokenizer,
64
- prompt=prompt,
65
- image_file=image, # Pass PIL Image directly
66
- output_path=output_path,
67
- base_size=1024,
68
- image_size=640,
69
- crop_mode=True,
70
- save_results=output_path is not None
71
- )
72
-
73
- return result
 
 
 
 
 
 
 
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}")