Spaces:
Runtime error
Runtime error
Download app.py from mohammed3536/Questions-generator: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/mohammed3536/Questions-generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/mohammed3536/Questions-generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mohammed3536/Questions-generator/resolve/main/app.py
1.65 kB
| 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() |