Dannyar608 commited on
Commit
0e95f56
·
verified ·
1 Parent(s): 66cb301

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +651 -20
app.py CHANGED
@@ -1,16 +1,16 @@
1
- # ========== STARTUP DEPENDENCY CHECK ==========
2
  import sys
3
  import subprocess
4
  import importlib
5
 
6
- # List of required packages with their import names and pip names
7
  required_packages = {
8
- 'gradio': 'gradio',
9
  'pandas': 'pandas',
10
  'PyPDF2': 'PyPDF2',
11
  'transformers': 'transformers',
12
  'pdfplumber': 'pdfplumber',
13
- 'typing_extensions': 'typing_extensions' # Often needed for transformers
14
  }
15
 
16
  def check_and_install_packages():
@@ -24,20 +24,15 @@ def check_and_install_packages():
24
  if missing_packages:
25
  print(f"Missing packages: {', '.join(missing_packages)}")
26
  print("Attempting to install...")
27
-
28
  try:
29
  subprocess.check_call([sys.executable, "-m", "pip", "install", *missing_packages])
30
- print("Installation successful. Please restart the application.")
31
- sys.exit(0)
32
  except subprocess.CalledProcessError as e:
33
  print(f"Failed to install packages. Error: {e}")
34
- print("Please install them manually with:")
35
- print(f"pip install {' '.join(missing_packages)}")
36
- sys.exit(1)
37
 
38
  check_and_install_packages()
39
 
40
- # ========== MAIN IMPORTS (AFTER DEPENDENCY CHECK) ==========
41
  import gradio as gr
42
  import pandas as pd
43
  import json
@@ -49,14 +44,650 @@ from transformers import pipeline
49
  from typing import List, Dict, Union
50
  import pdfplumber
51
 
52
- [... REST OF YOUR ORIGINAL CODE REMAINS EXACTLY THE SAME ...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
 
54
  if __name__ == "__main__":
55
- # Check if running in a notebook environment
56
- try:
57
- from IPython import get_ipython
58
- if 'IPKernelApp' not in get_ipython().config:
59
- app.launch()
60
- except:
61
- app.launch()
62
-
 
1
+ # ========== DEPENDENCY MANAGEMENT ==========
2
  import sys
3
  import subprocess
4
  import importlib
5
 
6
+ # List of required packages
7
  required_packages = {
8
+ 'gradio': 'gradio>=3.0',
9
  'pandas': 'pandas',
10
  'PyPDF2': 'PyPDF2',
11
  'transformers': 'transformers',
12
  'pdfplumber': 'pdfplumber',
13
+ 'typing_extensions': 'typing_extensions'
14
  }
15
 
16
  def check_and_install_packages():
 
24
  if missing_packages:
25
  print(f"Missing packages: {', '.join(missing_packages)}")
26
  print("Attempting to install...")
 
27
  try:
28
  subprocess.check_call([sys.executable, "-m", "pip", "install", *missing_packages])
29
+ print("Installation successful.")
 
30
  except subprocess.CalledProcessError as e:
31
  print(f"Failed to install packages. Error: {e}")
 
 
 
32
 
33
  check_and_install_packages()
34
 
35
+ # ========== MAIN IMPORTS ==========
36
  import gradio as gr
37
  import pandas as pd
38
  import json
 
44
  from typing import List, Dict, Union
45
  import pdfplumber
46
 
47
+ # ========== TRANSCRIPT PARSING ==========
48
+ class UniversalTranscriptParser:
49
+ def __init__(self):
50
+ self.patterns = {
51
+ 'miami_dade': self._compile_miami_dade_patterns(),
52
+ 'homeschool': self._compile_homeschool_patterns(),
53
+ 'doral_academy': self._compile_doral_academy_patterns()
54
+ }
55
+
56
+ self.grade_level_map = {
57
+ '09': '9th Grade', '10': '10th Grade', '11': '11th Grade', '12': '12th Grade',
58
+ '07': '7th Grade', '08': '8th Grade', 'MA': 'Middle School'
59
+ }
60
+
61
+ def parse_transcript(self, text: str) -> Dict[str, Union[Dict, List[Dict]]]:
62
+ transcript_type = self._identify_transcript_type(text)
63
+
64
+ if transcript_type == 'homeschool':
65
+ return self._parse_homeschool(text)
66
+ elif transcript_type == 'doral_academy':
67
+ return self._parse_doral_academy(text)
68
+ else:
69
+ return self._parse_miami_dade(text)
70
+
71
+ def _identify_transcript_type(self, text: str) -> str:
72
+ if re.search(r'Sample OFFICIAL HIGH SCHOOL TRANSCRIPT', text):
73
+ return 'homeschool'
74
+ elif re.search(r'DORAL ACADEMY HIGH SCHOOL', text):
75
+ return 'doral_academy'
76
+ return 'miami_dade'
77
+
78
+ def _parse_homeschool(self, text: str) -> Dict[str, Union[Dict, List[Dict]]]:
79
+ courses = []
80
+ current_grade = None
81
+ current_year = None
82
+
83
+ student_info = {}
84
+ name_match = re.search(r'Student Name:\s*(.+)\s*SSN:', text)
85
+ if name_match:
86
+ student_info['name'] = name_match.group(1).strip()
87
+
88
+ for line in text.split('\n'):
89
+ grade_match = re.match(r'^\|?\s*(\d+th Grade)\s*\|.*(\d{4}-\d{4})', line)
90
+ if grade_match:
91
+ current_grade = grade_match.group(1)
92
+ current_year = grade_match.group(2)
93
+ continue
94
+
95
+ course_match = re.match(
96
+ r'^\|?\s*([^\|]+?)\s*\|\s*([A-Z][+*]?)\s*\|\s*([^\|]+)\s*\|\s*(\d+\.?\d*)\s*\|\s*(\d+)',
97
+ line
98
+ )
99
+
100
+ if course_match and current_grade:
101
+ course_name = course_match.group(1).strip()
102
+ course_name = re.sub(r'^\|?\s*', '', course_name)
103
+
104
+ courses.append({
105
+ 'name': course_name,
106
+ 'grade_level': current_grade,
107
+ 'school_year': current_year,
108
+ 'grade': course_match.group(2),
109
+ 'credit_type': course_match.group(3).strip(),
110
+ 'credits': float(course_match.group(4)),
111
+ 'quality_points': int(course_match.group(5)),
112
+ 'transcript_type': 'homeschool'
113
+ })
114
+
115
+ gpa_data = {}
116
+ gpa_match = re.search(r'Cum\. GPA\s*\|\s*([\d\.]+)', text)
117
+ if gpa_match:
118
+ gpa_data['unweighted'] = gpa_match.group(1)
119
+ gpa_data['weighted'] = gpa_match.group(1)
120
+
121
+ return {
122
+ 'student_info': student_info,
123
+ 'courses': {'All': courses},
124
+ 'gpa': gpa_data,
125
+ 'grade_level': current_grade.replace('th Grade', '') if current_grade else "Unknown"
126
+ }
127
+
128
+ def _parse_doral_academy(self, text: str) -> Dict[str, Union[Dict, List[Dict]]]:
129
+ courses = []
130
+ student_info = {}
131
+ name_match = re.search(r'LEGAL NAME:\s*([^\n]+)', text)
132
+ if name_match:
133
+ student_info['name'] = name_match.group(1).strip()
134
+
135
+ year_pattern = re.compile(r'YEAR:\s*(\d{4}-\d{4})\s*GRADE LEVEL:\s*(\d{2})', re.MULTILINE)
136
+ year_matches = year_pattern.finditer(text)
137
+
138
+ grade_year_map = {}
139
+ for match in year_matches:
140
+ grade_year_map[match.group(2)] = match.group(1)
141
+
142
+ course_pattern = re.compile(
143
+ r'(\d)\s+(\d{7})\s+([^\n]+?)\s+([A-Z]{2})\s+([A-Z])\s+([A-Z])\s+([A-Z])\s+(\d\.\d{2})\s+(\d\.\d{2})',
144
+ re.MULTILINE
145
+ )
146
+
147
+ courses_by_grade = defaultdict(list)
148
+ for match in course_pattern.finditer(text):
149
+ grade_level_num = match.group(1)
150
+ grade_level = self.grade_level_map.get(grade_level_num, f"Grade {grade_level_num}")
151
+ school_year = grade_year_map.get(grade_level_num, "Unknown")
152
+
153
+ course_info = {
154
+ 'course_code': match.group(2),
155
+ 'name': match.group(3).strip(),
156
+ 'subject_area': match.group(4),
157
+ 'grade': match.group(5),
158
+ 'inclusion_status': match.group(6),
159
+ 'credit_status': match.group(7),
160
+ 'credits_attempted': float(match.group(8)),
161
+ 'credits': float(match.group(9)),
162
+ 'grade_level': grade_level,
163
+ 'school_year': school_year,
164
+ 'transcript_type': 'doral_academy'
165
+ }
166
+
167
+ courses_by_grade[grade_level_num].append(course_info)
168
+
169
+ gpa_data = {}
170
+ unweighted_match = re.search(r'Un-weighted GPA\s*([\d\.]+)', text)
171
+ weighted_match = re.search(r'Weighted GPA\s*([\d\.]+)', text)
172
+
173
+ if unweighted_match:
174
+ gpa_data['unweighted'] = unweighted_match.group(1)
175
+ if weighted_match:
176
+ gpa_data['weighted'] = weighted_match.group(1)
177
+
178
+ grade_level = "12" if re.search(r'GRADE LEVEL:\s*12', text) else "Unknown"
179
+
180
+ return {
181
+ 'student_info': student_info,
182
+ 'courses': dict(courses_by_grade),
183
+ 'gpa': gpa_data,
184
+ 'grade_level': grade_level
185
+ }
186
+
187
+ def _parse_miami_dade(self, text: str) -> Dict[str, Union[Dict, List[Dict]]]:
188
+ courses = []
189
+ courses_by_grade = defaultdict(list)
190
+
191
+ student_info = {}
192
+ name_match = re.search(r'0783977 - ([^,]+),\s*([^\n]+)', text)
193
+ if name_match:
194
+ student_info['name'] = f"{name_match.group(2)} {name_match.group(1)}"
195
+
196
+ course_pattern = re.compile(
197
+ r'([A-Z]-[A-Za-z\s&]+)\s*\|\s*(\d{4}-\d{4})\s*\|\s*(\d{2})\s*\|\s*([A-Z0-9]+)\s*\|\s*([^\|]+)\s*\|\s*([^\|]+)\s*\|\s*([^\|]+)\s*\|\s*([A-Z]?)\s*\|\s*([A-Z]?)\s*\|\s*([^\|]+)',
198
+ re.MULTILINE
199
+ )
200
+
201
+ for match in course_pattern.finditer(text):
202
+ grade_level = self.grade_level_map.get(match.group(3), match.group(3))
203
+ credits = match.group(10).strip()
204
+
205
+ course_info = {
206
+ 'requirement_category': match.group(1).strip(),
207
+ 'school_year': match.group(2),
208
+ 'grade_level': grade_level if isinstance(grade_level, str) else f"Grade {match.group(3)}",
209
+ 'course_code': match.group(4).strip(),
210
+ 'name': match.group(5).strip(),
211
+ 'term': match.group(6).strip(),
212
+ 'district_number': match.group(7).strip(),
213
+ 'grade': match.group(8),
214
+ 'inclusion_status': match.group(9),
215
+ 'credits': 0.0 if 'inProgress' in credits else float(credits.replace(' ', '')),
216
+ 'transcript_type': 'miami_dade'
217
+ }
218
+
219
+ courses_by_grade[match.group(3)].append(course_info)
220
+
221
+ gpa_data = {
222
+ 'weighted': extract_gpa(text, 'Weighted GPA'),
223
+ 'unweighted': extract_gpa(text, 'Un-weighted GPA')
224
+ }
225
+
226
+ grade_level = re.search(r'Current Grade:\s*(\d+)', text).group(1) if re.search(r'Current Grade:\s*(\d+)', text) else "Unknown"
227
+
228
+ return {
229
+ 'student_info': student_info,
230
+ 'courses': dict(courses_by_grade),
231
+ 'gpa': gpa_data,
232
+ 'grade_level': grade_level
233
+ }
234
+
235
+ def _compile_miami_dade_patterns(self):
236
+ return {
237
+ 'student': re.compile(r'Current Grade:\s*(\d+).*YOG\s*(\d{4})'),
238
+ 'course': re.compile(
239
+ r'([A-Z]-[A-Za-z\s&]+)\s*\|\s*(\d{4}-\d{4})\s*\|\s*(\d{2})\s*\|\s*([A-Z0-9]+)\s*\|\s*([^\|]+)\s*\|\s*([^\|]+)\s*\|\s*([^\|]+)\s*\|\s*([A-Z]?)\s*\|\s*([A-Z]?)\s*\|\s*([^\|]+)',
240
+ re.MULTILINE
241
+ )
242
+ }
243
+
244
+ def _compile_homeschool_patterns(self):
245
+ return {
246
+ 'student': re.compile(r'Student Name:\s*(.+)\s*SSN:'),
247
+ 'course': re.compile(
248
+ r'^\|?\s*([^\|]+?)\s*\|\s*([A-Z][+*]?)\s*\|\s*([^\|]+)\s*\|\s*(\d+\.?\d*)\s*\|\s*(\d+)'
249
+ )
250
+ }
251
+
252
+ def _compile_doral_academy_patterns(self):
253
+ return {
254
+ 'student': re.compile(r'LEGAL NAME:\s*([^\n]+)'),
255
+ 'course': re.compile(
256
+ r'(\d)\s+(\d{7})\s+([^\n]+?)\s+([A-Z]{2})\s+([A-Z])\s+([A-Z])\s+([A-Z])\s+(\d\.\d{2})\s+(\d\.\d{2})',
257
+ re.MULTILINE
258
+ )
259
+ }
260
+
261
+ def extract_gpa(text, gpa_type):
262
+ pattern = rf'{gpa_type}\s*([\d\.]+)'
263
+ match = re.search(pattern, text)
264
+ return match.group(1) if match else "N/A"
265
+
266
+ def parse_transcript(file):
267
+ parser = UniversalTranscriptParser()
268
+
269
+ if file.name.endswith('.pdf'):
270
+ text = ''
271
+ with pdfplumber.open(file.name) as pdf:
272
+ for page in pdf.pages:
273
+ text += page.extract_text() or '' + '\n'
274
+
275
+ parsed_data = parser.parse_transcript(text)
276
+
277
+ output_text = f"Student Transcript Summary\n{'='*40}\n"
278
+
279
+ if 'student_info' in parsed_data and 'name' in parsed_data['student_info']:
280
+ output_text += f"Student: {parsed_data['student_info']['name']}\n"
281
+
282
+ output_text += f"Current Grade Level: {parsed_data.get('grade_level', 'Unknown')}\n"
283
+
284
+ if 'gpa' in parsed_data:
285
+ gpa = parsed_data['gpa']
286
+ output_text += f"Weighted GPA: {gpa.get('weighted', 'N/A')}\n"
287
+ output_text += f"Unweighted GPA: {gpa.get('unweighted', 'N/A')}\n\n"
288
+
289
+ output_text += "Course History:\n{'='*40}\n"
290
+
291
+ if 'courses' in parsed_data:
292
+ courses_by_grade = parsed_data['courses']
293
+
294
+ def grade_sort_key(grade):
295
+ try:
296
+ num = int(re.search(r'\d+', grade).group())
297
+ return num
298
+ except (AttributeError, ValueError):
299
+ return float('inf')
300
+
301
+ grades_sorted = sorted(courses_by_grade.keys(), key=grade_sort_key)
302
+
303
+ for grade in grades_sorted:
304
+ output_text += f"\nGrade {grade}:\n{'-'*30}\n"
305
+ for course in courses_by_grade[grade]:
306
+ output_text += f"- {course.get('name', 'Unnamed Course')}"
307
+ if 'grade' in course and course['grade']:
308
+ output_text += f" (Grade: {course['grade']})"
309
+ if 'credits' in course:
310
+ output_text += f" | Credits: {course['credits']}"
311
+ if 'school_year' in course:
312
+ output_text += f" | Year: {course['school_year']}"
313
+ output_text += "\n"
314
+
315
+ return output_text, parsed_data
316
+ else:
317
+ return "Unsupported file format (PDF only for transcript parsing)", None
318
+
319
+ def transcript_display(transcript_dict):
320
+ if not transcript_dict or "courses" not in transcript_dict:
321
+ return "No course information available"
322
+
323
+ display = "### Detailed Course History\n"
324
+ courses_by_grade = transcript_dict["courses"]
325
+
326
+ if isinstance(courses_by_grade, dict):
327
+ def grade_sort_key(grade):
328
+ try:
329
+ num = int(re.search(r'\d+', grade).group())
330
+ return num
331
+ except (AttributeError, ValueError):
332
+ return float('inf')
333
+
334
+ grades_sorted = sorted(courses_by_grade.keys(), key=grade_sort_key)
335
+
336
+ for grade in grades_sorted:
337
+ display += f"\n**Grade {grade}**\n"
338
+ for course in courses_by_grade[grade]:
339
+ display += f"- {course.get('name', 'Unnamed Course')}"
340
+ if 'grade' in course and course['grade']:
341
+ display += f" (Grade: {course['grade']})"
342
+ if 'credits' in course:
343
+ display += f" | Credits: {course['credits']}"
344
+ if 'school_year' in course:
345
+ display += f" | Year: {course['school_year']}"
346
+ display += "\n"
347
+
348
+ if 'gpa' in transcript_dict:
349
+ gpa = transcript_dict['gpa']
350
+ display += "\n**GPA Information**\n"
351
+ display += f"- Unweighted: {gpa.get('unweighted', 'N/A')}\n"
352
+ display += f"- Weighted: {gpa.get('weighted', 'N/A')}\n"
353
+
354
+ return display
355
+
356
+ # ========== LEARNING STYLE QUIZ ==========
357
+ learning_style_questions = [
358
+ "When you study for a test, you prefer to:",
359
+ "When you need directions to a new place, you prefer:",
360
+ "When you learn a new skill, you prefer to:",
361
+ "When you're trying to concentrate, you:",
362
+ "When you meet new people, you remember them by:",
363
+ "When you're assembling furniture or a gadget, you:",
364
+ "When choosing a restaurant, you rely most on:",
365
+ "When you're in a waiting room, you typically:",
366
+ "When giving someone instructions, you tend to:",
367
+ "When you're trying to recall information, you:",
368
+ "When you're at a museum or exhibit, you:",
369
+ "When you're learning a new language, you prefer:",
370
+ "When you're taking notes in class, you:",
371
+ "When you're explaining something complex, you:",
372
+ "When you're at a party, you enjoy:",
373
+ "When you're trying to remember a phone number, you:",
374
+ "When you're relaxing, you prefer to:",
375
+ "When you're learning to use new software, you:",
376
+ "When you're giving a presentation, you rely on:",
377
+ "When you're solving a difficult problem, you:"
378
+ ]
379
+
380
+ learning_style_options = [
381
+ ["Read the textbook (Reading/Writing)", "Listen to lectures (Auditory)", "Use diagrams/charts (Visual)", "Practice problems (Kinesthetic)"],
382
+ ["Look at a map (Visual)", "Have someone tell you (Auditory)", "Write down directions (Reading/Writing)", "Try walking/driving there (Kinesthetic)"],
383
+ ["Read instructions (Reading/Writing)", "Have someone show you (Visual)", "Listen to explanations (Auditory)", "Try it yourself (Kinesthetic)"],
384
+ ["Need quiet (Reading/Writing)", "Need background noise (Auditory)", "Need to move around (Kinesthetic)", "Need visual stimulation (Visual)"],
385
+ ["Their face (Visual)", "Their name (Auditory)", "What you talked about (Reading/Writing)", "What you did together (Kinesthetic)"],
386
+ ["Read the instructions carefully (Reading/Writing)", "Look at the diagrams (Visual)", "Ask someone to explain (Auditory)", "Start putting pieces together (Kinesthetic)"],
387
+ ["Online photos of the food (Visual)", "Recommendations from friends (Auditory)", "Reading the menu online (Reading/Writing)", "Remembering how it felt to eat there (Kinesthetic)"],
388
+ ["Read magazines (Reading/Writing)", "Listen to music (Auditory)", "Watch TV (Visual)", "Fidget or move around (Kinesthetic)"],
389
+ ["Write them down (Reading/Writing)", "Explain verbally (Auditory)", "Demonstrate (Visual)", "Guide them physically (Kinesthetic)"],
390
+ ["See written words in your mind (Visual)", "Hear the information in your head (Auditory)", "Write it down to remember (Reading/Writing)", "Associate it with physical actions (Kinesthetic)"],
391
+ ["Read all the descriptions (Reading/Writing)", "Listen to audio guides (Auditory)", "Look at the displays (Visual)", "Touch interactive exhibits (Kinesthetic)"],
392
+ ["Study grammar rules (Reading/Writing)", "Listen to native speakers (Auditory)", "Use flashcards with images (Visual)", "Practice conversations (Kinesthetic)"],
393
+ ["Write detailed paragraphs (Reading/Writing)", "Record the lecture (Auditory)", "Draw diagrams and charts (Visual)", "Doodle while listening (Kinesthetic)"],
394
+ ["Write detailed steps (Reading/Writing)", "Explain verbally with examples (Auditory)", "Draw diagrams (Visual)", "Use physical objects to demonstrate (Kinesthetic)"],
395
+ ["Conversations with people (Auditory)", "Watching others or the environment (Visual)", "Writing notes or texting (Reading/Writing)", "Dancing or physical activities (Kinesthetic)"],
396
+ ["See the numbers in your mind (Visual)", "Say them aloud (Auditory)", "Write them down (Reading/Writing)", "Dial them on a keypad (Kinesthetic)"],
397
+ ["Read a book (Reading/Writing)", "Listen to music (Auditory)", "Watch TV/movies (Visual)", "Do something physical (Kinesthetic)"],
398
+ ["Read the manual (Reading/Writing)", "Ask someone to show you (Visual)", "Call tech support (Auditory)", "Experiment with the software (Kinesthetic)"],
399
+ ["Detailed notes (Reading/Writing)", "Verbal explanations (Auditory)", "Visual slides (Visual)", "Physical demonstrations (Kinesthetic)"],
400
+ ["Write out possible solutions (Reading/Writing)", "Talk through it with someone (Auditory)", "Draw diagrams (Visual)", "Build a model or prototype (Kinesthetic)"]
401
+ ]
402
+
403
+ def learning_style_quiz(*answers):
404
+ scores = {
405
+ "Visual": 0,
406
+ "Auditory": 0,
407
+ "Reading/Writing": 0,
408
+ "Kinesthetic": 0
409
+ }
410
+
411
+ for i, answer in enumerate(answers):
412
+ if answer == learning_style_options[i][0]:
413
+ scores["Reading/Writing"] += 1
414
+ elif answer == learning_style_options[i][1]:
415
+ scores["Auditory"] += 1
416
+ elif answer == learning_style_options[i][2]:
417
+ scores["Visual"] += 1
418
+ elif answer == learning_style_options[i][3]:
419
+ scores["Kinesthetic"] += 1
420
+
421
+ max_score = max(scores.values())
422
+ total_questions = len(learning_style_questions)
423
+
424
+ percentages = {style: (score/total_questions)*100 for style, score in scores.items()}
425
+
426
+ sorted_styles = sorted(scores.items(), key=lambda x: x[1], reverse=True)
427
+
428
+ result = "Your Learning Style Results:\n\n"
429
+ for style, score in sorted_styles:
430
+ result += f"{style}: {score}/{total_questions} ({percentages[style]:.1f}%)\n"
431
+
432
+ result += "\n"
433
+
434
+ primary_styles = [style for style, score in scores.items() if score == max_score]
435
+
436
+ if len(primary_styles) == 1:
437
+ result += f"Your primary learning style is: {primary_styles[0]}\n\n"
438
+ if primary_styles[0] == "Visual":
439
+ result += "Tips for Visual Learners:\n"
440
+ result += "- Use color coding in your notes\n"
441
+ result += "- Create mind maps and diagrams\n"
442
+ result += "- Watch educational videos\n"
443
+ result += "- Use flashcards with images\n"
444
+ elif primary_styles[0] == "Auditory":
445
+ result += "Tips for Auditory Learners:\n"
446
+ result += "- Record lectures and listen to them\n"
447
+ result += "- Participate in study groups\n"
448
+ result += "- Explain concepts out loud to yourself\n"
449
+ result += "- Use rhymes or songs to remember information\n"
450
+ elif primary_styles[0] == "Reading/Writing":
451
+ result += "Tips for Reading/Writing Learners:\n"
452
+ result += "- Write detailed notes\n"
453
+ result += "- Create summaries in your own words\n"
454
+ result += "- Read textbooks and articles\n"
455
+ result += "- Make lists to organize information\n"
456
+ else:
457
+ result += "Tips for Kinesthetic Learners:\n"
458
+ result += "- Use hands-on activities\n"
459
+ result += "- Take frequent movement breaks\n"
460
+ result += "- Create physical models\n"
461
+ result += "- Associate information with physical actions\n"
462
+ else:
463
+ result += f"You have multiple strong learning styles: {', '.join(primary_styles)}\n\n"
464
+ result += "You may benefit from combining different learning approaches.\n"
465
+
466
+ return result
467
+
468
+ # ========== SAVE STUDENT PROFILE ==========
469
+ def save_profile(name, age, interests, transcript, learning_style,
470
+ movie, movie_reason, show, show_reason,
471
+ book, book_reason, character, character_reason, blog):
472
+ age = int(age) if age else 0
473
+
474
+ favorites = {
475
+ "movie": movie,
476
+ "movie_reason": movie_reason,
477
+ "show": show,
478
+ "show_reason": show_reason,
479
+ "book": book,
480
+ "book_reason": book_reason,
481
+ "character": character,
482
+ "character_reason": character_reason
483
+ }
484
+
485
+ data = {
486
+ "name": name,
487
+ "age": age,
488
+ "interests": interests,
489
+ "transcript": transcript,
490
+ "learning_style": learning_style,
491
+ "favorites": favorites,
492
+ "blog": blog
493
+ }
494
+
495
+ os.makedirs("student_profiles", exist_ok=True)
496
+ json_path = os.path.join("student_profiles", f"{name.replace(' ', '_')}_profile.json")
497
+ with open(json_path, "w") as f:
498
+ json.dump(data, f, indent=2)
499
+
500
+ markdown_summary = f"""### Student Profile: {name}
501
+ **Age:** {age}
502
+ **Interests:** {interests}
503
+ **Learning Style:** {learning_style}
504
+ #### Transcript:
505
+ {transcript_display(transcript)}
506
+ #### Favorites:
507
+ - Movie: {favorites['movie']} ({favorites['movie_reason']})
508
+ - Show: {favorites['show']} ({favorites['show_reason']})
509
+ - Book: {favorites['book']} ({favorites['book_reason']})
510
+ - Character: {favorites['character']} ({favorites['character_reason']})
511
+ #### Blog:
512
+ {blog if blog else "_No blog provided_"}
513
+ """
514
+ return markdown_summary
515
+
516
+ # ========== AI TEACHING ASSISTANT ==========
517
+ def load_profile():
518
+ if not os.path.exists("student_profiles"):
519
+ return {}
520
+ files = [f for f in os.listdir("student_profiles") if f.endswith('.json')]
521
+ if files:
522
+ with open(os.path.join("student_profiles", files[0]), "r") as f:
523
+ return json.load(f)
524
+ return {}
525
+
526
+ def generate_response(message, history):
527
+ profile = load_profile()
528
+ if not profile:
529
+ return "Please complete and save your profile first using the previous tabs."
530
+
531
+ learning_style = profile.get("learning_style", "")
532
+ grade_level = profile.get("transcript", {}).get("grade_level", "unknown")
533
+ gpa = profile.get("transcript", {}).get("gpa", {})
534
+ interests = profile.get("interests", "")
535
+ courses = profile.get("transcript", {}).get("courses", {})
536
+
537
+ greetings = ["hi", "hello", "hey"]
538
+ study_help = ["study", "learn", "prepare", "exam"]
539
+ grade_help = ["grade", "gpa", "score"]
540
+ interest_help = ["interest", "hobby", "passion"]
541
+ course_help = ["courses", "classes", "transcript", "schedule"]
542
+
543
+ if any(greet in message.lower() for greet in greetings):
544
+ return f"Hello {profile.get('name', 'there')}! How can I help you today?"
545
+
546
+ elif any(word in message.lower() for word in study_help):
547
+ if "Visual" in learning_style:
548
+ response = ("Based on your visual learning style, I recommend:\n"
549
+ "- Creating mind maps or diagrams\n"
550
+ "- Using color-coded notes\n"
551
+ "- Watching educational videos")
552
+ elif "Auditory" in learning_style:
553
+ response = ("Based on your auditory learning style, I recommend:\n"
554
+ "- Recording lectures and listening to them\n"
555
+ "- Participating in study groups\n"
556
+ "- Explaining concepts out loud")
557
+ elif "Reading/Writing" in learning_style:
558
+ response = ("Based on your reading/writing learning style, I recommend:\n"
559
+ "- Writing detailed notes\n"
560
+ "- Creating summaries in your own words\n"
561
+ "- Reading textbooks and articles")
562
+ elif "Kinesthetic" in learning_style:
563
+ response = ("Based on your kinesthetic learning style, I recommend:\n"
564
+ "- Hands-on practice\n"
565
+ "- Creating physical models\n"
566
+ "- Taking frequent movement breaks")
567
+ else:
568
+ response = ("Here are some general study tips:\n"
569
+ "- Break study sessions into 25-minute chunks\n"
570
+ "- Review material regularly\n"
571
+ "- Teach concepts to someone else")
572
+
573
+ return response
574
+
575
+ elif any(word in message.lower() for word in grade_help):
576
+ return (f"Your GPA information:\n"
577
+ f"- Unweighted: {gpa.get('unweighted', 'N/A')}\n"
578
+ f"- Weighted: {gpa.get('weighted', 'N/A')}\n\n"
579
+ "To improve your grades, try:\n"
580
+ "- Setting specific goals\n"
581
+ "- Meeting with teachers\n"
582
+ "- Developing a study schedule")
583
+
584
+ elif any(word in message.lower() for word in interest_help):
585
+ return (f"I see you're interested in: {interests}\n\n"
586
+ "You might want to:\n"
587
+ "- Find clubs or activities related to these interests\n"
588
+ "- Explore career paths that align with them")
589
+
590
+ elif any(word in message.lower() for word in course_help):
591
+ response = "Here's a summary of your courses:\n"
592
+ if isinstance(courses, dict):
593
+ def grade_sort_key(grade):
594
+ try:
595
+ num = int(re.search(r'\d+', grade).group())
596
+ return num
597
+ except (AttributeError, ValueError):
598
+ return float('inf')
599
+
600
+ grades_sorted = sorted(courses.keys(), key=grade_sort_key)
601
+
602
+ for grade in grades_sorted:
603
+ response += f"\nGrade {grade}:\n"
604
+ for course in courses[grade]:
605
+ response += f"- {course.get('name', 'Unnamed Course')}"
606
+ if 'grade' in course:
607
+ response += f" (Grade: {course['grade']})"
608
+ response += "\n"
609
+ else:
610
+ response += "No detailed course information available."
611
+ return response
612
+
613
+ elif "help" in message.lower():
614
+ return ("I can help with:\n"
615
+ "- Study tips based on your learning style\n"
616
+ "- GPA and grade information\n"
617
+ "- Course history and schedules\n"
618
+ "- General academic advice\n\n"
619
+ "Try asking about study strategies or your grades!")
620
+
621
+ else:
622
+ return ("I'm your personalized teaching assistant. "
623
+ "I can help with study tips, grade information, and academic advice. "
624
+ "Try asking about how to study for your classes!")
625
+
626
+ # ========== GRADIO INTERFACE ==========
627
+ with gr.Blocks() as app:
628
+ with gr.Tab("Step 1: Upload Transcript"):
629
+ gr.Markdown("### Upload your transcript (PDF recommended)")
630
+ transcript_file = gr.File(label="Transcript file", file_types=[".pdf"])
631
+ transcript_output = gr.Textbox(label="Transcript Results", lines=20)
632
+ transcript_data = gr.State()
633
+ transcript_file.change(
634
+ fn=parse_transcript,
635
+ inputs=transcript_file,
636
+ outputs=[transcript_output, transcript_data]
637
+ )
638
+
639
+ with gr.Tab("Step 2: Learning Style Quiz"):
640
+ gr.Markdown("### Learning Style Quiz (20 Questions)")
641
+ quiz_components = []
642
+ for i, (question, options) in enumerate(zip(learning_style_questions, learning_style_options)):
643
+ quiz_components.append(gr.Radio(options, label=f"{i+1}. {question}"))
644
+
645
+ learning_output = gr.Textbox(label="Your Learning Style", lines=15)
646
+ gr.Button("Submit Quiz").click(
647
+ fn=learning_style_quiz,
648
+ inputs=quiz_components,
649
+ outputs=learning_output
650
+ )
651
+
652
+ with gr.Tab("Step 3: Personal Questions"):
653
+ name = gr.Textbox(label="What's your name?")
654
+ age = gr.Number(label="How old are you?", precision=0)
655
+ interests = gr.Textbox(label="What are your interests?")
656
+ movie = gr.Textbox(label="Favorite movie?")
657
+ movie_reason = gr.Textbox(label="Why do you like that movie?")
658
+ show = gr.Textbox(label="Favorite TV show?")
659
+ show_reason = gr.Textbox(label="Why do you like that show?")
660
+ book = gr.Textbox(label="Favorite book?")
661
+ book_reason = gr.Textbox(label="Why do you like that book?")
662
+ character = gr.Textbox(label="Favorite character?")
663
+ character_reason = gr.Textbox(label="Why do you like that character?")
664
+ blog_checkbox = gr.Checkbox(label="Do you want to write a blog?", value=False)
665
+ blog_text = gr.Textbox(label="Write your blog here", visible=False, lines=5)
666
+ blog_checkbox.change(lambda x: gr.update(visible=x), inputs=blog_checkbox, outputs=blog_text)
667
+
668
+ with gr.Tab("Step 4: Save & Review"):
669
+ output_summary = gr.Markdown()
670
+ save_btn = gr.Button("Save Profile")
671
+ save_btn.click(
672
+ fn=save_profile,
673
+ inputs=[name, age, interests, transcript_data, learning_output,
674
+ movie, movie_reason, show, show_reason,
675
+ book, book_reason, character, character_reason, blog_text],
676
+ outputs=output_summary
677
+ )
678
+
679
+ with gr.Tab("🤖 AI Teaching Assistant"):
680
+ gr.Markdown("## Your Personalized Learning Assistant")
681
+ chatbot = gr.ChatInterface(
682
+ fn=generate_response,
683
+ examples=[
684
+ "How should I study for my next test?",
685
+ "What's my GPA information?",
686
+ "Show me my course history",
687
+ "How can I improve my grades?"
688
+ ]
689
+ )
690
 
691
+ # ========== RUN APPLICATION ==========
692
  if __name__ == "__main__":
693
+ app.launch()