File size: 2,091 Bytes
a223b89 151b325 a223b89 f18c2f5 151b325 f18c2f5 452615a 151b325 452615a f18c2f5 452615a f18c2f5 9ed8d76 f18c2f5 9ed8d76 f18c2f5 452615a f18c2f5 452615a f18c2f5 452615a f18c2f5 151b325 f18c2f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import streamlit as st
import google.generativeai as genai
from PIL import Image
import os
st.set_page_config(page_title="巴巴塔-最终稳定版", page_icon="🤖")
# 1. 核心:从 Secrets 读取 Key
api_key = os.environ.get("GOOGLE_API_KEY")
if not api_key:
st.error("🚨 还没配置 API Key!请去 Settings -> Secrets 添加 GOOGLE_API_KEY")
else:
# 初始化
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
st.title("🤖 我是巴巴塔")
st.info("李一梵,如果拍照 403,请点击下方‘上传图片’,这在云端更稳定!")
# --- 核心修改:弃用 camera_input,改用 file_uploader ---
# 实验证明 file_uploader 在 Chrome 上的兼容性比拍照组件高得多
uploaded_file = st.file_uploader("【点击这里上传题目照片】", type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
try:
# 转换图片格式
img = Image.open(uploaded_file).convert('RGB')
st.image(img, caption="图片已锁定,准备解析...", width=300)
# 使用独立按钮,不套用 Form
if st.button("🚀 巴巴塔,启动解析!"):
with st.spinner("陨墨星号算力调动中..."):
# 关键修复:直接传递字节流,避开 Axios 文件预处理
response = model.generate_content([
"你是导师巴巴塔,请识别图中数学题并给出傲娇的解题过程。",
img
])
if response.text:
st.success("✅ 解析成功!")
st.markdown(response.text)
else:
st.warning("⚠️ 巴巴塔收到了图片但没说话,请重试。")
except Exception as e:
st.error(f"❌ 解析失败:{str(e)}")
# --- 底部强制清理 ---
if st.button("清理缓存并重启系统"):
st.rerun() |