Encourage-AI / app.py
arpit13's picture
Update app.py
a5314d2 verified
raw
history blame
5.15 kB
import gradio as gr
import openai
# Set OpenAI API Key
openai.api_key = "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"
openai.api_base = "https://api.groq.com/openai/v1"
# Dictionary to store categorized chats
saved_chats = {
"Stress Management": [],
"Career Advice": [],
"General": [],
"Suggestions": []
}
# Function to get response from GROQ API
def get_groq_response(message):
try:
response = openai.ChatCompletion.create(
model="llama-3.1-70b-versatile",
messages=[
{"role": "user", "content": message},
{"role": "system", "content": "You will talk like a Motivational Speaker to help people come out of stress."}
]
)
return response.choices[0].message["content"]
except Exception as e:
return f"Error: {str(e)}"
# Function to classify messages based on the topic
def classify_message(user_message, bot_response):
if "stress" in user_message.lower():
saved_chats["Stress Management"].append((user_message, bot_response))
return "Stress Management"
elif "career" in user_message.lower():
saved_chats["Career Advice"].append((user_message, bot_response))
return "Career Advice"
elif "suggestions" in user_message.lower():
saved_chats["Suggestions"].append((user_message, bot_response))
return "Suggestions"
else:
saved_chats["General"].append((user_message, bot_response))
return "General"
# Chatbot function
def chatbot(user_input, history=[]):
bot_response = get_groq_response(user_input)
topic = classify_message(user_input, bot_response)
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
return history, saved_chats
# Function to display saved chats
def display_saved_chats():
display = ""
for category, chats in saved_chats.items():
display += f"<div class='chat-category'><h3>{category}</h3>"
for user_message, bot_response in chats:
display += f"<p><strong>You:</strong> {user_message}<br><strong>Bot:</strong> {bot_response}</p>"
display += "</div>"
return display.strip()
# Gradio Interface setup
chat_interface = gr.Blocks(css="""
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fbc2eb, #a1c4fd, #c2e9fb);
background-size: 400% 400%;
animation: gradientBG 10s ease infinite;
margin: 0;
padding: 0;
color: #333;
}
@keyframes gradientBG {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
header, footer {
text-align: center;
background: linear-gradient(90deg, #ff758c, #ff7eb3);
color: white;
padding: 1rem;
border-radius: 15px;
margin-bottom: 1rem;
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
}
.chatbot-container {
display: flex;
flex-direction: column;
justify-content: space-between;
border-radius: 15px;
background: rgba(255, 255, 255, 0.8);
padding: 1rem;
height: 400px;
overflow-y: auto;
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
}
input, button {
border: none;
padding: 0.8rem;
border-radius: 10px;
margin: 0.5rem 0;
outline: none;
}
input {
background: #fff;
color: #333;
font-size: 1rem;
}
button {
background: linear-gradient(90deg, #6a11cb, #2575fc);
color: white;
font-weight: bold;
cursor: pointer;
transition: transform 0.2s, background 0.2s;
}
button:hover {
transform: scale(1.05);
background: linear-gradient(90deg, #2575fc, #6a11cb);
}
.chat-category {
background: rgba(0, 0, 0, 0.05);
border: 2px solid #ff7eb3;
margin: 1rem 0;
padding: 1rem;
border-radius: 10px;
transition: transform 0.2s, box-shadow 0.2s;
}
.chat-category:hover {
transform: translateY(-3px);
box-shadow: 0px 4px 15px rgba(255, 127, 179, 0.5);
}
""")
with chat_interface:
with gr.Row():
gr.Markdown("<h1 style='text-align:center;'>🌈 Vibrant Motivational Chatbot</h1>")
with gr.Row():
gr.Markdown("*Feeling stressed or unmotivated? Share your thoughts and let me help!*")
with gr.Row():
chatbot_output = gr.Chatbot(label="Motivator Bot")
with gr.Row():
user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
send_button = gr.Button("Send")
with gr.Row():
saved_chats_display = gr.HTML(label="Saved Chats", value=display_saved_chats())
refresh_button = gr.Button("Refresh Saved Chats")
def handle_interaction(user_input, history):
if not user_input.strip():
return history, display_saved_chats()
updated_history, _ = chatbot(user_input, history)
return updated_history, display_saved_chats()
send_button.click(handle_interaction, inputs=[user_input, chatbot_output], outputs=[chatbot_output, saved_chats_display])
refresh_button.click(display_saved_chats, inputs=[], outputs=saved_chats_display)
if _name_ == "_main_":
chat_interface.launch()