File size: 729 Bytes
558a694
 
 
4d59dc6
0c17f3d
 
558a694
 
0c17f3d
558a694
 
4d59dc6
558a694
 
4d59dc6
 
 
 
558a694
 
 
 
 
 
 
 
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
# prompt: gradio image 分类
import fastai
from fastai.vision import *
from fastai.vision.all import load_learner,PILImage
import gradio as gr

# Load the model
model = load_learner("model.pkl")

# Define an image classification function
def classify_image(image):
    img = PILImage.create(image)

    # Make a prediction
    pred_class, pred_idx, probs = model.predict(img)
    
    # Return the prediction as a dictionary
    return {model.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}

# Create the Gradio interface
image_input = gr.Image()
label_output = gr.Label(num_top_classes=3)
interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output)

# Launch the interface
interface.launch()