File size: 13,411 Bytes
bcc62dd
 
 
 
53dfd3e
 
 
 
 
 
 
 
 
 
 
 
 
966ed33
70c9fdf
5cc96b8
966ed33
bcc62dd
5cc96b8
 
70c9fdf
 
 
966ed33
 
 
 
 
 
 
 
 
5cc96b8
 
 
a58a36b
e7a0fbc
 
 
0208bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcc62dd
 
e7a0fbc
bcc62dd
e7a0fbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c9fdf
bcc62dd
e7a0fbc
bcc62dd
e7a0fbc
 
 
 
 
 
 
 
 
 
 
e4d155c
bcc62dd
 
 
966ed33
bcc62dd
 
 
 
 
 
 
 
966ed33
bcc62dd
966ed33
 
 
 
bcc62dd
 
 
 
 
 
 
 
 
 
 
966ed33
bcc62dd
966ed33
 
bcc62dd
 
 
 
53dfd3e
966ed33
bcc62dd
 
 
 
 
 
 
966ed33
bcc62dd
 
0208bfc
53dfd3e
 
 
 
 
 
 
 
 
 
 
 
 
 
0208bfc
bcc62dd
 
0208bfc
 
 
bcc62dd
 
 
966ed33
 
 
 
bcc62dd
 
 
 
 
966ed33
 
bcc62dd
 
 
 
 
 
 
 
 
 
e7a0fbc
 
 
bcc62dd
 
 
 
 
 
 
 
70c9fdf
 
0532153
70c9fdf
0141b9f
6edb1f6
133ecf3
e4d155c
966ed33
0208bfc
6edb1f6
e4d155c
133ecf3
 
62ba456
133ecf3
bcc62dd
70c9fdf
62ba456
 
966ed33
0208bfc
62ba456
bcc62dd
0208bfc
e4d155c
5cc96b8
 
0532153
bcc62dd
 
 
 
 
e7a0fbc
bcc62dd
 
e7a0fbc
 
 
 
bcc62dd
966ed33
 
 
 
 
bcc62dd
 
e7a0fbc
 
bcc62dd
 
 
 
966ed33
 
bcc62dd
 
 
 
966ed33
 
bcc62dd
 
 
 
e7a0fbc
 
bcc62dd
 
 
 
966ed33
 
bcc62dd
 
 
 
966ed33
 
bcc62dd
 
 
 
966ed33
bcc62dd
70c9fdf
 
966ed33
5cc96b8
6edb1f6
bcc62dd
0532153
bcc62dd
5cc96b8
 
 
 
 
bcc62dd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
GLM-OCR Hugging Face Space app with client-side header/footer fallback for MaaS.
Works for every PDF and every image: uses API bboxes when available, else minimal band.
"""
# Patch asyncio first (before Gradio imports it) to suppress Python 3.13 cleanup noise
import asyncio
try:
    _orig_close = asyncio.BaseEventLoop.close
    def _safe_close(self):
        try:
            _orig_close(self)
        except (ValueError, OSError):
            pass
    asyncio.BaseEventLoop.close = _safe_close
except Exception:
    pass

import logging
import os
import re
import sys
import tempfile
import yaml
import gradio as gr

import glmocr

# Log to stderr so Hugging Face / Docker show it in logs. Set GLMOCR_LOG=DEBUG for more detail.
_log_level = os.environ.get("GLMOCR_LOG", "INFO").upper()
logging.basicConfig(
    level=getattr(logging, _log_level, logging.INFO),
    format="[%(levelname)s] %(name)s: %(message)s",
    stream=sys.stderr,
)
log = logging.getLogger("glmocr_app")

GLMOCR_BASE = os.path.dirname(glmocr.__file__)
CONFIG_PATH = os.path.join(GLMOCR_BASE, "config.yaml")
FORMATTER_PATH = os.path.join(GLMOCR_BASE, "postprocess", "result_formatter.py")

# When MaaS is enabled, the cloud API ignores local layout config and does not return
# header/footer regions. We always run client-side OCR on top/bottom bands as fallback.
DEFAULT_ZONE_FRAC = float(os.environ.get("GLMOCR_DEFAULT_ZONE_FRAC", "0.12"))
# MaaS rejects very small images (400). Only send crops that meet minimum dimensions.
MIN_CROP_HEIGHT = int(os.environ.get("GLMOCR_MIN_CROP_HEIGHT", "112"))
MIN_CROP_PIXELS = int(os.environ.get("GLMOCR_MIN_CROP_PIXELS", "12544"))  # 112*112

# Single shared parser to avoid "GLM-OCR initialized" per request and asyncio cleanup issues.
_parser = None

def get_parser():
    global _parser
    if _parser is None:
        from glmocr import GlmOcr
        _parser = GlmOcr(
            api_key=os.environ.get("GLMOCR_API_KEY", "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"),
            mode="maas",
        )
    return _parser

# ---------------------------------------------------------------------------
# 1. Config: set MaaS and optionally include headers/footers for non-MaaS
# ---------------------------------------------------------------------------
try:
    with open(CONFIG_PATH, "r") as f:
        config = yaml.safe_load(f)
    config["pipeline"]["maas"]["enabled"] = True
    config["pipeline"]["maas"]["api_key"] = os.environ.get("GLMOCR_API_KEY", "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV")

    to_include_as_text = {"header", "footer"}
    layout_section = config.get("pipeline", {}).get("layout", {})
    if "label_task_mapping" in layout_section:
        mapping = layout_section["label_task_mapping"]
        abandon = mapping.get("abandon")
        if isinstance(abandon, list):
            mapping["abandon"] = [x for x in abandon if x not in to_include_as_text]
        text_labels = mapping.get("text")
        if isinstance(text_labels, list):
            for label in to_include_as_text:
                if label not in text_labels:
                    text_labels.append(label)

    formatter_section = config.get("pipeline", {}).get("result_formatter", {})
    if "abandon" in formatter_section:
        abandon = formatter_section["abandon"]
        if isinstance(abandon, list):
            formatter_section["abandon"] = [x for x in abandon if x not in to_include_as_text]
    if "label_visualization_mapping" in formatter_section:
        text_vis = formatter_section["label_visualization_mapping"].get("text")
        if isinstance(text_vis, list):
            for label in to_include_as_text:
                if label not in text_vis:
                    text_vis.append(label)

    with open(CONFIG_PATH, "w") as f:
        yaml.dump(config, f, default_flow_style=False, sort_keys=False)
except Exception:
    pass  # e.g. read-only package on Hugging Face; MaaS ignores layout anyway

# ---------------------------------------------------------------------------
# 2. result_formatter: stop stripping header/footer (best-effort; may be read-only)
# ---------------------------------------------------------------------------
try:
    with open(FORMATTER_PATH, "r") as f:
        source = f.read()
    for label in ('"header"', "'header'", '"footer"', "'footer'", '"doc_header"', "'doc_header'", '"doc_footer"', "'doc_footer'"):
        source = re.sub(r",\s*" + re.escape(label), "", source)
        source = re.sub(re.escape(label) + r"\s*,", "", source)
        source = re.sub(re.escape(label), "", source)
    with open(FORMATTER_PATH, "w") as f:
        f.write(source)
except Exception:
    pass


def get_header_footer_zones(regions, norm_height=1000):
    if not regions:
        log.debug("get_header_footer_zones: no regions")
        return None, None
    y_tops, y_bottoms = [], []
    for r in regions:
        bbox = r.get("bbox_2d") if isinstance(r, dict) else getattr(r, "bbox_2d", None)
        if bbox and len(bbox) >= 4:
            y_tops.append(bbox[1])
            y_bottoms.append(bbox[3])
    if not y_tops:
        log.debug("get_header_footer_zones: no bboxes in regions")
        return None, None
    he_frac = min(y_tops) / norm_height
    fs_frac = max(y_bottoms) / norm_height
    log.debug("get_header_footer_zones: regions=%s -> header_end_frac=%.3f footer_start_frac=%.3f", len(regions), he_frac, fs_frac)
    return he_frac, fs_frac


def extract_zone_text_pdf(pdf_path, page_num, y_start_frac, y_end_frac):
    try:
        import pymupdf as fitz
        doc = fitz.open(pdf_path)
        page = doc[page_num]
        h, w = page.rect.height, page.rect.width
        rect = fitz.Rect(0, h * y_start_frac, w, h * y_end_frac)
        text = page.get_text(clip=rect).strip()
        doc.close()
        log.debug("extract_zone_text_pdf page=%s y=%.2f-%.2f -> %d chars", page_num, y_start_frac, y_end_frac, len(text))
        return text
    except Exception as e:
        log.debug("extract_zone_text_pdf failed: %s", e)
        return ""


def ocr_zone(image_path, y_start_frac, y_end_frac):
    """Run OCR on a horizontal band. Pads small crops to meet API minimum size instead of skipping."""
    zone_name = "header" if y_end_frac < 0.5 else "footer"
    try:
        from PIL import Image
        img = Image.open(image_path).convert("RGB")
        w, h = img.size
        y0 = max(0, int(h * y_start_frac))
        y1 = min(h, int(h * y_end_frac))
        if y1 <= y0:
            log.info("[%s] ocr_zone: skip (zero height band y0=%s y1=%s)", zone_name, y0, y1)
            return ""
        crop = img.crop((0, y0, w, y1))
        cw, ch = crop.size
        # Pad to minimum size so MaaS accepts the image (avoids 400 on tiny strips)
        if ch < MIN_CROP_HEIGHT or (cw * ch) < MIN_CROP_PIXELS:
            need_h = max(ch, MIN_CROP_HEIGHT)
            need_w = max(cw, 1)
            if (need_w * need_h) < MIN_CROP_PIXELS:
                need_w = max(need_w, (MIN_CROP_PIXELS + need_h - 1) // need_h)
            canvas = Image.new("RGB", (need_w, need_h), (255, 255, 255))
            if zone_name == "header":
                canvas.paste(crop, (0, 0))  # crop at top
            else:
                canvas.paste(crop, (0, need_h - ch))  # crop at bottom
            crop = canvas
            cw, ch = crop.size
            log.info("[%s] ocr_zone: padded to %dx%d for API", zone_name, cw, ch)
        fd, path = tempfile.mkstemp(suffix=".jpg")
        os.close(fd)
        try:
            crop.save(path, "JPEG", quality=92)
            parser = get_parser()
            out = parser.parse(path)
            if not isinstance(out, list):
                out = [out]
            if out and getattr(out[0], "markdown_result", None):
                text = (out[0].markdown_result or "").strip()
                log.info("[%s] ocr_zone: crop %dx%d -> %d chars", zone_name, cw, ch, len(text))
                return text
            log.info("[%s] ocr_zone: crop %dx%d -> empty result from API", zone_name, cw, ch)
        finally:
            try:
                os.unlink(path)
            except Exception:
                pass
    except Exception as e:
        log.warning("[%s] ocr_zone failed: %s", zone_name, e, exc_info=True)
    return ""


def get_page_md_and_regions(page_result):
    md = ""
    if hasattr(page_result, "markdown_result") and page_result.markdown_result:
        md = (page_result.markdown_result or "").strip()
    regions = []
    if hasattr(page_result, "json_result"):
        jr = page_result.json_result
        if isinstance(jr, dict) and "regions" in jr:
            regions = jr.get("regions") or []
        elif isinstance(jr, list) and len(jr) > 0:
            r = jr[0] if isinstance(jr[0], list) else jr
            if isinstance(r, list):
                regions = r
            elif isinstance(r, dict) and "regions" in r:
                regions = r.get("regions") or []
    return md, regions


def run_ocr(uploaded_file):
    if uploaded_file is None:
        return "Please upload a file."
    try:
        import pymupdf as fitz

        path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
        is_pdf = path.lower().endswith(".pdf")
        log.info("run_ocr: path=%s is_pdf=%s", path, is_pdf)
        parser = get_parser()

        if is_pdf:
            doc = fitz.open(path)
            page_images = []
            for i in range(len(doc)):
                pix = doc[i].get_pixmap(matrix=fitz.Matrix(1.5, 1.5), alpha=False)
                img_path = os.path.join(tempfile.gettempdir(), f"maas_page_{i}.png")
                pix.save(img_path)
                page_images.append(img_path)
            doc.close()
            log.info("run_ocr: PDF has %d pages, running MaaS on page images", len(page_images))
            results = parser.parse(page_images)
        else:
            page_images = [path]
            results = parser.parse(path)

        if not isinstance(results, list):
            results = [results]

        all_pages = []
        for page_num, page_result in enumerate(results):
            page_md, regions = get_page_md_and_regions(page_result)
            header_end_frac, footer_start_frac = get_header_footer_zones(regions, 1000)

            # MaaS does not return header/footer regions; always use at least default bands
            he = header_end_frac if header_end_frac is not None else DEFAULT_ZONE_FRAC
            fs = footer_start_frac if footer_start_frac is not None else (1.0 - DEFAULT_ZONE_FRAC)
            if he <= 0:
                he = DEFAULT_ZONE_FRAC  # ensure we always OCR a top band
            if fs >= 1.0:
                fs = 1.0 - DEFAULT_ZONE_FRAC  # ensure we always OCR a bottom band

            log.info(
                "run_ocr: page %d regions=%s he=%.3f fs=%.3f body_md_len=%d",
                page_num, len(regions), he, fs, len(page_md or ""),
            )

            parts = []

            # Always run header band (top 8–12% of page)
            if page_num < len(page_images):
                img_path = page_images[page_num]
                hdr = ""
                if is_pdf:
                    hdr = extract_zone_text_pdf(path, page_num, 0, he)
                    if hdr and hdr.strip():
                        log.info("run_ocr: page %d header from PDF extract: %d chars", page_num, len(hdr.strip()))
                if not (hdr and hdr.strip()):
                    hdr = ocr_zone(img_path, 0, he)
                if hdr and hdr.strip():
                    parts.append(hdr.strip())
                elif not (hdr and hdr.strip()) and he > 0:
                    log.info("run_ocr: page %d header empty (PDF extract + ocr_zone)", page_num)

            if page_md:
                parts.append(page_md)

            # Always run footer band (bottom 8–12% of page)
            if page_num < len(page_images):
                img_path = page_images[page_num]
                ftr = ""
                if is_pdf:
                    ftr = extract_zone_text_pdf(path, page_num, fs, 1.0)
                    if ftr and ftr.strip():
                        log.info("run_ocr: page %d footer from PDF extract: %d chars", page_num, len(ftr.strip()))
                if not (ftr and ftr.strip()):
                    ftr = ocr_zone(img_path, fs, 1.0)
                if ftr and ftr.strip():
                    parts.append(ftr.strip())
                elif not (ftr and ftr.strip()) and fs < 1.0:
                    log.info("run_ocr: page %d footer empty (PDF extract + ocr_zone)", page_num)

            if parts:
                all_pages.append("\n\n".join(parts))

        log.info("run_ocr: done, %d pages in output", len(all_pages))
        return "\n\n---\n\n".join(all_pages) if all_pages else "(No content)"
    except Exception as e:
        import traceback
        log.exception("run_ocr failed: %s", e)
        return f"Error: {e}\n\n{traceback.format_exc()}"


with gr.Blocks(title="GLM-OCR") as demo:
    gr.Markdown("# GLM-OCR\nUpload a PDF or image. Headers and footers included.")
    file_in = gr.File(label="Upload PDF or image", file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"])
    run_btn = gr.Button("Run OCR", variant="primary")
    out = gr.Textbox(lines=40, label="Output")
    run_btn.click(fn=run_ocr, inputs=file_in, outputs=out)

if __name__ == "__main__":
    demo.launch()