File size: 9,344 Bytes
facb671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6fac4
facb671
 
 
 
 
cf6fac4
 
facb671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6fac4
facb671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6fac4
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 io
import re
import random

from transformers import pipeline

# Load question generation pipeline
# Using valhalla/t5-base-qg-hl for question generation with highlighting support
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-base-qg-hl")

# Load summarization pipeline for key sentence extraction (to identify key concepts)
summarizer = pipeline("summarization")

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"
        # Do not fallback on OCR because pytesseract requires system installation
        return text
    except Exception as e:
        return ""

def extract_text_from_image(file_bytes):
    # OCR disabled due to system dependencies on Tesseract
    return "OCR not supported in this environment. Please upload a PDF or TXT file containing selectable 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):
    # Clean excessive new lines and spaces
    text = re.sub(r'\n+', '\n', text)
    text = re.sub(r'[ ]{2,}', ' ', text)
    return text.strip()

def split_to_sentences(text):
    # Simple split by periods, question marks, and exclamation
    sentences = re.split(r'(?<=[.?!])\s+', text)
    return [s.strip() for s in sentences if s.strip()]

def highlight_answer_in_context(context, answer):
    # Highlight answer in context for the qg model input format
    # The model uses <hl> tokens to highlight answer: context <hl> answer <hl>
    # We find answer in context and mark it
    # If no direct answer found, just return context unchanged
    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(question_text):
    '''
    Generate MCQ with 1 correct + 3 incorrect options.
    Since no direct distractor generation model, we'll generate distractors by rephrasing or random shuffling.
    Here, for demonstration, we create options by slight modifications to the correct answer.
    '''
    correct_answer = question_text

    # Generate plausible options by shuffling words or changing order
    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:
            # If single word, generate random similar words (basic approach)
            option = correct_answer + random.choice(['.', ',', '?', '!'])
            options.add(option)

    options = list(options)
    random.shuffle(options)

    # Determine the letter of correct answer
    correct_letter = 'ABCD'[options.index(correct_answer)]

    return options, correct_letter

def generate_questions_mcq(context, num_questions):
    '''
    Generate MCQ questions based on context
    '''
    sentences = split_to_sentences(context)
    questions_structured = []
    used_questions = set()

    # Limit candidates to first 15 sentences for speed
    candidates = sentences[:15]

    for i, sentence in enumerate(candidates):
        # Attempt to generate question for candidate sentence as answer
        input_text = highlight_answer_in_context(context, sentence)
        question = qg_pipeline(input_text, max_length=64)[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:
        # fallback question if no generation
        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):
    '''
    Generate subjective questions based on context, use summarization for answers
    '''
    sentences = split_to_sentences(context)
    questions_structured = []
    used_questions = set()

    candidates = sentences[:20]

    for i, sentence in enumerate(candidates):
        input_text = highlight_answer_in_context(context, sentence)
        question = qg_pipeline(input_text, max_length=64)[0]['generated_text']
        if question in used_questions or not question.endswith('?'):
            continue
        used_questions.add(question)

        # Brief answer by summarizing sentence or context snippet
        answer = sentence
        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"
        ops = ['A', 'B', 'C', 'D']
        for opt_idx, option in enumerate(q['options']):
            output += f"  - {ops[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")):
        # OCR unsupported fallback message
        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)
        output = format_mcq_output(questions)
    else:
        questions = generate_questions_subjective(extracted_text, num_questions)
        output = format_subjective_output(questions)

    return output

with gr.Blocks(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;
}
""") 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()