Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +169 -385
src/streamlit_app.py
CHANGED
|
@@ -1,116 +1,92 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import zipfile
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import time
|
| 7 |
import tempfile
|
| 8 |
-
import random
|
| 9 |
|
| 10 |
-
# Page config
|
| 11 |
-
st.set_page_config(
|
| 12 |
-
page_title="Math Adventure Land",
|
| 13 |
-
layout="wide",
|
| 14 |
-
initial_sidebar_state="expanded"
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
st.markdown("""
|
| 19 |
<style>
|
| 20 |
-
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');
|
| 21 |
-
|
| 22 |
-
.main-container {
|
| 23 |
-
background: linear-gradient(45deg, #FF6B6B, #4ECDC4, #45B7D1, #96CEB4, #FFEAA7);
|
| 24 |
-
padding: 20px;
|
| 25 |
-
border-radius: 25px;
|
| 26 |
-
border: 8px dashed #FFD93D;
|
| 27 |
-
margin-bottom: 20px;
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
.fun-title {
|
| 31 |
-
font-size:
|
| 32 |
color: #FF6B6B;
|
| 33 |
text-align: center;
|
| 34 |
-
font-family: 'Comic
|
| 35 |
-
|
| 36 |
}
|
| 37 |
-
|
| 38 |
.question-box {
|
| 39 |
-
border:
|
| 40 |
-
padding:
|
| 41 |
-
border-radius:
|
| 42 |
-
background
|
| 43 |
-
font-size:
|
| 44 |
-
|
| 45 |
-
margin: 20px 0;
|
| 46 |
}
|
| 47 |
-
|
| 48 |
.stButton > button {
|
| 49 |
-
font-size:
|
| 50 |
-
background: #FF6B6B;
|
| 51 |
color: white;
|
| 52 |
-
border-radius:
|
| 53 |
-
padding:
|
| 54 |
border: none;
|
| 55 |
-
font-family: 'Comic Neue', cursive;
|
| 56 |
font-weight: bold;
|
| 57 |
}
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
.stTextInput > div > input {
|
| 60 |
-
font-size:
|
| 61 |
-
border
|
| 62 |
-
border:
|
| 63 |
-
padding:
|
| 64 |
-
font-family: 'Comic Neue', cursive;
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
height: 25px;
|
| 71 |
-
margin: 20px 0;
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
height: 100%;
|
| 78 |
}
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
}
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
border:
|
| 92 |
-
margin-bottom: 20px;
|
| 93 |
-
font-family: 'Comic Neue', cursive;
|
| 94 |
}
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
background: #FF6B6B;
|
| 100 |
-
border-radius: 25px;
|
| 101 |
color: white;
|
| 102 |
-
|
| 103 |
}
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
margin: 10px 0;
|
| 111 |
}
|
| 112 |
|
| 113 |
-
/* DISABLE ALL ANIMATIONS
|
| 114 |
* {
|
| 115 |
animation: none !important;
|
| 116 |
transition: none !important;
|
|
@@ -119,323 +95,131 @@ st.markdown("""
|
|
| 119 |
</style>
|
| 120 |
""", unsafe_allow_html=True)
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
if 'app_initialized' not in st.session_state:
|
| 125 |
-
st.session_state.app_initialized = True
|
| 126 |
-
st.session_state.temp_dir = tempfile.mkdtemp()
|
| 127 |
-
st.session_state.last_activity = time.time()
|
| 128 |
-
st.session_state.auto_progress = True
|
| 129 |
-
|
| 130 |
-
# Initialize age group specific states
|
| 131 |
-
st.session_state.current_age_group = None
|
| 132 |
-
st.session_state.age_group_states = {}
|
| 133 |
|
| 134 |
-
# Initialize
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
st.markdown("""
|
| 139 |
-
<div class="main-container">
|
| 140 |
-
<div class="animal-characters">
|
| 141 |
-
<span>🐱</span>
|
| 142 |
-
<span>🐶</span>
|
| 143 |
-
<span>🦄</span>
|
| 144 |
-
<span>🐼</span>
|
| 145 |
-
<span>🐯</span>
|
| 146 |
-
</div>
|
| 147 |
-
<div class="fun-title">
|
| 148 |
-
Welcome to Math Adventure Land!
|
| 149 |
-
</div>
|
| 150 |
-
<div style="text-align: center; font-size: 24px; color: #FF6B6B; font-family: 'Comic Neue', cursive;">
|
| 151 |
-
Where Numbers Become Friends!
|
| 152 |
-
</div>
|
| 153 |
-
</div>
|
| 154 |
-
""", unsafe_allow_html=True)
|
| 155 |
-
|
| 156 |
-
# Age group setup
|
| 157 |
age_groups = {
|
| 158 |
-
"
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
}
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
st.
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
st.
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
age_state['df_loaded'] = True
|
| 252 |
-
except Exception as e:
|
| 253 |
-
st.error(f"Error loading dataset: {e}")
|
| 254 |
-
st.stop()
|
| 255 |
-
|
| 256 |
-
# Category selection
|
| 257 |
-
category_emojis = {"Addition": "+", "Subtraction": "-", "Multiplication": "×", "Division": "÷", "Geometry": "📐", "Algebra": "📊"}
|
| 258 |
-
selected_category = st.selectbox("Choose Your Math Quest:", options=age_state['categories'])
|
| 259 |
-
|
| 260 |
-
# Update session if category changes
|
| 261 |
-
if age_state['category'] != selected_category:
|
| 262 |
-
age_state['category'] = selected_category
|
| 263 |
-
age_state['question_index'] = 0
|
| 264 |
-
age_state['show_answer'] = False
|
| 265 |
-
age_state['show_steps'] = False
|
| 266 |
-
st.rerun()
|
| 267 |
-
|
| 268 |
-
# Filter questions by selected category only
|
| 269 |
-
subset_df = age_state['df'][age_state['df']['category'] == selected_category].reset_index(drop=True)
|
| 270 |
-
|
| 271 |
-
# MATH QUESTIONS SECTION
|
| 272 |
-
if not subset_df.empty and age_state['question_index'] < len(subset_df):
|
| 273 |
-
question = subset_df.iloc[age_state['question_index']]
|
| 274 |
-
progress = int((age_state['question_index'] / len(subset_df)) * 100)
|
| 275 |
-
|
| 276 |
-
# Progress bar - NO ANIMATIONS
|
| 277 |
-
st.markdown(f"""
|
| 278 |
-
<div style="text-align: center; margin: 20px 0;">
|
| 279 |
-
<div style="font-family: 'Comic Neue', cursive; color: #FF6B6B; font-size: 18px;">
|
| 280 |
-
Progress: {age_state['question_index'] + 1} of {len(subset_df)}
|
| 281 |
-
</div>
|
| 282 |
-
<div class="progress-bar">
|
| 283 |
-
<div class="progress-fill" style="width: {progress}%;"></div>
|
| 284 |
-
</div>
|
| 285 |
-
</div>
|
| 286 |
-
""", unsafe_allow_html=True)
|
| 287 |
-
|
| 288 |
-
# Display question - NO ANIMATIONS
|
| 289 |
-
category_emoji = category_emojis.get(selected_category, "?")
|
| 290 |
-
st.markdown(f"""
|
| 291 |
-
<div class="question-box">
|
| 292 |
-
<div style="text-align: center; font-size: 30px; margin-bottom: 15px;">
|
| 293 |
-
Challenge {age_state['question_index'] + 1}
|
| 294 |
-
</div>
|
| 295 |
-
<b>Math Quest:</b><br><br>
|
| 296 |
-
{question['problem']}
|
| 297 |
-
</div>
|
| 298 |
-
""", unsafe_allow_html=True)
|
| 299 |
-
|
| 300 |
-
# Display image with error handling
|
| 301 |
-
if pd.notna(question.get('image')):
|
| 302 |
-
image_name = str(question['image']).strip()
|
| 303 |
-
image_found = False
|
| 304 |
-
temp_image_folder = os.path.join(st.session_state.temp_dir, age_groups[selected_age_group]["image_folder"])
|
| 305 |
-
for root, _, files in os.walk(temp_image_folder):
|
| 306 |
-
for file in files:
|
| 307 |
-
if file.lower().startswith(image_name.lower()) or os.path.splitext(file)[0].lower() == image_name.lower():
|
| 308 |
-
try:
|
| 309 |
-
st.image(Image.open(os.path.join(root, file)), use_column_width=True)
|
| 310 |
-
image_found = True
|
| 311 |
-
break
|
| 312 |
-
except:
|
| 313 |
-
pass
|
| 314 |
-
if not image_found:
|
| 315 |
-
st.warning("Image not available")
|
| 316 |
-
|
| 317 |
-
# Answer input
|
| 318 |
-
user_ans = st.text_input("Your Answer:", key=f"ans_{selected_age_group}_{selected_category}_{age_state['question_index']}")
|
| 319 |
-
|
| 320 |
-
# Action buttons
|
| 321 |
-
col_btn1, col_btn2, col_btn3 = st.columns([1, 1, 1])
|
| 322 |
-
|
| 323 |
-
with col_btn1:
|
| 324 |
-
if st.button("Submit Answer", key=f"submit_{selected_age_group}_{selected_category}_{age_state['question_index']}", use_container_width=True):
|
| 325 |
-
st.session_state.last_activity = time.time()
|
| 326 |
-
if str(user_ans).strip().lower() == str(question['answer']).strip().lower():
|
| 327 |
-
# SIMPLE SUCCESS MESSAGE - NO ANIMATIONS
|
| 328 |
-
st.success("CORRECT! Great job!")
|
| 329 |
-
|
| 330 |
-
success_messages = [
|
| 331 |
-
"You're a math superstar!",
|
| 332 |
-
"Excellent work!",
|
| 333 |
-
"Brilliant answer!",
|
| 334 |
-
"Perfect!",
|
| 335 |
-
"Outstanding!"
|
| 336 |
-
]
|
| 337 |
-
|
| 338 |
-
st.markdown(f"""
|
| 339 |
-
<div style="text-align: center; font-size: 24px; color: #FF6B6B; font-family: 'Comic Neue', cursive; padding: 20px;">
|
| 340 |
-
{random.choice(success_messages)}
|
| 341 |
-
</div>
|
| 342 |
-
""", unsafe_allow_html=True)
|
| 343 |
-
|
| 344 |
-
# Wait 2 seconds then continue
|
| 345 |
-
time.sleep(2)
|
| 346 |
-
age_state['question_index'] += 1
|
| 347 |
-
age_state['show_answer'] = False
|
| 348 |
-
age_state['show_steps'] = False
|
| 349 |
-
st.session_state.last_activity = time.time()
|
| 350 |
-
st.rerun()
|
| 351 |
-
else:
|
| 352 |
-
st.error("Not quite right. Try again!")
|
| 353 |
-
age_state['show_answer'] = True
|
| 354 |
-
age_state['show_steps'] = False
|
| 355 |
-
|
| 356 |
-
with col_btn2:
|
| 357 |
-
if st.button("Show Hint", key=f"hint_{selected_age_group}_{selected_category}_{age_state['question_index']}", use_container_width=True):
|
| 358 |
-
st.session_state.last_activity = time.time()
|
| 359 |
-
age_state['show_answer'] = True
|
| 360 |
-
st.info("Need some help? Here's the answer below!")
|
| 361 |
-
|
| 362 |
-
with col_btn3:
|
| 363 |
-
if st.button("Skip", key=f"skip_{selected_age_group}_{selected_category}_{age_state['question_index']}", use_container_width=True):
|
| 364 |
-
st.session_state.last_activity = time.time()
|
| 365 |
-
age_state['question_index'] += 1
|
| 366 |
-
age_state['show_answer'] = False
|
| 367 |
-
age_state['show_steps'] = False
|
| 368 |
-
st.info("Moving to next challenge...")
|
| 369 |
-
time.sleep(1)
|
| 370 |
-
st.rerun()
|
| 371 |
-
|
| 372 |
-
# Show answer
|
| 373 |
-
if age_state['show_answer']:
|
| 374 |
-
st.info(f"Correct Answer: {question['answer']}")
|
| 375 |
-
|
| 376 |
-
if selected_age_group in ["🚀 Math Adventurers (7-9)", "🌟 Math Wizards (13-15)"]:
|
| 377 |
-
if st.button("Show Steps", key=f"steps_{selected_age_group}_{selected_category}_{age_state['question_index']}"):
|
| 378 |
-
st.session_state.last_activity = time.time()
|
| 379 |
-
age_state['show_steps'] = True
|
| 380 |
-
if age_state['show_steps'] and pd.notna(question.get("steps", None)):
|
| 381 |
-
st.success(f"Step-by-Step Solution:\n{question['steps']}")
|
| 382 |
-
|
| 383 |
-
elif subset_df.empty:
|
| 384 |
-
st.warning("No questions available in this category. Try another one!")
|
| 385 |
-
else:
|
| 386 |
-
# COMPLETION MESSAGE - NO ANIMATIONS
|
| 387 |
-
st.markdown("""
|
| 388 |
-
<div class="completion-message">
|
| 389 |
-
<h1 style="font-family: 'Comic Neue', cursive;">
|
| 390 |
-
CONGRATULATIONS!
|
| 391 |
-
</h1>
|
| 392 |
-
<p style="font-size: 24px;">
|
| 393 |
-
You've completed all challenges in this category!
|
| 394 |
-
</p>
|
| 395 |
-
</div>
|
| 396 |
-
""", unsafe_allow_html=True)
|
| 397 |
-
|
| 398 |
-
# Auto-progress logic
|
| 399 |
-
if st.session_state.auto_progress:
|
| 400 |
-
st.markdown("""
|
| 401 |
-
<div class="countdown">
|
| 402 |
-
Next adventure starting in: 5 seconds
|
| 403 |
-
</div>
|
| 404 |
-
""", unsafe_allow_html=True)
|
| 405 |
-
|
| 406 |
-
time.sleep(5)
|
| 407 |
-
age_state['question_index'] = 0
|
| 408 |
-
age_state['show_answer'] = False
|
| 409 |
-
age_state['show_steps'] = False
|
| 410 |
-
|
| 411 |
-
# Move to next category
|
| 412 |
-
current_idx = age_state['categories'].index(selected_category)
|
| 413 |
-
if current_idx < len(age_state['categories']) - 1:
|
| 414 |
-
age_state['category'] = age_state['categories'][current_idx + 1]
|
| 415 |
-
else:
|
| 416 |
-
age_state['category'] = age_state['categories'][0]
|
| 417 |
-
|
| 418 |
st.rerun()
|
| 419 |
else:
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
| 439 |
|
| 440 |
# Cleanup function
|
| 441 |
def cleanup():
|
|
|
|
| 1 |
+
import altair as alt
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import zipfile
|
| 6 |
from PIL import Image
|
| 7 |
+
import sympy as sp
|
| 8 |
import time
|
| 9 |
import tempfile
|
|
|
|
| 10 |
|
| 11 |
+
# Page config
|
| 12 |
+
st.set_page_config(page_title="🎓 Smart Math Teacher", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Custom CSS with beautiful colors
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
.fun-title {
|
| 18 |
+
font-size: 40px;
|
| 19 |
color: #FF6B6B;
|
| 20 |
text-align: center;
|
| 21 |
+
font-family: 'Comic Sans MS', cursive;
|
| 22 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
|
| 23 |
}
|
|
|
|
| 24 |
.question-box {
|
| 25 |
+
border: 4px solid #4ECDC4;
|
| 26 |
+
padding: 20px;
|
| 27 |
+
border-radius: 15px;
|
| 28 |
+
background: linear-gradient(135deg, #FFF9C4, #E3F2FD);
|
| 29 |
+
font-size: 20px;
|
| 30 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
|
|
|
| 31 |
}
|
|
|
|
| 32 |
.stButton > button {
|
| 33 |
+
font-size: 18px;
|
| 34 |
+
background: linear-gradient(45deg, #FF6B6B, #FF8E53);
|
| 35 |
color: white;
|
| 36 |
+
border-radius: 10px;
|
| 37 |
+
padding: 10px;
|
| 38 |
border: none;
|
|
|
|
| 39 |
font-weight: bold;
|
| 40 |
}
|
| 41 |
+
.stButton > button:hover {
|
| 42 |
+
background: linear-gradient(45deg, #FF8E53, #FF6B6B);
|
| 43 |
+
}
|
| 44 |
.stTextInput > div > input {
|
| 45 |
+
font-size: 18px;
|
| 46 |
+
border: 2px solid #4ECDC4;
|
| 47 |
+
border-radius: 10px;
|
| 48 |
+
padding: 10px;
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
+
/* Main container background */
|
| 52 |
+
.stApp {
|
| 53 |
+
background: linear-gradient(135deg, #E3F2FD, #F3E5F5);
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
+
/* Progress bar styling */
|
| 57 |
+
.stProgress > div > div > div {
|
| 58 |
+
background: linear-gradient(45deg, #4ECDC4, #00C9FF);
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
+
/* Success message */
|
| 62 |
+
.stSuccess {
|
| 63 |
+
background: linear-gradient(135deg, #4ECDC4, #00C9FF);
|
| 64 |
+
color: white;
|
| 65 |
+
border-radius: 10px;
|
| 66 |
}
|
| 67 |
|
| 68 |
+
/* Info message */
|
| 69 |
+
.stInfo {
|
| 70 |
+
background: linear-gradient(135deg, #FFD93D, #FF9A3D);
|
| 71 |
+
color: white;
|
| 72 |
+
border-radius: 10px;
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
+
/* Error message */
|
| 76 |
+
.stError {
|
| 77 |
+
background: linear-gradient(135deg, #FF6B6B, #FF8E8E);
|
|
|
|
|
|
|
| 78 |
color: white;
|
| 79 |
+
border-radius: 10px;
|
| 80 |
}
|
| 81 |
|
| 82 |
+
/* Warning message */
|
| 83 |
+
.stWarning {
|
| 84 |
+
background: linear-gradient(135deg, #FFD93D, #FF9A3D);
|
| 85 |
+
color: white;
|
| 86 |
+
border-radius: 10px;
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
+
/* DISABLE ALL ANIMATIONS */
|
| 90 |
* {
|
| 91 |
animation: none !important;
|
| 92 |
transition: none !important;
|
|
|
|
| 95 |
</style>
|
| 96 |
""", unsafe_allow_html=True)
|
| 97 |
|
| 98 |
+
# Welcome title
|
| 99 |
+
st.markdown("<div class='fun-title'>🧠✨ Welcome to the Smart Math Teacher! ✨🧠</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# Initialize session state for temp directory
|
| 102 |
+
if 'temp_dir' not in st.session_state:
|
| 103 |
+
st.session_state.temp_dir = tempfile.mkdtemp()
|
| 104 |
|
| 105 |
+
# Age group setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
age_groups = {
|
| 107 |
+
"4-6 Age Group": {"dataset": "src/Datase_of_4-6_Age_Group.xlsx", "zip_file": "src/Image_for_group_4-6.zip", "image_folder": "Image_for_group_4-6"},
|
| 108 |
+
"7-9 Age Group": {"dataset": "src/Datase_of_7-9_Age_Group.xlsx", "zip_file": "src/Image_for_group_7-9.zip", "image_folder": "Image_for_group_7-9"},
|
| 109 |
+
"13-15 Age Group": {"dataset": "src/Datase_of_13-15_Age_Group.xlsx", "zip_file": "src/Image_for_group_13-15.zip", "image_folder": "Image_for_group_13-15"},
|
| 110 |
}
|
| 111 |
|
| 112 |
+
selected_age_group = st.selectbox("🧒 Select your Age Group:", list(age_groups.keys()))
|
| 113 |
+
|
| 114 |
+
# Initialize session
|
| 115 |
+
if "session_initialized" not in st.session_state or st.session_state.age_group != selected_age_group:
|
| 116 |
+
st.session_state.age_group = selected_age_group
|
| 117 |
+
st.session_state.category = None
|
| 118 |
+
st.session_state.question_index = 0
|
| 119 |
+
st.session_state.show_answer = False
|
| 120 |
+
st.session_state.show_steps = False
|
| 121 |
+
st.session_state.session_initialized = True
|
| 122 |
+
|
| 123 |
+
# Load dataset
|
| 124 |
+
group_info = age_groups[selected_age_group]
|
| 125 |
+
dataset_path = group_info["dataset"]
|
| 126 |
+
zip_path = group_info["zip_file"]
|
| 127 |
+
image_folder = group_info["image_folder"]
|
| 128 |
+
|
| 129 |
+
# Create image folder in temp directory
|
| 130 |
+
temp_image_folder = os.path.join(st.session_state.temp_dir, image_folder)
|
| 131 |
+
os.makedirs(temp_image_folder, exist_ok=True)
|
| 132 |
+
|
| 133 |
+
# Extract images if zip exists
|
| 134 |
+
try:
|
| 135 |
+
if os.path.exists(zip_path):
|
| 136 |
+
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 137 |
+
zip_ref.extractall(temp_image_folder)
|
| 138 |
+
except Exception as e:
|
| 139 |
+
st.warning(f"Error extracting images: {e}")
|
| 140 |
+
|
| 141 |
+
if not os.path.exists(dataset_path):
|
| 142 |
+
st.error(f"Dataset not found: {dataset_path}")
|
| 143 |
+
st.stop()
|
| 144 |
+
|
| 145 |
+
try:
|
| 146 |
+
df = pd.read_excel(dataset_path)
|
| 147 |
+
df['category'] = df['category'].astype(str).str.strip()
|
| 148 |
+
except Exception as e:
|
| 149 |
+
st.error(f"Error loading dataset: {e}")
|
| 150 |
+
st.stop()
|
| 151 |
+
|
| 152 |
+
# Category selection
|
| 153 |
+
categories = sorted(df['category'].dropna().unique())
|
| 154 |
+
selected_category = st.selectbox("📚 Choose a Math Category:", options=categories)
|
| 155 |
+
|
| 156 |
+
# Update session if category changes
|
| 157 |
+
if st.session_state.category != selected_category:
|
| 158 |
+
st.session_state.category = selected_category
|
| 159 |
+
st.session_state.question_index = 0
|
| 160 |
+
st.session_state.show_answer = False
|
| 161 |
+
st.session_state.show_steps = False
|
| 162 |
+
st.rerun()
|
| 163 |
+
|
| 164 |
+
# Filter questions by selected category only
|
| 165 |
+
subset_df = df[df['category'] == selected_category].reset_index(drop=True)
|
| 166 |
+
|
| 167 |
+
if not subset_df.empty and st.session_state.question_index < len(subset_df):
|
| 168 |
+
question = subset_df.iloc[st.session_state.question_index]
|
| 169 |
+
progress = int((st.session_state.question_index / len(subset_df)) * 100)
|
| 170 |
+
st.progress(progress)
|
| 171 |
+
|
| 172 |
+
st.markdown(f"<div class='question-box'>📘 <b>Question {st.session_state.question_index + 1}:</b><br><br>{question['problem']}</div>", unsafe_allow_html=True)
|
| 173 |
+
|
| 174 |
+
# Display image with error handling
|
| 175 |
+
if pd.notna(question.get('image')):
|
| 176 |
+
image_name = str(question['image']).strip()
|
| 177 |
+
image_found = False
|
| 178 |
+
for root, _, files in os.walk(temp_image_folder):
|
| 179 |
+
for file in files:
|
| 180 |
+
if file.lower().startswith(image_name.lower()) or os.path.splitext(file)[0].lower() == image_name.lower():
|
| 181 |
+
try:
|
| 182 |
+
st.image(Image.open(os.path.join(root, file)), use_column_width=True)
|
| 183 |
+
image_found = True
|
| 184 |
+
break
|
| 185 |
+
except:
|
| 186 |
+
pass
|
| 187 |
+
if not image_found:
|
| 188 |
+
st.warning("❌ Image not found.")
|
| 189 |
+
|
| 190 |
+
user_ans = st.text_input("📝 Your Answer:", key=f"ans_{st.session_state.question_index}")
|
| 191 |
+
|
| 192 |
+
if st.button("✅ Submit Answer"):
|
| 193 |
+
if str(user_ans).strip().lower() == str(question['answer']).strip().lower():
|
| 194 |
+
st.success("🎉 Correct! Well done!")
|
| 195 |
+
st.balloons()
|
| 196 |
+
time.sleep(2)
|
| 197 |
+
st.session_state.question_index += 1
|
| 198 |
+
st.session_state.show_answer = False
|
| 199 |
+
st.session_state.show_steps = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
st.rerun()
|
| 201 |
else:
|
| 202 |
+
st.error("❌ Try again or view the correct answer below.")
|
| 203 |
+
st.session_state.show_answer = True
|
| 204 |
+
st.session_state.show_steps = False
|
| 205 |
+
|
| 206 |
+
if st.session_state.show_answer:
|
| 207 |
+
st.info(f"✅ Correct Answer: **{question['answer']}**")
|
| 208 |
+
if selected_age_group in ["7-9 Age Group", "13-15 Age Group"]:
|
| 209 |
+
if st.button("🔍 Show Steps"):
|
| 210 |
+
st.session_state.show_steps = True
|
| 211 |
+
if st.session_state.show_steps and pd.notna(question.get("steps", None)):
|
| 212 |
+
st.success(f"### 🪄 Steps:\n{question['steps']}")
|
| 213 |
+
|
| 214 |
+
if st.button("⏭️ Skip"):
|
| 215 |
+
st.session_state.question_index += 1
|
| 216 |
+
st.session_state.show_answer = False
|
| 217 |
+
st.session_state.show_steps = False
|
| 218 |
+
st.rerun()
|
| 219 |
+
elif subset_df.empty:
|
| 220 |
+
st.warning("⚠️ No questions available in this category. Try another one.")
|
| 221 |
+
else:
|
| 222 |
+
st.success("🏁 You've completed all questions in this category!")
|
| 223 |
|
| 224 |
# Cleanup function
|
| 225 |
def cleanup():
|