Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,102 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
import json
|
| 4 |
-
import os
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
| 18 |
-
MODEL_ID,
|
| 19 |
-
trust_remote_code=True,
|
| 20 |
-
torch_dtype=torch.float32,
|
| 21 |
-
low_cpu_mem_usage=True,
|
| 22 |
-
device_map="cpu"
|
| 23 |
-
)
|
| 24 |
-
model.eval()
|
| 25 |
-
print("Model loaded successfully!")
|
| 26 |
|
| 27 |
def extract_document(image: Image.Image):
|
| 28 |
if image is None:
|
| 29 |
-
return {"error": "
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
gr.Markdown("
|
| 75 |
-
|
| 76 |
-
|
| 77 |
with gr.Row():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
submit_btn = gr.Button("Extract Data", variant="primary")
|
| 81 |
-
with gr.Column():
|
| 82 |
-
output_json = gr.JSON(label="Extracted JSON")
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
import json
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 10 |
+
|
| 11 |
+
MODEL = "qwen/qwen-2.5-vl-72b-instruct"
|
| 12 |
+
|
| 13 |
+
def image_to_base64(image: Image.Image):
|
| 14 |
+
buf = io.BytesIO()
|
| 15 |
+
image.save(buf, format="PNG")
|
| 16 |
+
return base64.b64encode(buf.getvalue()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def extract_document(image: Image.Image):
|
| 19 |
if image is None:
|
| 20 |
+
return {"error": "No image uploaded"}
|
| 21 |
+
|
| 22 |
+
img_b64 = image_to_base64(image)
|
| 23 |
+
|
| 24 |
+
prompt = """
|
| 25 |
+
You are a universal document understanding system.
|
| 26 |
+
|
| 27 |
+
Rules:
|
| 28 |
+
- Be document-agnostic
|
| 29 |
+
- Detect document type if possible
|
| 30 |
+
- Extract ALL visible structured data
|
| 31 |
+
- Extract tables completely (columns + rows)
|
| 32 |
+
- Preserve numbers exactly
|
| 33 |
+
- Use null for missing values
|
| 34 |
+
- Do NOT hallucinate
|
| 35 |
+
- Return ONLY valid JSON
|
| 36 |
+
|
| 37 |
+
Schema:
|
| 38 |
+
{
|
| 39 |
+
"document_type": string | null,
|
| 40 |
+
"confidence": number (0-1),
|
| 41 |
+
"summary": string,
|
| 42 |
+
"fields": { "<key>": "<value | null>" },
|
| 43 |
+
"tables": [
|
| 44 |
+
{
|
| 45 |
+
"table_name": string,
|
| 46 |
+
"columns": [string],
|
| 47 |
+
"rows": [[string | number | null]]
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
}
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
payload = {
|
| 54 |
+
"model": MODEL,
|
| 55 |
+
"messages": [
|
| 56 |
+
{
|
| 57 |
+
"role": "user",
|
| 58 |
+
"content": [
|
| 59 |
+
{"type": "input_text", "text": prompt},
|
| 60 |
+
{
|
| 61 |
+
"type": "input_image",
|
| 62 |
+
"image_base64": img_b64
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
}
|
| 66 |
+
],
|
| 67 |
+
"temperature": 0
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
headers = {
|
| 71 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 72 |
+
"Content-Type": "application/json"
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
r = requests.post(
|
| 76 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 77 |
+
headers=headers,
|
| 78 |
+
json=payload,
|
| 79 |
+
timeout=120
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
response = r.json()
|
| 83 |
+
text = response["choices"][0]["message"]["content"]
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
start = text.find("{")
|
| 87 |
+
end = text.rfind("}") + 1
|
| 88 |
+
return json.loads(text[start:end])
|
| 89 |
+
except Exception:
|
| 90 |
+
return {"raw_output": text}
|
| 91 |
+
|
| 92 |
+
with gr.Blocks(title="DocAI β Universal Document Extractor") as demo:
|
| 93 |
+
gr.Markdown("# π DocAI β Universal Document Intelligence")
|
| 94 |
+
gr.Markdown("Vision-LLM powered. No templates. Any document.")
|
| 95 |
+
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
+
img = gr.Image(type="pil", label="Upload document")
|
| 98 |
+
out = gr.JSON(label="Extracted JSON")
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
gr.Button("Extract").click(extract_document, img, out)
|
| 101 |
|
| 102 |
+
demo.launch()
|