Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the image classification pipeline
|
| 5 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 6 |
+
|
| 7 |
+
def predict(image):
|
| 8 |
+
if image is None:
|
| 9 |
+
return None
|
| 10 |
+
predictions = classifier(image)
|
| 11 |
+
return {p["label"]: p["score"] for p in predictions}
|
| 12 |
+
|
| 13 |
+
# Updated examples list to use cat.jpeg instead of cat.png
|
| 14 |
+
examples = [
|
| 15 |
+
"animal_images/hippo.png",
|
| 16 |
+
"animal_images/jaguar.png",
|
| 17 |
+
"animal_images/toucan.png",
|
| 18 |
+
"animal_images/sloth.png",
|
| 19 |
+
"animal_images/cat.jpeg", # Changed to jpeg
|
| 20 |
+
"animal_images/frog.png",
|
| 21 |
+
"animal_images/turtle.png"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=predict,
|
| 26 |
+
inputs=gr.Image(
|
| 27 |
+
type="pil",
|
| 28 |
+
label="Input Image",
|
| 29 |
+
interactive=True,
|
| 30 |
+
height=None
|
| 31 |
+
),
|
| 32 |
+
outputs=gr.Label(num_top_classes=3, label="Predictions"),
|
| 33 |
+
examples=examples,
|
| 34 |
+
title="Animal Classifier",
|
| 35 |
+
description="Upload an image of an animal."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|