Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,27 @@ from PIL import Image
|
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
with torch.no_grad():
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return f"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
inputs=gr.Image(type="pil"),
|
| 21 |
outputs="text",
|
| 22 |
-
title="Dog Breed
|
| 23 |
-
description="Upload
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
|
|
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Load the image processor and model from Hugging Face
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit")
|
| 8 |
+
breed_model = AutoModelForImageClassification.from_pretrained("wesleyacheng/dog-breeds-multiclass-image-classification-with-vit")
|
| 9 |
|
| 10 |
+
# This function takes an uploaded image and returns the predicted dog breed
|
| 11 |
+
def detect_breed(img):
|
| 12 |
+
inputs = processor(images=img, return_tensors="pt")
|
| 13 |
with torch.no_grad():
|
| 14 |
+
result = breed_model(**inputs)
|
| 15 |
+
predictions = result.logits
|
| 16 |
+
top_prediction = predictions.argmax(dim=1).item()
|
| 17 |
+
breed_name = breed_model.config.id2label[top_prediction]
|
| 18 |
+
return f"This looks like a {breed_name}!"
|
| 19 |
|
| 20 |
+
# Set up the Gradio web interface
|
| 21 |
+
app = gr.Interface(
|
| 22 |
+
fn=detect_breed,
|
| 23 |
inputs=gr.Image(type="pil"),
|
| 24 |
outputs="text",
|
| 25 |
+
title="Dog Breed Identifier 🐶",
|
| 26 |
+
description="Upload a photo of a dog and find out what breed it is! The model can recognize 120 different dog breeds."
|
| 27 |
)
|
| 28 |
|
| 29 |
+
app.launch()
|