maria355 commited on
Commit
2838aaa
·
verified ·
1 Parent(s): a4ebbbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -120
app.py CHANGED
@@ -955,150 +955,158 @@ def format_project_ideas(ideas):
955
 
956
  return result
957
 
 
 
 
 
 
958
  def user_onboarding(session_id, age, goals, knowledge_level, interests, study_time, learning_style):
959
- """Process user profile and provide initial recommendations"""
960
- # Save user profile data
961
- user_data = {
962
- 'age': age,
963
- 'goals': goals,
964
- 'knowledge_level': knowledge_level,
965
- 'interests': interests,
966
- 'study_time': study_time,
967
- 'learning_style': learning_style
 
 
968
  }
969
- save_session(session_id, user_data)
970
-
971
- # Generate recommendations
972
- learning_paths = recommend_learning_path(age, goals, knowledge_level, interests)
973
- resources = get_recommended_resources(interests, knowledge_level, learning_paths)
974
- project_ideas = get_project_ideas(learning_paths)
975
-
976
- # Save recommendations to session
977
- user_data.update({
978
- 'recommended_paths': learning_paths,
979
- 'recommended_resources': resources,
980
- 'recommended_projects': project_ideas
981
- })
982
- save_session(session_id, user_data)
983
 
984
- # Format welcome message with personalized recommendations
985
- welcome_message = f"""
986
- # Welcome to Your Personalized Learning Journey!
987
 
988
- Thank you for providing your profile. Based on your information, I've prepared some tailored recommendations to start your learning journey.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
989
 
990
- ## Your Profile Summary:
991
- - **Age:** {age}
992
- - **Knowledge Level:** {knowledge_level}
993
- - **Learning Goals:** {goals}
994
- - **Interests:** {interests}
995
- - **Available Study Time:** {study_time} hours per week
996
- - **Preferred Learning Style:** {learning_style}
997
 
998
- {format_learning_paths(learning_paths)}
 
 
999
 
1000
- {format_resources(resources)}
 
 
1001
 
1002
- {format_project_ideas(project_ideas)}
 
 
1003
 
1004
- ## Next Steps:
1005
- 1. Browse through the recommended learning paths and resources
1006
- 2. Ask me any questions about the topics you're interested in
1007
- 3. Request exercises, explanations, or code samples
1008
- 4. Try one of the project ideas to apply your knowledge
1009
 
1010
- I'm here to help you every step of the way! What would you like to explore first?
1011
- """
 
1012
 
1013
- return welcome_message
1014
-
1015
- def chatbot_interface(session_id, user_message):
1016
- """Main chatbot interface function"""
1017
- user_data = load_session(session_id)
1018
-
1019
- if not user_data or not user_data.get('age'):
1020
- return "Please complete your profile first by going to the Profile tab."
1021
 
1022
- response = chat_with_groq(user_message, session_id)
1023
- return response
1024
 
1025
- def generate_recommendations(session_id):
1026
- """Generate or refresh recommendations based on current profile"""
1027
- user_data = load_session(session_id)
1028
-
1029
- if not user_data or not user_data.get('age'):
1030
- return "Please complete your profile first by going to the Profile tab."
1031
-
1032
- # Generate fresh recommendations
1033
- learning_paths = recommend_learning_path(
1034
- user_data.get('age', ''),
1035
- user_data.get('goals', ''),
1036
- user_data.get('knowledge_level', ''),
1037
- user_data.get('interests', '')
1038
- )
1039
- resources = get_recommended_resources(
1040
- user_data.get('interests', ''),
1041
- user_data.get('knowledge_level', ''),
1042
- learning_paths
1043
- )
1044
- project_ideas = get_project_ideas(learning_paths)
1045
-
1046
- # Save recommendations to session
1047
- user_data.update({
1048
- 'recommended_paths': learning_paths,
1049
- 'recommended_resources': resources,
1050
- 'recommended_projects': project_ideas
1051
- })
1052
- save_session(session_id, user_data)
1053
-
1054
- # Format recommendations
1055
- recommendations = f"""
1056
- # Your Personalized Learning Recommendations
1057
-
1058
- {format_learning_paths(learning_paths)}
1059
-
1060
- {format_resources(resources)}
1061
-
1062
- {format_project_ideas(project_ideas)}
1063
- """
1064
 
1065
- return recommendations
 
1066
 
1067
  def handle_quiz_request(session_id, topic, difficulty):
1068
- """Handle quiz generation request"""
1069
- user_data = load_session(session_id)
 
 
 
 
 
1070
 
1071
- if not user_data or not user_data.get('age'):
1072
- return "Please complete your profile first by going to the Profile tab."
 
 
1073
 
1074
- quiz = generate_quiz(topic, difficulty)
1075
- return quiz
1076
 
1077
  def handle_study_plan_request(session_id, topic, time_available):
1078
- """Handle study plan generation request"""
1079
- user_data = load_session(session_id)
1080
-
1081
- if not user_data or not user_data.get('age'):
1082
- return "Please complete your profile first by going to the Profile tab."
1083
 
1084
- goals = user_data.get('goals', 'improving skills')
1085
- knowledge_level = user_data.get('knowledge_level', 'Beginner')
1086
- study_plan = create_study_plan(topic, time_available, goals, knowledge_level)
1087
 
1088
- # Save the generated study plan to the session
1089
- if 'study_plans' not in user_data:
1090
- user_data['study_plans'] = {}
1091
 
1092
- study_plan_id = f"{topic}_{time_available}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
1093
- user_data['study_plans'][study_plan_id] = {
1094
- 'topic': topic,
1095
- 'time_available': time_available,
1096
- 'plan': study_plan,
1097
- 'created_at': datetime.now().isoformat()
1098
- }
1099
- save_session(session_id, user_data)
1100
 
1101
- return study_plan
 
1102
 
1103
  def add_generative_ai_info():
1104
  """Return information about Generative AI"""
 
955
 
956
  return result
957
 
958
+ # Missing function implementations
959
+
960
+ # Dictionary to store user profiles by session ID
961
+ user_profiles = {}
962
+
963
  def user_onboarding(session_id, age, goals, knowledge_level, interests, study_time, learning_style):
964
+ """Process user profile information and generate personalized recommendations"""
965
+
966
+ # Store user profile information
967
+ user_profiles[session_id] = {
968
+ "age": age,
969
+ "goals": goals,
970
+ "knowledge_level": knowledge_level,
971
+ "interests": interests,
972
+ "study_time": study_time,
973
+ "learning_style": learning_style,
974
+ "last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
975
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
976
 
977
+ # Generate initial recommendations based on profile
978
+ return generate_recommendations(session_id)
 
979
 
980
+ def generate_recommendations(session_id):
981
+ """Generate personalized learning recommendations based on user profile"""
982
+
983
+ # Check if user profile exists
984
+ if session_id not in user_profiles:
985
+ return "Please complete your profile first to get personalized recommendations."
986
+
987
+ profile = user_profiles[session_id]
988
+ learning_style = profile["learning_style"]
989
+ knowledge_level = profile["knowledge_level"].lower()
990
+
991
+ # Map knowledge level to appropriate learning path
992
+ if "python" in profile["interests"].lower():
993
+ if knowledge_level == "beginner":
994
+ learning_path_key = "python_beginner"
995
+ else:
996
+ learning_path_key = "python_intermediate"
997
+ elif "data" in profile["interests"].lower():
998
+ if knowledge_level == "beginner":
999
+ learning_path_key = "data_science_beginner"
1000
+ else:
1001
+ learning_path_key = "data_science_advanced"
1002
+ elif "ai" in profile["interests"].lower() or "artificial" in profile["interests"].lower():
1003
+ if "gen" in profile["interests"].lower():
1004
+ learning_path_key = "generative_ai"
1005
+ elif "agent" in profile["interests"].lower():
1006
+ learning_path_key = "agentic_ai"
1007
+ else:
1008
+ learning_path_key = "ai_specialization"
1009
+ else:
1010
+ # Default path
1011
+ learning_path_key = "python_beginner"
1012
 
1013
+ # Get learning path and resources based on style and path
1014
+ learning_path = LEARNING_PATHS.get(learning_path_key, LEARNING_PATHS["python_beginner"])
1015
+ resources = LEARNING_RESOURCES.get(learning_style, LEARNING_RESOURCES["Combination"])
1016
+ path_resources = resources.get(learning_path_key, resources["python_beginner"])
 
 
 
1017
 
1018
+ # Get project ideas
1019
+ project_ideas = PROJECT_IDEAS.get(learning_style, PROJECT_IDEAS["Combination"])
1020
+ path_projects = project_ideas.get(learning_path_key, project_ideas["python_beginner"])
1021
 
1022
+ # Build the recommendation markdown
1023
+ markdown = f"## Your Personalized Learning Path: {learning_path['title']}\n\n"
1024
+ markdown += f"*{learning_path['description']}*\n\n"
1025
 
1026
+ markdown += "### Learning Modules\n"
1027
+ for i, module in enumerate(learning_path['modules'], 1):
1028
+ markdown += f"{i}. {module}\n"
1029
 
1030
+ markdown += "\n### Recommended Resources\n"
1031
+ for i, resource in enumerate(path_resources[:3], 1):
1032
+ markdown += f"{i}. [{resource['title']}]({resource['url']})\n"
 
 
1033
 
1034
+ markdown += "\n### Project Ideas\n"
1035
+ for i, project in enumerate(path_projects[:3], 1):
1036
+ markdown += f"{i}. {project}\n"
1037
 
1038
+ markdown += f"\n\nRecommendations generated based on your {learning_style} learning style preference."
 
 
 
 
 
 
 
1039
 
1040
+ return markdown
 
1041
 
1042
+ def chatbot_interface(session_id, message):
1043
+ """Process user messages and generate assistant responses"""
1044
+
1045
+ if not message:
1046
+ return "Please type a message to start the conversation."
1047
+
1048
+ # Build system message with context from user profile if available
1049
+ system_message = SYSTEM_PROMPT
1050
+
1051
+ if session_id in user_profiles:
1052
+ profile = user_profiles[session_id]
1053
+ system_message += f"\n\nUser Profile Information:\n"
1054
+ system_message += f"- Knowledge Level: {profile['knowledge_level']}\n"
1055
+ system_message += f"- Learning Style: {profile['learning_style']}\n"
1056
+ system_message += f"- Interests: {profile['interests']}\n"
1057
+ system_message += f"- Learning Goals: {profile['goals']}\n"
1058
+
1059
+ # Call the Groq API
1060
+ try:
1061
+ chat_completion = client.chat.completions.create(
1062
+ model="llama3-70b-8192", # or another appropriate model
1063
+ messages=[
1064
+ {"role": "system", "content": system_message},
1065
+ {"role": "user", "content": message}
1066
+ ],
1067
+ temperature=0.7,
1068
+ max_tokens=2048
1069
+ )
1070
+
1071
+ # Extract and return the response
1072
+ return chat_completion.choices[0].message.content
 
 
 
 
 
 
 
 
1073
 
1074
+ except Exception as e:
1075
+ return f"I apologize, but I encountered an error: {str(e)}\n\nPlease try again in a moment."
1076
 
1077
  def handle_quiz_request(session_id, topic, difficulty):
1078
+ """Generate a quiz based on the specified topic and difficulty"""
1079
+
1080
+ if not topic:
1081
+ return "Please specify a topic for the quiz."
1082
+
1083
+ # Build prompt for quiz generation
1084
+ prompt = f"Create a quiz on {topic} at {difficulty} level. Include 5 questions with multiple choice answers and provide the correct answers at the end."
1085
 
1086
+ # Add context from user profile if available
1087
+ if session_id in user_profiles:
1088
+ profile = user_profiles[session_id]
1089
+ prompt += f"\nThe user has a {profile['knowledge_level']} knowledge level and prefers {profile['learning_style']} learning style."
1090
 
1091
+ # Use chatbot interface with the quiz prompt
1092
+ return chatbot_interface(session_id, prompt)
1093
 
1094
  def handle_study_plan_request(session_id, topic, time_available):
1095
+ """Generate a personalized study plan based on topic and available time"""
 
 
 
 
1096
 
1097
+ if not topic:
1098
+ return "Please specify a topic for your study plan."
 
1099
 
1100
+ # Build prompt for study plan generation
1101
+ prompt = f"Create a detailed study plan for learning {topic} with {time_available} hours available per week. Include specific goals, resources, and milestones."
 
1102
 
1103
+ # Add context from user profile if available
1104
+ if session_id in user_profiles:
1105
+ profile = user_profiles[session_id]
1106
+ prompt += f"\nThe user has a {profile['knowledge_level']} knowledge level, prefers {profile['learning_style']} learning style, and has these goals: {profile['goals']}."
 
 
 
 
1107
 
1108
+ # Use chatbot interface with the study plan prompt
1109
+ return chatbot_interface(session_id, prompt)
1110
 
1111
  def add_generative_ai_info():
1112
  """Return information about Generative AI"""