arpit13 commited on
Commit
b014d75
·
verified ·
1 Parent(s): 2a7d8c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -69
app.py CHANGED
@@ -8,52 +8,40 @@ openai.api_base = "https://api.groq.com/openai/v1"
8
 
9
  # Function to get response from GROQ API
10
  def get_groq_response(message, category):
11
- system_message = ""
12
-
13
- if category == "Study Tips":
14
- system_message = "Provide effective study strategies, time management advice, and tips for staying organized and focused."
15
- elif category == "Exam Preparation":
16
- system_message = "Offer tips for preparing for exams, managing anxiety, and optimizing performance on test day."
17
- elif category == "Project Guidance":
18
- system_message = "Provide advice on how to tackle academic projects, group assignments, and presentations effectively."
19
- elif category == "Time Management":
20
- system_message = "Help the user manage their time effectively with practical scheduling and prioritization tips."
21
- elif category == "Motivation & Focus":
22
- system_message = "Provide encouraging advice to stay motivated and maintain focus on long-term goals."
23
- elif category == "Building Confidence":
24
- system_message = "Offer advice to help boost self-esteem, overcome self-doubt, and build confidence."
25
- elif category == "Friendship & Social Skills":
26
- system_message = "Provide tips for making friends, improving communication skills, and navigating social dynamics."
27
- elif category == "Networking & Career Building":
28
- system_message = "Offer advice on networking, building professional relationships, and finding internships or job opportunities."
29
- elif category == "Work-Life Balance":
30
- system_message = "Help the user balance academics, social life, and personal well-being."
31
- elif category == "Mental Health":
32
- system_message = "Provide empathetic advice to manage stress, anxiety, and other mental health challenges. Encourage seeking professional help when needed."
33
- elif category == "Physical Health":
34
- system_message = "Share tips for maintaining physical health, including fitness, sleep, and nutrition."
35
- elif category == "Stress Management":
36
- system_message = "Offer calming techniques and advice to handle stress effectively."
37
- elif category == "Budgeting Tips":
38
- system_message = "Provide practical advice for managing money, creating a budget, and saving effectively as a student."
39
- elif category == "Finding Part-Time Jobs":
40
- system_message = "Share tips for finding and balancing part-time work with academic responsibilities."
41
- elif category == "Scholarships & Financial Aid":
42
- system_message = "Provide advice on applying for scholarships, grants, and understanding financial aid options."
43
- elif category == "Resume Building":
44
- system_message = "Offer tips for creating an impressive resume tailored for internships or entry-level positions."
45
- elif category == "Interview Preparation":
46
- system_message = "Provide advice on how to prepare for interviews, including common questions and presentation tips."
47
- elif category == "Skill Development":
48
- system_message = "Suggest ways to build skills, gain certifications, and stand out in competitive fields."
49
- elif category == "Exploring New Hobbies":
50
- system_message = "Encourage trying new activities and provide ideas for hobbies that fit college life."
51
- elif category == "Clubs & Extracurriculars":
52
- system_message = "Offer advice on joining clubs, participating in activities, and enhancing the college experience."
53
- elif category == "Creative Projects":
54
- system_message = "Provide inspiration and resources for personal or group creative endeavors."
55
- else:
56
- system_message = "Category not recognized. Respond appropriately to the user's input."
57
 
58
  try:
59
  response = openai.ChatCompletion.create(
@@ -73,8 +61,28 @@ def chatbot(user_input, category, history=[]):
73
  history.append((f"You: {user_input}", f"Bot: {bot_response}"))
74
  return history, history
75
 
76
- # Gradio Interface with enhanced styling
77
- chat_interface = gr.Blocks(css="""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  body {
79
  font-family: 'Poppins', sans-serif;
80
  background: linear-gradient(120deg, #ff9a9e, #fad0c4, #a1c4fd);
@@ -83,11 +91,13 @@ body {
83
  padding: 0;
84
  color: #333;
85
  }
 
86
  @keyframes gradientBG {
87
  0% { background-position: 0% 50%; }
88
  50% { background-position: 100% 50%; }
89
  100% { background-position: 0% 50%; }
90
  }
 
91
  button {
92
  background: linear-gradient(90deg, #6a11cb, #2575fc);
93
  color: white;
@@ -99,10 +109,12 @@ button {
99
  cursor: pointer;
100
  transition: transform 0.2s ease, background 0.2s ease;
101
  }
 
102
  button:hover {
103
  background: linear-gradient(90deg, #2575fc, #6a11cb);
104
  transform: scale(1.1);
105
  }
 
106
  header {
107
  text-align: center;
108
  margin-bottom: 20px;
@@ -112,6 +124,7 @@ header {
112
  color: white;
113
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
114
  }
 
115
  .chat-container {
116
  border: 2px solid #ff7eb3;
117
  background: rgba(255, 255, 255, 0.8);
@@ -120,10 +133,21 @@ header {
120
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
121
  max-height: 300px;
122
  overflow-y: auto;
 
 
 
 
 
 
 
123
  }
124
- """)
125
 
126
- with chat_interface:
 
 
 
 
 
127
  with gr.Row():
128
  gr.Markdown("<h1 style='text-align:center;'>🌟 Vibrant Personal Assistant Chatbot 🌈</h1>")
129
 
@@ -131,37 +155,42 @@ with chat_interface:
131
  gr.Markdown("<p style='text-align:center;'>Select a category and type your message to get tailored responses.</p>")
132
 
133
  with gr.Row():
134
- user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=2)
135
- category_dropdown = gr.Dropdown(
136
- choices=[
137
- "Study Tips", "Exam Preparation", "Project Guidance", "Time Management", "Motivation & Focus",
138
- "Building Confidence", "Friendship & Social Skills", "Networking & Career Building", "Work-Life Balance",
139
- "Mental Health", "Physical Health", "Stress Management", "Budgeting Tips", "Finding Part-Time Jobs",
140
- "Scholarships & Financial Aid", "Resume Building", "Interview Preparation", "Skill Development",
141
- "Exploring New Hobbies", "Clubs & Extracurriculars", "Creative Projects"
142
- ],
143
- label="Choose Chat Category"
144
  )
145
-
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  with gr.Row():
 
147
  send_button = gr.Button("Send")
148
-
149
  with gr.Row():
150
- with gr.Column() as chat_container:
151
- chatbot_output = gr.Chatbot(label="Chat History")
152
 
153
- # Add functionality to handle interactions
154
- def handle_chat(user_input, category, history):
155
  if not user_input.strip():
156
  return history, history
157
- updated_history, _ = chatbot(user_input, category, history)
158
  return updated_history, updated_history
159
 
160
  send_button.click(
161
  handle_chat,
162
- inputs=[user_input, category_dropdown, chatbot_output],
163
  outputs=[chatbot_output, chatbot_output]
164
  )
165
 
166
  chat_interface.launch()
167
-
 
8
 
9
  # Function to get response from GROQ API
10
  def get_groq_response(message, category):
11
+ # Define system message based on category
12
+ system_messages = {
13
+ "Stress Management": "Provide soothing advice and tips to help the user manage stress. Be calm, empathetic, and reassuring.",
14
+ "Career Advice": "Offer professional and constructive career advice. Be encouraging, insightful, and action-oriented.",
15
+ "General": "Engage in general conversation. Be friendly, approachable, and easygoing.",
16
+ "Funny": "Respond with humorous remarks and witty commentary. Be entertaining but remain respectful.",
17
+ "Flirty": "Respond playfully and with charm. Keep it light-hearted, fun, and appropriate.",
18
+ "Scary": "Respond with spooky or thrilling remarks. Set a mysterious and eerie tone.",
19
+ "Business Mind": "Respond with a sharp, professional focus. Offer strategic advice and maintain a results-driven tone.",
20
+ "Extrovert": "Respond with high energy and enthusiasm. Be engaging, sociable, and vibrant.",
21
+ "Friendly Buddy": "Respond as a supportive and fun friend. Be informal, cheerful, and light-hearted.",
22
+ "Study Tips": "Provide effective study strategies, time management advice, and tips for staying organized and focused.",
23
+ "Exam Preparation": "Offer tips for preparing for exams, managing anxiety, and optimizing performance on test day.",
24
+ "Project Guidance": "Provide advice on how to tackle academic projects, group assignments, and presentations effectively.",
25
+ "Time Management": "Help the user manage their time effectively with practical scheduling and prioritization tips.",
26
+ "Motivation & Focus": "Provide encouraging advice to stay motivated and maintain focus on long-term goals.",
27
+ "Building Confidence": "Offer advice to help boost self-esteem, overcome self-doubt, and build confidence.",
28
+ "Friendship & Social Skills": "Provide tips for making friends, improving communication skills, and navigating social dynamics.",
29
+ "Networking & Career Building": "Offer advice on networking, building professional relationships, and finding internships or job opportunities.",
30
+ "Work-Life Balance": "Help the user balance academics, social life, and personal well-being.",
31
+ "Mental Health": "Provide empathetic advice to manage stress, anxiety, and other mental health challenges. Encourage seeking professional help when needed.",
32
+ "Physical Health": "Share tips for maintaining physical health, including fitness, sleep, and nutrition.",
33
+ "Budgeting Tips": "Provide practical advice for managing money, creating a budget, and saving effectively as a student.",
34
+ "Finding Part-Time Jobs": "Share tips for finding and balancing part-time work with academic responsibilities.",
35
+ "Scholarships & Financial Aid": "Provide advice on applying for scholarships, grants, and understanding financial aid options.",
36
+ "Resume Building": "Offer tips for creating an impressive resume tailored for internships or entry-level positions.",
37
+ "Interview Preparation": "Provide advice on how to prepare for interviews, including common questions and presentation tips.",
38
+ "Skill Development": "Suggest ways to build skills, gain certifications, and stand out in competitive fields.",
39
+ "Exploring New Hobbies": "Encourage trying new activities and provide ideas for hobbies that fit college life.",
40
+ "Clubs & Extracurriculars": "Offer advice on joining clubs, participating in activities, and enhancing the college experience.",
41
+ "Creative Projects": "Provide inspiration and resources for personal or group creative endeavors.",
42
+ }
43
+
44
+ system_message = system_messages.get(category, "Category not recognized. Respond appropriately to the user's input.")
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  try:
47
  response = openai.ChatCompletion.create(
 
61
  history.append((f"You: {user_input}", f"Bot: {bot_response}"))
62
  return history, history
63
 
64
+ # Categories grouped into main and subcategories
65
+ categories = {
66
+ "Academic Support": [
67
+ "Study Tips", "Exam Preparation", "Project Guidance", "Time Management", "Building Confidence"
68
+ ],
69
+ "Mental & Physical Wellness": [
70
+ "Stress Management", "Motivation & Focus", "Mental Health", "Physical Health", "Work-Life Balance"
71
+ ],
72
+ "Career & Financial": [
73
+ "Networking & Career Building", "Finding Part-Time Jobs", "Scholarships & Financial Aid",
74
+ "Resume Building", "Interview Preparation", "Budgeting Tips", "Skill Development"
75
+ ],
76
+ "Social & Personal Growth": [
77
+ "Friendship & Social Skills", "Exploring New Hobbies", "Clubs & Extracurriculars", "Creative Projects"
78
+ ],
79
+ "Fun & Miscellaneous": [
80
+ "Funny", "Flirty", "Scary", "Business Mind", "Extrovert", "Friendly Buddy"
81
+ ]
82
+ }
83
+
84
+ # Gradio Interface with grouped categories and custom CSS for colors and styles
85
+ with gr.Blocks(css="""
86
  body {
87
  font-family: 'Poppins', sans-serif;
88
  background: linear-gradient(120deg, #ff9a9e, #fad0c4, #a1c4fd);
 
91
  padding: 0;
92
  color: #333;
93
  }
94
+
95
  @keyframes gradientBG {
96
  0% { background-position: 0% 50%; }
97
  50% { background-position: 100% 50%; }
98
  100% { background-position: 0% 50%; }
99
  }
100
+
101
  button {
102
  background: linear-gradient(90deg, #6a11cb, #2575fc);
103
  color: white;
 
109
  cursor: pointer;
110
  transition: transform 0.2s ease, background 0.2s ease;
111
  }
112
+
113
  button:hover {
114
  background: linear-gradient(90deg, #2575fc, #6a11cb);
115
  transform: scale(1.1);
116
  }
117
+
118
  header {
119
  text-align: center;
120
  margin-bottom: 20px;
 
124
  color: white;
125
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
126
  }
127
+
128
  .chat-container {
129
  border: 2px solid #ff7eb3;
130
  background: rgba(255, 255, 255, 0.8);
 
133
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
134
  max-height: 300px;
135
  overflow-y: auto;
136
+ font-size: 1.1rem;
137
+ color: #333;
138
+ }
139
+
140
+ .chat-container .user-message {
141
+ font-weight: bold;
142
+ color: #6a11cb;
143
  }
 
144
 
145
+ .chat-container .bot-message {
146
+ font-weight: normal;
147
+ color: #2575fc;
148
+ }
149
+
150
+ """) as chat_interface:
151
  with gr.Row():
152
  gr.Markdown("<h1 style='text-align:center;'>🌟 Vibrant Personal Assistant Chatbot 🌈</h1>")
153
 
 
155
  gr.Markdown("<p style='text-align:center;'>Select a category and type your message to get tailored responses.</p>")
156
 
157
  with gr.Row():
158
+ main_category = gr.Radio(
159
+ label="Main Category",
160
+ choices=list(categories.keys()),
161
+ value="Academic Support"
 
 
 
 
 
 
162
  )
163
+ sub_category = gr.Dropdown(
164
+ label="Subcategory",
165
+ choices=categories["Academic Support"],
166
+ value="Study Tips"
167
+ )
168
+
169
+ def update_subcategories(selected_main_category):
170
+ """Update the subcategory dropdown based on the main category."""
171
+ new_subcategories = categories.get(selected_main_category, [])
172
+ return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None)
173
+
174
+ # Handle main category change to update subcategories
175
+ main_category.change(update_subcategories, inputs=main_category, outputs=sub_category)
176
+
177
  with gr.Row():
178
+ user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=2)
179
  send_button = gr.Button("Send")
180
+
181
  with gr.Row():
182
+ chatbot_output = gr.Chatbot(label="Chat History")
 
183
 
184
+ def handle_chat(user_input, sub_category, history):
 
185
  if not user_input.strip():
186
  return history, history
187
+ updated_history, _ = chatbot(user_input, sub_category, history)
188
  return updated_history, updated_history
189
 
190
  send_button.click(
191
  handle_chat,
192
+ inputs=[user_input, sub_category, chatbot_output],
193
  outputs=[chatbot_output, chatbot_output]
194
  )
195
 
196
  chat_interface.launch()