File size: 651 Bytes
e620c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import gradio as gr
from transformers import pipeline

# 1. 加载 Hugging Face Hub 上的图像分类模型
classifier = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")

# 2. 推理函数
def predict(image):
    results = classifier(image)
    # 只取前 3 个结果更清晰
    return {r["label"]: float(r["score"]) for r in results[:3]}

# 3. Gradio 界面
demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=3),
    title="MobileNet Image Classification",
    description="上传一张图片,输出前 3 类及概率"
)

if __name__ == "__main__":
    demo.launch()