Vishali-M commited on
Commit
2c7c3b3
·
verified ·
1 Parent(s): a990293

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import PyPDF2
4
+ import docx
5
+
6
+ # -----------------------------
7
+ # Load FLAN-T5 Model
8
+ # -----------------------------
9
+ generator = pipeline(
10
+ "text2text-generation",
11
+ model="google/flan-t5-base"
12
+ )
13
+
14
+ # -----------------------------
15
+ # Extract Text from File
16
+ # -----------------------------
17
+ def extract_text(file):
18
+
19
+ if file is None:
20
+ return ""
21
+
22
+ filename = file.name.lower()
23
+
24
+ # TXT File
25
+ if filename.endswith(".txt"):
26
+ with open(file.name, "r", encoding="utf-8", errors="ignore") as f:
27
+ return f.read()
28
+
29
+ # PDF File
30
+ elif filename.endswith(".pdf"):
31
+ text = ""
32
+ with open(file.name, "rb") as f:
33
+ reader = PyPDF2.PdfReader(f)
34
+
35
+ for page in reader.pages:
36
+ text += page.extract_text() or ""
37
+
38
+ return text
39
+
40
+ # DOCX File
41
+ elif filename.endswith(".docx"):
42
+ doc = docx.Document(file.name)
43
+ text = "\n".join([p.text for p in doc.paragraphs])
44
+ return text
45
+
46
+ return ""
47
+
48
+
49
+ # -----------------------------
50
+ # Generate Questions
51
+ # -----------------------------
52
+ def generate_questions(file,
53
+ question_type,
54
+ difficulty,
55
+ blooms_level,
56
+ num_questions):
57
+
58
+ text = extract_text(file)
59
+
60
+ if not text.strip():
61
+ return "No text extracted from file."
62
+
63
+ # Reduce text length for model
64
+ text = text[:2000]
65
+
66
+ # Prompt
67
+ prompt = f"""
68
+ Generate {num_questions} {question_type} questions.
69
+
70
+ Difficulty Level: {difficulty}
71
+
72
+ Bloom's Taxonomy Level: {blooms_level}
73
+
74
+ Instructions:
75
+ - If MCQ, provide 4 options and correct answer.
76
+ - If Descriptive, provide detailed answer key.
77
+ - Generate unit-wise academic questions.
78
+
79
+ Content:
80
+ {text}
81
+ """
82
+
83
+ result = generator(
84
+ prompt,
85
+ max_length=512,
86
+ do_sample=True,
87
+ temperature=0.7
88
+ )
89
+
90
+ return result[0]["generated_text"]
91
+
92
+
93
+ # -----------------------------
94
+ # Gradio UI
95
+ # -----------------------------
96
+ with gr.Blocks() as demo:
97
+
98
+ gr.Markdown("# 📘 AI Examination Paper Generator")
99
+ gr.Markdown(
100
+ "Upload syllabus file and generate MCQs or descriptive questions using Generative AI."
101
+ )
102
+
103
+ with gr.Row():
104
+
105
+ file_input = gr.File(
106
+ label="Upload PDF / DOCX / TXT File"
107
+ )
108
+
109
+ question_type = gr.Dropdown(
110
+ ["MCQ", "Descriptive", "Both"],
111
+ value="MCQ",
112
+ label="Question Type"
113
+ )
114
+
115
+ with gr.Row():
116
+
117
+ difficulty = gr.Dropdown(
118
+ ["Easy", "Medium", "Hard"],
119
+ value="Medium",
120
+ label="Difficulty Level"
121
+ )
122
+
123
+ blooms_level = gr.Dropdown(
124
+ [
125
+ "Remember",
126
+ "Understand",
127
+ "Apply",
128
+ "Analyze",
129
+ "Evaluate",
130
+ "Create"
131
+ ],
132
+ value="Apply",
133
+ label="Bloom's Taxonomy Level"
134
+ )
135
+
136
+ num_questions = gr.Slider(
137
+ minimum=1,
138
+ maximum=20,
139
+ value=5,
140
+ step=1,
141
+ label="Number of Questions"
142
+ )
143
+
144
+ generate_btn = gr.Button("Generate Questions")
145
+
146
+ output = gr.Textbox(
147
+ label="Generated Question Paper",
148
+ lines=25
149
+ )
150
+
151
+ generate_btn.click(
152
+ fn=generate_questions,
153
+ inputs=[
154
+ file_input,
155
+ question_type,
156
+ difficulty,
157
+ blooms_level,
158
+ num_questions
159
+ ],
160
+ outputs=output
161
+ )
162
+
163
+ demo.launch()