simecek/teaching_img_classifier
0
1from fastai.vision.all import * # Importing the necessary fastai modules2import gradio as gr # Importing Gradio for creating the web interface3import timm # Importing timm for model management4 5# Load the pre-trained model6learn = load_learner('model.pkl')7 8# Extract categories (class labels) from the DataLoader9categories = learn.dls.vocab10 11# Function to classify an image12def classify_image(img):13 _, _, probs = learn.predict(img)14 return dict(zip(categories, map(float, probs))) # Map categories to their probabilities15 16# Define Gradio input and output components 17image = gr.Image(width=224, height=224) # Image input18label = gr.Label() # Output label to display classification19examples = ['test_image1.jpg', 'test_image2.jpg'] # Example images for demonstration20 21# Create and launch the Gradio interface22intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)23intf.launch()