""" dots.ocr 1.5 — Hugging Face Spaces (GPU T4/A10G) Deploy: crea un nuovo Space su huggingface.co/new-space - SDK: Gradio - Hardware: T4 GPU (free) o A10G - Carica questo file come app.py + requirements.txt """ import os import json import torch import gradio as gr from PIL import Image from transformers import AutoModelForCausalLM, AutoProcessor from qwen_vl_utils import process_vision_info # ── Configurazione ────────────────────────────────────────────── MODEL_ID = "rednote-hilab/dots.ocr-1.5" DEVICE = "cuda" if torch.cuda.is_available() else "cpu" DTYPE = torch.bfloat16 if DEVICE == "cuda" else torch.float32 print(f"🖥️ Device: {DEVICE} | dtype: {DTYPE}") # ── Patch: mock flash_attn se non installato ──────────────────── import importlib if not importlib.util.find_spec("flash_attn"): import types flash_attn_mock = types.ModuleType("flash_attn") flash_attn_mock.flash_attn_varlen_func = None import sys sys.modules["flash_attn"] = flash_attn_mock print("⚠️ flash_attn non disponibile, mock installato (verrà usato sdpa)") # ── Caricamento modello ───────────────────────────────────────── print(f"📦 Caricamento modello {MODEL_ID}...") model = AutoModelForCausalLM.from_pretrained( MODEL_ID, dtype=DTYPE, device_map="auto", trust_remote_code=True, attn_implementation="sdpa", # usa SDPA invece di flash_attention_2 ) processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True) print("✅ Modello caricato!") # ── Prompt ────────────────────────────────────────────────────── PROMPTS = { "📄 Parsing completo (layout + testo)": """Please output the layout information from the PDF image, including each layout element's bbox, its category, and the corresponding text content within the bbox. 1. Bbox format: [x1, y1, x2, y2] 2. Layout Categories: The possible categories are ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title']. 3. Text Extraction & Formatting Rules: - Picture: For the 'Picture' category, the text field should be omitted. - Formula: Format its text as LaTeX. - Table: Format its text as HTML. - All Others (Text, Title, etc.): Format their text as Markdown. 4. Constraints: - The output text must be the original text from the image, with no translation. - All layout elements must be sorted according to human reading order. 5. Final Output: The entire output must be a single JSON object.""", "🔍 Solo rilevamento layout": """Please output only the layout detection information from the PDF image. For each detected element, output its bbox and category. 1. Bbox format: [x1, y1, x2, y2] 2. Layout Categories: ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title'] 3. Output must be a single JSON array sorted by reading order.""", "📝 Solo testo (OCR)": """Please extract all text from the image. Output the text in reading order as plain text. Do not translate. Exclude page headers and footers.""", "🌐 Web page parsing": """Please parse this web page screenshot. Extract all visible text content, UI elements, and their layout structure. Output as structured JSON.""", "📍 Scene text spotting": """Please detect and recognize all text visible in this scene/photo. For each text instance, output its bounding box and the recognized text. Output as JSON array.""", } def run_ocr(image: Image.Image, prompt_mode: str, max_tokens: int) -> tuple[str, str]: if image is None: return "⚠️ Carica un'immagine", "" prompt = PROMPTS.get(prompt_mode, list(PROMPTS.values())[0]) tmp_path = "/tmp/dots_ocr_input.png" image.save(tmp_path) messages = [ { "role": "user", "content": [ {"type": "image", "image": tmp_path}, {"type": "text", "text": prompt}, ], } ] text = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", ).to(DEVICE) with torch.no_grad(): generated_ids = model.generate(**inputs, max_new_tokens=max_tokens) generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output_text = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False, )[0] # Formatta JSON try: parsed = json.loads(output_text) json_output = json.dumps(parsed, indent=2, ensure_ascii=False) except json.JSONDecodeError: json_output = output_text # Estrai markdown markdown = "" try: parsed = json.loads(output_text) if isinstance(output_text, str) else output_text if isinstance(parsed, list): for item in parsed: cat = item.get("category", "") txt = item.get("text", "") if cat == "Title": markdown += f"# {txt}\n\n" elif cat == "Section-header": markdown += f"## {txt}\n\n" elif cat == "Formula": markdown += f"$$\n{txt}\n$$\n\n" elif cat == "Table": markdown += f"{txt}\n\n" elif cat in ("Page-header", "Page-footer"): continue elif txt: markdown += f"{txt}\n\n" else: markdown = output_text except (json.JSONDecodeError, TypeError): markdown = output_text return json_output, markdown def run_ocr_pdf(pdf_file, prompt_mode: str, max_tokens: int): if pdf_file is None: return "⚠️ Carica un PDF", "" import fitz # PyMuPDF doc = fitz.open(pdf_file.name) all_json = [] all_md = [] for i, page in enumerate(doc): pix = page.get_pixmap(dpi=200) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) j, m = run_ocr(img, prompt_mode, max_tokens) all_json.append(f"// === Pagina {i + 1} ===\n{j}") all_md.append(f"---\n### Pagina {i + 1}\n\n{m}") doc.close() return "\n\n".join(all_json), "\n".join(all_md) # ── Interfaccia Gradio ────────────────────────────────────────── with gr.Blocks(title="dots.ocr 1.5", theme=gr.themes.Soft()) as demo: gr.Markdown("# 🔍 dots.ocr 1.5 — Document OCR") gr.Markdown("Parsing multilingue di documenti con Vision-Language Model") with gr.Row(): prompt_mode = gr.Dropdown( choices=list(PROMPTS.keys()), value="📄 Parsing completo (layout + testo)", label="Modalità", ) max_tokens = gr.Slider( minimum=1000, maximum=24000, value=8000, step=1000, label="Max tokens output", ) with gr.Tabs(): with gr.TabItem("📷 Immagine"): with gr.Row(): img_input = gr.Image(type="pil", label="Carica immagine") with gr.Column(): img_json = gr.Code(label="Output JSON", language="json") img_md = gr.Markdown(label="Output Markdown") img_btn = gr.Button("🚀 Analizza immagine", variant="primary") img_btn.click( fn=run_ocr, inputs=[img_input, prompt_mode, max_tokens], outputs=[img_json, img_md], ) with gr.TabItem("📄 PDF"): with gr.Row(): pdf_input = gr.File(label="Carica PDF", file_types=[".pdf"]) with gr.Column(): pdf_json = gr.Code(label="Output JSON", language="json") pdf_md = gr.Markdown(label="Output Markdown") pdf_btn = gr.Button("🚀 Analizza PDF", variant="primary") pdf_btn.click( fn=run_ocr_pdf, inputs=[pdf_input, prompt_mode, max_tokens], outputs=[pdf_json, pdf_md], ) gr.Markdown("---\n*Powered by [dots.ocr 1.5](https://github.com/rednote-hilab/dots.ocr) su HF Spaces*") if __name__ == "__main__": demo.launch()