Spaces:
Build error
Build error
Download app.py from Vishali-M/QP_Generator: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/Vishali-M/QP_Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/Vishali-M/QP_Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Vishali-M/QP_Generator/resolve/main/app.py
3.52 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import PyPDF2 | |
| import docx | |
| # ----------------------------- | |
| # Load FLAN-T5 Model | |
| # ----------------------------- | |
| generator = pipeline( | |
| task="text-generation", | |
| model="google/flan-t5-base" | |
| ) | |
| # ----------------------------- | |
| # Extract Text from File | |
| # ----------------------------- | |
| def extract_text(file): | |
| if file is None: | |
| return "" | |
| filename = file.name.lower() | |
| # TXT File | |
| if filename.endswith(".txt"): | |
| with open(file.name, "r", encoding="utf-8", errors="ignore") as f: | |
| return f.read() | |
| # PDF File | |
| elif filename.endswith(".pdf"): | |
| text = "" | |
| with open(file.name, "rb") as f: | |
| reader = PyPDF2.PdfReader(f) | |
| for page in reader.pages: | |
| text += page.extract_text() or "" | |
| return text | |
| # DOCX File | |
| elif filename.endswith(".docx"): | |
| doc = docx.Document(file.name) | |
| text = "\n".join([p.text for p in doc.paragraphs]) | |
| return text | |
| return "" | |
| # ----------------------------- | |
| # Generate Questions | |
| # ----------------------------- | |
| def generate_questions(file, | |
| question_type, | |
| difficulty, | |
| blooms_level, | |
| num_questions): | |
| text = extract_text(file) | |
| if not text.strip(): | |
| return "No text extracted from file." | |
| # Reduce text length for model | |
| text = text[:2000] | |
| # Prompt | |
| prompt = f""" | |
| Generate {num_questions} {question_type} questions. | |
| Difficulty Level: {difficulty} | |
| Bloom's Taxonomy Level: {blooms_level} | |
| Instructions: | |
| - If MCQ, provide 4 options and correct answer. | |
| - If Descriptive, provide detailed answer key. | |
| - Generate unit-wise academic questions. | |
| Content: | |
| {text} | |
| """ | |
| result = generator( | |
| prompt, | |
| max_length=512, | |
| do_sample=True, | |
| temperature=0.7 | |
| ) | |
| return result[0]["generated_text"] | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📘 AI Examination Paper Generator") | |
| gr.Markdown( | |
| "Upload syllabus file and generate MCQs or descriptive questions using Generative AI." | |
| ) | |
| with gr.Row(): | |
| file_input = gr.File( | |
| label="Upload PDF / DOCX / TXT File" | |
| ) | |
| question_type = gr.Dropdown( | |
| ["MCQ", "Descriptive", "Both"], | |
| value="MCQ", | |
| label="Question Type" | |
| ) | |
| with gr.Row(): | |
| difficulty = gr.Dropdown( | |
| ["Easy", "Medium", "Hard"], | |
| value="Medium", | |
| label="Difficulty Level" | |
| ) | |
| blooms_level = gr.Dropdown( | |
| [ | |
| "Remember", | |
| "Understand", | |
| "Apply", | |
| "Analyze", | |
| "Evaluate", | |
| "Create" | |
| ], | |
| value="Apply", | |
| label="Bloom's Taxonomy Level" | |
| ) | |
| num_questions = gr.Slider( | |
| minimum=1, | |
| maximum=20, | |
| value=5, | |
| step=1, | |
| label="Number of Questions" | |
| ) | |
| generate_btn = gr.Button("Generate Questions") | |
| output = gr.Textbox( | |
| label="Generated Question Paper", | |
| lines=25 | |
| ) | |
| generate_btn.click( | |
| fn=generate_questions, | |
| inputs=[ | |
| file_input, | |
| question_type, | |
| difficulty, | |
| blooms_level, | |
| num_questions | |
| ], | |
| outputs=output | |
| ) | |
| demo.launch() |