rehan953 commited on
Commit
aef7ee0
·
verified ·
1 Parent(s): 2d37358

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,9 +1,9 @@
1
  #!/usr/bin/env python3
2
 
3
- import json
4
  import logging
5
  import os
6
  import subprocess
 
7
  import time
8
  from typing import Any, List
9
 
@@ -28,6 +28,25 @@ _parser = None
28
  _vllm_proc = None
29
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def _start_vllm_if_needed() -> None:
32
  global _vllm_proc
33
  if _vllm_proc is not None and _vllm_proc.poll() is None:
@@ -142,30 +161,34 @@ def _extract_md(result: Any) -> str:
142
  return str(md).strip() if md else ""
143
 
144
 
145
- def _extract_json(result: Any) -> str:
146
- if result is None:
147
- return "{}"
148
- if isinstance(result, list):
149
- payload = [getattr(item, "json_result", None) for item in result]
150
- return json.dumps(payload, ensure_ascii=False, indent=2)
151
- return json.dumps(getattr(result, "json_result", None), ensure_ascii=False, indent=2)
152
-
153
-
154
  def run_ocr(file_obj):
155
  if file_obj is None:
156
- return "Please upload a file.", "{}"
157
  path = file_obj.name if hasattr(file_obj, "name") else str(file_obj)
 
158
  try:
159
  parser = get_parser()
160
- result = parser.parse(path)
 
 
 
 
 
 
161
  md = _extract_md(result) or "(No content)"
162
- jr = _extract_json(result)
163
- return md, jr
164
  except Exception as e:
165
  import traceback
166
 
167
  log.exception("run_ocr failed: %s", e)
168
- return f"Error: {e}\n\n{traceback.format_exc()}", "{}"
 
 
 
 
 
 
 
169
 
170
 
171
  def build_demo():
@@ -181,8 +204,7 @@ def build_demo():
181
  )
182
  run_btn = gr.Button("Run OCR", variant="primary")
183
  out_md = gr.Textbox(label="Markdown", lines=30)
184
- out_json = gr.Textbox(label="JSON (layout details)", lines=20)
185
- run_btn.click(fn=run_ocr, inputs=file_in, outputs=[out_md, out_json])
186
  return demo
187
 
188
 
 
1
  #!/usr/bin/env python3
2
 
 
3
  import logging
4
  import os
5
  import subprocess
6
+ import tempfile
7
  import time
8
  from typing import Any, List
9
 
 
28
  _vllm_proc = None
29
 
30
 
31
+ def _render_pdf_pages_to_images(pdf_path: str) -> List[str]:
32
+ import pymupdf as fitz
33
+
34
+ doc = fitz.open(pdf_path)
35
+ page_images: List[str] = []
36
+ try:
37
+ for i in range(len(doc)):
38
+ page = doc[i]
39
+ pix = page.get_pixmap(matrix=fitz.Matrix(2.5, 2.5), alpha=False)
40
+ img_path = os.path.join(
41
+ tempfile.gettempdir(), f"glmocr_page_{os.getpid()}_{i}_{int(time.time()*1000)}.png"
42
+ )
43
+ pix.save(img_path)
44
+ page_images.append(img_path)
45
+ finally:
46
+ doc.close()
47
+ return page_images
48
+
49
+
50
  def _start_vllm_if_needed() -> None:
51
  global _vllm_proc
52
  if _vllm_proc is not None and _vllm_proc.poll() is None:
 
161
  return str(md).strip() if md else ""
162
 
163
 
 
 
 
 
 
 
 
 
 
164
  def run_ocr(file_obj):
165
  if file_obj is None:
166
+ return "Please upload a file."
167
  path = file_obj.name if hasattr(file_obj, "name") else str(file_obj)
168
+ page_images: List[str] = []
169
  try:
170
  parser = get_parser()
171
+ is_pdf = path.lower().endswith(".pdf")
172
+ if is_pdf:
173
+ page_images = _render_pdf_pages_to_images(path)
174
+ # Parse one rendered page per request item so we can preserve page boundaries.
175
+ result = parser.parse(page_images)
176
+ else:
177
+ result = parser.parse(path)
178
  md = _extract_md(result) or "(No content)"
179
+ return md
 
180
  except Exception as e:
181
  import traceback
182
 
183
  log.exception("run_ocr failed: %s", e)
184
+ return f"Error: {e}\n\n{traceback.format_exc()}"
185
+ finally:
186
+ for p in page_images:
187
+ try:
188
+ if isinstance(p, str) and p.endswith(".png") and "glmocr_page_" in os.path.basename(p):
189
+ os.unlink(p)
190
+ except Exception:
191
+ pass
192
 
193
 
194
  def build_demo():
 
204
  )
205
  run_btn = gr.Button("Run OCR", variant="primary")
206
  out_md = gr.Textbox(label="Markdown", lines=30)
207
+ run_btn.click(fn=run_ocr, inputs=file_in, outputs=out_md)
 
208
  return demo
209
 
210