fastai-Prac-3 / app.py
adcadcadcadc's picture
Update app.py
813376b verified
# AUTOGENERATED! DO NOT EDIT! File to edit: . (unless otherwise specified).
__all__ = ['is_cat', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
# Cell
from fastai.vision.all import *
import gradio as gr
# Function to check if an image is of a cat or dog based on filename capitalization
def is_cat(x):
return x[0].isupper()
# Load the trained model
learn = load_learner('model.pkl')
# Define categories for classification
categories = ('Dog', 'Cat')
# Image classification function
def classify_image(img):
pred, idx, probs = learn.predict(img)
return dict(zip(categories, map(float, probs)))
# Gradio UI components
image = gr.Image(type="pil") # Use "pil" to ensure image is passed correctly
label = gr.Label() # Updated from `gr.outputs.Label()`
examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg'] # Ensure these files exist
# Define the Gradio interface
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
# Launch the Gradio app
intf.launch(inline=False)