Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
# 设置OpenAI API密钥
|
7 |
+
openai.api_key = "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"
|
8 |
+
|
9 |
+
st.title("照片内容分析 App")
|
10 |
+
|
11 |
+
# 上传图片
|
12 |
+
uploaded_file = st.file_uploader("上传一张照片", type=["jpg", "jpeg", "png"])
|
13 |
+
|
14 |
+
if uploaded_file is not None:
|
15 |
+
# 显示上传的图片
|
16 |
+
image = Image.open(uploaded_file)
|
17 |
+
st.image(image, caption="上传的照片", use_column_width=True)
|
18 |
+
|
19 |
+
# 将图片转换为字节流
|
20 |
+
img_byte_arr = io.BytesIO()
|
21 |
+
image.save(img_byte_arr, format='PNG')
|
22 |
+
img_byte_arr = img_byte_arr.getvalue()
|
23 |
+
|
24 |
+
# 调用OpenAI的图像分析API
|
25 |
+
response = openai.Image.create(
|
26 |
+
prompt="Analyze the contents of this image",
|
27 |
+
n=1,
|
28 |
+
size="100x100",
|
29 |
+
response_format="json"
|
30 |
+
)
|
31 |
+
|
32 |
+
# 显示分析结果
|
33 |
+
st.subheader("照片内容分析结果")
|
34 |
+
st.write(response['choices'][0]['text'])
|
35 |
+
else:
|
36 |
+
st.write("请上传一张照片以进行分析。")
|