Spaces:
Sleeping
Sleeping
Update app.py
Browse filesHeader and footer returns. Remove the logs
app.py
CHANGED
|
@@ -18,20 +18,12 @@ except Exception:
|
|
| 18 |
import logging
|
| 19 |
import os
|
| 20 |
import re
|
| 21 |
-
import sys
|
| 22 |
import tempfile
|
| 23 |
import yaml
|
| 24 |
import gradio as gr
|
| 25 |
|
| 26 |
import glmocr
|
| 27 |
|
| 28 |
-
# Log to stderr so Hugging Face / Docker show it in logs. Set GLMOCR_LOG=DEBUG for more detail.
|
| 29 |
-
_log_level = os.environ.get("GLMOCR_LOG", "INFO").upper()
|
| 30 |
-
logging.basicConfig(
|
| 31 |
-
level=getattr(logging, _log_level, logging.INFO),
|
| 32 |
-
format="[%(levelname)s] %(name)s: %(message)s",
|
| 33 |
-
stream=sys.stderr,
|
| 34 |
-
)
|
| 35 |
log = logging.getLogger("glmocr_app")
|
| 36 |
|
| 37 |
GLMOCR_BASE = os.path.dirname(glmocr.__file__)
|
|
@@ -118,7 +110,6 @@ except Exception:
|
|
| 118 |
|
| 119 |
def get_header_footer_zones(regions, norm_height=1000):
|
| 120 |
if not regions:
|
| 121 |
-
log.debug("get_header_footer_zones: no regions")
|
| 122 |
return None, None
|
| 123 |
y_tops, y_bottoms = [], []
|
| 124 |
for r in regions:
|
|
@@ -127,24 +118,8 @@ def get_header_footer_zones(regions, norm_height=1000):
|
|
| 127 |
y_tops.append(bbox[1])
|
| 128 |
y_bottoms.append(bbox[3])
|
| 129 |
if not y_tops:
|
| 130 |
-
log.debug("get_header_footer_zones: no bboxes in regions")
|
| 131 |
return None, None
|
| 132 |
-
|
| 133 |
-
fs_frac = max(y_bottoms) / norm_height
|
| 134 |
-
log.debug("get_header_footer_zones: regions=%s -> header_end_frac=%.3f footer_start_frac=%.3f", len(regions), he_frac, fs_frac)
|
| 135 |
-
return he_frac, fs_frac
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
def _log_text(prefix, text, max_display=400):
|
| 139 |
-
"""Log extracted text for comparison with OCR; truncate long content."""
|
| 140 |
-
if not text or not text.strip():
|
| 141 |
-
log.info("%s: (empty)", prefix)
|
| 142 |
-
return
|
| 143 |
-
s = text.strip()
|
| 144 |
-
if len(s) <= max_display:
|
| 145 |
-
log.info("%s: %d chars | %s", prefix, len(s), s)
|
| 146 |
-
else:
|
| 147 |
-
log.info("%s: %d chars | %s ...", prefix, len(s), s[:max_display].rstrip())
|
| 148 |
|
| 149 |
|
| 150 |
def extract_zone_text_pdf(pdf_path, page_num, y_start_frac, y_end_frac):
|
|
@@ -157,26 +132,11 @@ def extract_zone_text_pdf(pdf_path, page_num, y_start_frac, y_end_frac):
|
|
| 157 |
rect = fitz.Rect(0, h * y_start_frac, w, h * y_end_frac)
|
| 158 |
text = page.get_text(clip=rect).strip()
|
| 159 |
doc.close()
|
| 160 |
-
_log_text("pymupdf clip page=%s y=%.2f-%.2f" % (page_num, y_start_frac, y_end_frac), text)
|
| 161 |
return text
|
| 162 |
-
except Exception
|
| 163 |
-
log.debug("extract_zone_text_pdf failed: %s", e)
|
| 164 |
return ""
|
| 165 |
|
| 166 |
|
| 167 |
-
def log_pymupdf_full_page(pdf_path, page_num):
|
| 168 |
-
"""Log full-page PyMuPDF text for comparison (length + prefix)."""
|
| 169 |
-
try:
|
| 170 |
-
import pymupdf as fitz
|
| 171 |
-
doc = fitz.open(pdf_path)
|
| 172 |
-
page = doc[page_num]
|
| 173 |
-
text = page.get_text().strip()
|
| 174 |
-
doc.close()
|
| 175 |
-
_log_text("pymupdf full page=%s" % page_num, text, max_display=500)
|
| 176 |
-
except Exception as e:
|
| 177 |
-
log.debug("log_pymupdf_full_page failed: %s", e)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
def extract_pdf_text_in_band(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 181 |
"""Extract all text whose word bbox falls in the given vertical band (by position).
|
| 182 |
Use a wider band (e.g. top 15% / bottom 15%) to capture header/footer that clip might miss."""
|
|
@@ -195,11 +155,8 @@ def extract_pdf_text_in_band(pdf_path, page_num, y_start_frac, y_end_frac):
|
|
| 195 |
y0, y1 = float(w[1]), float(w[3])
|
| 196 |
if y0 < y_hi and y1 > y_lo:
|
| 197 |
parts.append(w[4])
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
return text
|
| 201 |
-
except Exception as e:
|
| 202 |
-
log.debug("extract_pdf_text_in_band failed: %s", e)
|
| 203 |
return ""
|
| 204 |
|
| 205 |
|
|
@@ -213,7 +170,6 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
|
|
| 213 |
y0 = max(0, int(h * y_start_frac))
|
| 214 |
y1 = min(h, int(h * y_end_frac))
|
| 215 |
if y1 <= y0:
|
| 216 |
-
log.info("[%s] ocr_zone: skip (zero height band y0=%s y1=%s)", zone_name, y0, y1)
|
| 217 |
return ""
|
| 218 |
crop = img.crop((0, y0, w, y1))
|
| 219 |
cw, ch = crop.size
|
|
@@ -230,7 +186,6 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
|
|
| 230 |
canvas.paste(crop, (0, need_h - ch)) # crop at bottom
|
| 231 |
crop = canvas
|
| 232 |
cw, ch = crop.size
|
| 233 |
-
log.info("[%s] ocr_zone: padded to %dx%d for API", zone_name, cw, ch)
|
| 234 |
fd, path = tempfile.mkstemp(suffix=".jpg")
|
| 235 |
os.close(fd)
|
| 236 |
try:
|
|
@@ -241,10 +196,7 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
|
|
| 241 |
out = [out]
|
| 242 |
if out and getattr(out[0], "markdown_result", None):
|
| 243 |
text = (out[0].markdown_result or "").strip()
|
| 244 |
-
log.info("[%s] ocr_zone: crop %dx%d -> %d chars", zone_name, cw, ch, len(text))
|
| 245 |
-
_log_text("ocr_zone %s result" % zone_name, text)
|
| 246 |
return text
|
| 247 |
-
log.info("[%s] ocr_zone: crop %dx%d -> empty result from API", zone_name, cw, ch)
|
| 248 |
finally:
|
| 249 |
try:
|
| 250 |
os.unlink(path)
|
|
@@ -281,7 +233,6 @@ def run_ocr(uploaded_file):
|
|
| 281 |
|
| 282 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 283 |
is_pdf = path.lower().endswith(".pdf")
|
| 284 |
-
log.info("run_ocr: path=%s is_pdf=%s", path, is_pdf)
|
| 285 |
parser = get_parser()
|
| 286 |
|
| 287 |
if is_pdf:
|
|
@@ -293,7 +244,6 @@ def run_ocr(uploaded_file):
|
|
| 293 |
pix.save(img_path)
|
| 294 |
page_images.append(img_path)
|
| 295 |
doc.close()
|
| 296 |
-
log.info("run_ocr: PDF has %d pages, running MaaS on page images", len(page_images))
|
| 297 |
results = parser.parse(page_images)
|
| 298 |
else:
|
| 299 |
page_images = [path]
|
|
@@ -304,8 +254,6 @@ def run_ocr(uploaded_file):
|
|
| 304 |
|
| 305 |
all_pages = []
|
| 306 |
for page_num, page_result in enumerate(results):
|
| 307 |
-
if is_pdf:
|
| 308 |
-
log_pymupdf_full_page(path, page_num)
|
| 309 |
page_md, regions = get_page_md_and_regions(page_result)
|
| 310 |
header_end_frac, footer_start_frac = get_header_footer_zones(regions, 1000)
|
| 311 |
|
|
@@ -317,11 +265,6 @@ def run_ocr(uploaded_file):
|
|
| 317 |
if fs >= 1.0:
|
| 318 |
fs = 1.0 - DEFAULT_ZONE_FRAC # ensure we always OCR a bottom band
|
| 319 |
|
| 320 |
-
log.info(
|
| 321 |
-
"run_ocr: page %d regions=%s he=%.3f fs=%.3f body_md_len=%d",
|
| 322 |
-
page_num, len(regions), he, fs, len(page_md or ""),
|
| 323 |
-
)
|
| 324 |
-
|
| 325 |
parts = []
|
| 326 |
|
| 327 |
# Always run header band (top 8–12% of page)
|
|
@@ -332,15 +275,10 @@ def run_ocr(uploaded_file):
|
|
| 332 |
hdr = extract_zone_text_pdf(path, page_num, 0, he)
|
| 333 |
if not (hdr and hdr.strip()):
|
| 334 |
hdr = extract_pdf_text_in_band(path, page_num, 0, PDF_HEADER_BAND_FRAC)
|
| 335 |
-
if hdr and hdr.strip():
|
| 336 |
-
log.info("run_ocr: page %d header from PDF: %d chars", page_num, len(hdr.strip()))
|
| 337 |
if not (hdr and hdr.strip()):
|
| 338 |
hdr = ocr_zone(img_path, 0, he)
|
| 339 |
if hdr and hdr.strip():
|
| 340 |
parts.append(hdr.strip())
|
| 341 |
-
_log_text("run_ocr page %d final header (used in output)" % page_num, hdr.strip())
|
| 342 |
-
elif not (hdr and hdr.strip()) and he > 0:
|
| 343 |
-
log.info("run_ocr: page %d header empty (PDF + ocr_zone)", page_num)
|
| 344 |
|
| 345 |
if page_md:
|
| 346 |
parts.append(page_md)
|
|
@@ -353,20 +291,14 @@ def run_ocr(uploaded_file):
|
|
| 353 |
ftr = extract_zone_text_pdf(path, page_num, fs, 1.0)
|
| 354 |
if not (ftr and ftr.strip()):
|
| 355 |
ftr = extract_pdf_text_in_band(path, page_num, PDF_FOOTER_BAND_FRAC, 1.0)
|
| 356 |
-
if ftr and ftr.strip():
|
| 357 |
-
log.info("run_ocr: page %d footer from PDF: %d chars", page_num, len(ftr.strip()))
|
| 358 |
if not (ftr and ftr.strip()):
|
| 359 |
ftr = ocr_zone(img_path, fs, 1.0)
|
| 360 |
if ftr and ftr.strip():
|
| 361 |
parts.append(ftr.strip())
|
| 362 |
-
_log_text("run_ocr page %d final footer (used in output)" % page_num, ftr.strip())
|
| 363 |
-
elif not (ftr and ftr.strip()) and fs < 1.0:
|
| 364 |
-
log.info("run_ocr: page %d footer empty (PDF + ocr_zone)", page_num)
|
| 365 |
|
| 366 |
if parts:
|
| 367 |
all_pages.append("\n\n".join(parts))
|
| 368 |
|
| 369 |
-
log.info("run_ocr: done, %d pages in output", len(all_pages))
|
| 370 |
return "\n\n---\n\n".join(all_pages) if all_pages else "(No content)"
|
| 371 |
except Exception as e:
|
| 372 |
import traceback
|
|
|
|
| 18 |
import logging
|
| 19 |
import os
|
| 20 |
import re
|
|
|
|
| 21 |
import tempfile
|
| 22 |
import yaml
|
| 23 |
import gradio as gr
|
| 24 |
|
| 25 |
import glmocr
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
log = logging.getLogger("glmocr_app")
|
| 28 |
|
| 29 |
GLMOCR_BASE = os.path.dirname(glmocr.__file__)
|
|
|
|
| 110 |
|
| 111 |
def get_header_footer_zones(regions, norm_height=1000):
|
| 112 |
if not regions:
|
|
|
|
| 113 |
return None, None
|
| 114 |
y_tops, y_bottoms = [], []
|
| 115 |
for r in regions:
|
|
|
|
| 118 |
y_tops.append(bbox[1])
|
| 119 |
y_bottoms.append(bbox[3])
|
| 120 |
if not y_tops:
|
|
|
|
| 121 |
return None, None
|
| 122 |
+
return min(y_tops) / norm_height, max(y_bottoms) / norm_height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
def extract_zone_text_pdf(pdf_path, page_num, y_start_frac, y_end_frac):
|
|
|
|
| 132 |
rect = fitz.Rect(0, h * y_start_frac, w, h * y_end_frac)
|
| 133 |
text = page.get_text(clip=rect).strip()
|
| 134 |
doc.close()
|
|
|
|
| 135 |
return text
|
| 136 |
+
except Exception:
|
|
|
|
| 137 |
return ""
|
| 138 |
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
def extract_pdf_text_in_band(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 141 |
"""Extract all text whose word bbox falls in the given vertical band (by position).
|
| 142 |
Use a wider band (e.g. top 15% / bottom 15%) to capture header/footer that clip might miss."""
|
|
|
|
| 155 |
y0, y1 = float(w[1]), float(w[3])
|
| 156 |
if y0 < y_hi and y1 > y_lo:
|
| 157 |
parts.append(w[4])
|
| 158 |
+
return " ".join(parts).strip()
|
| 159 |
+
except Exception:
|
|
|
|
|
|
|
|
|
|
| 160 |
return ""
|
| 161 |
|
| 162 |
|
|
|
|
| 170 |
y0 = max(0, int(h * y_start_frac))
|
| 171 |
y1 = min(h, int(h * y_end_frac))
|
| 172 |
if y1 <= y0:
|
|
|
|
| 173 |
return ""
|
| 174 |
crop = img.crop((0, y0, w, y1))
|
| 175 |
cw, ch = crop.size
|
|
|
|
| 186 |
canvas.paste(crop, (0, need_h - ch)) # crop at bottom
|
| 187 |
crop = canvas
|
| 188 |
cw, ch = crop.size
|
|
|
|
| 189 |
fd, path = tempfile.mkstemp(suffix=".jpg")
|
| 190 |
os.close(fd)
|
| 191 |
try:
|
|
|
|
| 196 |
out = [out]
|
| 197 |
if out and getattr(out[0], "markdown_result", None):
|
| 198 |
text = (out[0].markdown_result or "").strip()
|
|
|
|
|
|
|
| 199 |
return text
|
|
|
|
| 200 |
finally:
|
| 201 |
try:
|
| 202 |
os.unlink(path)
|
|
|
|
| 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:
|
|
|
|
| 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]
|
|
|
|
| 254 |
|
| 255 |
all_pages = []
|
| 256 |
for page_num, page_result in enumerate(results):
|
|
|
|
|
|
|
| 257 |
page_md, regions = get_page_md_and_regions(page_result)
|
| 258 |
header_end_frac, footer_start_frac = get_header_footer_zones(regions, 1000)
|
| 259 |
|
|
|
|
| 265 |
if fs >= 1.0:
|
| 266 |
fs = 1.0 - DEFAULT_ZONE_FRAC # ensure we always OCR a bottom band
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
parts = []
|
| 269 |
|
| 270 |
# Always run header band (top 8–12% of page)
|
|
|
|
| 275 |
hdr = extract_zone_text_pdf(path, page_num, 0, he)
|
| 276 |
if not (hdr and hdr.strip()):
|
| 277 |
hdr = extract_pdf_text_in_band(path, page_num, 0, PDF_HEADER_BAND_FRAC)
|
|
|
|
|
|
|
| 278 |
if not (hdr and hdr.strip()):
|
| 279 |
hdr = ocr_zone(img_path, 0, he)
|
| 280 |
if hdr and hdr.strip():
|
| 281 |
parts.append(hdr.strip())
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
if page_md:
|
| 284 |
parts.append(page_md)
|
|
|
|
| 291 |
ftr = extract_zone_text_pdf(path, page_num, fs, 1.0)
|
| 292 |
if not (ftr and ftr.strip()):
|
| 293 |
ftr = extract_pdf_text_in_band(path, page_num, PDF_FOOTER_BAND_FRAC, 1.0)
|
|
|
|
|
|
|
| 294 |
if not (ftr and ftr.strip()):
|
| 295 |
ftr = ocr_zone(img_path, fs, 1.0)
|
| 296 |
if ftr and ftr.strip():
|
| 297 |
parts.append(ftr.strip())
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
if parts:
|
| 300 |
all_pages.append("\n\n".join(parts))
|
| 301 |
|
|
|
|
| 302 |
return "\n\n---\n\n".join(all_pages) if all_pages else "(No content)"
|
| 303 |
except Exception as e:
|
| 304 |
import traceback
|