Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model_name = "prithivMLmods/Dog-Breed-120"
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 8 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def predict(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze()
|
| 17 |
+
top_idx = probs.argmax().item()
|
| 18 |
+
label = model.config.id2label[top_idx]
|
| 19 |
+
confidence = probs[top_idx].item()
|
| 20 |
+
return f"{label} ({round(confidence*100, 1)}%)"
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=predict,
|
| 24 |
+
inputs=gr.Image(type="pil"),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="Dog Breed Classifier 🐶",
|
| 27 |
+
description="Upload a dog photo to identify its breed using the prithivMLmods/Dog-Breed-120 model."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
demo.launch()
|