import gradio as gr import PyPDF2 import random # Function to extract text from a PDF file def extract_text_from_pdf(pdf_file): with open(pdf_file, 'rb') as file: pdf_reader = PyPDF2.PdfFileReader(file) text = "" for page_num in range(pdf_reader.numPages): text += pdf_reader.getPage(page_num).extractText() return text # Function to generate quiz questions def generate_quiz(quiz_name, pdf_file, num_questions, topics): # Extract text from the provided PDF pdf_text = extract_text_from_pdf(pdf_file) # Split the text into sentences (you can customize this based on your content) sentences = pdf_text.split('.') # Generate quiz questions from sentences quiz_questions = random.sample(sentences, min(num_questions, len(sentences))) # Create a dictionary to store quiz details quiz_details = { 'quiz_name': quiz_name, 'num_questions': num_questions, 'topics': topics, 'questions': quiz_questions } return quiz_details # Function to evaluate quiz responses and calculate score def evaluate_quiz(quiz_responses, correct_answers): score = sum([1 for user_answer, correct_answer in zip(quiz_responses, correct_answers) if user_answer == correct_answer]) return score # Gradio Interface iface = gr.Interface( fn=generate_quiz, inputs=[ gr.Textbox("text", label="Quiz Name"), gr.File("pdf", label="Upload PDF Document"), gr.Number("number", label="Number of Questions"), gr.Textbox("text", label="Topics or Categories (comma-separated)"), ], outputs=gr.JSON(), live=True, capture_session=True ) # Launch the Gradio interface iface.launch() # After the quiz is generated, you can proceed with taking the quiz, collecting user responses, # and using the evaluate_quiz function to calculate the score.