Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import PyPDF2
|
| 3 |
+
|
| 4 |
+
def extract_text_from_pdf(pdf_file):
|
| 5 |
+
text = ""
|
| 6 |
+
with open(pdf_file, "rb") as file:
|
| 7 |
+
pdf_reader = PyPDF2.PdfFileReader(file)
|
| 8 |
+
num_pages = pdf_reader.numPages
|
| 9 |
+
for page_num in range(num_pages):
|
| 10 |
+
page = pdf_reader.getPage(page_num)
|
| 11 |
+
text += page.extractText()
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
def generate_quiz(quiz_name, pdf_file, num_questions, topics):
|
| 15 |
+
# Extract text from the provided PDF
|
| 16 |
+
pdf_text = extract_text_from_pdf(pdf_file)
|
| 17 |
+
|
| 18 |
+
# Create a PDF document for the quiz
|
| 19 |
+
# (Note: You can customize this part based on your quiz generation logic)
|
| 20 |
+
# For simplicity, this example just includes the extracted text in the output PDF.
|
| 21 |
+
output_pdf_name = f"{quiz_name}_output.pdf"
|
| 22 |
+
with open(output_pdf_name, "w", encoding="utf-8") as output_pdf:
|
| 23 |
+
output_pdf.write(f"Quiz Name: {quiz_name}\n")
|
| 24 |
+
output_pdf.write(f"Number of Questions: {num_questions}\n")
|
| 25 |
+
output_pdf.write(f"Topics: {', '.join(topics)}\n\n")
|
| 26 |
+
output_pdf.write("Extracted text from the provided PDF:\n")
|
| 27 |
+
output_pdf.write(pdf_text)
|
| 28 |
+
|
| 29 |
+
return f"Quiz '{quiz_name}' generated successfully as '{output_pdf_name}' with {num_questions} questions."
|
| 30 |
+
|
| 31 |
+
# Create Gradio interface
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=generate_quiz,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.TextInput("text", label="Quiz Name"),
|
| 36 |
+
gr.File("pdf", label="Upload PDF Document"),
|
| 37 |
+
gr.Number("number", label="Number of Questions"),
|
| 38 |
+
gr.Textbox("text", label="Topics (comma-separated)"),
|
| 39 |
+
],
|
| 40 |
+
outputs=gr.Textbox(),
|
| 41 |
+
live=True,
|
| 42 |
+
capture_session=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the Gradio interface
|
| 46 |
+
iface.launch()
|