Spaces:
Runtime error
Runtime error
Rename app.py to app
Browse files
app
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import skimage
|
| 4 |
+
learn = load_learner('model.pkl')
|
| 5 |
+
labels = learn.dls.vocab
|
| 6 |
+
def predict(img):
|
| 7 |
+
img = PILImage.create(img)
|
| 8 |
+
pred,pred_idx,probs = learn.predict(img)
|
| 9 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 10 |
+
|
| 11 |
+
interface = gr.Interface(
|
| 12 |
+
fn=predict,
|
| 13 |
+
inputs=gr.Image(),
|
| 14 |
+
outputs=gr.Label(num_top_classes=3)
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Enable the queue to handle POST requests
|
| 18 |
+
interface.queue(api_open=True)
|
| 19 |
+
|
| 20 |
+
# Launch the interface
|
| 21 |
+
interface.launch()
|
app.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pickle
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
# Load the trained model
|
| 7 |
-
with open("bird_classifier.pkl", "rb") as f:
|
| 8 |
-
model = pickle.load(f)
|
| 9 |
-
|
| 10 |
-
# Get class names automatically from the model
|
| 11 |
-
try:
|
| 12 |
-
class_names = model.classes_ # Works for scikit-learn models
|
| 13 |
-
except AttributeError:
|
| 14 |
-
# If the model doesn't have classes_, you'd need a fallback or custom logic
|
| 15 |
-
raise ValueError("Model does not have 'classes_' attribute. Please provide class names manually or adjust the code.")
|
| 16 |
-
|
| 17 |
-
# Define the prediction function
|
| 18 |
-
def classify_bird(image):
|
| 19 |
-
# Preprocess the image (adjust this based on how your model was trained)
|
| 20 |
-
img = Image.fromarray(image.astype("uint8"), "RGB") # Convert to PIL Image
|
| 21 |
-
img = img.resize((224, 224)) # Example resize, adjust to your model's input size
|
| 22 |
-
img_array = np.array(img) / 255.0 # Normalize if your model expects this
|
| 23 |
-
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 24 |
-
|
| 25 |
-
# Make prediction
|
| 26 |
-
prediction = model.predict(img_array) # Adjust based on your model's method
|
| 27 |
-
|
| 28 |
-
# Handle prediction output
|
| 29 |
-
if len(prediction.shape) > 1: # If prediction is a probability array (e.g., softmax output)
|
| 30 |
-
predicted_class = class_names[np.argmax(prediction)]
|
| 31 |
-
else: # If prediction is a single class index (e.g., scikit-learn's default)
|
| 32 |
-
predicted_class = class_names[prediction[0]]
|
| 33 |
-
|
| 34 |
-
return predicted_class
|
| 35 |
-
|
| 36 |
-
# Create the Gradio interface
|
| 37 |
-
interface = gr.Interface(
|
| 38 |
-
fn=classify_bird, # Prediction function
|
| 39 |
-
inputs=gr.Image(type="numpy"), # Input is an image, returned as NumPy array
|
| 40 |
-
outputs=gr.Textbox(), # Output is text (bird species)
|
| 41 |
-
title="Bird Classifier",
|
| 42 |
-
description="Upload an image of a bird and get its species predicted!"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Launch the app
|
| 46 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|