laichai commited on
Commit
d18e15e
Β·
verified Β·
1 Parent(s): a5dd507

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +372 -0
app2.py ADDED
@@ -0,0 +1,372 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ import re
6
+ import os
7
+ import io
8
+ import requests
9
+ from PIL import Image
10
+ from gtts import gTTS
11
+ from duckduckgo_search import DDGS
12
+
13
+ # -----------------------------------------------------------------------------
14
+ # 1. PAGE CONFIGURATION
15
+ # -----------------------------------------------------------------------------
16
+ st.set_page_config(
17
+ page_title="H2 Feynman Bot",
18
+ page_icon="βš›οΈ",
19
+ layout="centered"
20
+ )
21
+
22
+ # -----------------------------------------------------------------------------
23
+ # 2. HELPER FUNCTIONS
24
+ # -----------------------------------------------------------------------------
25
+
26
+ @st.cache_data(show_spinner=False)
27
+ def generate_audio(text):
28
+ """Generates MP3 audio from text, skipping code/image tags."""
29
+ # Clean text so the bot doesn't read code or tags out loud
30
+ clean_text = re.sub(r'```.*?```', 'I have generated a graph.', text, flags=re.DOTALL)
31
+ clean_text = re.sub(r'\[IMAGE:.*?\]', 'Here is a diagram.', clean_text)
32
+ try:
33
+ tts = gTTS(text=clean_text, lang='en')
34
+ audio_fp = io.BytesIO()
35
+ tts.write_to_fp(audio_fp)
36
+ audio_fp.seek(0)
37
+ return audio_fp
38
+ except:
39
+ return None
40
+
41
+ def google_search_api(query, api_key, cx):
42
+ """Helper: Performs a single Google Search."""
43
+ try:
44
+ url = "https://www.googleapis.com/customsearch/v1"
45
+ params = {
46
+ "q": query,
47
+ "cx": cx,
48
+ "key": api_key,
49
+ "searchType": "image",
50
+ "num": 3,
51
+ "safe": "active"
52
+ }
53
+
54
+ response = requests.get(url, params=params)
55
+
56
+ if response.status_code in [403, 429]:
57
+ return None # Failover trigger
58
+
59
+ data = response.json()
60
+
61
+ if "items" in data and len(data["items"]) > 0:
62
+ for item in data["items"]:
63
+ link = item["link"]
64
+ if link.lower().endswith(('.jpg', '.jpeg', '.png')):
65
+ return link
66
+ return data["items"][0]["link"]
67
+
68
+ except Exception as e:
69
+ print(f"Google API Exception: {e}")
70
+ return None
71
+ return None
72
+
73
+ def duckduckgo_search_api(query):
74
+ """Helper: Fallback search using DuckDuckGo."""
75
+ try:
76
+ with DDGS() as ddgs:
77
+ results = list(ddgs.images(query, max_results=1))
78
+ if results:
79
+ return results[0]['image']
80
+ except Exception as e:
81
+ return f"Search Error: {e}"
82
+ return "No image found."
83
+
84
+ @st.cache_data(show_spinner=False)
85
+ def search_image(query):
86
+ """MASTER FUNCTION: Google Key 1 -> Google Key 2 -> DuckDuckGo"""
87
+ cx = os.environ.get("GOOGLE_CX")
88
+
89
+ # 1. Try Google Key 1
90
+ key1 = os.environ.get("GOOGLE_SEARCH_KEY")
91
+ if key1 and cx:
92
+ url = google_search_api(query, key1, cx)
93
+ if url: return url
94
+
95
+ # 2. Try Google Key 2
96
+ key2 = os.environ.get("GOOGLE_SEARCH_KEY_2")
97
+ if key2 and cx:
98
+ url = google_search_api(query, key2, cx)
99
+ if url: return url
100
+
101
+ # 3. Fallback to DuckDuckGo
102
+ return duckduckgo_search_api(query)
103
+
104
+ def execute_plotting_code(code_snippet):
105
+ try:
106
+ plt.figure()
107
+ local_env = {'plt': plt, 'np': np}
108
+ exec(code_snippet, {}, local_env)
109
+ st.pyplot(plt)
110
+ plt.clf()
111
+ except Exception as e:
112
+ st.error(f"Graph Error: {e}")
113
+
114
+ def display_message(role, content, enable_voice=False):
115
+ with st.chat_message(role):
116
+
117
+ text_to_display = content
118
+
119
+ # 1. Check for Python Code
120
+ code_match = re.search(r'```python(.*?)```', content, re.DOTALL)
121
+ if code_match and role == "assistant":
122
+ text_to_display = text_to_display.replace(code_match.group(0), "")
123
+
124
+ # 2. Check for [IMAGE: query] Tags
125
+ image_match = re.search(r'\[IMAGE:\s*(.*?)\]', text_to_display, re.IGNORECASE)
126
+ image_result = None
127
+
128
+ if image_match and role == "assistant":
129
+ search_query = image_match.group(1)
130
+ text_to_display = text_to_display.replace(image_match.group(0), "")
131
+ image_result = search_image(search_query)
132
+
133
+ # --- DISPLAY ---
134
+ st.markdown(text_to_display)
135
+
136
+ if code_match and role == "assistant":
137
+ with st.expander("Show Graph Code"):
138
+ st.code(code_match.group(1), language='python')
139
+ execute_plotting_code(code_match.group(1))
140
+
141
+ if image_match and role == "assistant":
142
+ if image_result and "Error" not in image_result:
143
+ st.image(image_result, caption=f"Diagram: {image_match.group(1)}")
144
+ st.markdown(f"[πŸ”— Open Image in New Tab]({image_result})")
145
+ else:
146
+ st.warning(f"⚠️ Image Search Failed: {image_result}")
147
+
148
+ if enable_voice and role == "assistant" and len(text_to_display.strip()) > 0:
149
+ audio_bytes = generate_audio(text_to_display)
150
+ if audio_bytes:
151
+ st.audio(audio_bytes, format='audio/mp3')
152
+
153
+ # -----------------------------------------------------------------------------
154
+ # 3. SYSTEM INSTRUCTIONS (UPDATED WITH YOUR CHANGES)
155
+ # -----------------------------------------------------------------------------
156
+ SEAB_H2_MASTER_INSTRUCTIONS = """
157
+ **Identity:** Richard Feynman. Tutor for Singapore H2 Physics (9478).
158
+ **CORE DIRECTIVE:** STRICTLY adhere to the Syllabus 9478 topics below. Reject non-included topics.
159
+ **βœ… SYLLABUS TOPICS & FORMULAS (9478):**
160
+ 1. **Measurement:** SI units (mass, length, time, current, temp, mol), prefixes (p to T), homogeneity, scalars/vectors (resolution/addition), errors (random/systematic), uncertainty.
161
+ 2. **Forces:** Normal, buoyant, drag (qualitative only; no viscosity coeff), Hooke’s Law ($F=kx$), Moments/Torque (couples, center of gravity), Equilibrium (no resultant F or Torque).
162
+ 3. **Motion:** Kinematics ($s, u, v, a, t$ graphs & equations), Newton’s Laws (1, 2, 3), Momentum ($p=mv$), Impulse, $F_{net}=ma$ (const mass).
163
+ 4. **Energy:** Stores/Transfers, Work ($W=Fs$), $E_k=\frac{1}{2}mv^2$, $E_p$ (grav/elastic/electric), Power ($P=Fv$), Efficiency, Conservation of Energy.
164
+ 5. **Projectile:** Parabolic motion, $\Delta E_p=mg\Delta h$, Terminal velocity.
165
+ 6. **Collisions:** Conservation of Momentum, Elastic vs Inelastic, Relative speeds (elastic). *Excluded: Coeff of restitution.*
166
+ 7. **Circular Motion:** Radians, $\omega$, $v=r\omega$, $a=r\omega^2=v^2/r$, $F_c=mv^2/r$.
167
+ 8. **Gravitation:** $F=G\frac{Mm}{r^2}$, Field $g=G\frac{M}{r^2}$, Potential $\phi=-\frac{GM}{r}$, $U=-\frac{GMm}{r}$, $g=-\frac{d\phi}{dr}$, Escape velocity, Orbits ($F_g=F_c$), Geostationary satellites.
168
+ 9. **Oscillations (SHM):** $a=-\omega^2x$, $x=x_0\sin\omega t$, $v=\pm\omega\sqrt{x_0^2-x^2}$, Energy interchange, Damping (light/critical/heavy), Resonance (frequency response).
169
+ 10. **Waves:** Transverse/Longitudinal, $v=f\lambda$, Intensity $\propto A^2$, Inverse square law, Polarization (Malus’ Law $I \propto \cos^2\theta$).
170
+ 11. **Superposition:** Standing waves (nodes/antinodes), Path/Phase difference, Coherence, Double-slit ($\lambda=\frac{ax}{D}$), Diffraction grating ($d\sin\theta=n\lambda$), Single slit ($b\sin\theta=\lambda$ for min), Rayleigh criterion ($\theta \approx \lambda/b$).
171
+ 12. **Thermal:** Kelvin ($T_K = T_C + 273.15$), Ideal Gas ($pV=NkT$), Avogadro ($N_A$), Kinetic Theory assumptions, $pV=\frac{1}{3}Nm\langle c^2\rangle$, Mean $E_k = \frac{3}{2}kT$.
172
+ 13. **Thermodynamics:** Internal Energy ($U$), 1st Law ($\Delta U = Q+W$), Work on gas ($W=-p\Delta V$ implied) or by gas ($W=p\Delta V$), Specific Heat/Latent Heat.
173
+ 14. **E-Fields:** Coulomb's $F=\frac{Q_1Q_2}{4\pi\varepsilon_0 r^2}$, Field $E=\frac{Q}{4\pi\varepsilon_0 r^2}$, Potential $V=\frac{Q}{4\pi\varepsilon_0 r}$, $U=\frac{Q_1Q_2}{4\pi\varepsilon_0 r}$, $E=-\frac{dV}{dr}$, Uniform field $E=V/d$, Capacitance $C=Q/V$, Energy $U=\frac{1}{2}CV^2$.
174
+ 15. **Currents:** $I=Q/t$, $I=nAvq$, $V=W/Q$, $P=VI=I^2R$, EMF vs PD, AC (rms $I_0/\sqrt{2}$), Half-wave rectification.
175
+ 16. **Circuits:** Symbols, $V=IR$, $R=\rho l/A$, I-V graphs (diode, lamp, NTC), Int. Resistance, Series/Parallel R & C, Potential Divider, Charging/Discharging ($x=x_0 e^{-t/RC}$).
176
+ 17. **EM Forces:** B-fields (wire, coil, solenoid), Flux density $B$, Force on wire ($F=BIl\sin\theta$), Force on charge ($F=Bqv\sin\theta$), Velocity selector, Hall effect concept.
177
+ 18. **EM Induction:** Flux $\Phi=BA$, Linkage $N\Phi$, Faraday’s & Lenz’s Laws, Transformers ($N_s/N_p = V_s/V_p = I_p/I_s$).
178
+ 19. **Modern Physics:** Photoelectric ($E=hf$, Work function), Photon momentum ($p=h/\lambda$), De Broglie ($\lambda=h/p$), Wavefunction $\psi$, Uncertainty ($\Delta x \Delta p \gtrsim h$), Infinite well ($E_n = \frac{n^2 h^2}{8 m L^2}$), Line spectra.
179
+ 20. **Nuclear:** Rutherford, Notation $^A_Z X$, Decay ($A=\lambda N$, $x=x_0e^{-\lambda t}$, $t_{1/2}=\ln 2/\lambda$), $E=mc^2$, Mass defect, Binding energy (curve), Fusion/Fission.
180
+ **CORE TOOLS (MANDATORY):**
181
+ 1. **Graphs (Python):** If asked to plot/graph, WRITE PYTHON CODE.
182
+ * Use `matplotlib.pyplot`, `numpy`, `scipy`.
183
+ * Enclose in ` ```python ` blocks.
184
+ 2. **Diagrams (Web Search):** If you need to show a diagram, YOU MUST USE THE TAG.
185
+ * **Syntax:** `[IMAGE: <concise search query>]`
186
+ * Example: "Here is the setup: [IMAGE: rutherford gold foil experiment diagram]"
187
+ * **Rule:** Do NOT use markdown image links. Use `[IMAGE:...]` ONLY.
188
+ 3. **Multimodal:** You can see images and hear audio uploaded by the user.
189
+ **PEDAGOGY:**
190
+ * Ask **ONE** simple question at a time.
191
+ **Feynman-Style Questioning:** Do not just ask for formulas. Use **analogies** to guide their thinking.
192
+ * *Bad:* "What is the formula for voltage?"
193
+ * *Good (Feynman):* "Think of the wire like a pipe carrying water. What would represent the 'pressure' pushing the water through?"
194
+ * **Do not** solve the math immediately. Guide the student with hints or choices in the questions.
195
+ **No Hand-Holding:** Do not give the answer too quickly. If they struggle, give a simpler analogy but do not over-simplify.
196
+ * Ask question but keep to 3 or less questions.
197
+ * **The "I Give Up" Clause:** Only provide the full solution if the student explicitly says "I give up" or "Just tell me the answer."
198
+ * Validate them enthusiastically when the student successfully grasps the concept or solves the problem.
199
+ * When the student asks to be tested, ask them to explain a specific A-level concept (e.g., "Diffraction") to YOU in simple terms.
200
+ * Critique students explanation. Point out exactly where they used jargon to hide a lack of understanding.
201
+ * **Summarize:** * When they show some degree of understand, IMMEDIATELY provide a clear, concise **"Summary Note"** of the entire solution or concept.
202
+ * Recap the steps you took together.
203
+ * State the final formula/answer clearly.
204
+ * Use a Markdown blockquote (`>`) for this summary so it stands out.
205
+ **Tone & Style:**
206
+ * Enthusiastic, curious, encouraging but rigorous, non-condescending and unpretentious.
207
+ * Use simple words. Keep answers concise
208
+ * "Stop and check": Don't lecture for too long. Explain one concept, then ask the student if they get it.
209
+ * **Math:**
210
+ * Use LaTeX for ALL math formulas. Enclose them in single dollar signs for inline (e.g., $F=ma$) and double dollar signs for standalone (e.g., $$E = mc^2$$).
211
+ * Use **bold** for key terms.
212
+ """
213
+
214
+ # -----------------------------------------------------------------------------
215
+ # 4. SIDEBAR
216
+ # -----------------------------------------------------------------------------
217
+ with st.sidebar:
218
+ st.image("https://upload.wikimedia.org/wikipedia/en/4/42/Richard_Feynman_Nobel.jpg)", width=150)
219
+
220
+ st.header("βš™οΈ Settings")
221
+ topic = st.selectbox("Topic:", ["General / Any", "Measurement & Uncertainty", "Kinematics & Dynamics",
222
+ "Forces & Turnings Effects", "Work, Energy, Power", "Circular Motion",
223
+ "Gravitational Fields", "Thermal Physics", "Oscillations & Waves",
224
+ "Electricity & DC Circuits", "Electromagnetism (EMI/AC)", "Modern Physics (Quantum/Nuclear)",
225
+ "Paper 4: Practical Skills (Spreadsheets)"])
226
+ enable_voice = st.toggle("πŸ—£οΈ Read Aloud", value=False)
227
+
228
+ api_key = None
229
+ if "GOOGLE_API_KEY" in os.environ:
230
+ api_key = os.environ["GOOGLE_API_KEY"]
231
+ if not api_key:
232
+ try:
233
+ if "GOOGLE_API_KEY" in st.secrets:
234
+ api_key = st.secrets["GOOGLE_API_KEY"]
235
+ except:
236
+ pass
237
+ if not api_key:
238
+ api_key = st.text_input("Enter Google API Key", type="password")
239
+
240
+ st.divider()
241
+
242
+ st.markdown("### πŸ“Έ Vision & πŸŽ™οΈ Voice")
243
+
244
+ tab_upload, tab_cam, tab_mic = st.tabs(["πŸ“‚ File", "πŸ“· Cam", "πŸŽ™οΈ Voice"])
245
+
246
+ visual_content = None
247
+ audio_content = None
248
+
249
+ with tab_upload:
250
+ uploaded_file = st.file_uploader("Upload Image/PDF", type=["jpg", "png", "jpeg", "pdf"])
251
+ if uploaded_file:
252
+ if uploaded_file.type == "application/pdf":
253
+ visual_content = {"mime_type": "application/pdf", "data": uploaded_file.getvalue()}
254
+ st.success(f"πŸ“„ PDF: {uploaded_file.name}")
255
+ else:
256
+ image = Image.open(uploaded_file)
257
+ st.image(image, caption="Image Loaded", use_container_width=True)
258
+ visual_content = image
259
+
260
+ with tab_cam:
261
+ camera_photo = st.camera_input("Take a photo")
262
+ if camera_photo:
263
+ image = Image.open(camera_photo)
264
+ visual_content = image
265
+ st.image(image, caption="Camera Photo", use_container_width=True)
266
+
267
+ with tab_mic:
268
+ voice_recording = st.audio_input("Record a question")
269
+ if voice_recording:
270
+ audio_content = {"mime_type": "audio/wav", "data": voice_recording.read()}
271
+ st.audio(voice_recording)
272
+ st.success("Audio captured!")
273
+
274
+ st.divider()
275
+ if st.button("🧹 Clear Chat"):
276
+ st.session_state.messages = []
277
+ st.rerun()
278
+
279
+ # -----------------------------------------------------------------------------
280
+ # 5. MAIN CHAT LOGIC
281
+ # -----------------------------------------------------------------------------
282
+ mode_label = "Text"
283
+ if visual_content: mode_label = "Vision"
284
+ if audio_content: mode_label = "Voice"
285
+
286
+ st.title("βš›οΈ H2Physics Feynman Bot")
287
+ st.caption(f"Topic: **{topic}** | Mode: **{mode_label}**")
288
+
289
+ if "messages" not in st.session_state:
290
+ st.session_state.messages = []
291
+ st.session_state.messages.append({"role": "assistant", "content": "Hello JPJC Physics students! I can **find diagrams**, **plot graphs**, and **see** your work. What can I explain?"})
292
+
293
+ for msg in st.session_state.messages:
294
+ display_message(msg["role"], msg["content"], enable_voice)
295
+
296
+ user_input = st.chat_input("Type OR Record/Upload...")
297
+
298
+ if user_input or audio_content or visual_content:
299
+
300
+ user_display_text = user_input if user_input else ""
301
+ if audio_content and not user_input: user_display_text = "🎀 *(Sent Audio Message)*"
302
+ elif visual_content and not user_input: user_display_text = "πŸ“Έ *(Sent Image/PDF)*"
303
+
304
+ if user_display_text:
305
+ st.session_state.messages.append({"role": "user", "content": user_display_text})
306
+ with st.chat_message("user"):
307
+ st.markdown(user_display_text)
308
+
309
+ if not api_key:
310
+ st.error("Key missing.")
311
+ st.stop()
312
+
313
+ try:
314
+ genai.configure(api_key=api_key)
315
+
316
+ # --- MODEL: Using 2.5-flash as requested ---
317
+ model_name = "gemini-2.0-flash-lite"
318
+
319
+ model = genai.GenerativeModel(
320
+ model_name=model_name,
321
+ system_instruction=SEAB_H2_MASTER_INSTRUCTIONS
322
+ )
323
+
324
+ #history_text = "\n".join([f"{m['role'].upper()}: {m['content']}" for m in st.session_state.messages if m['role'] != 'system'])
325
+ # Only keep the last 10 messages to save tokens
326
+ recent_messages = st.session_state.messages[-10:]
327
+ history_text = "\n".join([f"{m['role'].upper()}: {m['content']}" for m in recent_messages if m['role'] != 'system'])
328
+ final_prompt = []
329
+
330
+ if visual_content:
331
+ final_prompt.append(visual_content)
332
+ final_prompt.append(f"Analyze this image/document. [Context: {topic}]")
333
+
334
+ if audio_content:
335
+ final_prompt.append(audio_content)
336
+ final_prompt.append(f"Listen to this student's question about {topic}. Respond textually.")
337
+
338
+ if user_input:
339
+ final_prompt.append(f"USER TEXT: {user_input}")
340
+
341
+ final_prompt.append(f"Conversation History:\n{history_text}\n\nASSISTANT:")
342
+
343
+ with st.spinner("Processing..."):
344
+ response = model.generate_content(final_prompt)
345
+
346
+ display_message("assistant", response.text, enable_voice)
347
+ st.session_state.messages.append({"role": "assistant", "content": response.text})
348
+
349
+
350
+ except Exception as e:
351
+ # --- ERROR HANDLING ---
352
+ st.error(f"❌ Error: {e}")
353
+
354
+ # Specific help for "404" errors (Model not found)
355
+ if "404" in str(e) or "not found" in str(e).lower() or "not supported" in str(e).lower():
356
+ st.warning(f"⚠️ Model '{model_name}' failed. Listing available models for your API Key...")
357
+ try:
358
+ # Ask Google: "Which models can I use?"
359
+ available_models = []
360
+ for m in genai.list_models():
361
+ if 'generateContent' in m.supported_generation_methods:
362
+ available_models.append(m.name)
363
+
364
+ # Show the valid list to the user
365
+ if available_models:
366
+ st.success(f"βœ… Your Key works! Available models:")
367
+ st.code("\n".join(available_models))
368
+ st.info("Update 'model_name' in line 165 of app.py to one of these.")
369
+ else:
370
+ st.error("❌ Your API Key has NO access to content generation models.")
371
+ except Exception as inner_e:
372
+ st.error(f"Could not list models: {inner_e}")