Spaces:
Running
Running
Download app.py from hugging-apps/aksonocr-lite: direct link, hf CLI and curl.
- Browser
- Download file 8.22 kB
-
https://huggingface.co/spaces/hugging-apps/aksonocr-lite/resolve/main/app.py
- Command line
-
hf download hf://spaces/hugging-apps/aksonocr-lite/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hugging-apps/aksonocr-lite/resolve/main/app.py
8.22 kB
| """AksonOCR-Lite — Thai/English document OCR on ONNX Runtime. | |
| Upload a page image or a PDF and get Markdown (with layout headings, reading order and | |
| HTML tables), the layout/text-line overlay, and the full JSON (blocks, lines, boxes). | |
| Adapted from the authors' own playground (tools/playground.py, Apache-2.0): | |
| https://github.com/Remtir/AksonOCR-Lite | |
| Weights: https://huggingface.co/Remtir/AksonOCR-Lite | |
| """ | |
| import json | |
| import tempfile | |
| import time | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| import aksonocr_lite | |
| from aksonocr_lite.inputs import load_pages | |
| from aksonocr_lite.orient import rotate | |
| DEVICE = "cpu" # 81M-param ONNX pipeline: ~1.5 s/page on CPU, no GPU needed | |
| PINK = (150, 87, 235) # BGR | |
| COLORS = {"table": (60, 160, 60), "image": (200, 140, 40), "chart": (200, 140, 40), "seal": (200, 140, 40), | |
| "doc_title": (40, 40, 200), "paragraph_title": (40, 90, 220), "header": (140, 140, 140), | |
| "footer": (140, 140, 140), "number": (140, 140, 140)} | |
| _CONVERTERS: dict[bool, aksonocr_lite.Converter] = {} | |
| _DEWARPERS: dict[str, object] = {} | |
| def _converter(tables: bool) -> aksonocr_lite.Converter: | |
| """Converter cache keyed by the `tables` option (ONNX sessions are lru-cached and shared).""" | |
| if tables not in _CONVERTERS: | |
| _CONVERTERS[tables] = aksonocr_lite.load(device=DEVICE, tables=tables) | |
| return _CONVERTERS[tables] | |
| _converter(True) # eager boot: fetch weights from the Hub once and build every ONNX session | |
| def overlay(img: np.ndarray, page, show_blocks: bool, show_lines: bool) -> np.ndarray: | |
| """Draw layout blocks (in reading order) and text-line polygons on a BGR page; return RGB.""" | |
| vis = img.copy() | |
| s = max(1, round(max(img.shape[:2]) / 1200)) | |
| if show_lines: | |
| for l in page.lines: | |
| cv2.polylines(vis, [np.int32(l.poly)], True, PINK, s) | |
| if show_blocks: | |
| for b in sorted(page.blocks, key=lambda b: b.order): | |
| c = COLORS.get(b.label, (200, 90, 30)) | |
| x0, y0, x1, y1 = map(int, b.bbox) | |
| cv2.rectangle(vis, (x0, y0), (x1, y1), c, 2 * s) | |
| tag = f"{int(b.order) + 1} {b.label}" | |
| (tw, th), _ = cv2.getTextSize(tag, cv2.FONT_HERSHEY_SIMPLEX, 0.5 * s, s) | |
| cv2.rectangle(vis, (x0, max(0, y0 - th - 6 * s)), (x0 + tw + 6 * s, y0), c, -1) | |
| cv2.putText(vis, tag, (x0 + 3 * s, y0 - 4 * s), cv2.FONT_HERSHEY_SIMPLEX, 0.5 * s, (255, 255, 255), s, | |
| cv2.LINE_AA) | |
| return cv2.cvtColor(vis, cv2.COLOR_BGR2RGB) | |
| def run_ocr(file, dewarp: bool = False, tables: bool = True, show_blocks: bool = True, | |
| show_lines: bool = True, max_pages: int = 5): | |
| """Run Thai/English OCR on an uploaded page image or PDF. | |
| Returns the Markdown (with headings, reading order and HTML tables), an overlay image per | |
| page showing the detected layout blocks and text lines, and the full layout JSON. | |
| Args: | |
| file: an image (png/jpg/webp/...) or a PDF file. | |
| dewarp: flatten photographed/curled pages first (downloads extra weights once). | |
| tables: parse detected tables into HTML tables with the Table Transformer. | |
| show_blocks: draw layout blocks in reading order on the overlay images. | |
| show_lines: draw detected text-line polygons on the overlay images. | |
| max_pages: maximum number of PDF pages to process. | |
| """ | |
| if file is None: | |
| raise gr.Error("Upload an image or a PDF first.") | |
| conv = _converter(bool(tables)) | |
| if dewarp and "d" not in _DEWARPERS: | |
| from aksonocr_lite.dewarp import Dewarper | |
| _DEWARPERS["d"] = Dewarper(DEVICE) | |
| dewarper = _DEWARPERS.get("d") if dewarp else None | |
| mds, pages, gallery, t0 = [], [], [], time.perf_counter() | |
| try: | |
| for i, img in enumerate(load_pages(file, max_pages=int(max_pages))): | |
| if dewarper is not None: | |
| img, _ = dewarper(img) | |
| page = conv.page(img, index=i) | |
| vis = overlay(rotate(img, page.rotation), page, show_blocks, show_lines) | |
| gallery.append((vis, f"page {i + 1}")) | |
| mds.append(page.markdown) | |
| pages.append(page.to_dict()) | |
| except gr.Error: | |
| raise | |
| except Exception as e: # unsupported file, corrupt PDF, ... | |
| raise gr.Error(f"OCR failed: {e}") from e | |
| if not pages: | |
| raise gr.Error("No pages found in that file.") | |
| secs = time.perf_counter() - t0 | |
| md = "\n\n---\n\n".join(mds) | |
| stats = (f"**{len(pages)} page(s) in {secs:.2f} s** ({secs / max(1, len(pages)):.2f} s/page) · " | |
| f"{sum(len(p['lines']) for p in pages)} lines · {sum(len(p['blocks']) for p in pages)} blocks" | |
| + (" · dewarped" if dewarp else "")) | |
| out = Path(tempfile.mkdtemp()) / f"{Path(file).stem or 'page'}.md" | |
| out.write_text(md, encoding="utf-8") | |
| return stats, md, md, gallery, json.dumps(pages, ensure_ascii=False, indent=1), str(out) | |
| CSS = """ | |
| #col-container { max-width: 1500px; margin: 0 auto; } | |
| .dark .gradio-container { color: var(--body-text-color); } | |
| .md-out { font-family: 'Sarabun', 'Noto Sans Thai', sans-serif; } | |
| footer { display: none !important; } | |
| """ | |
| INTRO = """ | |
| # AksonOCR-Lite | |
| Fast **Thai + English document OCR** on ONNX Runtime (81M params, ~330 MB of weights, no GPU needed). | |
| Upload a page image or a PDF and get **Markdown** with layout headings, reading order and HTML tables, | |
| plus the full layout JSON — blocks, text-line boxes, confidences and reading order. | |
| [GitHub](https://github.com/Remtir/AksonOCR-Lite) · [Weights](https://huggingface.co/Remtir/AksonOCR-Lite) · [Benchmarks](https://github.com/Remtir/AksonOCR-Lite/blob/main/BENCHMARKS.md) | |
| """ | |
| EXAMPLES = ["examples/thai-letter.png", "examples/financial-statement.png", "examples/handwritten-page.png"] | |
| with gr.Blocks(title="AksonOCR-Lite") as demo: | |
| gr.Markdown(INTRO) | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=320): | |
| f = gr.File(label="Image or PDF", file_types=["image", ".pdf"], type="filepath") | |
| with gr.Accordion("Options", open=True): | |
| dw = gr.Checkbox(label="Dewarp photographed pages (downloads extra weights once)", value=False) | |
| tb = gr.Checkbox(label="Tables to HTML (Table Transformer)", value=True) | |
| sb = gr.Checkbox(label="Show layout blocks", value=True) | |
| sl = gr.Checkbox(label="Show text lines", value=True) | |
| mp = gr.Slider(1, 20, value=5, step=1, label="Max PDF pages") | |
| go = gr.Button("Run OCR", variant="primary") | |
| stats = gr.Markdown() | |
| dl = gr.File(label="Download Markdown") | |
| with gr.Column(scale=2): | |
| with gr.Tabs(): | |
| with gr.Tab("Overlay"): | |
| gal = gr.Gallery(label="Blocks (reading order) and text lines", columns=1, height=760, | |
| object_fit="contain") | |
| with gr.Tab("Rendered"): | |
| rendered = gr.Markdown(elem_classes="md-out") | |
| with gr.Tab("Markdown"): | |
| raw = gr.Code(language="markdown", lines=30) | |
| with gr.Tab("JSON"): | |
| js = gr.Code(language="json", lines=30) | |
| gr.Examples( | |
| examples=[[p] for p in EXAMPLES], | |
| inputs=[f], | |
| outputs=[stats, rendered, raw, gal, js, dl], | |
| fn=run_ocr, | |
| cache_examples=True, | |
| cache_mode="lazy", | |
| label="Try the authors' showcase pages", | |
| ) | |
| gr.Markdown("<sub>Pipeline: page orientation → PP-DocLayoutV3 layout + reading order → PP-OCRv6 text lines → " | |
| "grapheme-cluster Thai recognizer → Table Transformer v1.1. Example images: the pages showcased in " | |
| "the [AksonOCR-Lite README](https://github.com/Remtir/AksonOCR-Lite) (Apache-2.0). " | |
| "App adapted from the authors' playground.</sub>") | |
| go.click(run_ocr, inputs=[f, dw, tb, sb, sl, mp], outputs=[stats, rendered, raw, gal, js, dl], api_name="run_ocr") | |
| f.upload(run_ocr, inputs=[f, dw, tb, sb, sl, mp], outputs=[stats, rendered, raw, gal, js, dl], api_name=None) | |
| demo.queue(default_concurrency_limit=2).launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True) | |