import streamlit as st import openai from PIL import Image import io # 设置OpenAI API密钥 openai.api_key = "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" st.title("照片内容分析 App") # 上传图片 uploaded_file = st.file_uploader("上传一张照片", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # 显示上传的图片 image = Image.open(uploaded_file) st.image(image, caption="上传的照片", use_column_width=True) # 将图片转换为字节流 img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='PNG') img_byte_arr = img_byte_arr.getvalue() # 调用OpenAI的图像分析API response = openai.Image.create( prompt="Analyze the contents of this image", n=1, size="100x100", response_format="json" ) # 显示分析结果 st.subheader("照片内容分析结果") st.write(response['choices'][0]['text']) else: st.write("请上传一张照片以进行分析。")