Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,12 +25,6 @@ Included (data-driven, no institution names):
|
|
| 25 |
|
| 26 |
Configure GLMOCR_API_KEY (environment variable). Optional: glmocr + gradio +
|
| 27 |
pymupdf + pillow installed.
|
| 28 |
-
|
| 29 |
-
When GLMOCR_PREFER_PDF_TEXT_BODY is 1 (default), searchable PDFs skip vision OCR
|
| 30 |
-
for the body and return each page's embedded text so output matches the PDF text
|
| 31 |
-
layer. Set GLMOCR_PREFER_PDF_TEXT_BODY=0 to force the image OCR path. Optional
|
| 32 |
-
GLMOCR_MIN_PDF_BODY_CHARS (default 120) is the minimum characters per page required
|
| 33 |
-
to use the text-layer path for the whole document.
|
| 34 |
"""
|
| 35 |
|
| 36 |
# Patch asyncio first (before Gradio imports it) to reduce Python 3.13 loop noise
|
|
@@ -55,7 +49,7 @@ import os
|
|
| 55 |
import re
|
| 56 |
import tempfile
|
| 57 |
from collections import Counter
|
| 58 |
-
from typing import List,
|
| 59 |
|
| 60 |
import yaml
|
| 61 |
|
|
@@ -194,41 +188,6 @@ def extract_zone_text_pdf(pdf_path, page_num, y_start_frac, y_end_frac):
|
|
| 194 |
return ""
|
| 195 |
|
| 196 |
|
| 197 |
-
def _prefer_pdf_text_body() -> bool:
|
| 198 |
-
v = os.environ.get("GLMOCR_PREFER_PDF_TEXT_BODY", "1").strip().lower()
|
| 199 |
-
return v in ("1", "true", "yes", "")
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
def extract_pdf_body_text_all_pages_if_suitable(pdf_path: str) -> Optional[List[str]]:
|
| 203 |
-
try:
|
| 204 |
-
import pymupdf as fitz
|
| 205 |
-
except ImportError:
|
| 206 |
-
return None
|
| 207 |
-
try:
|
| 208 |
-
min_c = int(os.environ.get("GLMOCR_MIN_PDF_BODY_CHARS", "120"))
|
| 209 |
-
except ValueError:
|
| 210 |
-
min_c = 120
|
| 211 |
-
try:
|
| 212 |
-
doc = fitz.open(pdf_path)
|
| 213 |
-
except Exception:
|
| 214 |
-
return None
|
| 215 |
-
try:
|
| 216 |
-
if len(doc) < 1:
|
| 217 |
-
return None
|
| 218 |
-
out: List[str] = []
|
| 219 |
-
for i in range(len(doc)):
|
| 220 |
-
t = (doc[i].get_text("text") or "").strip()
|
| 221 |
-
if len(t) < min_c:
|
| 222 |
-
return None
|
| 223 |
-
out.append(t)
|
| 224 |
-
return out
|
| 225 |
-
finally:
|
| 226 |
-
try:
|
| 227 |
-
doc.close()
|
| 228 |
-
except Exception:
|
| 229 |
-
pass
|
| 230 |
-
|
| 231 |
-
|
| 232 |
def extract_pdf_text_in_band(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 233 |
try:
|
| 234 |
import pymupdf as fitz
|
|
@@ -656,12 +615,6 @@ def run_ocr(uploaded_file):
|
|
| 656 |
try:
|
| 657 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 658 |
is_pdf = path.lower().endswith(".pdf")
|
| 659 |
-
|
| 660 |
-
if is_pdf and _prefer_pdf_text_body():
|
| 661 |
-
pdf_pages = extract_pdf_body_text_all_pages_if_suitable(path)
|
| 662 |
-
if pdf_pages is not None:
|
| 663 |
-
return "\n\n---page-separator---\n\n".join(pdf_pages)
|
| 664 |
-
|
| 665 |
parser = get_parser()
|
| 666 |
|
| 667 |
page_heights: List[int] = []
|
|
@@ -759,8 +712,8 @@ def _create_gradio_demo():
|
|
| 759 |
with gr.Blocks(title="GLM-OCR (simple)") as demo:
|
| 760 |
gr.Markdown(
|
| 761 |
"# GLM-OCR (simple)\n"
|
| 762 |
-
"
|
| 763 |
-
"
|
| 764 |
)
|
| 765 |
file_in = gr.File(
|
| 766 |
label="Upload PDF or image",
|
|
@@ -773,4 +726,4 @@ def _create_gradio_demo():
|
|
| 773 |
|
| 774 |
|
| 775 |
if __name__ == "__main__":
|
| 776 |
-
_create_gradio_demo().launch(
|
|
|
|
| 25 |
|
| 26 |
Configure GLMOCR_API_KEY (environment variable). Optional: glmocr + gradio +
|
| 27 |
pymupdf + pillow installed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
# Patch asyncio first (before Gradio imports it) to reduce Python 3.13 loop noise
|
|
|
|
| 49 |
import re
|
| 50 |
import tempfile
|
| 51 |
from collections import Counter
|
| 52 |
+
from typing import List, Tuple
|
| 53 |
|
| 54 |
import yaml
|
| 55 |
|
|
|
|
| 188 |
return ""
|
| 189 |
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
def extract_pdf_text_in_band(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 192 |
try:
|
| 193 |
import pymupdf as fitz
|
|
|
|
| 615 |
try:
|
| 616 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 617 |
is_pdf = path.lower().endswith(".pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
parser = get_parser()
|
| 619 |
|
| 620 |
page_heights: List[int] = []
|
|
|
|
| 712 |
with gr.Blocks(title="GLM-OCR (simple)") as demo:
|
| 713 |
gr.Markdown(
|
| 714 |
"# GLM-OCR (simple)\n"
|
| 715 |
+
"Upload a PDF or image. Header and footer bands are included; "
|
| 716 |
+
"body OCR is passed through with only light markdown cleanup."
|
| 717 |
)
|
| 718 |
file_in = gr.File(
|
| 719 |
label="Upload PDF or image",
|
|
|
|
| 726 |
|
| 727 |
|
| 728 |
if __name__ == "__main__":
|
| 729 |
+
_create_gradio_demo().launch()
|