import json import gradio as gr import PyPDF2 as pypdf from stylesheet import style from dotenv import load_dotenv from prompts import prompts from ai import ask_ai load_dotenv() css, js = style() # PDF To Text Conversion Function def convertPdfIntoText(file): if file is None: return "No file uploaded." pypdf_reader = pypdf.PdfReader(file.name) text = "" for page in pypdf_reader.pages: text += page.extract_text() + "\n" # print("---------PDF TEXT EXTRACTION DONE-----------") return text.strip() # Gradio Interface Setup with gr.Blocks(title="Quizy", css=css, head=js) as app: gr.Markdown("# Quizy: Quiz Generator") gr.Markdown("Upload a PDF file to generate a quiz based on its content.") file_input = gr.File(label="Upload PDF File", file_types=[".pdf"]) generate_btn = gr.Button("Generate Quiz", visible=True, elem_id="generate-btn") with gr.Accordion("Quiz", open=False, visible=False) as quiz_accordion: output_container = gr.HTML() submit_btn = gr.Button("Submit", elem_id="submit-btn", visible=False) def generate_quiz(file): if file is None: return "

No file uploaded.

", gr.Accordion(visible=False) inputData = convertPdfIntoText(file) systemPrompt, userPrompt = prompts(inputData) response = ask_ai(systemPrompt, userPrompt) clean_response = ( response.strip().removeprefix("```json").removesuffix("```") ) clean_response = clean_response.strip() quiz_data = json.loads(clean_response) html = "
" html += "
" for i, question in enumerate(quiz_data["quiz"]): question_type = question["type"] answer_json = json.dumps(question["answer"]).replace('"', """) html += f"
" html += f"

Question {i+1}: {question['question']}

" if question_type == "single_choice": if "options" in question: html += "
" for option in question["options"]: html += f"
{option}
" html += "
" elif question_type == "true_false": html += "
" html += ( f"
True
" ) html += f"
False
" html += "
" elif question_type == "multiple_choice": if "options" in question: html += "
" for option in question["options"]: html += f"
{option}
" html += "
" elif question_type == "fill_in_the_blank": html += ( "" ) html += "
" # Updated button to use validateForm first and then call checkAnswers if validation passes html += "" html += "
" # Close the form html += "
" html += "
" html += "" # Close quiz-answers return html, gr.Accordion(visible=True) generate_btn.click( fn=generate_quiz, inputs=file_input, outputs=[output_container, quiz_accordion], ) app.launch()