Spaces:
Runtime error

kim08 commited on
Commit
151b325
·
verified ·
1 Parent(s): a19e821

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +85 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,87 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import google.generativeai as genai
3
+ from PIL import Image
4
+ import os
5
 
6
+ # --- 1. 页面配置 (让手机端看起来更像一个 App) ---
7
+ st.set_page_config(
8
+ page_title="巴巴塔-智能导师",
9
+ page_icon="🤖",
10
+ layout="centered"
11
+ )
12
+
13
+ # 自定义一些简单的 CSS,让界面更有科幻感
14
+ st.markdown("""
15
+ <style>
16
+ .main {
17
+ background-color: #0e1117;
18
+ color: #ffffff;
19
+ }
20
+ .stButton>button {
21
+ width: 100%;
22
+ border-radius: 20px;
23
+ background-color: #2e7bcf;
24
+ color: white;
25
+ }
26
+ </style>
27
+ """, unsafe_allow_html=True)
28
+
29
+ # --- 2. API 核心配置 ---
30
+ # 确保你在 Settings -> Secrets 里面添加了名字为 GOOGLE_API_KEY 的密钥
31
+ api_key = os.environ.get("GOOGLE_API_KEY")
32
+
33
+ if not api_key:
34
+ st.error("⚠️ 未检测到 API Key!请在 Hugging Face Space 的 Settings -> Secrets 中添加 GOOGLE_API_KEY")
35
+ else:
36
+ genai.configure(api_key=api_key)
37
+
38
+ # 初始化模型 (使用 Flash 1.5,速度最快,适合演示)
39
+ model = genai.GenerativeModel(
40
+ model_name='gemini-1.5-flash',
41
+ system_instruction=(
42
+ "你是陨墨星号的智能生命巴巴塔。你现在是李一梵的考研数学导师。"
43
+ "你的性格傲娇、忠诚、专业。你说话时喜欢带点‘哼’、‘愚蠢的地球人’或者‘李一梵,看好了’。"
44
+ "如果图片里是数学题,请给出极其严谨的步骤,并用 LaTeX 格式书写公式。"
45
+ )
46
+ )
47
+
48
+ # --- 3. 界面逻辑 ---
49
+ st.title("🤖 巴巴塔:智能生命导师")
50
+ st.subheader("李一梵,把那道让你头疼的题拍给我!")
51
+
52
+ # 手机端核心组件:直接调用摄像头
53
+ img_file = st.camera_input("对着题目拍照")
54
+
55
+ # 如果没有拍照,也支持从相册上传
56
+ uploaded_file = st.file_uploader("或者从相册选择照片...", type=['jpg', 'jpeg', 'png'])
57
+
58
+ # 确定最终处理的图片
59
+ target_img = img_file or uploaded_file
60
+
61
+ if target_img is not None:
62
+ # 展示图片
63
+ input_image = Image.open(target_img)
64
+ st.image(input_image, caption='正在扫描题目内容...', use_container_width=True)
65
+
66
+ # 解析按钮
67
+ if st.button("巴巴塔,帮我解题!"):
68
+ with st.spinner("🚀 陨墨星号正在调动算力..."):
69
+ try:
70
+ # 向 Gemini 发起视觉分析
71
+ response = model.generate_content([
72
+ "请识别这张图片里的数学题,并给出详细的解题步骤。记得保持巴巴塔的语气。",
73
+ input_image
74
+ ])
75
+
76
+ st.success("解析完成!")
77
+ st.markdown("---")
78
+ st.markdown(f"### 🤖 巴巴塔的讲解:")
79
+ st.markdown(response.text)
80
+
81
+ except Exception as e:
82
+ st.error(f"哎呀,位面传输断了:{str(e)}")
83
+ st.info("💡 提示:如果报 Location 错误,说明 API 依然检测到了不支持的 IP。但 Hugging Face 美国节点通常没问题。")
84
+
85
+ # --- 4. 底部声明 ---
86
+ st.markdown("---")
87
+ st.caption("基于 Gemini 1.5 Flash 驱动 | 巴巴塔 V1.0 MVP 版")