import streamlit as st import sys, os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..')) from streamlit_app.utils.auth import require_login, get_token from streamlit_app.utils.state import init_tutor_state, reset_tutor, set_stage, get_stage from streamlit_app.utils import api_client from streamlit_app.utils.ui import apply_theme st.set_page_config(page_title="MathMind AI โ€” Tutor", page_icon="๐Ÿงฎ", layout="centered") apply_theme() require_login() init_tutor_state() grade = st.session_state.get("grade", 8) email = st.session_state.get("user_email", "") # โ”€โ”€ Sidebar โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ with st.sidebar: st.markdown("### ๐Ÿงฎ MathMind AI") st.caption(f"Logged in as {email}") st.divider() grade = st.selectbox( "Your Grade", options=list(range(6, 13)), index=list(range(6, 13)).index(grade), key="grade_select", ) st.session_state["grade"] = grade st.divider() if st.button("๐Ÿ”„ New Question", use_container_width=True): reset_tutor() st.rerun() st.page_link("pages/3_Dashboard.py", label="๐Ÿ“Š My Dashboard", use_container_width=True) # โ”€โ”€ Header โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ st.markdown( f"""

๐Ÿงฎ Math Tutor

Grade {grade} ยท Ask me any math question

""", unsafe_allow_html=True, ) stage = get_stage() # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• # STAGE 1: ASK # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• if stage == "ask": st.markdown('
๐Ÿ’ฌ What would you like to learn?
', unsafe_allow_html=True) examples = [ "How do I solve linear equations?", "Explain the Pythagorean theorem", "What are fractions and how do I add them?", "How does factoring quadratics work?", ] example_cols = st.columns(2) for i, ex in enumerate(examples): with example_cols[i % 2]: if st.button(ex, use_container_width=True, key=f"ex_{i}"): st.session_state["current_query"] = ex st.session_state["current_topic"] = ex[:80] set_stage("explain") st.rerun() query = st.text_area( "Or type your own question:", value=st.session_state.get("current_query", ""), placeholder="e.g. How do I find the area of a circle?", height=100, key="query_input", ) if st.button("๐Ÿš€ Get Explanation", type="primary", use_container_width=True): if not query.strip(): st.error("Please enter a math question first.") else: st.session_state["current_query"] = query.strip() st.session_state["current_topic"] = query.strip()[:80] set_stage("explain") st.rerun() # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• # STAGE 2: EXPLAIN (streaming) # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• elif stage == "explain": query = st.session_state["current_query"] st.markdown(f'
๐Ÿ“– Explanation for Grade {grade}
', unsafe_allow_html=True) st.markdown( f"""
Your question: {query}
""", unsafe_allow_html=True, ) if not st.session_state.get("current_explanation"): with st.spinner("MathMind AI is thinkingโ€ฆ"): try: explanation_chunks = [] placeholder = st.empty() full_text = "" for chunk in api_client.stream_explanation(query, grade): full_text += chunk placeholder.markdown(full_text + "โ–Œ") placeholder.markdown(full_text) st.session_state["current_explanation"] = full_text # Session ID comes back via Supabase โ€” we'll store in next call st.session_state["current_session_id"] = "pending" except Exception as e: st.error(f"Could not get explanation: {e}") if st.button("Try Again"): reset_tutor() st.rerun() st.stop() else: st.markdown(st.session_state["current_explanation"]) st.divider() col1, col2 = st.columns(2) with col1: if st.button("๐Ÿ“ Practice Problems", type="primary", use_container_width=True): set_stage("practice") st.rerun() with col2: if st.button("โญ๏ธ Skip to Test", use_container_width=True): set_stage("test") st.rerun() # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• # STAGE 3: PRACTICE PROBLEMS # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• elif stage == "practice": st.markdown('
โœ๏ธ Practice Problems
', unsafe_allow_html=True) st.caption("Try these problems to reinforce what you just learned.") if not st.session_state.get("practice_problems"): with st.spinner("Generating practice problemsโ€ฆ"): try: session_id = st.session_state.get("current_session_id", "temp") result = api_client.get_practice( topic=st.session_state["current_topic"], grade=grade, session_id=session_id, ) st.session_state["practice_problems"] = result.get("problems", []) except Exception as e: st.error(f"Could not generate problems: {e}") st.stop() problems = st.session_state["practice_problems"] all_answered = True for i, prob in enumerate(problems): with st.expander(f"Problem {i+1} โ€” {prob.get('difficulty','').capitalize()}", expanded=True): st.markdown(f"**{prob['question']}**") answer_key = f"practice_ans_{i}" user_ans = st.text_input("Your answer:", key=answer_key) if st.button("Check Answer ๐Ÿ’ก", key=f"check_{i}"): if not user_ans.strip(): st.warning("Please enter an answer first.") else: correct_ans = prob.get("answer", "").strip().lower() student_ans = user_ans.strip().lower() if student_ans == correct_ans or student_ans in correct_ans: st.success("โœ… Correct! Well done!") else: st.error(f"โŒ Not quite. **Hint:** {prob.get('hint', '')}") st.info(f"**Correct answer:** {prob['answer']}") if not user_ans.strip(): all_answered = False st.divider() if st.button("๐Ÿ“‹ Take Mini-Test", type="primary", use_container_width=True): set_stage("test") st.rerun() # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• # STAGE 4: MINI-TEST # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• elif stage == "test": st.markdown('
๐Ÿ“‹ Mini-Test
', unsafe_allow_html=True) st.caption("5 questions ยท Pass mark: 80% ยท You can retry if you don't pass!") if not st.session_state.get("test_questions"): with st.spinner("Generating your mini-testโ€ฆ"): try: session_id = st.session_state.get("current_session_id", "temp") result = api_client.generate_test( topic=st.session_state["current_topic"], grade=grade, session_id=session_id, ) st.session_state["test_questions"] = result.get("questions", []) except Exception as e: st.error(f"Could not generate test: {e}") st.stop() questions = st.session_state["test_questions"] answers = st.session_state.get("test_answers", {}) with st.form("test_form"): for i, q in enumerate(questions): st.markdown(f"**Question {i+1}:** {q['question']}") ans = st.text_input(f"Your answer:", key=f"test_q_{i}", value=answers.get(str(i), "")) answers[str(i)] = ans st.divider() submitted = st.form_submit_button("โœ… Submit Test", type="primary", use_container_width=True) if submitted: st.session_state["test_answers"] = answers # Build answers payload payload = [] for i, q in enumerate(questions): payload.append({ "question_index": i, "question": q["question"], "correct_answer": q["correct_answer"], "student_answer": answers.get(str(i), ""), }) with st.spinner("Evaluating your answersโ€ฆ"): try: session_id = st.session_state.get("current_session_id", "temp") result = api_client.evaluate_test( session_id=session_id, answers=payload, ) st.session_state["test_result"] = result # Check for new badges try: badge_result = api_client.check_badges(session_id) st.session_state["new_badges"] = badge_result.get("newly_awarded", []) except Exception: st.session_state["new_badges"] = [] set_stage("result") st.rerun() except Exception as e: st.error(f"Could not evaluate test: {e}") # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• # STAGE 5: RESULT # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• elif stage == "result": result = st.session_state.get("test_result", {}) score = result.get("score", 0) passed = result.get("passed", False) encour = result.get("encouragement", "Keep learning!") items = result.get("results", []) if passed: st.balloons() st.success(f"## ๐ŸŽ‰ You Passed! Score: {score}%") else: st.warning(f"## ๐Ÿ“š Score: {score}% โ€” Keep Going!") st.markdown(f"*{encour}*") st.divider() # Show question-level feedback st.markdown('
๐Ÿ“Š Question Breakdown
', unsafe_allow_html=True) questions = st.session_state.get("test_questions", []) answers = st.session_state.get("test_answers", {}) for item in items: idx = item["question_index"] q_text = questions[idx]["question"] if idx < len(questions) else f"Q{idx+1}" icon = "โœ…" if item["is_correct"] else "โŒ" with st.expander(f"{icon} Question {idx+1}: {q_text[:60]}โ€ฆ", expanded=not item["is_correct"]): st.write(f"**Your answer:** {answers.get(str(idx), 'โ€”')}") st.write(f"**Feedback:** {item['feedback']}") if not item["is_correct"] and idx < len(questions): steps = questions[idx].get("solution_steps", []) if steps: st.markdown("**Solution steps:**") for j, step in enumerate(steps, 1): st.markdown(f"{j}. {step}") # New badges new_badges = st.session_state.get("new_badges", []) if new_badges: st.divider() st.markdown('
๐Ÿ† New Badges Earned!
', unsafe_allow_html=True) cols = st.columns(len(new_badges)) for i, badge in enumerate(new_badges): with cols[i]: st.markdown(f"## {badge['emoji']}") st.markdown(f"**{badge['name']}**") st.caption(badge["description"]) st.divider() col1, col2 = st.columns(2) with col1: if not passed: if st.button("๐Ÿ”„ Retry Test", type="primary", use_container_width=True): st.session_state["test_questions"] = [] st.session_state["test_answers"] = {} st.session_state["test_result"] = None set_stage("test") st.rerun() with col2: if st.button("๐Ÿ†• New Question", use_container_width=True): reset_tutor() st.rerun()