Spaces:
Runtime error
Runtime error
| 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 | |