Spaces:
Sleeping
Sleeping
File size: 4,228 Bytes
8df15f7 3ba6e71 72f9a72 46bfe87 3ba6e71 46bfe87 72f9a72 46bfe87 8df15f7 46bfe87 8df15f7 72f9a72 46bfe87 db6c1b8 46bfe87 db6c1b8 46bfe87 db6c1b8 46bfe87 72f9a72 46bfe87 0a7bc28 46bfe87 0a7bc28 46bfe87 0a7bc28 46bfe87 1f183a9 46bfe87 db6c1b8 46bfe87 0a7bc28 46bfe87 91166b8 46bfe87 91166b8 46bfe87 91166b8 46bfe87 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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()
|