Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 1. 加载 Hugging Face Hub 上的图像分类模型
|
| 5 |
+
classifier = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
|
| 6 |
+
|
| 7 |
+
# 2. 推理函数
|
| 8 |
+
def predict(image):
|
| 9 |
+
results = classifier(image)
|
| 10 |
+
# 只取前 3 个结果更清晰
|
| 11 |
+
return {r["label"]: float(r["score"]) for r in results[:3]}
|
| 12 |
+
|
| 13 |
+
# 3. Gradio 界面
|
| 14 |
+
demo = gr.Interface(
|
| 15 |
+
fn=predict,
|
| 16 |
+
inputs=gr.Image(type="pil"),
|
| 17 |
+
outputs=gr.Label(num_top_classes=3),
|
| 18 |
+
title="MobileNet Image Classification",
|
| 19 |
+
description="上传一张图片,输出前 3 类及概率"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
demo.launch()
|