Spaces:
Running on Zero
Running on Zero
Deploy vision-ocr-studio on ZeroGPU
Browse filesFiles changed (3)
- README.md+21-8
- app.pyAdded+132
- requirements.txtAdded+6
12345678910111213---title: Vision Ocr Studioemoji: 🌍colorFrom: pinkcolorTo: pinksdk: gradiosdk_version: 6.31.0python_version: '3.12'app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526---title: Smart Document, Blueprint & Invoice OCR Studioemoji: 📄colorFrom: bluecolorTo: greensdk: gradiosdk_version: 6.13.0app_file: app.pypython_version: "3.12"startup_duration_timeout: 1hshort_description: Qwen2.5-VL Document, Blueprint, Table & Invoice OCRmodels:- Qwen/Qwen2.5-VL-3B-Instructtags:- ocr- vision-language- qwen2.5-vl- document-ai- invoice-parser- zerogpu- mcp-server---# 📄 Smart Document, Blueprint & Invoice OCR StudioUpload **complex invoices, electrical panel schedules, blueprints, receipts, tables, or UI screenshots** and extract clean **Markdown or structured JSON** powered by **[Qwen/Qwen2.5-VL-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-3B-Instruct)** on **ZeroGPU (`zero-a10g`)** at **$0/hour**.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132"""Qwen2.5-VL Smart Document, Blueprint & Invoice OCR Studio on ZeroGPU."""
import os
os.environ.setdefault("PYTORCH_ALLOC_CONF", "backend:cudaMallocAsync")os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "backend:cudaMallocAsync")
import spaces # Must be imported before torchimport gradio as grimport PIL.Imageimport torchfrom transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
MODEL_ID = "Qwen/Qwen2.5-VL-3B-Instruct"
EXTRACTION_PRESETS = { "Full Clean Markdown + Tables (Default)": ( "Extract all text, headings, and tables from this image into clean, well-formatted GitHub Flavored Markdown. " "Preserve exact numbers, part numbers, prices, and table columns." ), "Structured Invoice / Receipt JSON": ( "Extract all fields from this invoice or receipt as valid JSON with keys: vendor, date, invoice_number, " "line_items (description, quantity, unit_price, total), subtotal, tax, and total_amount." ), "Electrical Panel / Blueprint / Spec Analysis": ( "Analyze this electrical diagram, panel schedule, or technical blueprint. List all visible circuits, " "breaker ratings, wire gauges, load calculations, labels, and technical notes in structured Markdown." ), "Custom Question / Prompt": "",}
print(f"[vision-ocr-studio] Preloading {MODEL_ID} at module scope for ZeroGPU...", flush=True)model = Qwen2_5_VLForConditionalGeneration.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16,)model.to("cuda")model.eval()processor = AutoProcessor.from_pretrained(MODEL_ID)print("[vision-ocr-studio] Qwen2.5-VL OCR engine ready on ZeroGPU!", flush=True)
@spaces.GPU(duration=60)def analyze_document( image_path: str | None, preset: str = "Full Clean Markdown + Tables (Default)", custom_instruction: str = "", max_tokens: int = 1024,) -> str: """Run vision-language OCR or structured extraction on an uploaded document/image.""" if not image_path: raise gr.Error("Please upload a document, blueprint, invoice, or screenshot image first.")
instruction = (custom_instruction or "").strip() or EXTRACTION_PRESETS.get( preset, EXTRACTION_PRESETS["Full Clean Markdown + Tables (Default)"] ) img = PIL.Image.open(image_path).convert("RGB") if max(img.size) > 1600: scale = 1600.0 / max(img.size) img = img.resize((int(img.width * scale), int(img.height * scale)), PIL.Image.Resampling.LANCZOS)
messages = [ { "role": "user", "content": [ {"type": "image", "image": img}, {"type": "text", "text": instruction}, ], } ] text_prompt = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) inputs = processor( text=[text_prompt], images=[img], padding=True, return_tensors="pt", ).to("cuda")
with torch.no_grad(): generated_ids = model.generate(**inputs, max_new_tokens=int(max_tokens))
trimmed_ids = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output_text = processor.batch_decode( trimmed_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False )[0] return output_text.strip()
with gr.Blocks(title="Smart Document, Blueprint & Invoice OCR Studio", fill_width=True) as demo: gr.Markdown( "# 📄 Smart Document, Blueprint & Invoice OCR Studio\n" "Powered by **[Qwen/Qwen2.5-VL-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-3B-Instruct)** on **ZeroGPU (`zero-a10g`)** at **$0/hour**—extract clean **Markdown, JSON, tables, invoices, or electrical specs** from any image." ) with gr.Row(): with gr.Column(scale=1): img_in = gr.Image( label="Upload Document, Invoice, Electrical Schedule, Blueprint, or Screenshot", type="filepath", ) preset_dd = gr.Dropdown( choices=list(EXTRACTION_PRESETS), value="Full Clean Markdown + Tables (Default)", label="Extraction Mode", ) custom_box = gr.Textbox( label="Custom Instruction / Question (Optional — overrides preset when filled)", lines=3, placeholder="e.g., Extract all circuit breaker amps and wire sizes into a JSON array...", ) max_tok = gr.Slider(256, 2048, value=1024, step=128, label="Max Output Tokens") extract_btn = gr.Button("📄 Extract & Analyze Document (ZeroGPU)", variant="primary") with gr.Column(scale=1): md_out = gr.Textbox( label="Extracted Output (Markdown / JSON)", lines=18, buttons=["copy"], )
extract_btn.click( fn=analyze_document, inputs=[img_in, preset_dd, custom_box, max_tok], outputs=md_out, api_name="extract_document", )
if __name__ == "__main__": demo.launch(theme=gr.themes.Ocean(), mcp_server=True)123456transformers>=4.49.0accelerate>=1.6.0safetensorsqwen-vl-utilsPillowtorchvisionREADME.md+21-8
app.pyAdded+132
requirements.txtAdded+6