Spaces:
Sleeping
Sleeping
TianCai19
commited on
Commit
·
1e7aec9
1
Parent(s):
084cdda
first commit
Browse files- app.py +28 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 导入所需的库
|
| 2 |
+
import streamlit as st # 导入streamlit库
|
| 3 |
+
from transformers import pipeline # 导入transformers库中的pipeline模块
|
| 4 |
+
from PIL import Image # 导入PIL库中的Image模块
|
| 5 |
+
#PIL库是Python图像处理库,可以处理图片的格式转换、缩放、裁剪、旋转等操作
|
| 6 |
+
|
| 7 |
+
# 创建一个图像分类的pipeline
|
| 8 |
+
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
| 9 |
+
|
| 10 |
+
# 设置应用的标题
|
| 11 |
+
st.title("Hot Dog? Or Not?")
|
| 12 |
+
|
| 13 |
+
# 上传一个热狗候选图像
|
| 14 |
+
file_name = st.file_uploader("Upload a hot dog candidate image")
|
| 15 |
+
|
| 16 |
+
# 如果有上传的图像
|
| 17 |
+
if file_name is not None:
|
| 18 |
+
col1, col2 = st.columns(2) # 创建两列布局
|
| 19 |
+
|
| 20 |
+
image = Image.open(file_name) # 打开上传的图像
|
| 21 |
+
col1.image(image, use_column_width=True) # 在第一列显示图像
|
| 22 |
+
#use_column_width=True表示使用整列的宽度显示图片
|
| 23 |
+
|
| 24 |
+
predictions = pipeline(image) # 使用pipeline进行图像分类
|
| 25 |
+
|
| 26 |
+
col2.header("Probabilities") # 在第二列显示标题
|
| 27 |
+
for p in predictions:
|
| 28 |
+
col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%") # 在第二列显示分类结果及概率
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|