Spaces:
Sleeping
Sleeping
manishw7 commited on
Commit ·
1d10899
1
Parent(s): 2e6b0fc
Robust Fix: Pin env to Python 3.9 and Gradio 4.30
Browse files- README.md +2 -2
- app.py +22 -16
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -16,8 +16,8 @@ tags:
|
|
| 16 |
datasets:
|
| 17 |
- c3rl/IIIT-INDIC-HW-WORDS-Hindi
|
| 18 |
sdk: gradio
|
| 19 |
-
sdk_version: 4.
|
| 20 |
-
python_version:
|
| 21 |
app_file: app.py
|
| 22 |
pinned: true
|
| 23 |
---
|
|
|
|
| 16 |
datasets:
|
| 17 |
- c3rl/IIIT-INDIC-HW-WORDS-Hindi
|
| 18 |
sdk: gradio
|
| 19 |
+
sdk_version: 4.30.0
|
| 20 |
+
python_version: 3.9
|
| 21 |
app_file: app.py
|
| 22 |
pinned: true
|
| 23 |
---
|
app.py
CHANGED
|
@@ -6,14 +6,15 @@ from transformers import AutoTokenizer, TrOCRProcessor, ViTImageProcessor, Visio
|
|
| 6 |
|
| 7 |
# Configuration
|
| 8 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
|
|
|
| 9 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
| 10 |
|
| 11 |
-
print("Loading model...")
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
try:
|
| 15 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 16 |
-
except:
|
| 17 |
image_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 19 |
processor = TrOCRProcessor(image_processor=image_processor, tokenizer=tokenizer)
|
|
@@ -22,28 +23,33 @@ base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
|
| 22 |
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
|
| 23 |
model.to(device)
|
| 24 |
model.eval()
|
| 25 |
-
print(f"Model loaded on {device}")
|
| 26 |
|
| 27 |
def predict(image):
|
| 28 |
if image is None:
|
| 29 |
-
return "
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
|
|
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=predict,
|
| 40 |
-
inputs=gr.Image(type="pil", label="
|
| 41 |
outputs=gr.Textbox(label="Recognized Text"),
|
| 42 |
-
title="DevGen
|
| 43 |
-
description="
|
| 44 |
allow_flagging="never"
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
| 6 |
|
| 7 |
# Configuration
|
| 8 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
| 9 |
+
# Ensuring the HF Username is correct
|
| 10 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
| 11 |
|
| 12 |
+
print("System: Loading model components...")
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
try:
|
| 16 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 17 |
+
except Exception:
|
| 18 |
image_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 20 |
processor = TrOCRProcessor(image_processor=image_processor, tokenizer=tokenizer)
|
|
|
|
| 23 |
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
|
| 24 |
model.to(device)
|
| 25 |
model.eval()
|
| 26 |
+
print(f"System: Model loaded successfully on {device}")
|
| 27 |
|
| 28 |
def predict(image):
|
| 29 |
if image is None:
|
| 30 |
+
return "Error: No image uploaded"
|
| 31 |
+
try:
|
| 32 |
+
image = image.convert("RGB")
|
| 33 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values.to(device)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
generated_ids = model.generate(pixel_values)
|
| 36 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 37 |
+
return generated_text
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error during inference: {str(e)}"
|
| 40 |
|
| 41 |
+
# The most stable UI structure: gr.Interface
|
| 42 |
+
# This avoids the complex nested blocks that trigger the Gradio 4.x schema bug
|
| 43 |
demo = gr.Interface(
|
| 44 |
fn=predict,
|
| 45 |
+
inputs=gr.Image(type="pil", label="Devanagari Image"),
|
| 46 |
outputs=gr.Textbox(label="Recognized Text"),
|
| 47 |
+
title="DevGen OCR Demo",
|
| 48 |
+
description="Handwritten Devanagari word recognition.",
|
| 49 |
allow_flagging="never"
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
# Robust launch: No manual IP/Port.
|
| 54 |
+
# Gradio automatically configures itself for Hugging Face Spaces.
|
| 55 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -5,3 +5,5 @@ huggingface_hub==0.25.2
|
|
| 5 |
pillow
|
| 6 |
safetensors
|
| 7 |
sentencepiece
|
|
|
|
|
|
|
|
|
| 5 |
pillow
|
| 6 |
safetensors
|
| 7 |
sentencepiece
|
| 8 |
+
fastapi<0.113.0
|
| 9 |
+
uvicorn
|