Spaces:
Sleeping
Sleeping
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(): | |
def format_chats(category): | |
return "\n".join([f"**You**: {u}\n**Bot**: {b}" for u, b in saved_chats[category]]) or "No messages yet." | |
return ( | |
format_chats("Stress Management"), | |
format_chats("Career Advice"), | |
format_chats("General"), | |
format_chats("Suggestions") | |
) | |
# 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); | |
} | |
""") | |
with chat_interface: | |
with gr.Row(): | |
gr.Markdown("<h1 style='text-align:center;'>π Vibrant Motivational Chatbot</h1>") | |
with gr.Row(): | |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...") | |
send_button = gr.Button("Send") | |
with gr.Row(): | |
chatbot_output = gr.Chatbot(label="Chat History") | |
with gr.Row(): | |
with gr.Column(): | |
stress_display = gr.Textbox(label="Stress Management", interactive=False, lines=10) | |
with gr.Column(): | |
career_display = gr.Textbox(label="Career Advice", interactive=False, lines=10) | |
with gr.Column(): | |
general_display = gr.Textbox(label="General", interactive=False, lines=10) | |
with gr.Column(): | |
suggestions_display = gr.Textbox(label="Suggestions", interactive=False, lines=10) | |
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( | |
fn=handle_interaction, | |
inputs=[user_input, chatbot_output], | |
outputs=[chatbot_output, stress_display, career_display, general_display, suggestions_display] | |
) | |
chat_interface.launch() | |