Spaces:
Running on Zero
Running on Zero
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
# Model weights live in a separate Hugging Face Model repo (not in this Space's
|
| 7 |
-
# own storage, which has a much smaller free limit). This downloads them
|
| 8 |
-
# automatically the first time the Space starts.
|
| 9 |
-
model_path = "122Uswa/urdu-ocr-codesaviours-si26-uswa"
|
| 10 |
-
|
| 11 |
-
processor = TrOCRProcessor.from_pretrained(model_path)
|
| 12 |
-
model = VisionEncoderDecoderModel.from_pretrained(model_path)
|
| 13 |
-
model.eval()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def extract_urdu_text(image):
|
| 17 |
-
"""Takes an image, returns extracted Urdu text."""
|
| 18 |
-
if image is None:
|
| 19 |
-
return "Please upload an image"
|
| 20 |
-
pixel_values = processor(image, return_tensors="pt").pixel_values
|
| 21 |
-
with torch.no_grad():
|
| 22 |
-
generated_ids = model.generate(pixel_values, max_new_tokens=128)
|
| 23 |
-
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 24 |
-
return text if text.strip() else "Could not extract text from this image"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
interface = gr.Interface(
|
| 28 |
-
fn=extract_urdu_text,
|
| 29 |
-
inputs=gr.Image(type="pil", label="Upload Urdu Image"),
|
| 30 |
-
outputs=gr.Textbox(label="Extracted Urdu Text"),
|
| 31 |
-
title="Urdu OCR -- Code Saviours SI-26",
|
| 32 |
-
description="Upload an image containing Urdu text and get the extracted text.",
|
| 33 |
-
examples=[],
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|