Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
| 3 |
import skimage
|
| 4 |
|
|
|
|
| 5 |
learn = load_learner('export.pkl')
|
| 6 |
|
|
|
|
| 7 |
labels = learn.dls.vocab
|
|
|
|
|
|
|
| 8 |
def predict(img):
|
|
|
|
| 9 |
img = PILImage.create(img)
|
|
|
|
| 10 |
pred,pred_idx,probs = learn.predict(img)
|
|
|
|
| 11 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 12 |
|
|
|
|
| 13 |
title = "What's my Pet Breed?"
|
| 14 |
description = "A pet breed classifier trained on the Oxford Pets dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces."
|
| 15 |
article="<p style='text-align: center'><a href='https://tmabraham.github.io/blog/gradio_hf_spaces_tutorial' target='_blank'>Blog post</a></p>"
|
|
@@ -17,4 +26,5 @@ examples = ['cat.jpeg']
|
|
| 17 |
interpretation='default'
|
| 18 |
enable_queue=True
|
| 19 |
|
|
|
|
| 20 |
gr.Interface(fn=predict,inputs="image",outputs="label").launch()
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
import gradio as gr
|
| 3 |
from fastai.vision.all import *
|
| 4 |
import skimage
|
| 5 |
|
| 6 |
+
# Load a pre-trained deep learning model for image classification
|
| 7 |
learn = load_learner('export.pkl')
|
| 8 |
|
| 9 |
+
# Get the labels (classes) for the model's predictions
|
| 10 |
labels = learn.dls.vocab
|
| 11 |
+
|
| 12 |
+
# Define a function to make predictions on input images
|
| 13 |
def predict(img):
|
| 14 |
+
# Create a PIL image from the input image
|
| 15 |
img = PILImage.create(img)
|
| 16 |
+
# Use the loaded model to make predictions on the image
|
| 17 |
pred,pred_idx,probs = learn.predict(img)
|
| 18 |
+
# Create a dictionary to map labels to their corresponding probabilities
|
| 19 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 20 |
|
| 21 |
+
# Set up the Gradio interface with relevant information
|
| 22 |
title = "What's my Pet Breed?"
|
| 23 |
description = "A pet breed classifier trained on the Oxford Pets dataset with fastai. Created as a demo for Gradio and HuggingFace Spaces."
|
| 24 |
article="<p style='text-align: center'><a href='https://tmabraham.github.io/blog/gradio_hf_spaces_tutorial' target='_blank'>Blog post</a></p>"
|
|
|
|
| 26 |
interpretation='default'
|
| 27 |
enable_queue=True
|
| 28 |
|
| 29 |
+
# Launch the Gradio interface with the predict function for image classification
|
| 30 |
gr.Interface(fn=predict,inputs="image",outputs="label").launch()
|