Spaces:
Runtime error
Runtime error
File size: 2,091 Bytes
890bc9f 9dae650 890bc9f 586955d 890bc9f 586955d 80dddfa 35083a2 80dddfa 35083a2 586955d 890bc9f c164bba 890bc9f 3037da5 9dae650 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import gradio as gr #Gradio for creating the web interface
from fastai.vision.all import load_learner, PILImage #FastAI fxns for model loading and image processing
# Load the trained model
learner = load_learner('aj_classifier.pkl')
def classify_image(img):
"""
Function to classify an uploaded image using the trained model.
Args:
- img: The image uploaded by the user, received as a PILImage.
Returns:
- A string with the prediction and the probability of that prediction.
"""
# Use the model to predict the class of the image
# 'predict' method returns three values: the predicted class, its index, and the probabilities of all classes.
pred, pred_idx, probs = learner.predict(img)
# Format the prediction and its probability as a string to show to the user
return f"This is an Air Jordan {pred}; {(probs[pred_idx]* 100):.02f}% accurate"
# Create a Gradio interface
# This part sets up the Gradio web interface, specifying the function to call for predictions,
# the type of input it expects (an image), and the type of output (text).
examples = ['aj1.jpeg',
'aj4.jpeg',
'aj5.jpeg'
'aj11.png',
'aj13.png']
iface = gr.Interface(fn=classify_image,
inputs=gr.Image(type='pil'), # Specifies that the input should be an image, automatically converted to PILImage
outputs="text", # Specifies that the output is text (the prediction and probability)
title="Air Jordan Model Classifier", # Title of the web interface
description="Upload an image of Air Jordan sneakers, and the classifier will predict the model.",
examples=examples) #Add examples images
# This condition checks if the script is being run as the main program and launches the Gradio interface.
# It ensures that the Gradio server starts only when this script is executed directly, not when imported as a module.
if __name__ == "__main__":
iface.launch(share=True) # Starts the Gradio interface
|