import streamlit as st import fitz import os import requests import json import re import random st.set_page_config(page_title="AI Quiz Generator", page_icon="๐Ÿง ", layout="wide") st.markdown(""" """, unsafe_allow_html=True) # โ”€โ”€โ”€ Session State โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ if "questions" not in st.session_state: st.session_state.questions = [] if "answers" not in st.session_state: st.session_state.answers = {} if "revealed" not in st.session_state: st.session_state.revealed = {} if "quiz_submitted" not in st.session_state: st.session_state.quiz_submitted = False if "doc_title" not in st.session_state: st.session_state.doc_title = "" # โ”€โ”€โ”€ Helpers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ def extract_pdf_text(pdf_bytes: bytes) -> str: doc = fitz.open(stream=pdf_bytes, filetype="pdf") text = "" for page in doc: text += page.get_text("text") + "\n" doc.close() return text.strip() def generate_quiz(text: str, num_questions: int, difficulty: str, topic_focus: str, api_key: str) -> list: focus_instruction = f"Focus specifically on: {topic_focus}." if topic_focus else "Cover the most important concepts." prompt = f"""You are an expert educator and assessment designer. Create a high-quality multiple choice quiz based on the document content below. Document Content: {text[:4000]} Requirements: - Generate exactly {num_questions} questions - Difficulty level: {difficulty} - {focus_instruction} - Each question must have exactly 4 options (A, B, C, D) - Only one option is correct - Explanations must be educational and reference the document Respond ONLY with a valid JSON array in exactly this format: [ {{ "question": "", "options": {{ "A": "