Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,30 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import pipeline
|
3 |
|
|
|
4 |
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
5 |
|
|
|
6 |
def predict(input_img):
|
|
|
7 |
predictions = pipeline(input_img)
|
|
|
8 |
return input_img, {p["label"]: p["score"] for p in predictions}
|
9 |
|
|
|
10 |
gradio_app = gr.Interface(
|
|
|
11 |
predict,
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
15 |
)
|
16 |
|
|
|
17 |
if __name__ == "__main__":
|
18 |
gradio_app.launch()
|
|
|
1 |
+
# 导入 Gradio 库并命名为 gr
|
2 |
import gradio as gr
|
3 |
+
# 从 transformers 库中导入 pipeline 函数
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# 创建一个图像分类的管道,使用 "julien-c/hotdog-not-hotdog" 模型
|
7 |
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
8 |
|
9 |
+
# 定义预测函数,接受输入图像
|
10 |
def predict(input_img):
|
11 |
+
# 使用管道对输入图像进行预测
|
12 |
predictions = pipeline(input_img)
|
13 |
+
# 返回处理后的图像和预测结果的标签及分数
|
14 |
return input_img, {p["label"]: p["score"] for p in predictions}
|
15 |
|
16 |
+
# 创建 Gradio 界面
|
17 |
gradio_app = gr.Interface(
|
18 |
+
# 指定预测函数
|
19 |
predict,
|
20 |
+
# 定义输入组件为图像,允许上传或使用摄像头,输出类型为 PIL 图像
|
21 |
+
inputs=gr.Image(label="选择热狗候选图片", sources=['upload', 'webcam'], type="pil"),
|
22 |
+
# 定义输出组件为处理后的图像和标签,显示前两个结果
|
23 |
+
outputs=[gr.Image(label="处理后的图像"), gr.Label(label="结果", num_top_classes=2)],
|
24 |
+
# 设置界面标题
|
25 |
+
title="是热狗吗?",
|
26 |
)
|
27 |
|
28 |
+
# 如果当前模块是主模块,则启动 Gradio 应用
|
29 |
if __name__ == "__main__":
|
30 |
gradio_app.launch()
|