Svngoku commited on
Commit
7f3a813
·
verified ·
1 Parent(s): 4005c12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -12,6 +12,7 @@ from tenacity import retry, stop_after_attempt, wait_fixed
12
  import tempfile
13
  from typing import Union, Dict, List
14
  from contextlib import contextmanager
 
15
 
16
  # Constants
17
  DEFAULT_LANGUAGE = "English"
@@ -69,8 +70,15 @@ class OCRProcessor:
69
 
70
  def _get_file_content(self, file_input: Union[str, bytes]) -> bytes:
71
  if isinstance(file_input, str):
72
- with open(file_input, "rb") as f:
73
- return f.read()
 
 
 
 
 
 
 
74
  return file_input.read() if hasattr(file_input, 'read') else file_input
75
 
76
  def ocr_pdf_url(self, pdf_url: str) -> str:
@@ -140,7 +148,7 @@ class OCRProcessor:
140
  base64_url = f"data:image/jpeg;base64,{encoded_image}"
141
  ocr_response = self._call_ocr_api({"type": "image_url", "image_url": base64_url})
142
  markdown = self._extract_markdown(ocr_response)
143
-
144
  chat_response = self._call_chat_complete(
145
  model="pixtral-12b-latest",
146
  messages=[{
@@ -156,12 +164,12 @@ class OCRProcessor:
156
  response_format={"type": "json_object"},
157
  temperature=0
158
  )
159
-
160
  # Ensure the response is a dictionary
161
  response_content = chat_response.choices[0].message.content
162
  if isinstance(response_content, list):
163
  response_content = response_content[0] if response_content else "{}"
164
-
165
  content = json.loads(response_content)
166
  return self._format_structured_response(temp_path, content)
167
  except Exception as e:
@@ -267,4 +275,4 @@ def create_interface():
267
  return demo
268
 
269
  if __name__ == "__main__":
270
- create_interface().launch(share=True, debug=True)
 
12
  import tempfile
13
  from typing import Union, Dict, List
14
  from contextlib import contextmanager
15
+ import requests
16
 
17
  # Constants
18
  DEFAULT_LANGUAGE = "English"
 
70
 
71
  def _get_file_content(self, file_input: Union[str, bytes]) -> bytes:
72
  if isinstance(file_input, str):
73
+ if file_input.startswith("http"):
74
+ # Handle URLs
75
+ response = requests.get(file_input)
76
+ response.raise_for_status()
77
+ return response.content
78
+ else:
79
+ # Handle local file paths
80
+ with open(file_input, "rb") as f:
81
+ return f.read()
82
  return file_input.read() if hasattr(file_input, 'read') else file_input
83
 
84
  def ocr_pdf_url(self, pdf_url: str) -> str:
 
148
  base64_url = f"data:image/jpeg;base64,{encoded_image}"
149
  ocr_response = self._call_ocr_api({"type": "image_url", "image_url": base64_url})
150
  markdown = self._extract_markdown(ocr_response)
151
+
152
  chat_response = self._call_chat_complete(
153
  model="pixtral-12b-latest",
154
  messages=[{
 
164
  response_format={"type": "json_object"},
165
  temperature=0
166
  )
167
+
168
  # Ensure the response is a dictionary
169
  response_content = chat_response.choices[0].message.content
170
  if isinstance(response_content, list):
171
  response_content = response_content[0] if response_content else "{}"
172
+
173
  content = json.loads(response_content)
174
  return self._format_structured_response(temp_path, content)
175
  except Exception as e:
 
275
  return demo
276
 
277
  if __name__ == "__main__":
278
+ create_interface().launch(share=True, debug=True)