File size: 8,662 Bytes
2512970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import gradio as gr
import pdfplumber
from PIL import Image
import pytesseract
import io
import re
import random
from transformers import pipeline


# Use a stable and widely supported model for question generation
qg_pipeline = pipeline("text2text-generation", model="t5-base")  # standard T5 base model

summarizer = pipeline("summarization", model="facebook/bart-large-cnn")  # reliable summarizer


def extract_text_from_pdf(file_bytes):
    try:
        text = ""
        with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + "\n"
        if not text.strip():
            text = ocr_pdf(file_bytes)
        return text
    except Exception:
        return ""


def ocr_pdf(file_bytes):
    text = ""
    with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
        for page in pdf.pages:
            pil_image = page.to_image(resolution=300).original
            page_text = pytesseract.image_to_string(pil_image)
            text += page_text + "\n"
    return text


def extract_text_from_image(file_bytes):
    image = Image.open(io.BytesIO(file_bytes))
    text = pytesseract.image_to_string(image)
    return text


def extract_text_from_txt(file_bytes):
    try:
        text = file_bytes.decode("utf-8")
    except UnicodeDecodeError:
        text = file_bytes.decode("latin-1")
    return text


def clean_text(text):
    text = re.sub(r'\n+', '\n', text)
    text = re.sub(r'[ ]{2,}', ' ', text)
    return text.strip()


def split_to_sentences(text):
    sentences = re.split(r'(?<=[.?!])\s+', text)
    return [s.strip() for s in sentences if s.strip()]


def highlight_answer_in_context(context, answer):
    idx = context.lower().find(answer.lower())
    if idx != -1:
        part1 = context[:idx]
        part2 = context[idx + len(answer):]
        return f"{part1.strip()} <hl> {answer.strip()} <hl> {part2.strip()}"
    else:
        return context


def generate_mcq(answer):
    correct_answer = answer
    words = correct_answer.split()
    options = set()
    options.add(correct_answer)
    while len(options) < 4:
        if len(words) > 1:
            shuffled = words[:]
            random.shuffle(shuffled)
            option = ' '.join(shuffled)
            if option.lower() != correct_answer.lower():
                options.add(option)
        else:
            option = correct_answer + random.choice(['.', ',', '?', '!'])
            options.add(option)
    options = list(options)
    random.shuffle(options)
    correct_letter = 'ABCD'[options.index(correct_answer)]
    return options, correct_letter


def generate_questions_mcq(context, num_questions):
    sentences = split_to_sentences(context)
    questions_structured = []
    used_questions = set()
    candidates = sentences[:15]
    for sentence in candidates:
        input_text = highlight_answer_in_context(context, sentence)
        # Prefix input for T5 question generation
        input_text_for_model = "generate question: " + input_text
        # Generate question
        question = qg_pipeline(input_text_for_model, max_length=64, do_sample=False)[0]['generated_text']
        if question in used_questions or not question.endswith('?'):
            continue
        used_questions.add(question)
        options, correct_letter = generate_mcq(sentence)
        questions_structured.append({
            "question": question,
            "options": options,
            "correct_letter": correct_letter,
            "correct_answer": sentence,
            "explanation": f"Answer explanation: {sentence}"
        })
        if len(questions_structured) >= num_questions:
            break
    if not questions_structured:
        question = "What is the main topic discussed in the content?"
        options = ["Option A", "Option B", "Option C", "Option D"]
        questions_structured.append({
            "question": question,
            "options": options,
            "correct_letter": "A",
            "correct_answer": "Option A",
            "explanation": "Fallback explanation."
        })
    return questions_structured


def generate_questions_subjective(context, num_questions):
    sentences = split_to_sentences(context)
    questions_structured = []
    used_questions = set()
    candidates = sentences[:20]
    for sentence in candidates:
        input_text = highlight_answer_in_context(context, sentence)
        input_text_for_model = "generate question: " + input_text
        question = qg_pipeline(input_text_for_model, max_length=64, do_sample=False)[0]['generated_text']
        if question in used_questions or not question.endswith('?'):
            continue
        used_questions.add(question)
        answer = summarizer(sentence, max_length=50, min_length=10, do_sample=False)[0]['summary_text']
        questions_structured.append({
            "question": question,
            "answer": answer
        })
        if len(questions_structured) >= num_questions:
            break
    if not questions_structured:
        questions_structured.append({
            "question": "Describe the main topic discussed in the content.",
            "answer": "The main topic is an overview of the content provided."
        })
    return questions_structured


def format_mcq_output(questions):
    output = ""
    for idx, q in enumerate(questions, 1):
        output += f"- Q{idx}: {q['question']}\n"
        opts = ['A', 'B', 'C', 'D']
        for opt_idx, option in enumerate(q['options']):
            output += f"  - {opts[opt_idx]}. {option}\n"
        output += f"- Correct Answer: {q['correct_letter']}\n"
        output += f"- Explanation: {q['explanation']}\n\n"
    return output.strip()


def format_subjective_output(questions):
    output = ""
    for idx, q in enumerate(questions, 1):
        output += f"- Q{idx}: {q['question']}\n"
        output += f"- Suggested Answer: {q['answer']}\n\n"
    return output.strip()


def main_process(file, question_type, num_questions):
    if not file:
        return "Please upload a file."

    file_bytes = file.read()
    fname = file.name.lower()
    extracted_text = ""

    if fname.endswith(".pdf"):
        extracted_text = extract_text_from_pdf(file_bytes)
    elif fname.endswith((".png", ".jpg", ".jpeg", ".bmp", ".tiff")):
        extracted_text = extract_text_from_image(file_bytes)
    elif fname.endswith(".txt"):
        extracted_text = extract_text_from_txt(file_bytes)
    else:
        return "Unsupported file type. Please upload PDF, Image, or TXT."

    extracted_text = clean_text(extracted_text)

    if len(extracted_text) < 30:
        return "Extracted text is too short or empty. Please check your input file."

    if question_type == "MCQ":
        questions = generate_questions_mcq(extracted_text, num_questions)
        return format_mcq_output(questions)
    else:
        questions = generate_questions_subjective(extracted_text, num_questions)
        return format_subjective_output(questions)


css = """
#header {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-weight: 700;
  font-size: 28px;
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}
#footer {
  font-size: 12px;
  color: #666;
  margin-top: 30px;
  text-align: center;
}
.output-area {
  white-space: pre-wrap;
  background-color: #f3f4f6;
  padding: 15px;
  border-radius: 8px;
  font-family: monospace;
  max-height: 450px;
  overflow-y: auto;
}
.gr-button {
  background-color: #4f46e5;
  color: white;
  font-weight: bold;
  border-radius: 8px;
}
.gr-button:hover {
  background-color: #4338ca;
}
"""

with gr.Blocks(css=css) as demo:
    gr.Markdown("<div id='header'>📚 Study Content Question Generator</div>")
    with gr.Row():
        file_input = gr.File(label="Upload PDF, Image, or Text file", type="file")
        with gr.Column():
            question_type = gr.Radio(
                choices=["MCQ", "Subjective"], label="Question Type", value="MCQ"
            )
            num_questions = gr.Slider(
                1, 10, value=5, step=1, label="Number of Questions"
            )
            generate_btn = gr.Button("Generate Questions")
    output = gr.Textbox(
        label="Generated Questions",
        lines=20,
        interactive=False,
        elem_classes="output-area",
    )

    generate_btn.click(
        fn=main_process, inputs=[file_input, question_type, num_questions], outputs=output
    )

    gr.Markdown("<div id='footer'>Made with ❤️ using Hugging Face Spaces and Transformers</div>")


if __name__ == "__main__":
    demo.launch()