Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,124 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
-
import os
|
4 |
|
5 |
# Set OpenAI API Key
|
6 |
-
openai.api_key =
|
7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
elif category == "General":
|
17 |
-
system_message = "Provide general conversation. Be friendly and easygoing."
|
18 |
-
elif category == "Friendly Buddy":
|
19 |
-
system_message = "Respond as a supportive and fun friend. Be informal and light-hearted."
|
20 |
|
|
|
|
|
21 |
try:
|
22 |
response = openai.ChatCompletion.create(
|
23 |
model="llama-3.1-70b-versatile",
|
24 |
messages=[
|
25 |
-
{"role": "
|
26 |
-
{"role": "
|
27 |
]
|
28 |
)
|
29 |
return response.choices[0].message["content"]
|
30 |
except Exception as e:
|
31 |
return f"Error: {str(e)}"
|
32 |
|
33 |
-
# Function to
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
general_keywords = ["hello", "hi", "how", "what", "why", "chat", "talk"]
|
38 |
-
friendly_keywords = ["friend", "buddy", "hangout", "fun", "relax"]
|
39 |
-
|
40 |
-
if any(keyword in user_input.lower() for keyword in stress_keywords):
|
41 |
return "Stress Management"
|
42 |
-
elif
|
|
|
43 |
return "Career Advice"
|
44 |
-
elif
|
45 |
-
|
46 |
-
|
47 |
-
return "Friendly Buddy"
|
48 |
else:
|
49 |
-
|
50 |
-
|
51 |
-
# Function to process input and generate category-specific responses
|
52 |
-
def process_input(user_input, history):
|
53 |
-
category = recognize_category(user_input)
|
54 |
-
bot_response = get_groq_response(user_input, category)
|
55 |
-
history.append((category, user_input, bot_response))
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
64 |
return (
|
65 |
-
|
66 |
-
|
67 |
-
"
|
68 |
-
"
|
69 |
-
history
|
70 |
)
|
71 |
|
72 |
-
# Gradio
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
with gr.Row():
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
fn=
|
91 |
-
inputs=[user_input,
|
92 |
-
outputs=[
|
93 |
)
|
94 |
|
95 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
|
|
3 |
|
4 |
# Set OpenAI API Key
|
5 |
+
openai.api_key = "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"
|
6 |
openai.api_base = "https://api.groq.com/openai/v1"
|
7 |
|
8 |
+
# Dictionary to store categorized chats
|
9 |
+
saved_chats = {
|
10 |
+
"Stress Management": [],
|
11 |
+
"Career Advice": [],
|
12 |
+
"General": [],
|
13 |
+
"Suggestions": []
|
14 |
+
}
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Function to get response from GROQ API
|
17 |
+
def get_groq_response(message):
|
18 |
try:
|
19 |
response = openai.ChatCompletion.create(
|
20 |
model="llama-3.1-70b-versatile",
|
21 |
messages=[
|
22 |
+
{"role": "user", "content": message},
|
23 |
+
{"role": "system", "content": "You will talk like a Motivational Speaker to help people come out of stress."}
|
24 |
]
|
25 |
)
|
26 |
return response.choices[0].message["content"]
|
27 |
except Exception as e:
|
28 |
return f"Error: {str(e)}"
|
29 |
|
30 |
+
# Function to classify messages based on the topic
|
31 |
+
def classify_message(user_message, bot_response):
|
32 |
+
if "stress" in user_message.lower():
|
33 |
+
saved_chats["Stress Management"].append((user_message, bot_response))
|
|
|
|
|
|
|
|
|
34 |
return "Stress Management"
|
35 |
+
elif "career" in user_message.lower():
|
36 |
+
saved_chats["Career Advice"].append((user_message, bot_response))
|
37 |
return "Career Advice"
|
38 |
+
elif "suggestions" in user_message.lower():
|
39 |
+
saved_chats["Suggestions"].append((user_message, bot_response))
|
40 |
+
return "Suggestions"
|
|
|
41 |
else:
|
42 |
+
saved_chats["General"].append((user_message, bot_response))
|
43 |
+
return "General"
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Chatbot function
|
46 |
+
def chatbot(user_input, history=[]):
|
47 |
+
bot_response = get_groq_response(user_input)
|
48 |
+
topic = classify_message(user_input, bot_response)
|
49 |
+
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
|
50 |
+
return history, saved_chats
|
51 |
|
52 |
+
# Function to display saved chats
|
53 |
+
def display_saved_chats():
|
54 |
+
def format_chats(category):
|
55 |
+
return "\n".join([f"**You**: {u}\n**Bot**: {b}" for u, b in saved_chats[category]]) or "No messages yet."
|
56 |
+
|
57 |
return (
|
58 |
+
format_chats("Stress Management"),
|
59 |
+
format_chats("Career Advice"),
|
60 |
+
format_chats("General"),
|
61 |
+
format_chats("Suggestions")
|
|
|
62 |
)
|
63 |
|
64 |
+
# Gradio Interface setup
|
65 |
+
chat_interface = gr.Blocks(css="""
|
66 |
+
body {
|
67 |
+
font-family: 'Poppins', sans-serif;
|
68 |
+
background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fbc2eb, #a1c4fd, #c2e9fb);
|
69 |
+
background-size: 400% 400%;
|
70 |
+
animation: gradientBG 10s ease infinite;
|
71 |
+
margin: 0;
|
72 |
+
padding: 0;
|
73 |
+
color: #333;
|
74 |
+
}
|
75 |
|
76 |
+
@keyframes gradientBG {
|
77 |
+
0% { background-position: 0% 50%; }
|
78 |
+
50% { background-position: 100% 50%; }
|
79 |
+
100% { background-position: 0% 50%; }
|
80 |
+
}
|
81 |
|
82 |
+
header, footer {
|
83 |
+
text-align: center;
|
84 |
+
background: linear-gradient(90deg, #ff758c, #ff7eb3);
|
85 |
+
color: white;
|
86 |
+
padding: 1rem;
|
87 |
+
border-radius: 15px;
|
88 |
+
margin-bottom: 1rem;
|
89 |
+
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
|
90 |
+
}
|
91 |
+
""")
|
92 |
+
|
93 |
+
with chat_interface:
|
94 |
+
with gr.Row():
|
95 |
+
gr.Markdown("<h1 style='text-align:center;'>🌈 Vibrant Motivational Chatbot</h1>")
|
96 |
with gr.Row():
|
97 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
|
98 |
+
send_button = gr.Button("Send")
|
99 |
+
with gr.Row():
|
100 |
+
chatbot_output = gr.Chatbot(label="Chat History")
|
101 |
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
stress_display = gr.Textbox(label="Stress Management", interactive=False, lines=10)
|
105 |
+
with gr.Column():
|
106 |
+
career_display = gr.Textbox(label="Career Advice", interactive=False, lines=10)
|
107 |
+
with gr.Column():
|
108 |
+
general_display = gr.Textbox(label="General", interactive=False, lines=10)
|
109 |
+
with gr.Column():
|
110 |
+
suggestions_display = gr.Textbox(label="Suggestions", interactive=False, lines=10)
|
111 |
+
|
112 |
+
def handle_interaction(user_input, history):
|
113 |
+
if not user_input.strip():
|
114 |
+
return history, *display_saved_chats()
|
115 |
+
updated_history, _ = chatbot(user_input, history)
|
116 |
+
return updated_history, *display_saved_chats()
|
117 |
|
118 |
+
send_button.click(
|
119 |
+
fn=handle_interaction,
|
120 |
+
inputs=[user_input, chatbot_output],
|
121 |
+
outputs=[chatbot_output, stress_display, career_display, general_display, suggestions_display]
|
122 |
)
|
123 |
|
124 |
+
chat_interface.launch()
|