Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,48 +5,14 @@ import os
|
|
| 5 |
import re
|
| 6 |
from PyPDF2 import PdfReader
|
| 7 |
from collections import defaultdict
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
grade_level_pattern = r"(Grade|Year)\s*[:]?\s*(\d+|Freshman|Sophomore|Junior|Senior)"
|
| 12 |
-
grade_match = re.search(grade_level_pattern, text, re.IGNORECASE)
|
| 13 |
-
current_grade_level = grade_match.group(2) if grade_match else "Unknown"
|
| 14 |
-
|
| 15 |
-
course_pattern = r"""
|
| 16 |
-
(?:^|\n)
|
| 17 |
-
(?: (Grade|Year)\s*[:]?\s*(\d+|Freshman|Sophomore|Junior|Senior)\s*[\n-]* )?
|
| 18 |
-
(
|
| 19 |
-
(?:[A-Z]{2,}\s?\d{3})
|
| 20 |
-
|
|
| 21 |
-
[A-Z][a-z]+(?:\s[A-Z][a-z]+)*
|
| 22 |
-
)
|
| 23 |
-
\s*
|
| 24 |
-
(?: [:\-]?\s* ([A-F][+-]?|\d{2,3}%)? )?
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
courses_by_grade = defaultdict(list)
|
| 28 |
-
current_grade = current_grade_level
|
| 29 |
-
|
| 30 |
-
for match in re.finditer(course_pattern, text, re.VERBOSE | re.MULTILINE):
|
| 31 |
-
grade_context, grade_level, course, grade = match.groups()
|
| 32 |
-
|
| 33 |
-
if grade_context:
|
| 34 |
-
current_grade = grade_level
|
| 35 |
-
|
| 36 |
-
if course:
|
| 37 |
-
course_info = {"course": course.strip()}
|
| 38 |
-
if grade:
|
| 39 |
-
course_info["grade"] = grade.strip()
|
| 40 |
-
courses_by_grade[current_grade].append(course_info)
|
| 41 |
-
|
| 42 |
-
return dict(courses_by_grade)
|
| 43 |
|
|
|
|
| 44 |
def parse_transcript(file):
|
| 45 |
-
if file.name.endswith('.
|
| 46 |
-
df = pd.read_csv(file)
|
| 47 |
-
elif file.name.endswith('.xlsx'):
|
| 48 |
-
df = pd.read_excel(file)
|
| 49 |
-
elif file.name.endswith('.pdf'):
|
| 50 |
text = ''
|
| 51 |
reader = PdfReader(file)
|
| 52 |
for page in reader.pages:
|
|
@@ -58,17 +24,16 @@ def parse_transcript(file):
|
|
| 58 |
grade_match = re.search(r'(Grade|Year)[\s:]*(\d+|Freshman|Sophomore|Junior|Senior)', text, re.IGNORECASE)
|
| 59 |
grade_level = grade_match.group(2) if grade_match else "Unknown"
|
| 60 |
|
| 61 |
-
#
|
| 62 |
gpa_data = {'weighted': "N/A", 'unweighted': "N/A"}
|
| 63 |
gpa_patterns = [
|
| 64 |
r'Weighted GPA[\s:]*(\d\.\d{1,2})',
|
| 65 |
r'GPA \(Weighted\)[\s:]*(\d\.\d{1,2})',
|
| 66 |
-
r'Cumulative GPA \(Weighted\)[\s:]*(\d\.\d{1,2})',
|
| 67 |
r'Unweighted GPA[\s:]*(\d\.\d{1,2})',
|
| 68 |
r'GPA \(Unweighted\)[\s:]*(\d\.\d{1,2})',
|
| 69 |
-
r'Cumulative GPA \(Unweighted\)[\s:]*(\d\.\d{1,2})',
|
| 70 |
r'GPA[\s:]*(\d\.\d{1,2})'
|
| 71 |
]
|
|
|
|
| 72 |
for pattern in gpa_patterns:
|
| 73 |
for match in re.finditer(pattern, text, re.IGNORECASE):
|
| 74 |
gpa_value = match.group(1)
|
|
@@ -82,8 +47,6 @@ def parse_transcript(file):
|
|
| 82 |
if gpa_data['weighted'] == "N/A":
|
| 83 |
gpa_data['weighted'] = gpa_value
|
| 84 |
|
| 85 |
-
courses_by_grade = extract_courses_with_grade_levels(text)
|
| 86 |
-
|
| 87 |
output_text = f"Grade Level: {grade_level}\n\n"
|
| 88 |
if gpa_data['weighted'] != "N/A" or gpa_data['unweighted'] != "N/A":
|
| 89 |
output_text += "GPA Information:\n"
|
|
@@ -94,43 +57,12 @@ def parse_transcript(file):
|
|
| 94 |
else:
|
| 95 |
output_text += "No GPA information found\n"
|
| 96 |
|
| 97 |
-
output_text += "\n(Courses not shown here)"
|
| 98 |
-
|
| 99 |
return output_text, {
|
| 100 |
"gpa": gpa_data,
|
| 101 |
-
"grade_level": grade_level
|
| 102 |
-
"courses": courses_by_grade
|
| 103 |
}
|
| 104 |
else:
|
| 105 |
-
return "
|
| 106 |
-
|
| 107 |
-
# For CSV/XLSX fallback
|
| 108 |
-
gpa = "N/A"
|
| 109 |
-
for col in ['GPA', 'Grade Point Average', 'Cumulative GPA']:
|
| 110 |
-
if col in df.columns:
|
| 111 |
-
gpa = df[col].iloc[0] if isinstance(df[col].iloc[0], (float, int)) else "N/A"
|
| 112 |
-
break
|
| 113 |
-
|
| 114 |
-
grade_level = "N/A"
|
| 115 |
-
for col in ['Grade Level', 'Grade', 'Class', 'Year']:
|
| 116 |
-
if col in df.columns:
|
| 117 |
-
grade_level = df[col].iloc[0]
|
| 118 |
-
break
|
| 119 |
-
|
| 120 |
-
courses = []
|
| 121 |
-
for col in ['Course', 'Subject', 'Course Name', 'Class']:
|
| 122 |
-
if col in df.columns:
|
| 123 |
-
courses = df[col].tolist()
|
| 124 |
-
break
|
| 125 |
-
|
| 126 |
-
output_text = f"Grade Level: {grade_level}\nGPA: {gpa}\n\nCourses:\n"
|
| 127 |
-
output_text += "\n".join(f"- {course}" for course in courses)
|
| 128 |
-
|
| 129 |
-
return output_text, {
|
| 130 |
-
"gpa": {"unweighted": gpa, "weighted": "N/A"},
|
| 131 |
-
"grade_level": grade_level,
|
| 132 |
-
"courses": courses
|
| 133 |
-
}
|
| 134 |
|
| 135 |
# ========== LEARNING STYLE QUIZ ==========
|
| 136 |
learning_style_questions = [
|
|
@@ -138,22 +70,7 @@ learning_style_questions = [
|
|
| 138 |
"When you need directions to a new place, you prefer:",
|
| 139 |
"When you learn a new skill, you prefer to:",
|
| 140 |
"When you're trying to concentrate, you:",
|
| 141 |
-
"When you meet new people, you remember them by:"
|
| 142 |
-
"When you're relaxing, you prefer to:",
|
| 143 |
-
"When you're explaining something to someone, you:",
|
| 144 |
-
"When you're trying to remember something, you:",
|
| 145 |
-
"When you're in a classroom, you learn best when:",
|
| 146 |
-
"When you're trying to solve a problem, you:",
|
| 147 |
-
"When you're taking notes, you:",
|
| 148 |
-
"When you're learning new software, you prefer to:",
|
| 149 |
-
"When you're at a museum, you spend the most time:",
|
| 150 |
-
"When you're assembling furniture, you:",
|
| 151 |
-
"When you're learning new vocabulary, you:",
|
| 152 |
-
"When you're giving a presentation, you prefer:",
|
| 153 |
-
"When you're at a party, you enjoy:",
|
| 154 |
-
"When you're taking a break from studying, you:",
|
| 155 |
-
"When you're learning dance moves, you:",
|
| 156 |
-
"When you're choosing a book, you prefer:"
|
| 157 |
]
|
| 158 |
|
| 159 |
learning_style_options = [
|
|
@@ -161,22 +78,7 @@ learning_style_options = [
|
|
| 161 |
["Look at a map (Visual)", "Have someone tell you (Auditory)", "Write down directions (Reading/Writing)", "Try walking/driving there (Kinesthetic)"],
|
| 162 |
["Read instructions (Reading/Writing)", "Have someone show you (Visual)", "Listen to explanations (Auditory)", "Try it yourself (Kinesthetic)"],
|
| 163 |
["Need quiet (Reading/Writing)", "Need background noise (Auditory)", "Need to move around (Kinesthetic)", "Need visual stimulation (Visual)"],
|
| 164 |
-
["Their face (Visual)", "Their name (Auditory)", "What you talked about (Reading/Writing)", "What you did together (Kinesthetic)"]
|
| 165 |
-
["Read (Reading/Writing)", "Listen to music (Auditory)", "Watch TV (Visual)", "Do something active (Kinesthetic)"],
|
| 166 |
-
["Write it down (Reading/Writing)", "Tell them verbally (Auditory)", "Show them (Visual)", "Demonstrate physically (Kinesthetic)"],
|
| 167 |
-
["See it written down (Visual)", "Say it out loud (Auditory)", "Write it down (Reading/Writing)", "Do it physically (Kinesthetic)"],
|
| 168 |
-
["Reading materials (Reading/Writing)", "Listening to lectures (Auditory)", "Seeing diagrams (Visual)", "Doing hands-on activities (Kinesthetic)"],
|
| 169 |
-
["Write down steps (Reading/Writing)", "Talk through it (Auditory)", "Draw diagrams (Visual)", "Try different approaches (Kinesthetic)"],
|
| 170 |
-
["Write detailed notes (Reading/Writing)", "Record lectures (Auditory)", "Draw mind maps (Visual)", "Take minimal notes (Kinesthetic)"],
|
| 171 |
-
["Read the manual (Reading/Writing)", "Have someone explain it (Auditory)", "Watch tutorial videos (Visual)", "Just start using it (Kinesthetic)"],
|
| 172 |
-
["Reading descriptions (Reading/Writing)", "Listening to audio guides (Auditory)", "Looking at exhibits (Visual)", "Interactive displays (Kinesthetic)"],
|
| 173 |
-
["Read instructions first (Reading/Writing)", "Ask someone to help (Auditory)", "Look at diagrams (Visual)", "Start assembling (Kinesthetic)"],
|
| 174 |
-
["Write them repeatedly (Reading/Writing)", "Say them repeatedly (Auditory)", "Use flashcards (Visual)", "Use them in conversation (Kinesthetic)"],
|
| 175 |
-
["Having detailed notes (Reading/Writing)", "Speaking freely (Auditory)", "Using visual aids (Visual)", "Demonstrating something (Kinesthetic)"],
|
| 176 |
-
["Conversations (Auditory)", "People-watching (Visual)", "Dancing/games (Kinesthetic)", "Reading about people (Reading/Writing)"],
|
| 177 |
-
["Read for fun (Reading/Writing)", "Listen to music (Auditory)", "Watch videos (Visual)", "Exercise (Kinesthetic)"],
|
| 178 |
-
["Watch demonstrations (Visual)", "Listen to instructions (Auditory)", "Read choreography (Reading/Writing)", "Try the moves (Kinesthetic)"],
|
| 179 |
-
["Text-heavy books (Reading/Writing)", "Audiobooks (Auditory)", "Books with pictures (Visual)", "Interactive books (Kinesthetic)"]
|
| 180 |
]
|
| 181 |
|
| 182 |
def learning_style_quiz(*answers):
|
|
@@ -187,35 +89,25 @@ def learning_style_quiz(*answers):
|
|
| 187 |
"Kinesthetic": 0
|
| 188 |
}
|
| 189 |
|
| 190 |
-
# Map each answer to a learning style
|
| 191 |
for i, answer in enumerate(answers):
|
| 192 |
-
if answer
|
| 193 |
scores["Reading/Writing"] += 1
|
| 194 |
-
elif answer
|
| 195 |
scores["Auditory"] += 1
|
| 196 |
-
elif answer
|
| 197 |
scores["Visual"] += 1
|
| 198 |
-
elif answer
|
| 199 |
scores["Kinesthetic"] += 1
|
| 200 |
|
| 201 |
-
# Get the highest score(s)
|
| 202 |
max_score = max(scores.values())
|
| 203 |
dominant_styles = [style for style, score in scores.items() if score == max_score]
|
| 204 |
|
| 205 |
-
# Generate result
|
| 206 |
if len(dominant_styles) == 1:
|
| 207 |
-
|
| 208 |
else:
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
# Add detailed breakdown
|
| 212 |
-
result += "\n\nDetailed Scores:\n"
|
| 213 |
-
for style, score in sorted(scores.items(), key=lambda x: x[1], reverse=True):
|
| 214 |
-
result += f"{style}: {score}/20\n"
|
| 215 |
-
|
| 216 |
-
return result
|
| 217 |
|
| 218 |
-
# ==========
|
| 219 |
def save_profile(name, age, interests, transcript, learning_style, favorites, blog):
|
| 220 |
data = {
|
| 221 |
"name": name,
|
|
@@ -230,56 +122,66 @@ def save_profile(name, age, interests, transcript, learning_style, favorites, bl
|
|
| 230 |
json_path = os.path.join("student_profiles", f"{name.replace(' ', '_')}_profile.json")
|
| 231 |
with open(json_path, "w") as f:
|
| 232 |
json.dump(data, f, indent=2)
|
| 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 |
# ========== GRADIO INTERFACE ==========
|
| 267 |
with gr.Blocks() as app:
|
|
|
|
| 268 |
with gr.Tab("Step 1: Upload Transcript"):
|
| 269 |
-
transcript_file = gr.File(label="Upload your transcript (
|
| 270 |
transcript_output = gr.Textbox(label="Transcript Output")
|
| 271 |
transcript_data = gr.State()
|
| 272 |
-
transcript_file.change(
|
| 273 |
|
| 274 |
with gr.Tab("Step 2: Learning Style Quiz"):
|
| 275 |
-
gr.Markdown("### Complete this 20-question quiz to determine your learning style")
|
| 276 |
quiz_components = []
|
| 277 |
for i, (question, options) in enumerate(zip(learning_style_questions, learning_style_options)):
|
| 278 |
-
quiz_components.append(
|
| 279 |
-
gr.Radio(choices=options, label=f"{i+1}. {question}")
|
| 280 |
-
)
|
| 281 |
|
| 282 |
-
learning_output = gr.Textbox(label="Learning Style Result"
|
| 283 |
gr.Button("Submit Quiz").click(
|
| 284 |
learning_style_quiz,
|
| 285 |
inputs=quiz_components,
|
|
@@ -300,141 +202,34 @@ with gr.Blocks() as app:
|
|
| 300 |
character_reason = gr.Textbox(label="Why do you like that character?")
|
| 301 |
blog_checkbox = gr.Checkbox(label="Do you want to write a blog?", value=False)
|
| 302 |
blog_text = gr.Textbox(label="Write your blog here", visible=False, lines=5)
|
| 303 |
-
blog_checkbox.change(
|
| 304 |
|
| 305 |
-
with gr.Tab("Step 4: Save
|
| 306 |
-
output_summary = gr.Markdown()
|
| 307 |
save_btn = gr.Button("Save Profile")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
-
|
| 310 |
-
book, book_reason, character, character_reason, blog, transcript, learning_style):
|
| 311 |
-
favorites = {
|
| 312 |
-
"movie": movie,
|
| 313 |
-
"movie_reason": movie_reason,
|
| 314 |
-
"show": show,
|
| 315 |
-
"show_reason": show_reason,
|
| 316 |
-
"book": book,
|
| 317 |
-
"book_reason": book_reason,
|
| 318 |
-
"character": character,
|
| 319 |
-
"character_reason": character_reason,
|
| 320 |
-
}
|
| 321 |
-
return save_profile(name, age, interests, transcript, learning_style, favorites, blog)
|
| 322 |
-
|
| 323 |
-
save_btn.click(fn=gather_and_save,
|
| 324 |
-
inputs=[name, age, interests, movie, movie_reason, show, show_reason,
|
| 325 |
-
book, book_reason, character, character_reason, blog_text,
|
| 326 |
-
transcript_data, learning_output],
|
| 327 |
-
outputs=output_summary)
|
| 328 |
-
# Add these new imports at the top
|
| 329 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 330 |
-
import torch
|
| 331 |
-
from openai import OpenAI # Make sure to install with pip install openai
|
| 332 |
-
|
| 333 |
-
# ========== AI CHATBOT SETUP ==========
|
| 334 |
-
# Initialize DeepSeek model for information retrieval
|
| 335 |
-
deepseek_model_name = "deepseek-ai/deepseek-llm-7b"
|
| 336 |
-
deepseek_tokenizer = AutoTokenizer.from_pretrained(deepseek_model_name)
|
| 337 |
-
deepseek_model = AutoModelForCausalLM.from_pretrained(deepseek_model_name, torch_dtype=torch.float16)
|
| 338 |
-
|
| 339 |
-
# Initialize ChatGPT (you'll need an OpenAI API key)
|
| 340 |
-
client = OpenAI(api_key="your-openai-api-key") # Replace with your actual API key
|
| 341 |
-
|
| 342 |
-
def retrieve_information_with_deepseek(query, student_profile):
|
| 343 |
-
# Prepare context from student profile
|
| 344 |
-
profile_context = f"""
|
| 345 |
-
Student Profile:
|
| 346 |
-
Name: {student_profile.get('name', 'N/A')}
|
| 347 |
-
Age: {student_profile.get('age', 'N/A')}
|
| 348 |
-
Grade Level: {student_profile.get('transcript', {}).get('grade_level', 'N/A')}
|
| 349 |
-
GPA: {student_profile.get('transcript', {}).get('gpa', {}).get('unweighted', 'N/A')} (Unweighted)
|
| 350 |
-
Learning Style: {student_profile.get('learning_style', 'N/A')}
|
| 351 |
-
Interests: {student_profile.get('interests', 'N/A')}
|
| 352 |
-
"""
|
| 353 |
-
|
| 354 |
-
# Format the prompt for DeepSeek
|
| 355 |
-
prompt = f"""
|
| 356 |
-
[CONTEXT]
|
| 357 |
-
{profile_context}
|
| 358 |
-
|
| 359 |
-
[QUERY]
|
| 360 |
-
{query}
|
| 361 |
-
|
| 362 |
-
Based on the student profile and educational context, provide the most accurate and relevant information to answer the query.
|
| 363 |
-
"""
|
| 364 |
-
|
| 365 |
-
# Generate response with DeepSeek
|
| 366 |
-
inputs = deepseek_tokenizer(prompt, return_tensors="pt")
|
| 367 |
-
outputs = deepseek_model.generate(**inputs, max_new_tokens=200)
|
| 368 |
-
accurate_response = deepseek_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 369 |
-
|
| 370 |
-
return accurate_response
|
| 371 |
-
|
| 372 |
-
def generate_chat_response_with_chatgpt(message, history, student_profile):
|
| 373 |
-
# First retrieve accurate information with DeepSeek
|
| 374 |
-
accurate_info = retrieve_information_with_deepseek(message, student_profile)
|
| 375 |
-
|
| 376 |
-
# Prepare conversation history
|
| 377 |
-
chat_history = "\n".join([f"User: {h[0]}\nAI: {h[1]}" for h in history])
|
| 378 |
-
|
| 379 |
-
# Create ChatGPT prompt
|
| 380 |
-
prompt = f"""
|
| 381 |
-
You are a personalized teaching assistant. Use the following accurate information to craft a natural, helpful response:
|
| 382 |
-
|
| 383 |
-
[ACCURATE INFORMATION]
|
| 384 |
-
{accurate_info}
|
| 385 |
-
|
| 386 |
-
[CONVERSATION HISTORY]
|
| 387 |
-
{chat_history}
|
| 388 |
-
|
| 389 |
-
[NEW MESSAGE]
|
| 390 |
-
User: {message}
|
| 391 |
-
|
| 392 |
-
Respond in a friendly, conversational tone while ensuring all factual information remains accurate.
|
| 393 |
-
"""
|
| 394 |
-
|
| 395 |
-
# Get response from ChatGPT
|
| 396 |
-
response = client.chat.completions.create(
|
| 397 |
-
model="gpt-3.5-turbo",
|
| 398 |
-
messages=[
|
| 399 |
-
{"role": "system", "content": "You are a helpful teaching assistant."},
|
| 400 |
-
{"role": "user", "content": prompt}
|
| 401 |
-
],
|
| 402 |
-
temperature=0.7
|
| 403 |
-
)
|
| 404 |
-
|
| 405 |
-
return response.choices[0].message.content
|
| 406 |
-
|
| 407 |
-
# ========== UPDATE GRADIO INTERFACE ==========
|
| 408 |
-
# Add this new tab to your existing with gr.Blocks() as app:
|
| 409 |
-
with gr.Blocks() as app:
|
| 410 |
-
# ... (keep all your existing tabs) ...
|
| 411 |
-
|
| 412 |
with gr.Tab("🤖 AI Teaching Assistant"):
|
| 413 |
gr.Markdown("## Your Personalized Learning Assistant")
|
| 414 |
-
gr.Markdown("Chat with your AI assistant for personalized learning support")
|
| 415 |
-
|
| 416 |
chatbot = gr.ChatInterface(
|
| 417 |
-
fn=lambda message, history:
|
| 418 |
-
message,
|
| 419 |
-
history,
|
| 420 |
-
student_profile=gr.State()
|
| 421 |
-
),
|
| 422 |
examples=[
|
| 423 |
-
"How should I study for my
|
| 424 |
-
"Can you explain this concept
|
| 425 |
-
"What are some good study strategies
|
| 426 |
-
"How can I improve my grades
|
| 427 |
-
]
|
| 428 |
-
additional_inputs=[transcript_data, learning_output]
|
| 429 |
)
|
| 430 |
-
|
| 431 |
-
# This connects the profile data to the chatbot
|
| 432 |
-
@app.load
|
| 433 |
-
def load_profile():
|
| 434 |
-
profile_path = os.path.join("student_profiles", "student_profile.json")
|
| 435 |
-
if os.path.exists(profile_path):
|
| 436 |
-
with open(profile_path, "r") as f:
|
| 437 |
-
return json.load(f)
|
| 438 |
-
return {}
|
| 439 |
|
| 440 |
app.launch()
|
|
|
|
| 5 |
import re
|
| 6 |
from PyPDF2 import PdfReader
|
| 7 |
from collections import defaultdict
|
| 8 |
+
from openai import OpenAI # pip install openai
|
| 9 |
|
| 10 |
+
# Initialize OpenAI client (you'll need an API key)
|
| 11 |
+
client = OpenAI(api_key="your-api-key-here") # Replace with your actual API key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# ========== TRANSCRIPT PARSING FUNCTIONS ==========
|
| 14 |
def parse_transcript(file):
|
| 15 |
+
if file.name.endswith('.pdf'):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
text = ''
|
| 17 |
reader = PdfReader(file)
|
| 18 |
for page in reader.pages:
|
|
|
|
| 24 |
grade_match = re.search(r'(Grade|Year)[\s:]*(\d+|Freshman|Sophomore|Junior|Senior)', text, re.IGNORECASE)
|
| 25 |
grade_level = grade_match.group(2) if grade_match else "Unknown"
|
| 26 |
|
| 27 |
+
# GPA extraction
|
| 28 |
gpa_data = {'weighted': "N/A", 'unweighted': "N/A"}
|
| 29 |
gpa_patterns = [
|
| 30 |
r'Weighted GPA[\s:]*(\d\.\d{1,2})',
|
| 31 |
r'GPA \(Weighted\)[\s:]*(\d\.\d{1,2})',
|
|
|
|
| 32 |
r'Unweighted GPA[\s:]*(\d\.\d{1,2})',
|
| 33 |
r'GPA \(Unweighted\)[\s:]*(\d\.\d{1,2})',
|
|
|
|
| 34 |
r'GPA[\s:]*(\d\.\d{1,2})'
|
| 35 |
]
|
| 36 |
+
|
| 37 |
for pattern in gpa_patterns:
|
| 38 |
for match in re.finditer(pattern, text, re.IGNORECASE):
|
| 39 |
gpa_value = match.group(1)
|
|
|
|
| 47 |
if gpa_data['weighted'] == "N/A":
|
| 48 |
gpa_data['weighted'] = gpa_value
|
| 49 |
|
|
|
|
|
|
|
| 50 |
output_text = f"Grade Level: {grade_level}\n\n"
|
| 51 |
if gpa_data['weighted'] != "N/A" or gpa_data['unweighted'] != "N/A":
|
| 52 |
output_text += "GPA Information:\n"
|
|
|
|
| 57 |
else:
|
| 58 |
output_text += "No GPA information found\n"
|
| 59 |
|
|
|
|
|
|
|
| 60 |
return output_text, {
|
| 61 |
"gpa": gpa_data,
|
| 62 |
+
"grade_level": grade_level
|
|
|
|
| 63 |
}
|
| 64 |
else:
|
| 65 |
+
return "Currently only PDF transcripts are supported", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# ========== LEARNING STYLE QUIZ ==========
|
| 68 |
learning_style_questions = [
|
|
|
|
| 70 |
"When you need directions to a new place, you prefer:",
|
| 71 |
"When you learn a new skill, you prefer to:",
|
| 72 |
"When you're trying to concentrate, you:",
|
| 73 |
+
"When you meet new people, you remember them by:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
]
|
| 75 |
|
| 76 |
learning_style_options = [
|
|
|
|
| 78 |
["Look at a map (Visual)", "Have someone tell you (Auditory)", "Write down directions (Reading/Writing)", "Try walking/driving there (Kinesthetic)"],
|
| 79 |
["Read instructions (Reading/Writing)", "Have someone show you (Visual)", "Listen to explanations (Auditory)", "Try it yourself (Kinesthetic)"],
|
| 80 |
["Need quiet (Reading/Writing)", "Need background noise (Auditory)", "Need to move around (Kinesthetic)", "Need visual stimulation (Visual)"],
|
| 81 |
+
["Their face (Visual)", "Their name (Auditory)", "What you talked about (Reading/Writing)", "What you did together (Kinesthetic)"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
]
|
| 83 |
|
| 84 |
def learning_style_quiz(*answers):
|
|
|
|
| 89 |
"Kinesthetic": 0
|
| 90 |
}
|
| 91 |
|
|
|
|
| 92 |
for i, answer in enumerate(answers):
|
| 93 |
+
if answer == learning_style_options[i][0]:
|
| 94 |
scores["Reading/Writing"] += 1
|
| 95 |
+
elif answer == learning_style_options[i][1]:
|
| 96 |
scores["Auditory"] += 1
|
| 97 |
+
elif answer == learning_style_options[i][2]:
|
| 98 |
scores["Visual"] += 1
|
| 99 |
+
elif answer == learning_style_options[i][3]:
|
| 100 |
scores["Kinesthetic"] += 1
|
| 101 |
|
|
|
|
| 102 |
max_score = max(scores.values())
|
| 103 |
dominant_styles = [style for style, score in scores.items() if score == max_score]
|
| 104 |
|
|
|
|
| 105 |
if len(dominant_styles) == 1:
|
| 106 |
+
return f"Your primary learning style is: {dominant_styles[0]}"
|
| 107 |
else:
|
| 108 |
+
return f"You have multiple strong learning styles: {', '.join(dominant_styles)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
# ========== PROFILE MANAGEMENT ==========
|
| 111 |
def save_profile(name, age, interests, transcript, learning_style, favorites, blog):
|
| 112 |
data = {
|
| 113 |
"name": name,
|
|
|
|
| 122 |
json_path = os.path.join("student_profiles", f"{name.replace(' ', '_')}_profile.json")
|
| 123 |
with open(json_path, "w") as f:
|
| 124 |
json.dump(data, f, indent=2)
|
| 125 |
+
|
| 126 |
+
return "Profile saved successfully!"
|
| 127 |
+
|
| 128 |
+
def load_profile():
|
| 129 |
+
profile_path = os.path.join("student_profiles", "student_profile.json")
|
| 130 |
+
if os.path.exists(profile_path):
|
| 131 |
+
with open(profile_path, "r") as f:
|
| 132 |
+
return json.load(f)
|
| 133 |
+
return {}
|
| 134 |
+
|
| 135 |
+
# ========== AI TEACHING ASSISTANT ==========
|
| 136 |
+
def generate_response(message, history, profile_data):
|
| 137 |
+
try:
|
| 138 |
+
# Prepare the prompt with profile information
|
| 139 |
+
prompt = f"""
|
| 140 |
+
You are a personalized teaching assistant. Here's the student profile:
|
| 141 |
+
|
| 142 |
+
Name: {profile_data.get('name', 'N/A')}
|
| 143 |
+
Age: {profile_data.get('age', 'N/A')}
|
| 144 |
+
Grade Level: {profile_data.get('transcript', {}).get('grade_level', 'N/A')}
|
| 145 |
+
GPA: {profile_data.get('transcript', {}).get('gpa', {}).get('unweighted', 'N/A')}
|
| 146 |
+
Learning Style: {profile_data.get('learning_style', 'N/A')}
|
| 147 |
+
Interests: {profile_data.get('interests', 'N/A')}
|
| 148 |
+
|
| 149 |
+
Current conversation:
|
| 150 |
+
{history}
|
| 151 |
+
|
| 152 |
+
Student's message: {message}
|
| 153 |
+
|
| 154 |
+
Provide a helpful, personalized response considering the student's profile.
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
response = client.chat.completions.create(
|
| 158 |
+
model="gpt-3.5-turbo",
|
| 159 |
+
messages=[
|
| 160 |
+
{"role": "system", "content": "You are a helpful teaching assistant."},
|
| 161 |
+
{"role": "user", "content": prompt}
|
| 162 |
+
],
|
| 163 |
+
temperature=0.7
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
return response.choices[0].message.content
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return f"Sorry, I encountered an error: {str(e)}"
|
| 169 |
|
| 170 |
# ========== GRADIO INTERFACE ==========
|
| 171 |
with gr.Blocks() as app:
|
| 172 |
+
# Profile tabs (keep your existing tabs)
|
| 173 |
with gr.Tab("Step 1: Upload Transcript"):
|
| 174 |
+
transcript_file = gr.File(label="Upload your transcript (PDF)")
|
| 175 |
transcript_output = gr.Textbox(label="Transcript Output")
|
| 176 |
transcript_data = gr.State()
|
| 177 |
+
transcript_file.change(parse_transcript, inputs=transcript_file, outputs=[transcript_output, transcript_data])
|
| 178 |
|
| 179 |
with gr.Tab("Step 2: Learning Style Quiz"):
|
|
|
|
| 180 |
quiz_components = []
|
| 181 |
for i, (question, options) in enumerate(zip(learning_style_questions, learning_style_options)):
|
| 182 |
+
quiz_components.append(gr.Radio(options, label=f"{i+1}. {question}"))
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
learning_output = gr.Textbox(label="Learning Style Result")
|
| 185 |
gr.Button("Submit Quiz").click(
|
| 186 |
learning_style_quiz,
|
| 187 |
inputs=quiz_components,
|
|
|
|
| 202 |
character_reason = gr.Textbox(label="Why do you like that character?")
|
| 203 |
blog_checkbox = gr.Checkbox(label="Do you want to write a blog?", value=False)
|
| 204 |
blog_text = gr.Textbox(label="Write your blog here", visible=False, lines=5)
|
| 205 |
+
blog_checkbox.change(lambda x: gr.update(visible=x), inputs=blog_checkbox, outputs=blog_text)
|
| 206 |
|
| 207 |
+
with gr.Tab("Step 4: Save Profile"):
|
|
|
|
| 208 |
save_btn = gr.Button("Save Profile")
|
| 209 |
+
save_output = gr.Textbox(label="Save Status")
|
| 210 |
+
|
| 211 |
+
save_btn.click(
|
| 212 |
+
save_profile,
|
| 213 |
+
inputs=[name, age, interests, transcript_data, learning_output,
|
| 214 |
+
{"movie": movie, "movie_reason": movie_reason,
|
| 215 |
+
"show": show, "show_reason": show_reason,
|
| 216 |
+
"book": book, "book_reason": book_reason,
|
| 217 |
+
"character": character, "character_reason": character_reason},
|
| 218 |
+
blog_text],
|
| 219 |
+
outputs=save_output
|
| 220 |
+
)
|
| 221 |
|
| 222 |
+
# AI Teaching Assistant Tab
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
with gr.Tab("🤖 AI Teaching Assistant"):
|
| 224 |
gr.Markdown("## Your Personalized Learning Assistant")
|
|
|
|
|
|
|
| 225 |
chatbot = gr.ChatInterface(
|
| 226 |
+
fn=lambda message, history: generate_response(message, history, load_profile()),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
examples=[
|
| 228 |
+
"How should I study for my next test?",
|
| 229 |
+
"Can you explain this concept in a way that matches my learning style?",
|
| 230 |
+
"What are some good study strategies for me?",
|
| 231 |
+
"How can I improve my grades?"
|
| 232 |
+
]
|
|
|
|
| 233 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
app.launch()
|