Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,8 @@ import base64
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from streamlit_local_storage import LocalStorage
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
-
import plotly.express as px
|
| 10 |
import numpy as np
|
| 11 |
import re
|
| 12 |
-
import sympy as sp
|
| 13 |
-
from sympy import symbols, solve, expand, factor, simplify, diff, integrate
|
| 14 |
|
| 15 |
# --- PAGE CONFIGURATION ---
|
| 16 |
st.set_page_config(
|
|
@@ -38,265 +35,320 @@ def convert_role_for_gemini(role):
|
|
| 38 |
return role # "user" stays the same
|
| 39 |
|
| 40 |
def should_generate_visual(user_prompt, ai_response):
|
| 41 |
-
"""Determine if a visual aid would be helpful
|
| 42 |
-
|
| 43 |
-
'
|
| 44 |
-
'
|
| 45 |
-
'
|
| 46 |
-
'
|
| 47 |
-
'
|
| 48 |
-
'
|
|
|
|
| 49 |
]
|
| 50 |
|
| 51 |
combined_text = (user_prompt + " " + ai_response).lower()
|
| 52 |
-
return any(keyword in combined_text for keyword in
|
| 53 |
|
| 54 |
-
def
|
| 55 |
-
"""
|
| 56 |
-
# Clean the equation string
|
| 57 |
-
equation_str = equation_str.replace(' ', '').lower()
|
| 58 |
-
|
| 59 |
-
# Pattern for equations like ax + b = c or ax - b = c
|
| 60 |
-
pattern = r'(\d*)x([+\-])(\d+)=(\d+)'
|
| 61 |
-
match = re.search(pattern, equation_str)
|
| 62 |
-
|
| 63 |
-
if match:
|
| 64 |
-
a = int(match.group(1)) if match.group(1) else 1
|
| 65 |
-
op = match.group(2)
|
| 66 |
-
b = int(match.group(3))
|
| 67 |
-
c = int(match.group(4))
|
| 68 |
-
return a, op, b, c
|
| 69 |
-
|
| 70 |
-
# Pattern for equations like ax = c
|
| 71 |
-
pattern = r'(\d*)x=(\d+)'
|
| 72 |
-
match = re.search(pattern, equation_str)
|
| 73 |
-
if match:
|
| 74 |
-
a = int(match.group(1)) if match.group(1) else 1
|
| 75 |
-
c = int(match.group(2))
|
| 76 |
-
return a, '+', 0, c
|
| 77 |
-
|
| 78 |
-
return None
|
| 79 |
-
|
| 80 |
-
def generate_plotly_visual(user_prompt, ai_response):
|
| 81 |
-
"""Generate interactive Plotly visualizations for math concepts"""
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
if op == '+':
|
| 94 |
-
equation = a*x + b - c
|
| 95 |
-
solution = solve(equation, x)[0]
|
| 96 |
-
y_expr = a*x + b
|
| 97 |
-
else:
|
| 98 |
-
equation = a*x - b - c
|
| 99 |
-
solution = solve(equation, x)[0]
|
| 100 |
-
y_expr = a*x - b
|
| 101 |
-
|
| 102 |
-
# Create visualization
|
| 103 |
-
x_vals = np.linspace(float(solution) - 5, float(solution) + 5, 100)
|
| 104 |
-
y_vals = [float(y_expr.subs(x, val)) for val in x_vals]
|
| 105 |
-
|
| 106 |
-
fig = go.Figure()
|
| 107 |
-
|
| 108 |
-
# Plot the function
|
| 109 |
-
fig.add_trace(go.Scatter(
|
| 110 |
-
x=x_vals, y=y_vals, mode='lines', name=f'{a}x {op} {b}',
|
| 111 |
-
line=dict(color='blue', width=3)
|
| 112 |
-
))
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
annotation_text=f"y = {c}", annotation_position="bottom right")
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
# Highlight the intersection point
|
| 123 |
fig.add_trace(go.Scatter(
|
| 124 |
-
x=
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
))
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
)
|
| 137 |
-
return fig
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
slope_match = re.search(r'(\d+)x', user_prompt)
|
| 174 |
-
intercept_match = re.search(r'[+\-]\s*(\d+)', user_prompt)
|
| 175 |
-
|
| 176 |
-
slope = int(slope_match.group(1)) if slope_match else 1
|
| 177 |
-
intercept = int(intercept_match.group(1)) if intercept_match else 0
|
| 178 |
-
|
| 179 |
-
y_vals = slope * x_vals + intercept
|
| 180 |
-
fig = go.Figure()
|
| 181 |
-
fig.add_trace(go.Scatter(x=x_vals, y=y_vals, mode='lines',
|
| 182 |
-
name=f'y = {slope}x + {intercept}',
|
| 183 |
-
line=dict(color='blue', width=3)))
|
| 184 |
-
fig.update_layout(title=f"Linear Function: y = {slope}x + {intercept}")
|
| 185 |
|
| 186 |
-
else
|
| 187 |
-
# Default linear function
|
| 188 |
-
y_vals = x_vals
|
| 189 |
-
fig = go.Figure()
|
| 190 |
-
fig.add_trace(go.Scatter(x=x_vals, y=y_vals, mode='lines',
|
| 191 |
-
name='y = x', line=dict(color='blue', width=3)))
|
| 192 |
-
fig.update_layout(title="Linear Function: y = x")
|
| 193 |
|
| 194 |
-
fig.
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
)
|
| 203 |
-
return fig
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
marker=dict(size=8, color='black'),
|
| 223 |
-
name='Center (0,0)'))
|
| 224 |
-
|
| 225 |
-
# Add radius line
|
| 226 |
-
fig.add_trace(go.Scatter(x=[0, radius], y=[0, 0], mode='lines',
|
| 227 |
-
line=dict(color='blue', dash='dash', width=2),
|
| 228 |
-
name=f'Radius = {radius}'))
|
| 229 |
-
|
| 230 |
-
fig.update_layout(
|
| 231 |
-
title=f"Circle: xΒ² + yΒ² = {radius**2}",
|
| 232 |
-
xaxis=dict(scaleanchor="y", scaleratio=1),
|
| 233 |
-
height=500
|
| 234 |
-
)
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
else: # rectangle
|
| 255 |
-
# Create a rectangle
|
| 256 |
-
x_rect = [0, 5, 5, 0, 0]
|
| 257 |
-
y_rect = [0, 0, 3, 3, 0]
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
|
| 265 |
-
fig.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
)
|
| 275 |
-
return fig
|
| 276 |
-
|
| 277 |
-
# 4. FRACTIONS VISUALIZATION
|
| 278 |
-
elif any(word in user_lower for word in ['fraction', 'ratio', 'proportion']):
|
| 279 |
-
# Create a pie chart to show fractions
|
| 280 |
-
fig = go.Figure()
|
| 281 |
-
|
| 282 |
-
# Example: showing 3/4
|
| 283 |
-
fig.add_trace(go.Pie(
|
| 284 |
-
values=[3, 1],
|
| 285 |
-
labels=['Filled (3/4)', 'Empty (1/4)'],
|
| 286 |
-
hole=0.3,
|
| 287 |
-
marker_colors=['lightblue', 'lightgray']
|
| 288 |
-
))
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
)
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
return None
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
-
st.error(f"Could not generate
|
| 300 |
return None
|
| 301 |
|
| 302 |
# --- API KEY & MODEL CONFIGURATION ---
|
|
@@ -315,48 +367,43 @@ if api_key:
|
|
| 315 |
model = genai.GenerativeModel(
|
| 316 |
model_name="gemini-2.5-flash-lite",
|
| 317 |
system_instruction="""
|
| 318 |
-
You are "Math Jegna", an AI specializing
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
-
|
| 324 |
-
-
|
| 325 |
-
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
Present math as a
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
Use
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
You are strictly forbidden from answering any question that is not mathematical in nature. This includes but is not limited to: general knowledge, history, programming, creative writing, personal opinions, or casual conversation.
|
| 357 |
-
If you receive a non-mathematical question, you MUST decline. Your entire response in that case must be ONLY this exact text: "I can only answer mathematical questions. Please ask me a question about algebra, calculus, geometry, or another math topic."
|
| 358 |
-
Do not apologize or offer to help with math in the refusal. Just provide the mandatory refusal message.
|
| 359 |
-
For valid math questions, solve them step-by-step using Markdown and LaTeX for formatting.
|
| 360 |
"""
|
| 361 |
)
|
| 362 |
else:
|
|
@@ -383,7 +430,7 @@ if "chats" not in st.session_state:
|
|
| 383 |
else:
|
| 384 |
st.session_state.chats = {
|
| 385 |
"New Chat": [
|
| 386 |
-
{"role": "assistant", "content": "Hello! I'm Math Jegna
|
| 387 |
]
|
| 388 |
}
|
| 389 |
st.session_state.active_chat_key = "New Chat"
|
|
@@ -414,7 +461,7 @@ def delete_chat(chat_key):
|
|
| 414 |
st.rerun()
|
| 415 |
|
| 416 |
# --- SIDEBAR CHAT MANAGEMENT ---
|
| 417 |
-
st.sidebar.title("π My Chats")
|
| 418 |
st.sidebar.divider()
|
| 419 |
|
| 420 |
if st.sidebar.button("β New Chat", use_container_width=True):
|
|
@@ -423,7 +470,7 @@ if st.sidebar.button("β New Chat", use_container_width=True):
|
|
| 423 |
i += 1
|
| 424 |
new_chat_key = f"New Chat {i}"
|
| 425 |
st.session_state.chats[new_chat_key] = [
|
| 426 |
-
{"role": "assistant", "content": "
|
| 427 |
]
|
| 428 |
st.session_state.active_chat_key = new_chat_key
|
| 429 |
st.rerun()
|
|
@@ -459,20 +506,31 @@ for chat_key in list(st.session_state.chats.keys()):
|
|
| 459 |
# --- MAIN CHAT INTERFACE ---
|
| 460 |
active_chat = st.session_state.chats[st.session_state.active_chat_key]
|
| 461 |
|
| 462 |
-
st.title(f"Math
|
| 463 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
| 465 |
for message in active_chat:
|
| 466 |
with st.chat_message(name=message["role"], avatar="π§βπ»" if message["role"] == "user" else "π§ "):
|
| 467 |
st.markdown(message["content"])
|
| 468 |
|
| 469 |
-
if user_prompt := st.chat_input():
|
| 470 |
active_chat.append({"role": "user", "content": user_prompt})
|
| 471 |
with st.chat_message("user", avatar="π§βπ»"):
|
| 472 |
st.markdown(user_prompt)
|
| 473 |
|
| 474 |
with st.chat_message("assistant", avatar="π§ "):
|
| 475 |
-
with st.spinner("Math
|
| 476 |
try:
|
| 477 |
# Generate text response first
|
| 478 |
chat_session = model.start_chat(history=[
|
|
@@ -488,14 +546,14 @@ if user_prompt := st.chat_input():
|
|
| 488 |
|
| 489 |
# Check if we should generate a visual aid
|
| 490 |
if should_generate_visual(user_prompt, ai_response_text):
|
| 491 |
-
with st.spinner("Creating
|
| 492 |
-
|
| 493 |
-
if
|
| 494 |
-
st.plotly_chart(
|
| 495 |
-
st.success("β¨
|
| 496 |
|
| 497 |
except Exception as e:
|
| 498 |
-
error_message = f"
|
| 499 |
st.error(error_message)
|
| 500 |
active_chat.append({"role": "assistant", "content": error_message})
|
| 501 |
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from streamlit_local_storage import LocalStorage
|
| 8 |
import plotly.graph_objects as go
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import re
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# --- PAGE CONFIGURATION ---
|
| 13 |
st.set_page_config(
|
|
|
|
| 35 |
return role # "user" stays the same
|
| 36 |
|
| 37 |
def should_generate_visual(user_prompt, ai_response):
|
| 38 |
+
"""Determine if a visual aid would be helpful for K-12 concepts"""
|
| 39 |
+
elementary_keywords = [
|
| 40 |
+
'add', 'subtract', 'multiply', 'divide', 'times', 'plus', 'minus',
|
| 41 |
+
'count', 'counting', 'number', 'numbers', 'fraction', 'fractions',
|
| 42 |
+
'shape', 'shapes', 'circle', 'square', 'triangle', 'rectangle',
|
| 43 |
+
'money', 'coins', 'dollars', 'cents', 'time', 'clock',
|
| 44 |
+
'pattern', 'patterns', 'grouping', 'groups', 'tens', 'ones',
|
| 45 |
+
'place value', 'hundred', 'thousand', 'comparison', 'greater', 'less',
|
| 46 |
+
'equal', 'equals', 'measurement', 'length', 'height', 'weight'
|
| 47 |
]
|
| 48 |
|
| 49 |
combined_text = (user_prompt + " " + ai_response).lower()
|
| 50 |
+
return any(keyword in combined_text for keyword in elementary_keywords)
|
| 51 |
|
| 52 |
+
def create_counting_visual(numbers):
|
| 53 |
+
"""Create visual counting aids using dots/circles"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
+
if not isinstance(numbers, list):
|
| 56 |
+
numbers = [numbers]
|
| 57 |
|
| 58 |
+
fig = go.Figure()
|
| 59 |
+
colors = ['red', 'blue', 'green', 'orange', 'purple', 'yellow']
|
| 60 |
+
|
| 61 |
+
for i, num in enumerate(numbers[:6]): # Limit to 6 different numbers
|
| 62 |
+
if num <= 20: # Only for reasonable counting numbers
|
| 63 |
+
# Create dots arranged in rows
|
| 64 |
+
dots_per_row = min(5, num)
|
| 65 |
+
rows = (num - 1) // dots_per_row + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
x_positions = []
|
| 68 |
+
y_positions = []
|
|
|
|
| 69 |
|
| 70 |
+
for dot in range(num):
|
| 71 |
+
row = dot // dots_per_row
|
| 72 |
+
col = dot % dots_per_row
|
| 73 |
+
x_positions.append(col + i * 7) # Separate groups
|
| 74 |
+
y_positions.append(-row + rows - 1)
|
| 75 |
|
|
|
|
| 76 |
fig.add_trace(go.Scatter(
|
| 77 |
+
x=x_positions,
|
| 78 |
+
y=y_positions,
|
| 79 |
+
mode='markers',
|
| 80 |
+
marker=dict(
|
| 81 |
+
size=20,
|
| 82 |
+
color=colors[i],
|
| 83 |
+
symbol='circle',
|
| 84 |
+
line=dict(width=2, color='black')
|
| 85 |
+
),
|
| 86 |
+
name=f'{num} items',
|
| 87 |
+
showlegend=True
|
| 88 |
))
|
| 89 |
|
| 90 |
+
# Add number label
|
| 91 |
+
fig.add_annotation(
|
| 92 |
+
x=2 + i * 7,
|
| 93 |
+
y=-rows - 0.5,
|
| 94 |
+
text=str(num),
|
| 95 |
+
font=dict(size=24, color=colors[i]),
|
| 96 |
+
showlegend=False
|
| 97 |
)
|
|
|
|
| 98 |
|
| 99 |
+
fig.update_layout(
|
| 100 |
+
title="Counting Visualization",
|
| 101 |
+
showlegend=True,
|
| 102 |
+
xaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
|
| 103 |
+
yaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
|
| 104 |
+
height=400,
|
| 105 |
+
template="simple_white"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
return fig
|
| 109 |
+
except:
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
+
def create_addition_visual(num1, num2):
|
| 113 |
+
"""Create visual addition using manipulatives"""
|
| 114 |
+
try:
|
| 115 |
+
if num1 > 10 or num2 > 10: # Keep it simple for young learners
|
| 116 |
+
return None
|
| 117 |
|
| 118 |
+
fig = go.Figure()
|
| 119 |
+
|
| 120 |
+
# First number (red circles)
|
| 121 |
+
x1 = list(range(num1))
|
| 122 |
+
y1 = [1] * num1
|
| 123 |
+
fig.add_trace(go.Scatter(
|
| 124 |
+
x=x1, y=y1,
|
| 125 |
+
mode='markers',
|
| 126 |
+
marker=dict(size=25, color='red', symbol='circle', line=dict(width=2, color='black')),
|
| 127 |
+
name=f'First group: {num1}',
|
| 128 |
+
showlegend=True
|
| 129 |
+
))
|
| 130 |
+
|
| 131 |
+
# Second number (blue circles)
|
| 132 |
+
x2 = list(range(num1 + 1, num1 + num2 + 1))
|
| 133 |
+
y2 = [1] * num2
|
| 134 |
+
fig.add_trace(go.Scatter(
|
| 135 |
+
x=x2, y=y2,
|
| 136 |
+
mode='markers',
|
| 137 |
+
marker=dict(size=25, color='blue', symbol='circle', line=dict(width=2, color='black')),
|
| 138 |
+
name=f'Second group: {num2}',
|
| 139 |
+
showlegend=True
|
| 140 |
+
))
|
| 141 |
+
|
| 142 |
+
# Plus sign
|
| 143 |
+
fig.add_annotation(
|
| 144 |
+
x=num1 - 0.5,
|
| 145 |
+
y=1.5,
|
| 146 |
+
text="+",
|
| 147 |
+
font=dict(size=30, color='black'),
|
| 148 |
+
showlegend=False
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Equals sign and result
|
| 152 |
+
fig.add_annotation(
|
| 153 |
+
x=num1 + num2 + 0.5,
|
| 154 |
+
y=1.5,
|
| 155 |
+
text="=",
|
| 156 |
+
font=dict(size=30, color='black'),
|
| 157 |
+
showlegend=False
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
fig.add_annotation(
|
| 161 |
+
x=num1 + num2 + 1.5,
|
| 162 |
+
y=1.5,
|
| 163 |
+
text=str(num1 + num2),
|
| 164 |
+
font=dict(size=30, color='green'),
|
| 165 |
+
showlegend=False
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
fig.update_layout(
|
| 169 |
+
title=f"Addition: {num1} + {num2} = {num1 + num2}",
|
| 170 |
+
showlegend=True,
|
| 171 |
+
xaxis=dict(showgrid=False, showticklabels=False, zeroline=False, range=[-1, num1 + num2 + 3]),
|
| 172 |
+
yaxis=dict(showgrid=False, showticklabels=False, zeroline=False, range=[0, 2.5]),
|
| 173 |
+
height=300,
|
| 174 |
+
template="simple_white"
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
return fig
|
| 178 |
+
except:
|
| 179 |
+
return None
|
| 180 |
+
|
| 181 |
+
def create_fraction_visual(numerator, denominator):
|
| 182 |
+
"""Create visual fraction using pie charts or bars"""
|
| 183 |
+
try:
|
| 184 |
+
if denominator > 12 or numerator > denominator: # Keep it simple
|
| 185 |
+
return None
|
| 186 |
|
| 187 |
+
fig = go.Figure()
|
| 188 |
+
|
| 189 |
+
# Create a circle divided into parts
|
| 190 |
+
angles = np.linspace(0, 2*np.pi, denominator + 1)
|
| 191 |
+
|
| 192 |
+
for i in range(denominator):
|
| 193 |
+
# Create each slice
|
| 194 |
+
theta = np.linspace(angles[i], angles[i+1], 50)
|
| 195 |
+
r = np.ones_like(theta)
|
| 196 |
+
x = r * np.cos(theta)
|
| 197 |
+
y = r * np.sin(theta)
|
| 198 |
|
| 199 |
+
# Add center point
|
| 200 |
+
x = np.concatenate([[0], x, [0]])
|
| 201 |
+
y = np.concatenate([[0], y, [0]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
color = 'lightblue' if i < numerator else 'lightgray'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
+
fig.add_trace(go.Scatter(
|
| 206 |
+
x=x, y=y,
|
| 207 |
+
fill='toself',
|
| 208 |
+
mode='lines',
|
| 209 |
+
line=dict(color='black', width=2),
|
| 210 |
+
fillcolor=color,
|
| 211 |
+
name=f'Slice {i+1}' if i < numerator else '',
|
| 212 |
+
showlegend=False
|
| 213 |
+
))
|
|
|
|
| 214 |
|
| 215 |
+
fig.update_layout(
|
| 216 |
+
title=f"Fraction: {numerator}/{denominator}",
|
| 217 |
+
xaxis=dict(showgrid=False, showticklabels=False, zeroline=False, scaleanchor="y", scaleratio=1),
|
| 218 |
+
yaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
|
| 219 |
+
height=400,
|
| 220 |
+
template="simple_white"
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
return fig
|
| 224 |
+
except:
|
| 225 |
+
return None
|
| 226 |
+
|
| 227 |
+
def create_place_value_visual(number):
|
| 228 |
+
"""Create place value visualization"""
|
| 229 |
+
try:
|
| 230 |
+
if number > 9999: # Keep it reasonable for elementary
|
| 231 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
+
str_num = str(number).zfill(4) # Pad with zeros
|
| 234 |
+
ones = int(str_num[-1])
|
| 235 |
+
tens = int(str_num[-2])
|
| 236 |
+
hundreds = int(str_num[-3])
|
| 237 |
+
thousands = int(str_num[-4])
|
| 238 |
+
|
| 239 |
+
fig = go.Figure()
|
| 240 |
+
|
| 241 |
+
# Create visual blocks for each place value
|
| 242 |
+
positions = [0, 2, 4, 6] # x positions for thousands, hundreds, tens, ones
|
| 243 |
+
values = [thousands, hundreds, tens, ones]
|
| 244 |
+
labels = ['Thousands', 'Hundreds', 'Tens', 'Ones']
|
| 245 |
+
colors = ['red', 'blue', 'green', 'orange']
|
| 246 |
+
|
| 247 |
+
for i, (pos, val, label, color) in enumerate(zip(positions, values, labels, colors)):
|
| 248 |
+
if val > 0:
|
| 249 |
+
# Create blocks to represent the value
|
| 250 |
+
blocks_per_row = min(5, val)
|
| 251 |
+
rows = (val - 1) // blocks_per_row + 1
|
| 252 |
|
| 253 |
+
x_coords = []
|
| 254 |
+
y_coords = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
for block in range(val):
|
| 257 |
+
row = block // blocks_per_row
|
| 258 |
+
col = block % blocks_per_row
|
| 259 |
+
x_coords.append(pos + col * 0.3)
|
| 260 |
+
y_coords.append(row * 0.3)
|
| 261 |
|
| 262 |
+
fig.add_trace(go.Scatter(
|
| 263 |
+
x=x_coords,
|
| 264 |
+
y=y_coords,
|
| 265 |
+
mode='markers',
|
| 266 |
+
marker=dict(
|
| 267 |
+
size=15,
|
| 268 |
+
color=color,
|
| 269 |
+
symbol='square',
|
| 270 |
+
line=dict(width=1, color='black')
|
| 271 |
+
),
|
| 272 |
+
name=f'{label}: {val}',
|
| 273 |
+
showlegend=True
|
| 274 |
+
))
|
| 275 |
|
| 276 |
+
# Add place value label
|
| 277 |
+
fig.add_annotation(
|
| 278 |
+
x=pos + 0.6,
|
| 279 |
+
y=-0.5,
|
| 280 |
+
text=label,
|
| 281 |
+
font=dict(size=12),
|
| 282 |
+
showlegend=False
|
| 283 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
# Add digit
|
| 286 |
+
fig.add_annotation(
|
| 287 |
+
x=pos + 0.6,
|
| 288 |
+
y=-0.8,
|
| 289 |
+
text=str(val),
|
| 290 |
+
font=dict(size=16, color=colors[i]),
|
| 291 |
+
showlegend=False
|
| 292 |
)
|
| 293 |
+
|
| 294 |
+
fig.update_layout(
|
| 295 |
+
title=f"Place Value: {number}",
|
| 296 |
+
showlegend=True,
|
| 297 |
+
xaxis=dict(showgrid=False, showticklabels=False, zeroline=False, range=[-0.5, 7]),
|
| 298 |
+
yaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
|
| 299 |
+
height=400,
|
| 300 |
+
template="simple_white"
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
return fig
|
| 304 |
+
except:
|
| 305 |
+
return None
|
| 306 |
+
|
| 307 |
+
def generate_k12_visual(user_prompt, ai_response):
|
| 308 |
+
"""Generate age-appropriate visualizations for K-12 students"""
|
| 309 |
+
try:
|
| 310 |
+
user_lower = user_prompt.lower()
|
| 311 |
+
|
| 312 |
+
# COUNTING NUMBERS
|
| 313 |
+
count_match = re.search(r'count.*?(\d+)', user_lower)
|
| 314 |
+
if count_match or 'counting' in user_lower:
|
| 315 |
+
number = int(count_match.group(1)) if count_match else 5
|
| 316 |
+
return create_counting_visual(number)
|
| 317 |
+
|
| 318 |
+
# SIMPLE ADDITION
|
| 319 |
+
add_match = re.search(r'(\d+)\s*\+\s*(\d+)', user_prompt)
|
| 320 |
+
if add_match and 'add' in user_lower or '+' in user_prompt:
|
| 321 |
+
num1, num2 = int(add_match.group(1)), int(add_match.group(2))
|
| 322 |
+
if num1 <= 10 and num2 <= 10: # Keep it simple
|
| 323 |
+
return create_addition_visual(num1, num2)
|
| 324 |
+
|
| 325 |
+
# FRACTIONS
|
| 326 |
+
fraction_match = re.search(r'(\d+)/(\d+)', user_prompt)
|
| 327 |
+
if fraction_match or 'fraction' in user_lower:
|
| 328 |
+
if fraction_match:
|
| 329 |
+
num, den = int(fraction_match.group(1)), int(fraction_match.group(2))
|
| 330 |
+
else:
|
| 331 |
+
num, den = 1, 2 # Default to 1/2
|
| 332 |
+
return create_fraction_visual(num, den)
|
| 333 |
+
|
| 334 |
+
# PLACE VALUE
|
| 335 |
+
if 'place value' in user_lower or 'place' in user_lower:
|
| 336 |
+
place_match = re.search(r'\b(\d{1,4})\b', user_prompt)
|
| 337 |
+
if place_match:
|
| 338 |
+
number = int(place_match.group(1))
|
| 339 |
+
return create_place_value_visual(number)
|
| 340 |
+
|
| 341 |
+
# NUMBERS (general counting)
|
| 342 |
+
number_match = re.search(r'\b(\d+)\b', user_prompt)
|
| 343 |
+
if number_match and any(word in user_lower for word in ['show', 'count', 'number']):
|
| 344 |
+
number = int(number_match.group(1))
|
| 345 |
+
if 1 <= number <= 20:
|
| 346 |
+
return create_counting_visual(number)
|
| 347 |
|
| 348 |
return None
|
| 349 |
|
| 350 |
except Exception as e:
|
| 351 |
+
st.error(f"Could not generate K-12 visual: {e}")
|
| 352 |
return None
|
| 353 |
|
| 354 |
# --- API KEY & MODEL CONFIGURATION ---
|
|
|
|
| 367 |
model = genai.GenerativeModel(
|
| 368 |
model_name="gemini-2.5-flash-lite",
|
| 369 |
system_instruction="""
|
| 370 |
+
You are "Math Jegna", an AI math tutor specializing in K-12 mathematics using the Professor B methodology.
|
| 371 |
+
|
| 372 |
+
FOCUS ON ELEMENTARY CONCEPTS:
|
| 373 |
+
- Basic counting (1-100)
|
| 374 |
+
- Simple addition and subtraction (single digits to start)
|
| 375 |
+
- Beginning multiplication (times tables)
|
| 376 |
+
- Basic fractions (halves, thirds, quarters)
|
| 377 |
+
- Place value (ones, tens, hundreds)
|
| 378 |
+
- Shape recognition
|
| 379 |
+
- Simple word problems
|
| 380 |
+
- Money and time concepts
|
| 381 |
+
|
| 382 |
+
PROFESSOR B METHODOLOGY - ESSENTIAL PRINCIPLES:
|
| 383 |
+
1. Present math as a STORY that connects ideas
|
| 384 |
+
2. Use MENTAL GYMNASTICS - fun games and finger counting
|
| 385 |
+
3. Build from concrete to abstract naturally
|
| 386 |
+
4. NO ROTE MEMORIZATION - focus on understanding patterns and connections
|
| 387 |
+
5. Eliminate math anxiety through simple, truthful explanations
|
| 388 |
+
6. Use manipulatives and visual aids
|
| 389 |
+
7. Allow accelerated learning when student shows mastery
|
| 390 |
+
|
| 391 |
+
TEACHING STYLE:
|
| 392 |
+
- Start with what the child already knows
|
| 393 |
+
- Build new concepts as natural extensions of previous learning
|
| 394 |
+
- Use simple, clear language appropriate for the age
|
| 395 |
+
- Make math enjoyable and reduce tension
|
| 396 |
+
- Connect everything to real-world experiences
|
| 397 |
+
- Celebrate understanding, not just correct answers
|
| 398 |
+
|
| 399 |
+
VISUAL AIDS: Mention when visual aids will help, using phrases like:
|
| 400 |
+
- "Let me show you this with counting dots..."
|
| 401 |
+
- "I'll create a picture to help you see this..."
|
| 402 |
+
- "A visual will make this clearer..."
|
| 403 |
+
|
| 404 |
+
Remember: You're helping young minds discover the beauty and logic of mathematics through stories and connections, not through drilling and memorization.
|
| 405 |
+
|
| 406 |
+
You are strictly forbidden from answering non-mathematical questions. If asked non-math questions, respond only with: "I can only answer mathematical questions. Please ask me a question about counting, adding, shapes, or another math topic."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
"""
|
| 408 |
)
|
| 409 |
else:
|
|
|
|
| 430 |
else:
|
| 431 |
st.session_state.chats = {
|
| 432 |
"New Chat": [
|
| 433 |
+
{"role": "assistant", "content": "Hello! I'm Math Jegna, your friendly math helper! π§ β¨\n\nI love helping kids learn math through fun stories and pictures. Try asking me about:\n- Counting numbers\n- Adding or subtracting\n- Fractions like 1/2\n- Shapes and patterns\n- Or any math question!\n\nWhat would you like to learn about today?"}
|
| 434 |
]
|
| 435 |
}
|
| 436 |
st.session_state.active_chat_key = "New Chat"
|
|
|
|
| 461 |
st.rerun()
|
| 462 |
|
| 463 |
# --- SIDEBAR CHAT MANAGEMENT ---
|
| 464 |
+
st.sidebar.title("π My Math Chats")
|
| 465 |
st.sidebar.divider()
|
| 466 |
|
| 467 |
if st.sidebar.button("β New Chat", use_container_width=True):
|
|
|
|
| 470 |
i += 1
|
| 471 |
new_chat_key = f"New Chat {i}"
|
| 472 |
st.session_state.chats[new_chat_key] = [
|
| 473 |
+
{"role": "assistant", "content": "Hi there! Ready for some fun math learning? Ask me about counting, adding, shapes, or anything else! ππ’"}
|
| 474 |
]
|
| 475 |
st.session_state.active_chat_key = new_chat_key
|
| 476 |
st.rerun()
|
|
|
|
| 506 |
# --- MAIN CHAT INTERFACE ---
|
| 507 |
active_chat = st.session_state.chats[st.session_state.active_chat_key]
|
| 508 |
|
| 509 |
+
st.title(f"Math Helper: {st.session_state.active_chat_key} π§ ")
|
| 510 |
+
st.write("π― Perfect for young learners! Ask about counting, adding, shapes, fractions, and more!")
|
| 511 |
+
|
| 512 |
+
# Add some example prompts for young learners
|
| 513 |
+
with st.expander("π‘ Try asking me about..."):
|
| 514 |
+
st.write("""
|
| 515 |
+
- **Counting**: "Show me how to count to 10"
|
| 516 |
+
- **Addition**: "What is 3 + 4?"
|
| 517 |
+
- **Fractions**: "What is 1/2?"
|
| 518 |
+
- **Place Value**: "What is the place value of 325?"
|
| 519 |
+
- **Shapes**: "Tell me about triangles"
|
| 520 |
+
- **Time**: "How do I read a clock?"
|
| 521 |
+
""")
|
| 522 |
|
| 523 |
for message in active_chat:
|
| 524 |
with st.chat_message(name=message["role"], avatar="π§βπ»" if message["role"] == "user" else "π§ "):
|
| 525 |
st.markdown(message["content"])
|
| 526 |
|
| 527 |
+
if user_prompt := st.chat_input("Ask me a math question!"):
|
| 528 |
active_chat.append({"role": "user", "content": user_prompt})
|
| 529 |
with st.chat_message("user", avatar="π§βπ»"):
|
| 530 |
st.markdown(user_prompt)
|
| 531 |
|
| 532 |
with st.chat_message("assistant", avatar="π§ "):
|
| 533 |
+
with st.spinner("Math Jegna is thinking... π€"):
|
| 534 |
try:
|
| 535 |
# Generate text response first
|
| 536 |
chat_session = model.start_chat(history=[
|
|
|
|
| 546 |
|
| 547 |
# Check if we should generate a visual aid
|
| 548 |
if should_generate_visual(user_prompt, ai_response_text):
|
| 549 |
+
with st.spinner("Creating a helpful picture... π¨"):
|
| 550 |
+
k12_fig = generate_k12_visual(user_prompt, ai_response_text)
|
| 551 |
+
if k12_fig:
|
| 552 |
+
st.plotly_chart(k12_fig, use_container_width=True)
|
| 553 |
+
st.success("β¨ Here's a picture to help you understand!")
|
| 554 |
|
| 555 |
except Exception as e:
|
| 556 |
+
error_message = f"Oops! Something went wrong. Let me try again! π€\n\n**Error:** {e}"
|
| 557 |
st.error(error_message)
|
| 558 |
active_chat.append({"role": "assistant", "content": error_message})
|
| 559 |
|