File size: 3,517 Bytes
2c7c3b3
 
 
 
 
 
 
 
 
7cdb1a6
2c7c3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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()