Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,41 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from ultralytics import YOLO
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load YOLO model (update path to your model file if needed)
|
| 6 |
+
model = YOLO('best_animal_classifier.pt')
|
| 7 |
+
|
| 8 |
+
class_names = ['butterflies', 'chickens', 'elephants', 'horses', 'spiders', 'squirrels']
|
| 9 |
+
|
| 10 |
+
def predict_animal(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return {}
|
| 13 |
+
# Run prediction without verbose logging for cleaner output
|
| 14 |
+
results = model.predict(image, verbose=False)
|
| 15 |
+
|
| 16 |
+
# Extract the probabilities; fallback if attribute unavailable
|
| 17 |
+
try:
|
| 18 |
+
probs = results[0].probs.data.cpu().numpy()
|
| 19 |
+
except AttributeError:
|
| 20 |
+
# If 'probs' not available, generate dummy equal probabilities (prevent crash)
|
| 21 |
+
probs = np.ones(len(class_names)) / len(class_names)
|
| 22 |
+
|
| 23 |
+
# Map class names to probability scores
|
| 24 |
+
return {class_names[i]: float(probs[i]) for i in range(len(class_names))}
|
| 25 |
+
|
| 26 |
+
# Enhanced UI with modern theme and layout
|
| 27 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 28 |
+
gr.Markdown("# 🐾 Animal Type Classifier")
|
| 29 |
+
gr.Markdown("Upload an image of an animal below and get predictions for butterflies, chickens, elephants, horses, spiders, or squirrels.")
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
img_input = gr.Image(type="pil", label="Upload Animal Image")
|
| 33 |
+
label_output = gr.Label(num_top_classes=6, label="Prediction Scores")
|
| 34 |
+
|
| 35 |
+
predict_button = gr.Button("Classify Animal")
|
| 36 |
+
predict_button.click(fn=predict_animal, inputs=img_input, outputs=label_output)
|
| 37 |
+
|
| 38 |
+
gr.Markdown("Developed with Ultralytics YOLO and Gradio framework.")
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
demo.launch()
|