Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -170,34 +170,54 @@ custom_css = """
|
|
| 170 |
"""
|
| 171 |
|
| 172 |
def respond(message, history):
|
| 173 |
-
|
| 174 |
-
history
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
with gr.Blocks(css=custom_css) as demo:
|
| 178 |
gr.HTML("<div id='header'>DivaBot</div>")
|
| 179 |
-
|
| 180 |
chatbot = gr.Chatbot(elem_id="chatbox", height=400)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
with gr.Column():
|
| 184 |
-
# HTML custom input bar
|
| 185 |
-
gr.HTML("""
|
| 186 |
-
<div class="input-container">
|
| 187 |
-
<input id="user-input" placeholder="Type your message..." />
|
| 188 |
-
<button id="send-btn">➤</button>
|
| 189 |
-
</div>
|
| 190 |
-
""")
|
| 191 |
-
|
| 192 |
-
# Normal Gradio textbox (hidden) to still connect logic
|
| 193 |
msg = gr.Textbox(visible=False)
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 196 |
|
| 197 |
demo.launch()
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
-
#launching chatbot
|
| 202 |
-
#chatbot.launch()
|
| 203 |
#You may run into errors when you're trying different models. To see the error messages, set debug to True in launch()
|
|
|
|
| 170 |
"""
|
| 171 |
|
| 172 |
def respond(message, history):
|
| 173 |
+
messages = [{"role": "assistant", "content": "You are a friendly chatbot."}]
|
| 174 |
+
if history:
|
| 175 |
+
messages.extend(history)
|
| 176 |
+
messages.append({"role": "user", "content": message})
|
| 177 |
+
response = client.chat_completion(messages, max_tokens=100)
|
| 178 |
+
return history + [(message, response['choices'][0]['message']['content'].strip())], ""
|
| 179 |
|
| 180 |
with gr.Blocks(css=custom_css) as demo:
|
| 181 |
gr.HTML("<div id='header'>DivaBot</div>")
|
|
|
|
| 182 |
chatbot = gr.Chatbot(elem_id="chatbox", height=400)
|
| 183 |
|
| 184 |
+
# Hidden textbox to store the message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
msg = gr.Textbox(visible=False)
|
| 186 |
|
| 187 |
+
# Visible custom input bar with send button
|
| 188 |
+
gr.HTML("""
|
| 189 |
+
<div class="input-container">
|
| 190 |
+
<input id="user-input" placeholder="Type your message..." />
|
| 191 |
+
<button id="send-btn">➤</button>
|
| 192 |
+
</div>
|
| 193 |
+
<script>
|
| 194 |
+
const sendBtn = document.getElementById('send-btn');
|
| 195 |
+
const userInput = document.getElementById('user-input');
|
| 196 |
+
sendBtn.onclick = () => {
|
| 197 |
+
const value = userInput.value;
|
| 198 |
+
if (value.trim() !== "") {
|
| 199 |
+
// Set the hidden Gradio textbox value
|
| 200 |
+
const textbox = document.querySelector('textarea');
|
| 201 |
+
textbox.value = value;
|
| 202 |
+
textbox.dispatchEvent(new Event('input', { bubbles: true }));
|
| 203 |
+
// Trigger submit
|
| 204 |
+
document.querySelector('textarea').closest('form').dispatchEvent(new Event('submit', { bubbles: true }));
|
| 205 |
+
userInput.value = "";
|
| 206 |
+
}
|
| 207 |
+
};
|
| 208 |
+
userInput.addEventListener("keypress", function(e) {
|
| 209 |
+
if (e.key === "Enter") {
|
| 210 |
+
sendBtn.click();
|
| 211 |
+
e.preventDefault();
|
| 212 |
+
}
|
| 213 |
+
});
|
| 214 |
+
</script>
|
| 215 |
+
""")
|
| 216 |
+
|
| 217 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 218 |
|
| 219 |
demo.launch()
|
| 220 |
|
| 221 |
|
| 222 |
|
|
|
|
|
|
|
| 223 |
#You may run into errors when you're trying different models. To see the error messages, set debug to True in launch()
|