Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,220 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
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 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
|
5 |
+
# Set OpenAI API Key
|
6 |
+
openai.api_key = os.getenv("GROQ_API_KEY")
|
7 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
|
|
8 |
|
9 |
+
# Dictionary to store categorized chats
|
10 |
+
saved_chats = {
|
11 |
+
"Stress Management": [],
|
12 |
+
"Career Advice": [],
|
13 |
+
"General": [],
|
14 |
+
"Suggestions": []
|
15 |
+
}
|
16 |
|
17 |
+
# Function to get response from GROQ API
|
18 |
+
def get_groq_response(message):
|
19 |
+
try:
|
20 |
+
response = openai.ChatCompletion.create(
|
21 |
+
model="llama-3.1-70b-versatile",
|
22 |
+
messages=[
|
23 |
+
{"role": "user", "content": message},
|
24 |
+
{"role": "system", "content": "You will talk like a Motivational Speaker to help people come out of stress."}
|
25 |
+
]
|
26 |
+
)
|
27 |
+
return response.choices[0].message["content"]
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error: {str(e)}"
|
30 |
|
31 |
+
# Function to classify messages based on the topic
|
32 |
+
def classify_message(user_message, bot_response):
|
33 |
+
if "stress" in user_message.lower():
|
34 |
+
saved_chats["Stress Management"].append((user_message, bot_response))
|
35 |
+
return "Stress Management"
|
36 |
+
elif "career" in user_message.lower():
|
37 |
+
saved_chats["Career Advice"].append((user_message, bot_response))
|
38 |
+
return "Career Advice"
|
39 |
+
elif "suggestions" in user_message.lower():
|
40 |
+
saved_chats["Suggestions"].append((user_message, bot_response))
|
41 |
+
return "Suggestions"
|
42 |
+
else:
|
43 |
+
saved_chats["General"].append((user_message, bot_response))
|
44 |
+
return "General"
|
45 |
|
46 |
+
# Chatbot function
|
47 |
+
def chatbot(user_input, history=[]):
|
48 |
+
bot_response = get_groq_response(user_input)
|
49 |
+
topic = classify_message(user_input, bot_response)
|
50 |
+
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
|
51 |
+
return history, saved_chats
|
52 |
|
53 |
+
# Custom HTML, CSS, and JavaScript
|
54 |
+
custom_html = """
|
55 |
+
<!DOCTYPE html>
|
56 |
+
<html lang="en">
|
57 |
+
<head>
|
58 |
+
<meta charset="UTF-8">
|
59 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
60 |
+
<title>Motivational Chatbot</title>
|
61 |
+
<style>
|
62 |
+
body {
|
63 |
+
font-family: 'Poppins', sans-serif;
|
64 |
+
background: #121212;
|
65 |
+
color: #f3f3f3;
|
66 |
+
margin: 0;
|
67 |
+
padding: 0;
|
68 |
+
display: flex;
|
69 |
+
justify-content: center;
|
70 |
+
align-items: center;
|
71 |
+
height: 100vh;
|
72 |
+
}
|
73 |
+
.container {
|
74 |
+
width: 90%;
|
75 |
+
max-width: 800px;
|
76 |
+
background: #1e1e1e;
|
77 |
+
border-radius: 15px;
|
78 |
+
box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.5);
|
79 |
+
overflow: hidden;
|
80 |
+
display: flex;
|
81 |
+
flex-direction: column;
|
82 |
+
}
|
83 |
+
header {
|
84 |
+
background: #282828;
|
85 |
+
padding: 20px;
|
86 |
+
text-align: center;
|
87 |
+
color: #ffffff;
|
88 |
+
border-bottom: 2px solid #ff6a95;
|
89 |
+
}
|
90 |
+
header h1 {
|
91 |
+
margin: 0;
|
92 |
+
font-size: 1.8rem;
|
93 |
+
}
|
94 |
+
header p {
|
95 |
+
margin: 5px 0 0;
|
96 |
+
font-size: 1rem;
|
97 |
+
color: #cccccc;
|
98 |
+
}
|
99 |
+
main {
|
100 |
+
flex: 1;
|
101 |
+
padding: 20px;
|
102 |
+
display: flex;
|
103 |
+
flex-direction: column;
|
104 |
+
}
|
105 |
+
.chat-container {
|
106 |
+
display: flex;
|
107 |
+
flex-direction: column;
|
108 |
+
height: 100%;
|
109 |
+
}
|
110 |
+
#chat-output {
|
111 |
+
flex: 1;
|
112 |
+
overflow-y: auto;
|
113 |
+
background: #212121;
|
114 |
+
border-radius: 10px;
|
115 |
+
padding: 10px;
|
116 |
+
margin-bottom: 10px;
|
117 |
+
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
|
118 |
+
}
|
119 |
+
.chat-input {
|
120 |
+
display: flex;
|
121 |
+
gap: 10px;
|
122 |
+
}
|
123 |
+
textarea {
|
124 |
+
flex: 1;
|
125 |
+
padding: 10px;
|
126 |
+
border-radius: 5px;
|
127 |
+
border: none;
|
128 |
+
background: #333;
|
129 |
+
color: #fff;
|
130 |
+
resize: none;
|
131 |
+
font-size: 1rem;
|
132 |
+
}
|
133 |
+
textarea:focus {
|
134 |
+
outline: none;
|
135 |
+
box -shadow: 0 0 5px #ff6a95;
|
136 |
+
}
|
137 |
+
button {
|
138 |
+
padding: 10px 20px;
|
139 |
+
background: linear-gradient(45deg, #ff6a95, #ff4b81);
|
140 |
+
border: none;
|
141 |
+
border-radius: 5px;
|
142 |
+
color: #fff;
|
143 |
+
font-weight: bold;
|
144 |
+
cursor: pointer;
|
145 |
+
transition: background 0.3s, transform 0.3s;
|
146 |
+
}
|
147 |
+
button:hover {
|
148 |
+
background: linear-gradient(45deg, #ff4b81, #ff6a95);
|
149 |
+
transform: scale(1.05);
|
150 |
+
}
|
151 |
+
footer {
|
152 |
+
background: #282828;
|
153 |
+
text-align: center;
|
154 |
+
padding: 10px;
|
155 |
+
color: #999;
|
156 |
+
font-size: 0.9rem;
|
157 |
+
border-top: 2px solid #ff6a95;
|
158 |
+
}
|
159 |
+
</style>
|
160 |
+
</head>
|
161 |
+
<body>
|
162 |
+
<div class="container">
|
163 |
+
<header>
|
164 |
+
<h1>✨ Motivational Chatbot ✨</h1>
|
165 |
+
<p>Your personal motivational speaker!</p>
|
166 |
+
</header>
|
167 |
+
<main>
|
168 |
+
<div class="chat-container">
|
169 |
+
<div id="chat-output"></div>
|
170 |
+
<div class="chat-input">
|
171 |
+
<textarea id="user-input" placeholder="Type your message here..."></textarea>
|
172 |
+
<button id="send-btn">Send</button>
|
173 |
+
</div>
|
174 |
+
</div>
|
175 |
+
</main>
|
176 |
+
<footer>
|
177 |
+
<p>Developed with ❤️ using OpenAI APIs</p>
|
178 |
+
</footer>
|
179 |
+
</div>
|
180 |
+
<script>
|
181 |
+
document.getElementById("send-btn").addEventListener("click", async () => {
|
182 |
+
const userInput = document.getElementById("user-input").value.trim();
|
183 |
+
if (!userInput) return;
|
184 |
|
185 |
+
// Display user input
|
186 |
+
const chatOutput = document.getElementById("chat-output");
|
187 |
+
const userMessage = `<div class="user-message"><strong>You:</strong> ${userInput}</div>`;
|
188 |
+
chatOutput.innerHTML += userMessage;
|
|
|
|
|
|
|
|
|
189 |
|
190 |
+
// Call backend
|
191 |
+
const response = await fetch("/chat", {
|
192 |
+
method: "POST",
|
193 |
+
headers: { "Content-Type": "application/json" },
|
194 |
+
body: JSON.stringify({ user_input: userInput })
|
195 |
+
});
|
196 |
+
const botResponse = await response.json();
|
197 |
|
198 |
+
// Display bot response
|
199 |
+
const botMessage = `<div class="bot-message"><strong>Bot:</strong> ${botResponse}</div>`;
|
200 |
+
chatOutput.innerHTML += botMessage;
|
201 |
|
202 |
+
// Clear input
|
203 |
+
document.getElementById("user-input").value = "";
|
204 |
+
chatOutput.scrollTop = chatOutput.scrollHeight;
|
205 |
+
});
|
206 |
+
</script>
|
207 |
+
</body>
|
208 |
+
</html>
|
209 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
+
# Launch Gradio interface with custom frontend
|
212 |
+
interface = gr.Interface(
|
213 |
+
fn=chatbot,
|
214 |
+
inputs=[gr.Textbox(lines=2, label="Your Message"), gr.State()],
|
215 |
+
outputs=[gr.JSON(), gr.State()],
|
216 |
+
live=True
|
217 |
+
)
|
218 |
|
219 |
+
app = gr.HTML(custom_html)
|
220 |
+
interface.launch(share=True)
|