Download src/streamlit_app.py from kim08/bbt: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/kim08/bbt/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/kim08/bbt/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/kim08/bbt/resolve/main/src/streamlit_app.py
2.09 kB
| 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() |