Spaces:
Runtime error
Runtime error
File size: 1,652 Bytes
cbaa01e c103c8b cbaa01e 401556e cbaa01e | 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 | import gradio as gr
import PyPDF2
def extract_text_from_pdf(pdf_file):
text = ""
with open(pdf_file, "rb") as file:
pdf_reader = PyPDF2.PdfReader(file)
num_pages = pdf_reader.numPages
for page_num in range(num_pages):
page = pdf_reader.getPage(page_num)
text += page.extractText()
return text
def generate_quiz(quiz_name, pdf_file, num_questions, topics):
# Extract text from the provided PDF
pdf_text = extract_text_from_pdf(pdf_file)
# Create a PDF document for the quiz
# (Note: You can customize this part based on your quiz generation logic)
# For simplicity, this example just includes the extracted text in the output PDF.
output_pdf_name = f"{quiz_name}_output.pdf"
with open(output_pdf_name, "w", encoding="utf-8") as output_pdf:
output_pdf.write(f"Quiz Name: {quiz_name}\n")
output_pdf.write(f"Number of Questions: {num_questions}\n")
output_pdf.write(f"Topics: {', '.join(topics)}\n\n")
output_pdf.write("Extracted text from the provided PDF:\n")
output_pdf.write(pdf_text)
return f"Quiz '{quiz_name}' generated successfully as '{output_pdf_name}' with {num_questions} questions."
# Create 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 (comma-separated)"),
],
outputs=gr.Textbox(),
live=True,
capture_session=True
)
# Launch the Gradio interface
iface.launch() |