Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,158 +3,52 @@ import openai
|
|
3 |
import os
|
4 |
|
5 |
# Set OpenAI API Key
|
6 |
-
openai.api_key = os.getenv("
|
7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
8 |
|
9 |
-
# Dictionary to store categorized chats
|
10 |
-
saved_chats = {
|
11 |
-
"Stress Management": [],
|
12 |
-
"Career Advice": [],
|
13 |
-
"General": [],
|
14 |
-
"Suggestions": []
|
15 |
-
}
|
16 |
-
|
17 |
# Function to get response from GROQ API
|
18 |
-
def get_groq_response(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
try:
|
20 |
response = openai.ChatCompletion.create(
|
21 |
model="llama-3.1-70b-versatile",
|
22 |
messages=[
|
23 |
-
{"role": "
|
24 |
-
{"role": "
|
25 |
]
|
26 |
)
|
27 |
return response.choices[0].message["content"]
|
28 |
except Exception as e:
|
29 |
return f"Error: {str(e)}"
|
30 |
|
31 |
-
# Function to classify messages based on the topic
|
32 |
-
def classify_message(user_message, bot_response):
|
33 |
-
if "stress" in user_message.lower():
|
34 |
-
saved_chats["Stress Management"].append((user_message, bot_response))
|
35 |
-
return "Stress Management"
|
36 |
-
elif "career" in user_message.lower():
|
37 |
-
saved_chats["Career Advice"].append((user_message, bot_response))
|
38 |
-
return "Career Advice"
|
39 |
-
elif "suggestions" in user_message.lower():
|
40 |
-
saved_chats["Suggestions"].append((user_message, bot_response))
|
41 |
-
return "Suggestions"
|
42 |
-
else:
|
43 |
-
saved_chats["General"].append((user_message, bot_response))
|
44 |
-
return "General"
|
45 |
-
|
46 |
# Chatbot function
|
47 |
-
def chatbot(user_input, history=[]):
|
48 |
-
bot_response = get_groq_response(user_input)
|
49 |
-
|
50 |
-
history
|
51 |
-
return history, saved_chats
|
52 |
-
|
53 |
-
# Function to display saved chats
|
54 |
-
def display_saved_chats():
|
55 |
-
display = ""
|
56 |
-
for category, chats in saved_chats.items():
|
57 |
-
display += f"<div class='chat-category'><h3>{category}</h3>"
|
58 |
-
for user_message, bot_response in chats:
|
59 |
-
display += f"<p><strong>You:</strong> {user_message}<br><strong>Bot:</strong> {bot_response}</p>"
|
60 |
-
display += "</div>"
|
61 |
-
return display.strip()
|
62 |
|
63 |
# Gradio Interface setup
|
64 |
-
chat_interface = gr.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
}
|
79 |
-
header, footer {
|
80 |
-
text-align: center;
|
81 |
-
background: linear-gradient(90deg, #ff758c, #ff7eb3);
|
82 |
-
color: white;
|
83 |
-
padding: 1rem;
|
84 |
-
border-radius: 15px;
|
85 |
-
margin-bottom: 1rem;
|
86 |
-
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
|
87 |
-
}
|
88 |
-
.chatbot-container {
|
89 |
-
display: flex;
|
90 |
-
flex-direction: column;
|
91 |
-
justify-content: space-between;
|
92 |
-
border-radius: 15px;
|
93 |
-
background: rgba(255, 255, 255, 0.8);
|
94 |
-
padding: 1rem;
|
95 |
-
height: 400px;
|
96 |
-
overflow-y: auto;
|
97 |
-
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
|
98 |
-
}
|
99 |
-
input, button {
|
100 |
-
border: none;
|
101 |
-
padding: 0.8rem;
|
102 |
-
border-radius: 10px;
|
103 |
-
margin: 0.5rem 0;
|
104 |
-
outline: none;
|
105 |
-
}
|
106 |
-
input {
|
107 |
-
background: #fff;
|
108 |
-
color: #ffffff;
|
109 |
-
font-size: 1rem;
|
110 |
-
}
|
111 |
-
button {
|
112 |
-
background: linear-gradient(90deg, #6a11cb, #2575fc);
|
113 |
-
color: white;
|
114 |
-
font-weight: bold;
|
115 |
-
cursor: pointer;
|
116 |
-
transition: transform 0.2s, background 0.2s;
|
117 |
-
}
|
118 |
-
button:hover {
|
119 |
-
transform: scale(1.05);
|
120 |
-
background: linear-gradient(90deg, #2575fc, #6a11cb);
|
121 |
-
}
|
122 |
-
.chat-category {
|
123 |
-
background: rgba(0, 0, 0, 0.05);
|
124 |
-
border: 2px solid #ff7eb3;
|
125 |
-
margin: 1rem 0;
|
126 |
-
padding: 1rem;
|
127 |
-
border-radius: 10px;
|
128 |
-
transition: transform 0.2s, box-shadow 0.2s;
|
129 |
-
}
|
130 |
-
.chat-category:hover {
|
131 |
-
transform: translateY(-3px);
|
132 |
-
box-shadow: 0px 4px 15px rgba(255, 127, 179, 0.5);
|
133 |
-
}
|
134 |
-
""")
|
135 |
-
|
136 |
-
with chat_interface:
|
137 |
-
with gr.Row():
|
138 |
-
gr.Markdown("<h1 style='text-align:center;'>🌈 Vibrant Motivational Chatbot</h1>")
|
139 |
-
with gr.Row():
|
140 |
-
gr.Markdown("**Feeling stressed or unmotivated? Share your thoughts and let me help!**")
|
141 |
-
with gr.Row():
|
142 |
-
chatbot_output = gr.Chatbot(label="Motivator Bot")
|
143 |
-
with gr.Row():
|
144 |
-
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
|
145 |
-
send_button = gr.Button("Send")
|
146 |
-
with gr.Row():
|
147 |
-
saved_chats_display = gr.HTML(label="Saved Chats", value=display_saved_chats())
|
148 |
-
refresh_button = gr.Button("Refresh Saved Chats")
|
149 |
-
|
150 |
-
def handle_interaction(user_input, history):
|
151 |
-
if not user_input.strip():
|
152 |
-
return history, display_saved_chats()
|
153 |
-
updated_history, _ = chatbot(user_input, history)
|
154 |
-
return updated_history, display_saved_chats()
|
155 |
-
|
156 |
-
send_button.click(handle_interaction, inputs=[user_input, chatbot_output], outputs=[chatbot_output, saved_chats_display])
|
157 |
-
refresh_button.click(display_saved_chats, inputs=[], outputs=saved_chats_display)
|
158 |
-
|
159 |
-
if __name__ == "__main__":
|
160 |
-
chat_interface.launch()
|
|
|
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 |
+
# System message to tailor responses based on the category
|
12 |
+
system_message = ""
|
13 |
+
if category == "Stress Management":
|
14 |
+
system_message = "Provide soothing advice and tips to help the user manage stress. Be calm and empathetic."
|
15 |
+
elif category == "Career Advice":
|
16 |
+
system_message = "Provide professional and constructive career advice. Be encouraging and helpful."
|
17 |
+
elif category == "General":
|
18 |
+
system_message = "Provide general conversation. Be friendly and easygoing."
|
19 |
+
elif category == "Friendly Buddy":
|
20 |
+
system_message = "Respond as a supportive and fun friend. Be informal and light-hearted."
|
21 |
+
|
22 |
try:
|
23 |
response = openai.ChatCompletion.create(
|
24 |
model="llama-3.1-70b-versatile",
|
25 |
messages=[
|
26 |
+
{"role": "system", "content": system_message},
|
27 |
+
{"role": "user", "content": message}
|
28 |
]
|
29 |
)
|
30 |
return response.choices[0].message["content"]
|
31 |
except Exception as e:
|
32 |
return f"Error: {str(e)}"
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Chatbot function
|
35 |
+
def chatbot(user_input, category, history=[]):
|
36 |
+
bot_response = get_groq_response(user_input, category)
|
37 |
+
history.append((user_input, bot_response))
|
38 |
+
return history, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Gradio Interface setup
|
41 |
+
chat_interface = gr.Interface(
|
42 |
+
fn=chatbot,
|
43 |
+
inputs=[
|
44 |
+
"text", # User input
|
45 |
+
gr.Dropdown(choices=["Stress Management", "Career Advice", "General", "Friendly Buddy"], label="Choose Chat Category"), # Category selection
|
46 |
+
"state" # History
|
47 |
+
],
|
48 |
+
outputs=["chatbot", "state"],
|
49 |
+
live=False,
|
50 |
+
title="Meet your Personal Assistant: Your Helpful and Caring Chatbot",
|
51 |
+
description="This chatbot is here to help you with anything you need, whether it’s answering questions, offering support, or guiding you through tasks. With a friendly and empathetic approach, it listens carefully to your concerns and provides thoughtful, understanding responses. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable."
|
52 |
+
)
|
53 |
+
|
54 |
+
chat_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|