muhammadrazapathan commited on
Commit
8d04a30
ยท
verified ยท
1 Parent(s): dbba844

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +236 -0
app.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import sqlite3
3
+ import fitz # PyMuPDF
4
+ import re
5
+ import matplotlib.pyplot as plt
6
+ import pandas as pd
7
+ import numpy as np
8
+ import requests
9
+ import os
10
+ from collections import Counter
11
+ from datetime import datetime
12
+
13
+ # ==============================
14
+ # DATABASE SETUP
15
+ # ==============================
16
+
17
+ conn = sqlite3.connect("exam_trends.db", check_same_thread=False)
18
+ cursor = conn.cursor()
19
+
20
+ cursor.execute("""
21
+ CREATE TABLE IF NOT EXISTS past_questions (
22
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
23
+ year INTEGER,
24
+ question TEXT,
25
+ topic TEXT
26
+ )
27
+ """)
28
+
29
+ cursor.execute("""
30
+ CREATE TABLE IF NOT EXISTS current_questions (
31
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
32
+ question TEXT,
33
+ topic TEXT
34
+ )
35
+ """)
36
+
37
+ conn.commit()
38
+
39
+
40
+ # ==============================
41
+ # PDF TEXT EXTRACTION
42
+ # ==============================
43
+
44
+ def extract_text_from_pdf(file):
45
+ text = ""
46
+ pdf = fitz.open(stream=file.read(), filetype="pdf")
47
+ for page in pdf:
48
+ text += page.get_text()
49
+ return text
50
+
51
+
52
+ # ==============================
53
+ # SIMPLE QUESTION SEGMENTATION
54
+ # ==============================
55
+
56
+ def extract_questions(text):
57
+ questions = re.split(r'\n\d+\.|\nQ\d+', text)
58
+ return [q.strip() for q in questions if len(q.strip()) > 20]
59
+
60
+
61
+ # ==============================
62
+ # SIMPLE TOPIC CLASSIFIER
63
+ # (Basic keyword approach)
64
+ # ==============================
65
+
66
+ def classify_topic(question):
67
+ question = question.lower()
68
+
69
+ if "integral" in question or "derivative" in question:
70
+ return "Calculus"
71
+ elif "triangle" in question or "angle" in question:
72
+ return "Geometry"
73
+ elif "probability" in question:
74
+ return "Probability"
75
+ elif "matrix" in question:
76
+ return "Linear Algebra"
77
+ else:
78
+ return "Algebra"
79
+
80
+
81
+ # ==============================
82
+ # STORE PAST PAPERS
83
+ # ==============================
84
+
85
+ def upload_past_papers(files):
86
+ for file in files:
87
+ text = extract_text_from_pdf(file)
88
+ questions = extract_questions(text)
89
+
90
+ year = datetime.now().year # Simplified
91
+
92
+ for q in questions:
93
+ topic = classify_topic(q)
94
+ cursor.execute(
95
+ "INSERT INTO past_questions (year, question, topic) VALUES (?, ?, ?)",
96
+ (year, q, topic),
97
+ )
98
+
99
+ conn.commit()
100
+ return "Past papers uploaded and processed successfully."
101
+
102
+
103
+ # ==============================
104
+ # STORE CURRENT PAPER
105
+ # ==============================
106
+
107
+ def upload_current_paper(file):
108
+ text = extract_text_from_pdf(file)
109
+ questions = extract_questions(text)
110
+
111
+ for q in questions:
112
+ topic = classify_topic(q)
113
+ cursor.execute(
114
+ "INSERT INTO current_questions (question, topic) VALUES (?, ?)",
115
+ (q, topic),
116
+ )
117
+
118
+ conn.commit()
119
+ return "Current paper uploaded successfully."
120
+
121
+
122
+ # ==============================
123
+ # TREND ANALYSIS ENGINE
124
+ # ==============================
125
+
126
+ def analyze_trends():
127
+ cursor.execute("SELECT topic FROM past_questions")
128
+ past_topics = [row[0] for row in cursor.fetchall()]
129
+
130
+ cursor.execute("SELECT topic FROM current_questions")
131
+ current_topics = [row[0] for row in cursor.fetchall()]
132
+
133
+ past_count = Counter(past_topics)
134
+ current_count = Counter(current_topics)
135
+
136
+ results = []
137
+
138
+ for topic, freq in past_count.items():
139
+ frequency_weight = freq
140
+ recency_weight = freq * 0.2
141
+ gap_weight = 1 if topic not in current_count else 0
142
+
143
+ prediction_score = (
144
+ frequency_weight * 0.5
145
+ + recency_weight * 0.3
146
+ + gap_weight * 0.2
147
+ )
148
+
149
+ results.append((topic, prediction_score))
150
+
151
+ results.sort(key=lambda x: x[1], reverse=True)
152
+
153
+ df = pd.DataFrame(results, columns=["Topic", "Prediction Score"])
154
+ return df
155
+
156
+
157
+ # ==============================
158
+ # HEATMAP VISUALIZATION
159
+ # ==============================
160
+
161
+ def generate_heatmap():
162
+ cursor.execute("SELECT topic FROM past_questions")
163
+ topics = [row[0] for row in cursor.fetchall()]
164
+ count = Counter(topics)
165
+
166
+ df = pd.DataFrame.from_dict(count, orient='index', columns=['Frequency'])
167
+ df = df.sort_values(by='Frequency', ascending=False)
168
+
169
+ plt.figure()
170
+ plt.imshow(np.array(df['Frequency']).reshape(-1,1))
171
+ plt.yticks(range(len(df.index)), df.index)
172
+ plt.xticks([])
173
+ plt.title("Topic Heatmap")
174
+ plt.colorbar()
175
+ plt.tight_layout()
176
+
177
+ return plt
178
+
179
+
180
+ # ==============================
181
+ # AI QUESTION GENERATOR (Simple)
182
+ # ==============================
183
+
184
+ def generate_predicted_questions():
185
+ df = analyze_trends()
186
+ top_topics = df["Topic"].head(3).tolist()
187
+
188
+ output = "Predicted High Probability Questions:\n\n"
189
+
190
+ for topic in top_topics:
191
+ output += f"โ€ข Create an exam-level question from {topic}\n"
192
+
193
+ return output
194
+
195
+
196
+ # ==============================
197
+ # GRADIO UI
198
+ # ==============================
199
+
200
+ with gr.Blocks() as demo:
201
+ gr.Markdown("# ๐ŸŽ“ AI Predictive Exam Intelligence System")
202
+
203
+ with gr.Tab("๐Ÿ“‚ Upload Past 5 Years Papers"):
204
+ past_files = gr.File(file_count="multiple")
205
+ past_button = gr.Button("Upload & Analyze")
206
+ past_output = gr.Textbox()
207
+
208
+ past_button.click(upload_past_papers, inputs=past_files, outputs=past_output)
209
+
210
+ with gr.Tab("๐Ÿ“„ Upload Current Year Paper"):
211
+ current_file = gr.File()
212
+ current_button = gr.Button("Upload Current Paper")
213
+ current_output = gr.Textbox()
214
+
215
+ current_button.click(upload_current_paper, inputs=current_file, outputs=current_output)
216
+
217
+ with gr.Tab("๐Ÿ“Š Trend Analysis"):
218
+ analyze_button = gr.Button("Run Trend Analysis")
219
+ trend_output = gr.Dataframe()
220
+
221
+ analyze_button.click(analyze_trends, outputs=trend_output)
222
+
223
+ with gr.Tab("๐Ÿ”ฅ Topic Heatmap"):
224
+ heatmap_button = gr.Button("Generate Heatmap")
225
+ heatmap_output = gr.Plot()
226
+
227
+ heatmap_button.click(generate_heatmap, outputs=heatmap_output)
228
+
229
+ with gr.Tab("๐Ÿ”ฎ Predicted Questions"):
230
+ predict_button = gr.Button("Generate Predictions")
231
+ predict_output = gr.Textbox()
232
+
233
+ predict_button.click(generate_predicted_questions, outputs=predict_output)
234
+
235
+
236
+ demo.launch()