Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
GLM-OCR Hugging Face Space app with client-side header/footer fallback for MaaS.
|
| 3 |
Works for every PDF and every image: uses API bboxes when available, else minimal band.
|
|
@@ -40,6 +82,19 @@ 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 |
# ---------------------------------------------------------------------------
|
| 44 |
# 1. Config: set MaaS and optionally include headers/footers for non-MaaS
|
| 45 |
# ---------------------------------------------------------------------------
|
|
@@ -177,8 +232,8 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
|
|
| 177 |
os.close(fd)
|
| 178 |
try:
|
| 179 |
crop.save(path, "JPEG", quality=92)
|
| 180 |
-
|
| 181 |
-
out = parse(path)
|
| 182 |
if not isinstance(out, list):
|
| 183 |
out = [out]
|
| 184 |
if out and getattr(out[0], "markdown_result", None):
|
|
@@ -217,10 +272,10 @@ def run_ocr(uploaded_file):
|
|
| 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,10 +286,10 @@ def run_ocr(uploaded_file):
|
|
| 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,8 +325,8 @@ def run_ocr(uploaded_file):
|
|
| 270 |
if page_md:
|
| 271 |
parts.append(page_md)
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
if
|
| 275 |
img_path = page_images[page_num]
|
| 276 |
ftr = ""
|
| 277 |
if is_pdf:
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Models
|
| 4 |
+
Datasets
|
| 5 |
+
Spaces
|
| 6 |
+
Community
|
| 7 |
+
Docs
|
| 8 |
+
Enterprise
|
| 9 |
+
Pricing
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Spaces:
|
| 13 |
+
SimpleCodeAI
|
| 14 |
+
/
|
| 15 |
+
glm-ocr-fixed
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
like
|
| 19 |
+
0
|
| 20 |
+
|
| 21 |
+
Logs
|
| 22 |
+
App
|
| 23 |
+
Files
|
| 24 |
+
Community
|
| 25 |
+
Settings
|
| 26 |
+
glm-ocr-fixed
|
| 27 |
+
/
|
| 28 |
+
app.py
|
| 29 |
+
|
| 30 |
+
hoytshao's picture
|
| 31 |
+
hoytshao
|
| 32 |
+
Update app.py
|
| 33 |
+
f9f6641
|
| 34 |
+
verified
|
| 35 |
+
5 days ago
|
| 36 |
+
raw
|
| 37 |
+
|
| 38 |
+
Copy download link
|
| 39 |
+
history
|
| 40 |
+
blame
|
| 41 |
+
|
| 42 |
+
12.7 kB
|
| 43 |
"""
|
| 44 |
GLM-OCR Hugging Face Space app with client-side header/footer fallback for MaaS.
|
| 45 |
Works for every PDF and every image: uses API bboxes when available, else minimal band.
|
|
|
|
| 82 |
PDF_HEADER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_HEADER_BAND", "0.15")) # top 15%
|
| 83 |
PDF_FOOTER_BAND_FRAC = float(os.environ.get("GLMOCR_PDF_FOOTER_BAND", "0.85")) # bottom 15%
|
| 84 |
|
| 85 |
+
# Single shared parser to avoid "GLM-OCR initialized" per request and asyncio cleanup issues.
|
| 86 |
+
_parser = None
|
| 87 |
+
|
| 88 |
+
def get_parser():
|
| 89 |
+
global _parser
|
| 90 |
+
if _parser is None:
|
| 91 |
+
from glmocr import GlmOcr
|
| 92 |
+
_parser = GlmOcr(
|
| 93 |
+
api_key=os.environ.get("GLMOCR_API_KEY", "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"),
|
| 94 |
+
mode="maas",
|
| 95 |
+
)
|
| 96 |
+
return _parser
|
| 97 |
+
|
| 98 |
# ---------------------------------------------------------------------------
|
| 99 |
# 1. Config: set MaaS and optionally include headers/footers for non-MaaS
|
| 100 |
# ---------------------------------------------------------------------------
|
|
|
|
| 232 |
os.close(fd)
|
| 233 |
try:
|
| 234 |
crop.save(path, "JPEG", quality=92)
|
| 235 |
+
parser = get_parser()
|
| 236 |
+
out = parser.parse(path)
|
| 237 |
if not isinstance(out, list):
|
| 238 |
out = [out]
|
| 239 |
if out and getattr(out[0], "markdown_result", None):
|
|
|
|
| 272 |
return "Please upload a file."
|
| 273 |
try:
|
| 274 |
import pymupdf as fitz
|
|
|
|
| 275 |
|
| 276 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 277 |
is_pdf = path.lower().endswith(".pdf")
|
| 278 |
+
parser = get_parser()
|
| 279 |
|
| 280 |
if is_pdf:
|
| 281 |
doc = fitz.open(path)
|
|
|
|
| 286 |
pix.save(img_path)
|
| 287 |
page_images.append(img_path)
|
| 288 |
doc.close()
|
| 289 |
+
results = parser.parse(page_images)
|
| 290 |
else:
|
| 291 |
page_images = [path]
|
| 292 |
+
results = parser.parse(path)
|
| 293 |
|
| 294 |
if not isinstance(results, list):
|
| 295 |
results = [results]
|
|
|
|
| 325 |
if page_md:
|
| 326 |
parts.append(page_md)
|
| 327 |
|
| 328 |
+
# Always run footer band (bottom 8–12% of page)
|
| 329 |
+
if page_num < len(page_images):
|
| 330 |
img_path = page_images[page_num]
|
| 331 |
ftr = ""
|
| 332 |
if is_pdf:
|