Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from unsloth import FastVisionModel
|
| 5 |
+
from transformers import AutoModel
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
print("Loading model...")
|
| 9 |
+
model, tokenizer = FastVisionModel.from_pretrained(
|
| 10 |
+
"https://huggingface.co/nomypython/urdu-ocr-deepseek", # ← Change this!
|
| 11 |
+
load_in_4bit=True,
|
| 12 |
+
auto_model=AutoModel,
|
| 13 |
+
trust_remote_code=True,
|
| 14 |
+
use_gradient_checkpointing="unsloth",
|
| 15 |
+
)
|
| 16 |
+
FastVisionModel.for_inference(model)
|
| 17 |
+
print("✓ Model loaded!")
|
| 18 |
+
|
| 19 |
+
def extract_urdu_text(image):
|
| 20 |
+
if image is None:
|
| 21 |
+
return "⚠️ Please upload an image!"
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
temp_path = "temp.png"
|
| 25 |
+
image.save(temp_path)
|
| 26 |
+
|
| 27 |
+
result = model.infer(
|
| 28 |
+
tokenizer,
|
| 29 |
+
prompt="<image>\nExtract Urdu text from this image:",
|
| 30 |
+
image_file=temp_path,
|
| 31 |
+
output_path="./temp_results",
|
| 32 |
+
image_size=640,
|
| 33 |
+
base_size=640,
|
| 34 |
+
crop_mode=False,
|
| 35 |
+
save_results=False,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if os.path.exists(temp_path):
|
| 39 |
+
os.remove(temp_path)
|
| 40 |
+
|
| 41 |
+
return result if result else "⚠️ No text detected"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"❌ Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
with gr.Blocks(title="Urdu OCR") as demo:
|
| 46 |
+
gr.Markdown("# 🔤 Urdu OCR - اردو او سی آر")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
with gr.Column():
|
| 50 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 51 |
+
extract_btn = gr.Button("🚀 Extract Text", variant="primary")
|
| 52 |
+
with gr.Column():
|
| 53 |
+
text_output = gr.Textbox(
|
| 54 |
+
label="Extracted Text",
|
| 55 |
+
lines=10,
|
| 56 |
+
rtl=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
extract_btn.click(extract_urdu_text, image_input, text_output)
|
| 60 |
+
image_input.change(extract_urdu_text, image_input, text_output)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|