Spaces:
Runtime error
Runtime error
Fix app.py: lazy loading, correct Qwen2.5-VL class, qwen_vl_utils
Browse files
app.py
CHANGED
|
@@ -1,25 +1,96 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoProcessor, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def ocr_predict(image: Image.Image):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Gradio interface
|
| 17 |
iface = gr.Interface(
|
| 18 |
fn=ocr_predict,
|
| 19 |
inputs=gr.Image(type="pil"),
|
| 20 |
-
outputs=gr.Textbox(),
|
| 21 |
title="OdiaGenAI OCR",
|
| 22 |
-
description="Upload Odia images or scanned documents for OCR."
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import os
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
MODEL_ID = "OdiaGenAIOCR/odia-ocr-qwen-finetuned_v3"
|
| 7 |
+
CHECKPOINT = "checkpoint-1300"
|
| 8 |
+
|
| 9 |
+
_model = None
|
| 10 |
+
_processor = None
|
| 11 |
+
|
| 12 |
+
def load_model():
|
| 13 |
+
global _model, _processor
|
| 14 |
+
if _model is not None:
|
| 15 |
+
return _model, _processor
|
| 16 |
+
|
| 17 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 18 |
+
from peft import PeftModel
|
| 19 |
+
|
| 20 |
+
BASE_MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
|
| 23 |
+
_processor = AutoProcessor.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
revision=CHECKPOINT,
|
| 26 |
+
trust_remote_code=True,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
_model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 30 |
+
MODEL_ID,
|
| 31 |
+
revision=CHECKPOINT,
|
| 32 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 33 |
+
device_map="auto" if device == "cuda" else None,
|
| 34 |
+
trust_remote_code=True,
|
| 35 |
+
)
|
| 36 |
+
if device == "cpu":
|
| 37 |
+
_model = _model.to(device)
|
| 38 |
+
_model.eval()
|
| 39 |
+
return _model, _processor
|
| 40 |
+
|
| 41 |
|
| 42 |
def ocr_predict(image: Image.Image):
|
| 43 |
+
if image is None:
|
| 44 |
+
return "Please upload an image."
|
| 45 |
+
|
| 46 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
+
try:
|
| 48 |
+
model, processor = load_model()
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Model load error: {e}"
|
| 51 |
+
|
| 52 |
+
from qwen_vl_utils import process_vision_info
|
| 53 |
+
|
| 54 |
+
messages = [
|
| 55 |
+
{
|
| 56 |
+
"role": "user",
|
| 57 |
+
"content": [
|
| 58 |
+
{"type": "image", "image": image},
|
| 59 |
+
{"type": "text", "text": "Please perform OCR on this image and output only the recognized text."},
|
| 60 |
+
],
|
| 61 |
+
}
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 65 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 66 |
+
inputs = processor(
|
| 67 |
+
text=[text],
|
| 68 |
+
images=image_inputs,
|
| 69 |
+
videos=video_inputs,
|
| 70 |
+
padding=True,
|
| 71 |
+
return_tensors="pt",
|
| 72 |
+
).to(device)
|
| 73 |
+
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 76 |
+
|
| 77 |
+
generated_ids_trimmed = [
|
| 78 |
+
out_ids[len(in_ids):]
|
| 79 |
+
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 80 |
+
]
|
| 81 |
+
output_text = processor.batch_decode(
|
| 82 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 83 |
+
)
|
| 84 |
+
return output_text[0]
|
| 85 |
+
|
| 86 |
|
|
|
|
| 87 |
iface = gr.Interface(
|
| 88 |
fn=ocr_predict,
|
| 89 |
inputs=gr.Image(type="pil"),
|
| 90 |
+
outputs=gr.Textbox(label="Recognized Odia Text"),
|
| 91 |
title="OdiaGenAI OCR",
|
| 92 |
+
description="Upload Odia images or scanned documents for OCR using the fine-tuned Qwen2.5-VL model.",
|
| 93 |
+
examples=[],
|
| 94 |
)
|
| 95 |
|
| 96 |
iface.launch()
|