import streamlit as st class _QuizGUI: def __init__(self, quiz_data): self.quiz_data = quiz_data if "current_question" not in st.session_state: st.session_state.current_question = 0 if "user_answers" not in st.session_state: st.session_state.user_answers = [] if "completed" not in st.session_state: st.session_state.completed = False if "score" not in st.session_state: st.session_state.score = 0 def check_answer(self): selected_option = st.session_state[f"selected_option_{st.session_state.current_question}"] question_data = self.quiz_data[st.session_state.current_question] if selected_option == "---" or selected_option is None: st.warning("Please select an option before proceeding.") return st.session_state.user_answers.append(selected_option) if selected_option == question_data["correct_answer"]: st.session_state.score += 1 if st.session_state.current_question + 1 < len(self.quiz_data): st.session_state.current_question += 1 else: st.session_state.completed = True def load_quiz(self, metadata): if metadata: st.title(f"Subject: {metadata.get("subject", "Unknown Subject")}") st.header(f"Topic: {metadata.get("topic", "Unknown Topic")}") st.markdown(f""" **Quiz Type: {metadata.get("exam_type")}** Number of Questions: {metadata.get("num_questions")} "Timestamp: {metadata.get("timestamp")} """) else: st.warning("No metadata was found.") def start_quiz(self): self.quiz_data = st.session_state.quiz_data if not st.session_state.completed: question_data = self.quiz_data[st.session_state.current_question] st.write(f"**Question {st.session_state.current_question + 1}:** {question_data['question']}") st.selectbox( "Select an option:", ["---"] + question_data["options"], key=f"selected_option_{st.session_state.current_question}" ) st.button("Next", on_click=self.check_answer) else: st.success(f"Quiz completed! Your final score is {st.session_state.score}/{len(self.quiz_data)}.") st.write("### Quiz Summary") for i, question_data in enumerate(self.quiz_data): st.write(f"**Question {i + 1}:** {question_data['question']}") st.write(f"- **Options:** {', '.join(question_data['options'])}") st.write(f"- **Correct Answer:** {question_data['correct_answer']}") st.write(f"- **Your Answer:** {st.session_state.user_answers[i]}") if st.session_state.user_answers[i] == question_data["correct_answer"]: st.write("- **Result:** ✅ Correct") else: st.write("- **Result:** ❌ Incorrect")