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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -78
app.py CHANGED
@@ -8,40 +8,52 @@ 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
- # 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,28 +73,57 @@ def chatbot(user_input, category, history=[]):
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
85
- with gr.Blocks() as chat_interface:
86
  with gr.Row():
87
  gr.Markdown("<h1 style='text-align:center;'>🌟 Vibrant Personal Assistant Chatbot 🌈</h1>")
88
 
@@ -90,42 +131,37 @@ with gr.Blocks() as chat_interface:
90
  gr.Markdown("<p style='text-align:center;'>Select a category and type your message to get tailored responses.</p>")
91
 
92
  with gr.Row():
93
- main_category = gr.Radio(
94
- label="Main Category",
95
- choices=list(categories.keys()),
96
- value="Academic Support"
97
- )
98
- sub_category = gr.Dropdown(
99
- label="Subcategory",
100
- choices=categories["Academic Support"],
101
- value="Study Tips"
 
102
  )
103
-
104
- def update_subcategories(selected_main_category):
105
- """Update the subcategory dropdown based on the main category."""
106
- new_subcategories = categories.get(selected_main_category, [])
107
- return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None)
108
-
109
- # Handle main category change to update subcategories
110
- main_category.change(update_subcategories, inputs=main_category, outputs=sub_category)
111
-
112
  with gr.Row():
113
- user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=2)
114
  send_button = gr.Button("Send")
115
-
116
  with gr.Row():
117
- chatbot_output = gr.Chatbot(label="Chat History")
 
118
 
119
- def handle_chat(user_input, sub_category, history):
 
120
  if not user_input.strip():
121
  return history, history
122
- updated_history, _ = chatbot(user_input, sub_category, history)
123
  return updated_history, updated_history
124
 
125
  send_button.click(
126
  handle_chat,
127
- inputs=[user_input, sub_category, chatbot_output],
128
  outputs=[chatbot_output, chatbot_output]
129
  )
130
 
131
  chat_interface.launch()
 
 
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
  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);
81
+ animation: gradientBG 10s ease infinite;
82
+ margin: 0;
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;
94
+ padding: 0.8rem 1.5rem;
95
+ font-size: 1rem;
96
+ font-weight: bold;
97
+ border-radius: 20px;
98
+ border: none;
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;
109
+ padding: 10px;
110
+ border-radius: 15px;
111
+ background: linear-gradient(90deg, #ff758c, #ff7eb3);
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);
118
+ border-radius: 15px;
119
+ padding: 15px;
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
  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
+