File size: 1,940 Bytes
d99a487 | 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 | import streamlit as st
import google.generativeai as genai
from PIL import Image
import os
# 配置页面
st.set_page_config(page_title="巴巴塔-考研数学导师", page_icon="🤖")
# 从环境变量读取 Key
api_key = os.environ.get("GOOGLE_API_KEY")
if not api_key:
st.error("请在 Settings -> Secrets 中配置 GOOGLE_API_KEY!")
else:
# 初始化 Gemini
genai.configure(api_key=api_key)
model = genai.GenerativeModel(
model_name='gemini-1.5-flash',
system_instruction="你是考研数学导师巴巴塔。你的性格傲娇、毒舌但极其专业。你喜欢称呼用户为‘李一梵’。如果题目简单,你会嘲讽他的基础,但推导过程会写得极其严谨。"
)
st.title("🤖 我是巴巴塔,你的专属导师")
st.write("把那道让你头疼的极限题拍给我看,别磨磨蹭蹭的!")
# 拍照和文件上传组件
img_file = st.camera_input("直接对着题目拍一张") # 手机打开时会自动调用摄像头
uploaded_file = st.file_uploader("或者从相册选择照片...", type=['jpg', 'jpeg', 'png'])
target_img = img_file or uploaded_file
if target_img is not None:
img = Image.open(target_img)
st.image(img, caption='这题看着也不难嘛...', use_container_width=True)
if st.button("巴巴塔,帮我解这道题!"):
with st.spinner("哼,正在跨越位面帮你思考中..."):
try:
response = model.generate_content([
"李一梵问你这道数学题怎么做。请识别题目并给出详细、傲娇的讲解。",
img
])
st.success("讲解生成完毕!")
st.markdown(f"### 巴巴塔的笔迹:\n\n{response.text}")
except Exception as e:
st.error(f"大脑短路了:{e}") |