Spaces:
Running
Running
File size: 6,469 Bytes
73633b5 a50befe 73633b5 a50befe a6cd9e3 a50befe a6cd9e3 a50befe a6cd9e3 a50befe a6cd9e3 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 a50befe 73633b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 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 | import streamlit as st
import sys, os
# β
FIX: Add project root first
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from config import APP_TITLE, APP_ICON, MAX_QUESTIONS
# β
FIRST Streamlit call
st.set_page_config(
page_title=APP_TITLE,
page_icon=APP_ICON,
layout="centered",
)
# Add project root to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.evaluator import score_quiz
from app.components import (
render_question_card,
render_result_card,
render_score_summary
)
# βββββββββββββββββββββββββββββββββββββββββββββ
# CACHE MODEL (important for performance)
# βββββββββββββββββββββββββββββββββββββββββββββ
@st.cache_resource
def load_pipeline():
from src.mcq_builder import build_quiz
return build_quiz
build_quiz = load_pipeline()
# βββββββββββββββββββββββββββββββββββββββββββββ
# SESSION STATE
# βββββββββββββββββββββββββββββββββββββββββββββ
def init_state():
defaults = {
"screen": "input",
"mcqs": [],
"current_q": 0,
"user_answers": [],
"quiz_result": None,
}
for k, v in defaults.items():
if k not in st.session_state:
st.session_state[k] = v
init_state()
# βββββββββββββββββββββββββββββββββββββββββββββ
# RESET
# βββββββββββββββββββββββββββββββββββββββββββββ
def reset():
st.session_state.screen = "input"
st.session_state.mcqs = []
st.session_state.current_q = 0
st.session_state.user_answers = []
st.session_state.quiz_result = None
# βββββββββββββββββββββββββββββββββββββββββββββ
# SCREEN 1: INPUT
# βββββββββββββββββββββββββββββββββββββββββββββ
def screen_input():
st.title(f"{APP_ICON} {APP_TITLE}")
st.write("Paste text to generate MCQs")
passage = st.text_area(
"Your passage",
height=250,
placeholder="Paste content here..."
)
num_questions = st.slider(
"Number of questions",
3,
MAX_QUESTIONS,
5
)
if st.button("Generate Quiz", type="primary"):
if not passage or len(passage.split()) < 30:
st.warning("Enter at least 30 words")
return
with st.spinner("Generating questions..."):
try:
mcqs = build_quiz(passage, num_questions=num_questions)
except Exception as e:
st.error(f"Error: {e}")
return
if not mcqs:
st.error("Failed to generate questions")
return
st.session_state.mcqs = mcqs
st.session_state.user_answers = [-1] * len(mcqs)
st.session_state.current_q = 0
st.session_state.screen = "quiz"
st.rerun()
# βββββββββββββββββββββββββββββββββββββββββββββ
# SCREEN 2: QUIZ
# βββββββββββββββββββββββββββββββββββββββββββββ
def screen_quiz():
mcqs = st.session_state.mcqs
current = st.session_state.current_q
total = len(mcqs)
mcq = mcqs[current]
st.progress(current / total, text=f"Q {current+1}/{total}")
selected_label = render_question_card(mcq, current)
col1, col2, col3 = st.columns([1, 2, 1])
# Previous
with col1:
if current > 0:
if st.button("β Prev"):
st.session_state.current_q -= 1
st.rerun()
# Next / Submit
with col3:
if selected_label:
idx = ord(selected_label) - ord("A")
st.session_state.user_answers[current] = idx
if current < total - 1:
if st.button("Next β", type="primary"):
if selected_label is None:
st.warning("Select an answer")
else:
st.session_state.current_q += 1
st.rerun()
else:
if st.button("Submit", type="primary"):
result = score_quiz(
st.session_state.mcqs,
st.session_state.user_answers
)
st.session_state.quiz_result = result
st.session_state.screen = "results"
st.rerun()
# Quit
with col2:
if st.button("Quit"):
reset()
st.rerun()
# βββββββββββββββββββββββββββββββββββββββββββββ
# SCREEN 3: RESULTS
# βββββββββββββββββββββββββββββββββββββββββββββ
def screen_results():
result = st.session_state.quiz_result
st.title("Quiz Complete")
render_score_summary(result)
for i, r in enumerate(result["results"]):
render_result_card(r, i + 1)
col1, col2 = st.columns(2)
with col1:
if st.button("New Quiz"):
reset()
st.rerun()
with col2:
if st.button("Retry"):
st.session_state.user_answers = [-1] * len(st.session_state.mcqs)
st.session_state.current_q = 0
st.session_state.screen = "quiz"
st.rerun()
# βββββββββββββββββββββββββββββββββββββββββββββ
# ROUTER
# βββββββββββββββββββββββββββββββββββββββββββββ
if st.session_state.screen == "input":
screen_input()
elif st.session_state.screen == "quiz":
screen_quiz()
elif st.session_state.screen == "results":
screen_results() |