SatyamSinghal commited on
Commit
a6bca1b
·
verified ·
1 Parent(s): a9e7a3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -169
app.py CHANGED
@@ -3,54 +3,24 @@ import openai
3
  import os
4
 
5
  # Set OpenAI API Key
6
- openai.api_key = os.getenv("TRY_NEW_THINGS")
7
- 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": "Talk in a way I talk,Provide soothing advice and tips to help the user manage stress. Be calm, empathetic, and reassuring.",
14
- "Career Advice": "Talk in a way I talk, Offer professional and constructive career advice. Be encouraging, insightful, and action-oriented.",
15
- "General": "Talk in a way I talk, Engage in general conversation. Be friendly, approachable, and easygoing.",
16
- "Funny": "Talk in a way I talk, Respond with humorous remarks and witty commentary. Be entertaining but remain respectful and comedy with some relatable jokes.",
17
- "Flirty": "Talk in a way I talk, Respond playfully and with charm. Keep it light-hearted, fun, and appropriate and flirt in extreme way and you are a girl .",
18
- "Scary": "Talk in a way I talk, Respond with spooky or thrilling remarks. Set a mysterious and eerie tone with some ghost talking with horror and scarry twist.",
19
- "Business Mind": "Talk in a way I talk, Respond with a sharp, professional focus. Offer strategic advice and maintain a results-driven tone.",
20
- "Extrovert": "Talk in a way I talk, Respond with high energy and enthusiasm. Be engaging, sociable, and vibrant.",
21
- "Friendly Buddy": "Talk in a way I talk, Respond as a supportive and fun friend. Be informal, cheerful, and light-hearted.",
22
- "Study Tips": "Talk in a way I talk, Provide effective study strategies, time management advice, and tips for staying organized and focused.",
23
- "Exam Preparation": "Talk in a way I talk, Offer tips for preparing for exams, managing anxiety, and optimizing performance on test day.",
24
- "Project Guidance": "Talk in a way I talk, Provide advice on how to tackle academic projects, group assignments, and presentations effectively.",
25
- "Time Management": "Talk in a way I talk, Help the user manage their time effectively with practical scheduling and prioritization tips.",
26
- "Motivation & Focus": "Talk in a way I talk, Provide encouraging advice to stay motivated and maintain focus on long-term goals.",
27
- "Building Confidence": "Talk in a way I talk, Offer advice to help boost self-esteem, overcome self-doubt, and build confidence.",
28
- "Friendship & Social Skills": "Talk in a way I talk, Provide tips for making friends, improving communication skills, and navigating social dynamics.",
29
- "Networking & Career Building": "Talk in a way I talk, Offer advice on networking, building professional relationships, and finding internships or job opportunities.",
30
- "Work-Life Balance": "Talk in a way I talk, Help the user balance academics, social life, and personal well-being.",
31
- "Mental Health": "Talk in a way I talk, Provide empathetic advice to manage stress, anxiety, and other mental health challenges. Encourage seeking professional help when needed.",
32
- "Physical Health": "Talk in a way I talk, Share tips for maintaining physical health, including fitness, sleep, and nutrition.",
33
- "Budgeting Tips": "Talk in a way I talk, Provide practical advice for managing money, creating a budget, and saving effectively as a student.",
34
- "Finding Part-Time Jobs": "Talk in a way I talk, Share tips for finding and balancing part-time work with academic responsibilities.",
35
- "Scholarships & Financial Aid": "Talk in a way I talk, Provide advice on applying for scholarships, grants, and understanding financial aid options.",
36
- "Resume Building": "Talk in a way I talk, Offer tips for creating an impressive resume tailored for internships or entry-level positions.",
37
- "Interview Preparation": "Talk in a way I talk, Provide advice on how to prepare for interviews, including common questions and presentation tips.",
38
- "Skill Development": "Talk in a way I talk, Suggest ways to build skills, gain certifications, and stand out in competitive fields.",
39
- "Exploring New Hobbies": "Talk in a way I talk, Encourage trying new activities and provide ideas for hobbies that fit college life.",
40
- "Clubs & Extracurriculars": "Talk in a way I talk, Offer advice on joining clubs, participating in activities, and enhancing the college experience.",
41
- "Creative Projects": "Talk in a way I talk, Provide inspiration and resources for personal or group creative endeavors.",
42
- "Abusive(USE ON YOUR OWN RISK)": "Talk in a way I talk, keep the talk very abusive and 18+ , use very toxic abusive words and roast the user with savage replies",
43
- "Roasting(USE ON YOUR OWN RISK)": "Talk in a way I talk, Deliver witty and humorous insults with a creative edge with some double meaning 18+ content.",
44
- "Attitude": "Talk in a way I talk, Respond with bold confidence and a touch of assertiveness.",
45
- "Anime-Fan": "Talk in a way I talk, a person deeply passionate about Japanese animation (anime), frequently engaging with the medium by watching shows, collecting merchandise, participating in cosplay, attending conventions, and discussing the storylines and characters with other fans, often exhibiting a strong interest in Japanese culture more broadly",
46
-
47
  }
48
-
49
- system_message = system_messages.get(category, "Category not recognized. Respond appropriately to the user's input.")
50
-
51
  try:
52
  response = openai.ChatCompletion.create(
53
- model="llama-3.1-70b-versatile",
54
  messages=[
55
  {"role": "system", "content": system_message},
56
  {"role": "user", "content": message}
@@ -63,131 +33,21 @@ def get_groq_response(message, category):
63
  # Chatbot function
64
  def chatbot(user_input, category, history=[]):
65
  bot_response = get_groq_response(user_input, category)
66
-
67
- # Format messages to comply with 'messages' format
68
- history.append({"role": "user", "content": user_input})
69
- history.append({"role": "assistant", "content": bot_response})
70
-
71
  return history, history
72
 
73
- # Categories grouped into main and subcategories
74
  categories = {
75
- "Academic Support": [
76
- "Study Tips", "Exam Preparation", "Project Guidance", "Time Management",
77
- ],
78
- "Mental & Physical Wellness": [
79
- "Stress Management", "Motivation & Focus", "Mental Health", "Physical Health", "Work-Life Balance"
80
- ],
81
- "Career & Financial": [
82
- "Networking & Career Building", "Finding Part-Time Jobs", "Scholarships & Financial Aid",
83
- "Resume Building", "Interview Preparation", "Budgeting Tips", "Skill Development", "Business Mind",
84
- ],
85
- "Personal Growth": [
86
- "Friendship & Social Skills", "Exploring New Hobbies", "Clubs & Extracurriculars", "Creative Projects", "Building Confidence"
87
- ],
88
- "Fun & Casual": [
89
- "Funny", "Flirty", "Scary", "Extrovert", "Friendly Buddy", "Abusive(USE ON YOUR OWN RISK)", "Roasting(USE ON YOUR OWN RISK)", "Attitude", " Poetic", "Anime-Fan"
90
- ]
91
  }
92
 
93
- # Gradio Interface with grouped categories and custom CSS for colors and styles
94
- with gr.Blocks(css="""
95
- body {
96
- font-family: 'Poppins', sans-serif;
97
- background: linear-gradient(120deg, #ff9a9e, #fad0c4, #a1c4fd);
98
- animation: gradientBG 10s ease infinite;
99
- margin: 0;
100
- padding: 0;
101
- color: #333;
102
- }
103
- @keyframes gradientBG {
104
- 0% { background-position: 0% 50%; }
105
- 50% { background-position: 100% 50%; }
106
- 100% { background-position: 0% 50%; }
107
- }
108
- button {
109
- background: linear-gradient(90deg, #ff7eb3, #ff758c); /* Bright gradient */
110
- color: white;
111
- padding: 0.6rem 1.2rem;
112
- font-size: 0.9rem;
113
- font-weight: bold;
114
- border-radius: 20px;
115
- border: none;
116
- cursor: pointer;
117
- transition: transform 0.2s ease, background 0.2s ease;
118
- }
119
- button:hover {
120
- background: linear-gradient(90deg, #ff758c, #ff7eb3); /* Reverse gradient for hover effect */
121
- transform: scale(1.03);
122
- }
123
- header {
124
- text-align: center;
125
- margin-bottom: 20px;
126
- padding: 10px;
127
- border-radius: 15px;
128
- background: linear-gradient(90deg, #ff758c, #ff7eb3);
129
- color: white;
130
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
131
- }
132
- .chat-container {
133
- border: 2px solid #ff7eb3;
134
- background: rgba(255, 255, 255, 0.9);
135
- border-radius: 15px;
136
- padding: 20px;
137
- box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
138
- max-height: 400px; /* Increase height for better visibility */
139
- width: 80%; /* Wider chat container */
140
- margin: 0 auto;
141
- overflow-y: auto; /* Enable vertical scrolling */
142
- font-size: 1.1rem;
143
- color: #333;
144
- line-height: 1.6;
145
- scrollbar-width: thin; /* Thin scrollbar for modern browsers */
146
- scrollbar-color: #ff758c #fad0c4; /* Custom scrollbar colors */
147
- }
148
- /* Add custom scrollbar styles for webkit browsers */
149
- .chat-container::-webkit-scrollbar {
150
- width: 10px;
151
- }
152
- .chat-container::-webkit-scrollbar-thumb {
153
- background: #ff758c;
154
- border-radius: 10px;
155
- }
156
- .chat-container::-webkit-scrollbar-thumb:hover {
157
- background: #ff7eb3;
158
- }
159
- .chat-container::-webkit-scrollbar-track {
160
- background: #fad0c4;
161
- border-radius: 10px;
162
- }
163
- .chat-container .user-message {
164
- font-weight: bold;
165
- color: #6a11cb;
166
- margin-bottom: 10px;
167
- }
168
- .chat-container .bot-message {
169
- font-weight: normal;
170
- color: #2575fc;
171
- margin-bottom: 10px;
172
- }
173
- #header-title {
174
- font-size: 2.5rem;
175
- font-weight: bold;
176
- }
177
- #description {
178
- font-size: 1.2rem;
179
- color: #555;
180
- margin-top: 10px;
181
- text-align: center;
182
- font-style: italic;
183
- }
184
- """) as chat_interface:
185
  with gr.Row():
186
- gr.Markdown("<h1 id='header-title'>Encourage AI 🌟</h1>")
187
 
188
- with gr.Row():
189
- gr.Markdown("<p id='description'>ChatSphere is a dynamic, AI-powered chatbot designed to offer personalized guidance across a range of topics—from academic support to career advice, wellness, and more. With its intuitive interface and tailored responses, ChatSphere acts as your go-to companion for life’s challenges, providing helpful insights, motivation, and practical tips to help you succeed in both personal and professional journeys.</p>")
190
-
191
  with gr.Row():
192
  main_category = gr.Radio(
193
  label="Main Category",
@@ -199,28 +59,26 @@ header {
199
  choices=categories["Academic Support"],
200
  value="Study Tips"
201
  )
202
-
203
  def update_subcategories(selected_main_category):
204
- """Update the subcategory dropdown based on the main category."""
205
  new_subcategories = categories.get(selected_main_category, [])
206
  return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None)
207
-
208
- # Handle main category change to update subcategories
209
  main_category.change(update_subcategories, inputs=main_category, outputs=sub_category)
210
-
211
  with gr.Row():
212
- user_input = gr.Textbox(label="Your Message", placeholder="Type something...", lines=2)
213
  send_button = gr.Button("Send")
214
-
215
  with gr.Row():
216
- chatbot_output = gr.Chatbot(label="Chat History", type="messages", elem_id="chat-container")
217
-
218
  def handle_chat(user_input, sub_category, history):
219
  if not user_input.strip():
220
  return history, history
221
  updated_history, _ = chatbot(user_input, sub_category, history)
222
  return updated_history, updated_history
223
-
224
  send_button.click(
225
  handle_chat,
226
  inputs=[user_input, sub_category, chatbot_output],
 
3
  import os
4
 
5
  # Set OpenAI API Key
6
+ openai.api_key = os.getenv("OPENAI_API_KEY") or "your_api_key_here"
7
+ openai.api_base = "https://api.openai.com/v1"
8
 
9
+ # Function to get response from OpenAI 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
+ # Add more categories as needed...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
18
+
19
+ system_message = system_messages.get(category, "Respond appropriately to the user's input.")
20
+
21
  try:
22
  response = openai.ChatCompletion.create(
23
+ model="gpt-4",
24
  messages=[
25
  {"role": "system", "content": system_message},
26
  {"role": "user", "content": message}
 
33
  # Chatbot function
34
  def chatbot(user_input, category, history=[]):
35
  bot_response = get_groq_response(user_input, category)
36
+ history.append((user_input, bot_response))
 
 
 
 
37
  return history, history
38
 
39
+ # Categories
40
  categories = {
41
+ "Academic Support": ["Study Tips", "Exam Preparation", "Project Guidance", "Time Management"],
42
+ "Mental & Physical Wellness": ["Stress Management", "Motivation & Focus", "Mental Health", "Physical Health"],
43
+ "Career & Financial": ["Networking & Career Building", "Resume Building", "Interview Preparation"],
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  }
45
 
46
+ # Gradio Interface
47
+ with gr.Blocks() as chat_interface:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  with gr.Row():
49
+ gr.Markdown("<h1>Encourage AI 🌟</h1>")
50
 
 
 
 
51
  with gr.Row():
52
  main_category = gr.Radio(
53
  label="Main Category",
 
59
  choices=categories["Academic Support"],
60
  value="Study Tips"
61
  )
62
+
63
  def update_subcategories(selected_main_category):
 
64
  new_subcategories = categories.get(selected_main_category, [])
65
  return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None)
66
+
 
67
  main_category.change(update_subcategories, inputs=main_category, outputs=sub_category)
68
+
69
  with gr.Row():
70
+ user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
71
  send_button = gr.Button("Send")
72
+
73
  with gr.Row():
74
+ chatbot_output = gr.Chatbot(label="Chat History")
75
+
76
  def handle_chat(user_input, sub_category, history):
77
  if not user_input.strip():
78
  return history, history
79
  updated_history, _ = chatbot(user_input, sub_category, history)
80
  return updated_history, updated_history
81
+
82
  send_button.click(
83
  handle_chat,
84
  inputs=[user_input, sub_category, chatbot_output],