Dannyar608 commited on
Commit
4967aac
·
verified ·
1 Parent(s): d8684a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -44
app.py CHANGED
@@ -5,10 +5,6 @@ import os
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):
@@ -126,50 +122,77 @@ def save_profile(name, age, interests, transcript, learning_style, favorites, bl
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")
@@ -220,14 +243,14 @@ with gr.Blocks() as app:
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
  )
 
5
  import re
6
  from PyPDF2 import PdfReader
7
  from collections import defaultdict
 
 
 
 
8
 
9
  # ========== TRANSCRIPT PARSING FUNCTIONS ==========
10
  def parse_transcript(file):
 
122
  return "Profile saved successfully!"
123
 
124
  def load_profile():
125
+ files = [f for f in os.listdir("student_profiles") if f.endswith('.json')]
126
+ if files:
127
+ with open(os.path.join("student_profiles", files[0]), "r") as f:
128
  return json.load(f)
129
  return {}
130
 
131
+ # ========== RULE-BASED TEACHING ASSISTANT ==========
132
  def generate_response(message, history, profile_data):
133
+ # Get learning style from profile
134
+ learning_style = profile_data.get("learning_style", "")
135
+
136
+ # Common responses
137
+ greetings = ["hi", "hello", "hey"]
138
+ study_help = ["study", "learn", "prepare"]
139
+ grade_help = ["grade", "gpa", "score"]
140
+
141
+ if any(greet in message.lower() for greet in greetings):
142
+ return f"Hello {profile_data.get('name', 'there')}! How can I help you today?"
143
+
144
+ elif any(word in message.lower() for word in study_help):
145
+ if "Visual" in learning_style:
146
+ return ("Based on your visual learning style, I recommend:\n"
147
+ "- Creating mind maps or diagrams\n"
148
+ "- Using color-coded notes\n"
149
+ "- Watching educational videos")
150
+ elif "Auditory" in learning_style:
151
+ return ("Based on your auditory learning style, I recommend:\n"
152
+ "- Recording lectures and listening to them\n"
153
+ "- Participating in study groups\n"
154
+ "- Explaining concepts out loud")
155
+ elif "Reading/Writing" in learning_style:
156
+ return ("Based on your reading/writing learning style, I recommend:\n"
157
+ "- Writing detailed notes\n"
158
+ "- Creating summaries in your own words\n"
159
+ "- Reading textbooks and articles")
160
+ elif "Kinesthetic" in learning_style:
161
+ return ("Based on your kinesthetic learning style, I recommend:\n"
162
+ "- Hands-on practice\n"
163
+ "- Creating physical models\n"
164
+ "- Taking frequent movement breaks")
165
+ else:
166
+ return ("Here are some general study tips:\n"
167
+ "- Break study sessions into 25-minute chunks\n"
168
+ "- Review material regularly\n"
169
+ "- Teach concepts to someone else")
170
+
171
+ elif any(word in message.lower() for word in grade_help):
172
+ gpa = profile_data.get("transcript", {}).get("gpa", {})
173
+ return (f"Your GPA information:\n"
174
+ f"- Unweighted: {gpa.get('unweighted', 'N/A')}\n"
175
+ f"- Weighted: {gpa.get('weighted', 'N/A')}\n\n"
176
+ "To improve your grades, try:\n"
177
+ "- Setting specific goals\n"
178
+ "- Meeting with teachers\n"
179
+ "- Developing a study schedule")
180
+
181
+ elif "help" in message.lower():
182
+ return ("I can help with:\n"
183
+ "- Study tips based on your learning style\n"
184
+ "- GPA and grade information\n"
185
+ "- General academic advice\n\n"
186
+ "Try asking about study strategies or your grades!")
187
+
188
+ else:
189
+ return ("I'm your personalized teaching assistant. "
190
+ "I can help with study tips, grade information, and academic advice. "
191
+ "Try asking about how to study for your classes!")
192
 
193
  # ========== GRADIO INTERFACE ==========
194
  with gr.Blocks() as app:
195
+ # Profile tabs
196
  with gr.Tab("Step 1: Upload Transcript"):
197
  transcript_file = gr.File(label="Upload your transcript (PDF)")
198
  transcript_output = gr.Textbox(label="Transcript Output")
 
243
  )
244
 
245
  # AI Teaching Assistant Tab
246
+ with gr.Tab("🤖 Teaching Assistant"):
247
  gr.Markdown("## Your Personalized Learning Assistant")
248
  chatbot = gr.ChatInterface(
249
  fn=lambda message, history: generate_response(message, history, load_profile()),
250
  examples=[
251
  "How should I study for my next test?",
252
+ "What's my GPA information?",
253
+ "Help me with study strategies",
254
  "How can I improve my grades?"
255
  ]
256
  )