Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastai.vision.all import PILImage
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
def predict(image):
|
7 |
+
# Transform the image
|
8 |
+
pil_image = PILImage.create(image)
|
9 |
+
|
10 |
+
# Predict
|
11 |
+
preds, _, probs = learn.predict(pil_image)
|
12 |
+
|
13 |
+
# Apply the threshold
|
14 |
+
threshold = 0.425
|
15 |
+
classes = [learn.dls.vocab[i] for i in range(len(probs)) if probs[i] > threshold]
|
16 |
+
|
17 |
+
pet_type = "dog" if "dog" in classes else "cat" if "cat" in classes else None
|
18 |
+
breeds = [breed for breed in classes if breed not in ["cat", "dog"]]
|
19 |
+
|
20 |
+
if pet_type:
|
21 |
+
breed_info = f"The breed is {'/'.join(breeds)}." if breeds else "The breed is not identified."
|
22 |
+
return f"This is a {pet_type}. {breed_info}"
|
23 |
+
else:
|
24 |
+
return "This is not a cat, nor a dog."
|
25 |
+
|
26 |
+
|
27 |
+
# Define the Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=predict,
|
30 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
31 |
+
outputs="text",
|
32 |
+
live=True,
|
33 |
+
title="Cat and Dog Image Classifier",
|
34 |
+
description="Upload an image of a cat or a dog, and the model will identify the type and breed.",
|
35 |
+
article="This model has been trained on the Oxford Pets dataset and might not recognize all types dog and cat breeds. For best results, use clear images."
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
# Launch the interface
|
40 |
+
iface.launch(share=True)
|