Spaces:
Configuration error
Configuration error
Make equation multipass optional and integrate region OCR across all outputs
Browse files577 unmodified lines57857958058158258358410 unmodified lines59559659759859960060177 unmodified lines67968068168268368468513 unmodified lines6997007017027037047057 unmodified lines7137147157167177187196 unmodified lines7267277287297307317324 unmodified lines7377387397407417427437447457467477487497507519 unmodified lines76176276376476576676776876977077177256 unmodified lines82983083183283383462 unmodified lines89789889990090190217 unmodified lines920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954577 unmodified linesmarkdown = markdown.replace(f'**[Figure {i + 1}]**', f'\n\n\n\n', 1)return markdowndef _infer_with_prompt(image, prompt):tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')image.save(tmp.name, 'JPEG', quality=95)tmp.close()10 unmodified linesoutput_path=out_dir,base_size=BASE_SIZE,image_size=IMAGE_SIZE,crop_mode=CROP_MODE,save_results=False)finally:77 unmodified linesreturn refined_refs@spaces.GPU(duration=90)def process_image(image, task, custom_prompt):model.cuda() # GPU is available here — works on ZeroGPU and locallyif image is None:return "Error: Upload an image", "", "", None, []13 unmodified lineselse:prompt = TASK_PROMPTS[task]["prompt"]has_grounding = TASK_PROMPTS[task]["has_grounding"]result = _infer_with_prompt(image, prompt)if not result:return "No text detected", "", "", None, []7 unmodified linesif has_grounding and '<|ref|>' in result:refs = extract_grounding_references(result)if task == "📋 Markdown":refs.extend(_refine_equation_refs(image, result))if refs:img_out, crops = draw_bounding_boxes(image, refs, True)6 unmodified linesreturn cleaned, markdown, result_for_layout, img_out, crops@spaces.GPU(duration=90)def process_pdf(path, task, custom_prompt, page_num):doc = fitz.open(path)total_pages = len(doc)if page_num < 1 or page_num > total_pages:4 unmodified linesimg = Image.open(BytesIO(pix.tobytes("png")))doc.close()return process_image(img, task, custom_prompt)def process_file(path, task, custom_prompt, page_num):if not path:return "Error: Upload a file", "", "", None, []if path.lower().endswith('.pdf'):return process_pdf(path, task, custom_prompt, page_num)else:return process_image(Image.open(path), task, custom_prompt)def _extract_editor_image(editor_value):if editor_value is None:9 unmodified linesreturn backgroundreturn Nonedef process_region_ocr(editor_value):image = _extract_editor_image(editor_value)if image is None:return "Draw/crop a region first, then click OCR Region.", ""text, markdown, _, _, _ = process_image(image, "📝 Free OCR", "")return text, to_math_html(markdown)def toggle_prompt(task):if task == "✏️ Custom":56 unmodified linesinput_img = gr.Image(label="Input Image", type="pil", height=300)page_selector = gr.Number(label="Select Page", value=1, minimum=1, step=1, visible=False)task = gr.Dropdown(list(TASK_PROMPTS.keys()), value="📋 Markdown", label="Task")prompt = gr.Textbox(label="Prompt", lines=2, visible=False)btn = gr.Button("Extract", variant="primary", size="lg")with gr.Accordion("Region OCR (Draw/Crop)", open=False):62 unmodified lines- **Describe**: General image description- **Custom**: Your own prompt- **Region OCR (new)**: In the left panel, open **Region OCR (Draw/Crop)**, draw/crop a target area, then click **OCR Region**### Free OCR vs Locate (important)- **Free OCR does not take a selected region**. It runs OCR on the whole image/page.17 unmodified linesfile_in.change(load_image, [file_in, page_selector], [region_editor])page_selector.change(load_image, [file_in, page_selector], [region_editor])input_img.change(lambda img: img, [input_img], [region_editor])region_btn.click(process_region_ocr, [region_editor], [region_text_out, region_html_out])def run(image, file_path, task, custom_prompt, page_num):if file_path:cleaned, markdown, raw, img_out, crops = process_file(file_path, task, custom_prompt, int(page_num))elif image is not None:cleaned, markdown, raw, img_out, crops = process_image(image, task, custom_prompt)else:return "Error: Upload a file or image", "", "", "", "", "", "", None, [], gr.DownloadButton(visible=False)# Text tab: convert \[...\] → $$...$$ and \(...\) → $...$ for readabilitytext_display = re.sub(r'\\\[(.+?)\\\]',lambda m: f'\n$$\n{m.group(1).strip()}\n$$\n',cleaned, flags=re.DOTALL)text_display = re.sub(r'\\\((.+?)\\\)', lambda m: f'${m.group(1).strip()}$', text_display)# Download file: write cleaned markdown to a temp .md filedl_tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.md', mode='w', encoding='utf-8')dl_tmp.write(cleaned)dl_tmp.close()mathjax_html = to_mathjax_html(markdown)spatial_html = to_spatial_html(raw, markdown)return (text_display, to_math_html(markdown), mathjax_html, mathjax_html, spatial_html, spatial_html, raw, img_out, crops,gr.DownloadButton(value=dl_tmp.name, visible=True))submit_event = btn.click(run, [input_img, file_in, task, prompt, page_selector],[text_out, md_out, html_out, html_source_out, spatial_out, spatial_source_out, raw_out, img_out, gallery, download_btn])submit_event.then(select_boxes, [task], [tabs])if __name__ == "__main__":577 unmodified lines57857958058158258358458558610 unmodified lines59759859960060160260377 unmodified lines68168268368468568668713 unmodified lines7017027037047057067077 unmodified lines7157167177187197207216 unmodified lines7287297307317327337344 unmodified lines7397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717729 unmodified lines78278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785856 unmodified lines91591691791891992092162 unmodified lines98498598698798898999017 unmodified lines100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046577 unmodified linesmarkdown = markdown.replace(f'**[Figure {i + 1}]**', f'\n\n\n\n', 1)return markdowndef _infer_with_prompt(image, prompt, crop_mode=None):if crop_mode is None:crop_mode = CROP_MODEtmp = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')image.save(tmp.name, 'JPEG', quality=95)tmp.close()10 unmodified linesoutput_path=out_dir,base_size=BASE_SIZE,image_size=IMAGE_SIZE,crop_mode=crop_mode,save_results=False)finally:77 unmodified linesreturn refined_refs@spaces.GPU(duration=90)def process_image(image, task, custom_prompt, enable_equation_zoom=True, infer_crop_mode=None):model.cuda() # GPU is available here — works on ZeroGPU and locallyif image is None:return "Error: Upload an image", "", "", None, []13 unmodified lineselse:prompt = TASK_PROMPTS[task]["prompt"]has_grounding = TASK_PROMPTS[task]["has_grounding"]result = _infer_with_prompt(image, prompt, crop_mode=infer_crop_mode)if not result:return "No text detected", "", "", None, []7 unmodified linesif has_grounding and '<|ref|>' in result:refs = extract_grounding_references(result)if task == "📋 Markdown" and enable_equation_zoom:refs.extend(_refine_equation_refs(image, result))if refs:img_out, crops = draw_bounding_boxes(image, refs, True)6 unmodified linesreturn cleaned, markdown, result_for_layout, img_out, crops@spaces.GPU(duration=90)def process_pdf(path, task, custom_prompt, page_num, enable_equation_zoom=True, infer_crop_mode=None):doc = fitz.open(path)total_pages = len(doc)if page_num < 1 or page_num > total_pages:4 unmodified linesimg = Image.open(BytesIO(pix.tobytes("png")))doc.close()return process_image(img,task,custom_prompt,enable_equation_zoom=enable_equation_zoom,infer_crop_mode=infer_crop_mode,)def process_file(path, task, custom_prompt, page_num, enable_equation_zoom=True, infer_crop_mode=None):if not path:return "Error: Upload a file", "", "", None, []if path.lower().endswith('.pdf'):return process_pdf(path,task,custom_prompt,page_num,enable_equation_zoom=enable_equation_zoom,infer_crop_mode=infer_crop_mode,)else:return process_image(Image.open(path),task,custom_prompt,enable_equation_zoom=enable_equation_zoom,infer_crop_mode=infer_crop_mode,)def _extract_editor_image(editor_value):if editor_value is None:9 unmodified linesreturn backgroundreturn Nonedef _dedupe_consecutive_lines(text: str) -> str:if not text:return textout = []prev = Noneblank_count = 0for line in text.splitlines():if not line.strip():blank_count += 1if blank_count <= 2:out.append("")continueblank_count = 0norm = re.sub(r'\s+', ' ', line).strip()if norm and norm == prev:continueout.append(line)prev = normreturn "\n".join(out).strip()def _compose_ui_outputs(cleaned, markdown, raw, img_out, gallery_items):text_display = re.sub(r'\\\[(.+?)\\\]',lambda m: f'\n$$\n{m.group(1).strip()}\n$$\n',cleaned,flags=re.DOTALL)text_display = re.sub(r'\\\((.+?)\\\)', lambda m: f'${m.group(1).strip()}$', text_display)dl_tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.md', mode='w', encoding='utf-8')dl_tmp.write(cleaned)dl_tmp.close()markdown_html = to_math_html(markdown)mathjax_html = to_mathjax_html(markdown)spatial_html = to_spatial_html(raw, markdown)return (text_display,markdown_html,mathjax_html,mathjax_html,spatial_html,spatial_html,raw,img_out,gallery_items,gr.DownloadButton(value=dl_tmp.name, visible=True),text_display,markdown_html,)def run_region(editor_value, task, custom_prompt, enable_equation_zoom):image = _extract_editor_image(editor_value)if image is None:msg = "Draw/crop a region first, then click OCR Region."return (msg, "", "", "", "", "", "", None, [], gr.DownloadButton(visible=False), msg, "")cleaned, markdown, raw, img_out, crops = process_image(image,task,custom_prompt,enable_equation_zoom=enable_equation_zoom,infer_crop_mode=False,)# Region workflows are single-area; collapse obvious duplicate lines.cleaned = _dedupe_consecutive_lines(cleaned)markdown = _dedupe_consecutive_lines(markdown)gallery_items = [image] + (crops or [])return _compose_ui_outputs(cleaned, markdown, raw, img_out, gallery_items)def toggle_prompt(task):if task == "✏️ Custom":56 unmodified linesinput_img = gr.Image(label="Input Image", type="pil", height=300)page_selector = gr.Number(label="Select Page", value=1, minimum=1, step=1, visible=False)task = gr.Dropdown(list(TASK_PROMPTS.keys()), value="📋 Markdown", label="Task")equation_zoom = gr.Checkbox(label="Equation Zoom (multipass)", value=False)prompt = gr.Textbox(label="Prompt", lines=2, visible=False)btn = gr.Button("Extract", variant="primary", size="lg")with gr.Accordion("Region OCR (Draw/Crop)", open=False):62 unmodified lines- **Describe**: General image description- **Custom**: Your own prompt- **Region OCR (new)**: In the left panel, open **Region OCR (Draw/Crop)**, draw/crop a target area, then click **OCR Region**- **Equation Zoom (multipass)**: Optional nested equation refinement for Markdown. Off by default for speed/stability.### Free OCR vs Locate (important)- **Free OCR does not take a selected region**. It runs OCR on the whole image/page.17 unmodified linesfile_in.change(load_image, [file_in, page_selector], [region_editor])page_selector.change(load_image, [file_in, page_selector], [region_editor])input_img.change(lambda img: img, [input_img], [region_editor])region_btn.click(run_region,[region_editor, task, prompt, equation_zoom],[text_out, md_out, html_out, html_source_out, spatial_out, spatial_source_out, raw_out, img_out, gallery, download_btn, region_text_out, region_html_out])def run(image, file_path, task, custom_prompt, page_num, enable_equation_zoom):if file_path:cleaned, markdown, raw, img_out, crops = process_file(file_path,task,custom_prompt,int(page_num),enable_equation_zoom=enable_equation_zoom,)elif image is not None:cleaned, markdown, raw, img_out, crops = process_image(image,task,custom_prompt,enable_equation_zoom=enable_equation_zoom,)else:msg = "Error: Upload a file or image"return (msg, "", "", "", "", "", "", None, [], gr.DownloadButton(visible=False), msg, "")return _compose_ui_outputs(cleaned, markdown, raw, img_out, crops)submit_event = btn.click(run,[input_img, file_in, task, prompt, page_selector, equation_zoom],[text_out, md_out, html_out, html_source_out, spatial_out, spatial_source_out, raw_out, img_out, gallery, download_btn, region_text_out, region_html_out])submit_event.then(select_boxes, [task], [tabs])if __name__ == "__main__":
app.py+129-37