Spaces:
Running on Zero
Running on Zero
multimodalart HF Staff
Bound output tokens; per-task GPU duration sized from measurement
9c800f4 verified Download app.py from hugging-apps/typhoon-ocr2-2b: direct link, hf CLI and curl.
- Browser
- Download file 14.4 kB
-
https://huggingface.co/spaces/hugging-apps/typhoon-ocr2-2b/resolve/main/app.py
- Command line
-
hf download hf://spaces/hugging-apps/typhoon-ocr2-2b/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hugging-apps/typhoon-ocr2-2b/resolve/main/app.py
14.4 kB
| import spaces # ZeroGPU: must come before torch / CUDA-touching imports | |
| import json | |
| import re | |
| import time | |
| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| from transformers import AutoModelForImageTextToText, AutoProcessor | |
| MODEL_ID = "typhoon-ai/typhoon-ocr2-2b" | |
| # The model card allows up to 16,384 new tokens, but a page that long decodes | |
| # for ~9 minutes at the ~30 tok/s measured on this Space — far past any ZeroGPU | |
| # per-call reservation. It also documents a repetition-loop failure mode | |
| # ("repeating text until it reaches the token limit ... most on dense | |
| # multi-column pages"), where truncation is the graceful outcome: parse_elements() | |
| # below keeps every complete element. The densest example page emits ~1,230 | |
| # tokens, so 2,048 tokens gives ~65% headroom over real pages while keeping the | |
| # worst case inside the GPU reservation (~71 s at 30 tok/s). Questions want a | |
| # short answer and get a tighter cap so their reservation can stay small. | |
| MAX_NEW_TOKENS = 2048 | |
| MAX_NEW_TOKENS_QA = 1024 | |
| # --------------------------------------------------------------------------- | |
| # Model + processor, loaded at module scope and moved to CUDA eagerly. | |
| # (The model card uses device_map="auto"; on ZeroGPU .to("cuda") is the rule.) | |
| # --------------------------------------------------------------------------- | |
| model = AutoModelForImageTextToText.from_pretrained(MODEL_ID, dtype=torch.bfloat16).to("cuda") | |
| processor = AutoProcessor.from_pretrained(MODEL_ID) | |
| # --------------------------------------------------------------------------- | |
| # Task modes | |
| # --------------------------------------------------------------------------- | |
| TASK_PARSE = "Full-page parsing (layout -> Markdown)" | |
| TASK_KIE = "Key information extraction (JSON)" | |
| TASK_QA = "Ask a question about the document" | |
| DEFAULT_TASK = TASK_PARSE | |
| # --------------------------------------------------------------------------- | |
| # Prompts and helpers below are copied from the model card (Usage section). | |
| # --------------------------------------------------------------------------- | |
| PARSING_PROMPT = """- Extract layout information from the provided PDF image. | |
| - For each layout element, output its bbox, category, and the text content within the bbox. | |
| - Bbox format: [x1, y1, x2, y2]. | |
| - Allowed layout categories: ['header', 'title', 'text', 'figure', 'table', 'formula', 'figure_caption', 'table_caption', 'formula_caption', 'figure_footnote', 'table_footnote', 'page_footnote', 'footer']. | |
| - Text extraction and formatting: | |
| 1) For 'figure', the text field must be an empty string. | |
| 2) For 'formula', format text as LaTeX. | |
| 3) For 'table', format text as HTML. | |
| 4) For all other categories (e.g., text, title), format text as Markdown. | |
| - The output text must be exactly the original text from the image, with no translation or rewriting. | |
| - Sort all layout elements in human reading order. | |
| - Final output must be a single JSON object.""" | |
| KIE_PROMPT = ( | |
| "Perform open-schema key information extraction (WYSIWYG) on this image: " | |
| "one JSON object, printed labels as keys, values copied exactly, \"\" for blank " | |
| "fields, arrays for repeated rows. Return only JSON." | |
| ) | |
| # Prompts from the model card's example gallery (verbatim, in Thai). | |
| SNACK_PROMPT = "จากรูปภาพกรุณาดึงข้อมูล เปอร์เซ็นต์ของส่วนประกอบแต่ละอย่าง จากรูปภาพเป็นรูปแบบ JSON เป็นภาษาไทย เฉพาะอันที่มีเปอร์เซ็นต์\n\nโดยมี key ตามนี้\nมะพร้าว\nน้ำตาล\nเกลือสมุทร\nกลิ่นคาราเมล\nตอบเฉพาะข้อความตามภาพตรง ๆ อย่างกระชับ โดยไม่ใส่คำอธิบายเพิ่มเติม" | |
| PM25_PROMPT = "ตามข้อมูลในภาพกรุณาจับคู่รายการระดับความเข้มข้น PM2.5 (จำนวนจำกัด) เกี่ยวข้องดังต่อไปนี้: 'ดีมาก', 'ดี', 'ปานกลาง', 'เริ่มมีผลกระทบต่อสุขภาพ' และ 'มีผลกระทบต่อสุขภาพ' กับค่าที่จะถูกจับคู่ได้แก่ '7 จังหวัด', '0 จังหวัด', '28 จังหวัด', '16 จังหวัด' และ '25 จังหวัด' เลือกรายละเอียดจากเนื้อหาในเอกสารแล้วให้คำตอบกลับมาในรูปแบบ JSON โดยตรง:\nตอบเฉพาะข้อความตามภาพตรง ๆ อย่างกระชับ โดยไม่ใส่คำอธิบายเพิ่มเติม" | |
| HANDWRITTEN_PROMPT = "ข้อที่ 4 ในภาพนี้ เป็นข้อความที่เขียนด้วยมือว่าอะไรบ้าง ตอบเฉพาะข้อความตามภาพตรง ๆ อย่างกระชับ โดยไม่ใส่คำอธิบายเพิ่มเติม" | |
| REGION_PROMPT = "แบ่งความยาวและความสูงของรูปภาพออกเป็น 1000 ส่วน แล้วช่วยดึงข้อความที่อยู่ในพิกัด [786, 615, 931, 675] ของรูปภาพออกมาให้หน่อย ตอบเฉพาะข้อความตามภาพตรง ๆ อย่างกระชับ โดยไม่ใส่คำอธิบายเพิ่มเติม" | |
| def parse_elements(raw: str) -> list: | |
| """Parse the model output: a JSON list of elements inside a ```json fence.""" | |
| m = re.search(r"```json\n(.*?)(?:\n```|$)", raw, re.DOTALL) | |
| text = (m.group(1) if m else raw).strip() | |
| try: | |
| return json.loads(text) | |
| except json.JSONDecodeError: | |
| # Output stopped at the token limit: drop the last, incomplete element. | |
| cut = text.rfind('{"bbox":') | |
| return json.loads(text[:cut].rstrip().rstrip(",") + "]") | |
| def to_markdown(elements: list, keep_header_footer: bool = False) -> str: | |
| skip = set() if keep_header_footer else {"header", "footer", "page_footnote"} | |
| return "\n\n".join( | |
| e["text"] for e in elements if e.get("text") and e["category"] not in skip | |
| ) | |
| def upscale_min_long(image: Image.Image, min_long: int = 1600) -> Image.Image: | |
| """Enlarge the image so that its long side is at least `min_long` pixels, | |
| keeping the aspect ratio. Images already large enough are unchanged.""" | |
| scale = min_long / max(image.size) | |
| if scale <= 1: | |
| return image | |
| return image.resize( | |
| (round(image.width * scale), round(image.height * scale)), Image.LANCZOS | |
| ) | |
| def _resolve_prompt(task: str, question: str) -> str: | |
| if task == TASK_PARSE: | |
| return PARSING_PROMPT | |
| if task == TASK_KIE: | |
| return KIE_PROMPT | |
| return question.strip() | |
| def _duration(image, task=TASK_PARSE, question="", *args, **kwargs): | |
| """Per-task GPU reservation (seconds). | |
| Sized from durations measured on this Space against the model authors' | |
| own example pages: full-page parsing 41 s, key-info extraction 17 s, | |
| question answering 2 s. Each value covers that mode's worst case at the | |
| measured ~30 tok/s (2,048 tokens -> ~71 s; 1,024 -> ~37 s) so a long | |
| decodes finishes inside its reservation instead of aborting, while the | |
| shorter modes keep a small reservation for queue priority and quota. | |
| Extras are swallowed: Gradio injects `progress=` positionally. | |
| """ | |
| if task == TASK_QA: | |
| return 45 | |
| return 90 | |
| def run_ocr( | |
| image, | |
| task: str = DEFAULT_TASK, | |
| question: str = "", | |
| keep_header_footer: bool = False, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| """Run Typhoon OCR V2 2B on a document image. | |
| Args: | |
| image: the document page / photo to read. | |
| task: one of "Full-page parsing (layout -> Markdown)", | |
| "Key information extraction (JSON)" or "Ask a question about the document". | |
| question: the question to ask (used only in question mode). | |
| keep_header_footer: in parsing mode, also keep header/footer/page-footnote | |
| elements in the Markdown output. | |
| Returns: | |
| Tuple of (markdown, raw_output, seconds): the formatted result, the raw | |
| model output, and the wall-clock seconds the generation took. | |
| """ | |
| t0 = time.perf_counter() | |
| prompt = _resolve_prompt(task, question) | |
| if not prompt: | |
| raise gr.Error("Enter a question for the document, or pick another task.") | |
| pil_image = Image.open(image).convert("RGB") if isinstance(image, str) else image | |
| if pil_image is None: | |
| raise gr.Error("Upload a document image first.") | |
| pil_image = upscale_min_long(pil_image.convert("RGB")) | |
| messages = [{"role": "user", "content": [ | |
| {"type": "image", "image": pil_image}, | |
| {"type": "text", "text": prompt}, | |
| ]}] | |
| inputs = processor.apply_chat_template( | |
| messages, | |
| tokenize=True, | |
| add_generation_prompt=True, | |
| return_dict=True, | |
| return_tensors="pt", | |
| enable_thinking=False, | |
| processor_kwargs={"images_kwargs": {"min_pixels": 2048, "max_pixels": 16777216}}, | |
| ).to(model.device) | |
| max_new = MAX_NEW_TOKENS_QA if task == TASK_QA else MAX_NEW_TOKENS | |
| with torch.inference_mode(): | |
| out = model.generate(**inputs, max_new_tokens=max_new, do_sample=False) | |
| raw = processor.decode( | |
| out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True | |
| ) | |
| seconds = round(time.perf_counter() - t0, 1) | |
| if task == TASK_PARSE: | |
| try: | |
| elements = parse_elements(raw) | |
| md = to_markdown(elements, keep_header_footer=keep_header_footer) | |
| if not md.strip(): | |
| md = "_No text elements found on the page._" | |
| except (json.JSONDecodeError, KeyError, IndexError, TypeError): | |
| md = "_Could not parse the model output as layout JSON — see the raw output._" | |
| else: | |
| md = raw.strip() | |
| return md, raw, seconds | |
| # --------------------------------------------------------------------------- | |
| # Example gallery (images and prompts from the model card) | |
| # --------------------------------------------------------------------------- | |
| EXAMPLES = [ | |
| ("assets/parsing_exam_input.jpg", TASK_PARSE, "", "Full-page parsing — Thai exam paper"), | |
| ("assets/parsing_book_input.jpg", TASK_PARSE, "", "Full-page parsing — Thai training handout"), | |
| ("assets/kie_receipt.jpg", TASK_KIE, "", "Key info — restaurant bill"), | |
| ("assets/kie_infographic.jpg", TASK_KIE, "", "Key info — rice-seed infographic"), | |
| ("assets/kie_snack.jpg", TASK_QA, SNACK_PROMPT, "Ingredients % — snack bag"), | |
| ("assets/kim_pm25.jpg", TASK_QA, PM25_PROMPT, "KIE mapping — PM2.5 advisory table"), | |
| ("assets/handwritten.jpg", TASK_QA, HANDWRITTEN_PROMPT, "Handwriting — lecture notes"), | |
| ("assets/region_sign.jpg", TASK_QA, REGION_PROMPT, "Region reading — street sign"), | |
| ] | |
| CSS = """ | |
| #col-container { max-width: 1200px; margin: 0 auto; } | |
| .dark .gradio-container { color: var(--body-text-color); } | |
| """ | |
| with gr.Blocks(title="Typhoon OCR V2 2B") as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown( | |
| """# Typhoon OCR V2 2B | |
| Thai/English OCR and document parsing: one image in, structured text out — full-page | |
| layout parsing (bboxes → Markdown/HTML/LaTeX), open-schema key-information extraction | |
| as JSON, or free-form questions about the document. | |
| Model: [typhoon-ai/typhoon-ocr2-2b](https://huggingface.co/typhoon-ai/typhoon-ocr2-2b) · | |
| 2.2B params · Apache-2.0 weights (use subject to OpenTyphoon Terms)""" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| image_in = gr.Image( | |
| type="pil", label="Document image", | |
| sources=["upload", "clipboard"], height=420, | |
| ) | |
| task = gr.Radio( | |
| [TASK_PARSE, TASK_KIE, TASK_QA], | |
| value=DEFAULT_TASK, | |
| label="Task", | |
| info=( | |
| "Parsing uses the model's fixed layout prompt; key-info extraction " | |
| "pulls all fields as JSON; question mode asks anything." | |
| ), | |
| ) | |
| question = gr.Textbox( | |
| label="Question (question mode only)", | |
| placeholder="e.g. ข้อที่ 4 ในภาพนี้เขียนว่าอะไรบ้าง?", | |
| lines=2, | |
| ) | |
| with gr.Accordion("Advanced", open=False): | |
| keep_header_footer = gr.Checkbox( | |
| value=False, | |
| label="Keep header/footer/page-footnote in Markdown", | |
| info="Parsing mode only. The model card's evaluation drops them.", | |
| ) | |
| run_btn = gr.Button("Run OCR", variant="primary") | |
| with gr.Column(scale=1): | |
| md_out = gr.Markdown(label="Result") | |
| with gr.Accordion("Raw model output", open=False): | |
| raw_out = gr.Code(language="markdown", label="Raw output") | |
| time_out = gr.Number(label="Seconds", precision=1, interactive=False) | |
| gr.Examples( | |
| examples=[[e[0], e[1], e[2]] for e in EXAMPLES], | |
| inputs=[image_in, task, question], | |
| outputs=[md_out, raw_out, time_out], | |
| fn=run_ocr, | |
| example_labels=[e[3] for e in EXAMPLES], | |
| cache_examples=True, | |
| cache_mode="lazy", | |
| api_visibility="undocumented", | |
| ) | |
| run_btn.click( | |
| run_ocr, | |
| inputs=[image_in, task, question, keep_header_footer], | |
| outputs=[md_out, raw_out, time_out], | |
| api_name="run_ocr", | |
| ) | |
| demo.launch(mcp_server=True, theme=gr.themes.Citrus(), css=CSS) | |