Spaces:
No application file
No application file
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load a Hugging Face model for text-to-text generation
|
| 6 |
+
mcq_generator = pipeline("text2text-generation", model="t5-small", tokenizer="t5-small")
|
| 7 |
+
|
| 8 |
+
def extract_text_from_pdf(pdf_file):
|
| 9 |
+
"""Extract text from an uploaded PDF file."""
|
| 10 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 11 |
+
text = ""
|
| 12 |
+
for page in pdf_reader.pages:
|
| 13 |
+
text += page.extract_text()
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
def generate_mcqs(pdf_file):
|
| 17 |
+
"""Generate MCQs from the extracted text of the PDF."""
|
| 18 |
+
# Extract text from the PDF
|
| 19 |
+
pdf_text = extract_text_from_pdf(pdf_file)
|
| 20 |
+
|
| 21 |
+
# Generate MCQs (this is a basic prompt for the model; you can refine it)
|
| 22 |
+
prompt = f"Generate MCQs from the following text: {pdf_text[:2000]}" # Limiting text to 2000 characters
|
| 23 |
+
results = mcq_generator(prompt, max_length=512, num_return_sequences=5)
|
| 24 |
+
|
| 25 |
+
# Extract questions from the model's output
|
| 26 |
+
mcqs = [result['generated_text'] for result in results]
|
| 27 |
+
return "\n\n".join(mcqs)
|
| 28 |
+
|
| 29 |
+
# Gradio UI
|
| 30 |
+
with gr.Blocks() as mcq_app:
|
| 31 |
+
gr.Markdown("# PDF-based MCQ Generator")
|
| 32 |
+
pdf_input = gr.File(label="Upload PDF", type="file")
|
| 33 |
+
generate_button = gr.Button("Generate MCQs")
|
| 34 |
+
mcq_output = gr.Textbox(label="Generated MCQs", lines=15)
|
| 35 |
+
|
| 36 |
+
generate_button.click(fn=generate_mcqs, inputs=pdf_input, outputs=mcq_output)
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
| 39 |
+
mcq_app.launch()
|