File size: 5,128 Bytes
8df15f7
3ba6e71
4fbdca3
3ba6e71
72f9a72
4fbdca3
3ba6e71
 
46bfe87
 
 
 
 
 
 
72f9a72
46bfe87
 
8df15f7
 
 
 
46bfe87
 
8df15f7
 
 
 
 
72f9a72
46bfe87
 
 
 
db6c1b8
46bfe87
 
db6c1b8
46bfe87
 
 
db6c1b8
46bfe87
 
72f9a72
46bfe87
 
 
 
 
 
0a7bc28
46bfe87
 
a5314d2
 
 
 
 
 
 
0a7bc28
46bfe87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5314d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fbdca3
a5314d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bfe87
 
 
 
 
a5314d2
4fbdca3
a5314d2
 
0a7bc28
46bfe87
 
 
a5314d2
 
46bfe87
 
 
a5314d2
46bfe87
a5314d2
 
 
 
91166b8
4fbdca3
a5314d2
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import gradio as gr
import openai
import os

# Set OpenAI API Key
openai.api_key = os.getenv("GROQ_API_KEY")
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: #ffffff;
    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()