rehan953 commited on
Commit
cd8c2ad
·
verified ·
1 Parent(s): 55f6804

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -23
app.py CHANGED
@@ -40,19 +40,6 @@ MIN_CROP_PIXELS = int(os.environ.get("GLMOCR_MIN_CROP_PIXELS", "12544")) # 112*
40
  PDF_HEADER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_HEADER_BAND", "0.15")) # top 15%
41
  PDF_FOOTER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_FOOTER_BAND", "0.85")) # bottom 15%
42
 
43
- # Single shared parser to avoid "GLM-OCR initialized" per request and asyncio cleanup issues.
44
- _parser = None
45
-
46
- def get_parser():
47
- global _parser
48
- if _parser is None:
49
- from glmocr import GlmOcr
50
- _parser = GlmOcr(
51
- api_key=os.environ.get("GLMOCR_API_KEY", "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"),
52
- mode="maas",
53
- )
54
- return _parser
55
-
56
  # ---------------------------------------------------------------------------
57
  # 1. Config: set MaaS and optionally include headers/footers for non-MaaS
58
  # ---------------------------------------------------------------------------
@@ -190,8 +177,8 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
190
  os.close(fd)
191
  try:
192
  crop.save(path, "JPEG", quality=92)
193
- parser = get_parser()
194
- out = parser.parse(path)
195
  if not isinstance(out, list):
196
  out = [out]
197
  if out and getattr(out[0], "markdown_result", None):
@@ -230,10 +217,10 @@ def run_ocr(uploaded_file):
230
  return "Please upload a file."
231
  try:
232
  import pymupdf as fitz
 
233
 
234
  path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
235
  is_pdf = path.lower().endswith(".pdf")
236
- parser = get_parser()
237
 
238
  if is_pdf:
239
  doc = fitz.open(path)
@@ -244,10 +231,10 @@ def run_ocr(uploaded_file):
244
  pix.save(img_path)
245
  page_images.append(img_path)
246
  doc.close()
247
- results = parser.parse(page_images)
248
  else:
249
  page_images = [path]
250
- results = parser.parse(path)
251
 
252
  if not isinstance(results, list):
253
  results = [results]
@@ -283,10 +270,7 @@ def run_ocr(uploaded_file):
283
  if page_md:
284
  parts.append(page_md)
285
 
286
- # Always run footer band (bottom 8–12% of page)
287
- # Only extract footer if the API actually detected a footer_start_frac via bbox.
288
- # When footer_start_frac is None it means MaaS found no footer region — skip to
289
- # avoid extracting body content that sits near the bottom of the page.
290
  if footer_start_frac is not None and page_num < len(page_images):
291
  img_path = page_images[page_num]
292
  ftr = ""
@@ -317,4 +301,4 @@ with gr.Blocks(title="GLM-OCR") as demo:
317
  run_btn.click(fn=run_ocr, inputs=file_in, outputs=out)
318
 
319
  if __name__ == "__main__":
320
- demo.launch()
 
40
  PDF_HEADER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_HEADER_BAND", "0.15")) # top 15%
41
  PDF_FOOTER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_FOOTER_BAND", "0.85")) # bottom 15%
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # ---------------------------------------------------------------------------
44
  # 1. Config: set MaaS and optionally include headers/footers for non-MaaS
45
  # ---------------------------------------------------------------------------
 
177
  os.close(fd)
178
  try:
179
  crop.save(path, "JPEG", quality=92)
180
+ from glmocr import parse
181
+ out = parse(path)
182
  if not isinstance(out, list):
183
  out = [out]
184
  if out and getattr(out[0], "markdown_result", None):
 
217
  return "Please upload a file."
218
  try:
219
  import pymupdf as fitz
220
+ from glmocr import parse
221
 
222
  path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
223
  is_pdf = path.lower().endswith(".pdf")
 
224
 
225
  if is_pdf:
226
  doc = fitz.open(path)
 
231
  pix.save(img_path)
232
  page_images.append(img_path)
233
  doc.close()
234
+ results = parse(page_images)
235
  else:
236
  page_images = [path]
237
+ results = parse(path)
238
 
239
  if not isinstance(results, list):
240
  results = [results]
 
270
  if page_md:
271
  parts.append(page_md)
272
 
273
+ # Only run footer band if API actually detected a footer region via bbox
 
 
 
274
  if footer_start_frac is not None and page_num < len(page_images):
275
  img_path = page_images[page_num]
276
  ftr = ""
 
301
  run_btn.click(fn=run_ocr, inputs=file_in, outputs=out)
302
 
303
  if __name__ == "__main__":
304
+ demo.launch()