Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and feature extractor
|
| 7 |
+
model_id = "shahad-alh/arabichar-finetuned-v2"
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 9 |
+
extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
| 10 |
+
|
| 11 |
+
# Prediction function
|
| 12 |
+
def predict(img: Image.Image):
|
| 13 |
+
inputs = extractor(images=img, return_tensors="pt")
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
predicted = outputs.logits.argmax(-1).item()
|
| 17 |
+
label = model.config.id2label[str(predicted)]
|
| 18 |
+
return label
|
| 19 |
+
|
| 20 |
+
gr.Interface(
|
| 21 |
+
fn=predict,
|
| 22 |
+
inputs=gr.Image(type="pil", label="Upload Arabic Letter"),
|
| 23 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 24 |
+
allow_flagging="never",
|
| 25 |
+
allow_api=True,
|
| 26 |
+
title="Arabic Character Classifier"
|
| 27 |
+
).launch()
|