Spaces:
Configuration error
Configuration error
Download streamlit_app/pages/2_Tutor.py from rockysreenivasa/MathTutor: direct link, hf CLI and curl.
- Browser
- Download file 14.4 kB
-
https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/pages/2_Tutor.py
- Command line
-
hf download hf://spaces/rockysreenivasa/MathTutor/streamlit_app/pages/2_Tutor.py
-
curl -L -o 2_Tutor.py https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/pages/2_Tutor.py
14.4 kB
| 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""" | |
| <div class="mm-hero"> | |
| <h1>๐งฎ Math Tutor</h1> | |
| <p class="mm-subtle">Grade {grade} ยท Ask me any math question</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| stage = get_stage() | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # STAGE 1: ASK | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| if stage == "ask": | |
| st.markdown('<div class="mm-section-title">๐ฌ What would you like to learn?</div>', 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'<div class="mm-section-title">๐ Explanation for Grade {grade}</div>', unsafe_allow_html=True) | |
| st.markdown( | |
| f""" | |
| <div class="mm-card"> | |
| <strong>Your question:</strong> {query} | |
| </div> | |
| """, | |
| 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('<div class="mm-section-title">โ๏ธ Practice Problems</div>', 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('<div class="mm-section-title">๐ Mini-Test</div>', 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('<div class="mm-section-title">๐ Question Breakdown</div>', 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('<div class="mm-section-title">๐ New Badges Earned!</div>', 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() | |