Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
4 |
-
import time
|
5 |
|
6 |
hf_token = os.getenv("hf_token")
|
7 |
|
@@ -25,22 +24,20 @@ def get_response(user_input):
|
|
25 |
response = ""
|
26 |
for chunk in stream:
|
27 |
response += chunk.choices[0].delta.content
|
28 |
-
|
29 |
-
time.sleep(0.05) # Optional: Adjust speed of the stream (in seconds)
|
30 |
|
31 |
def chat_interface():
|
32 |
with gr.Blocks() as demo:
|
33 |
-
with gr.Row():
|
34 |
-
with gr.Column():
|
35 |
chat_output = gr.Chatbot(
|
36 |
elem_id="chat-box",
|
37 |
label="Xylaria 1.4 Senoa Chatbot",
|
38 |
-
show_label=False
|
39 |
-
type="messages" # Specify type for correct message format
|
40 |
)
|
41 |
|
42 |
-
with gr.Row(elem_id="input-row"):
|
43 |
-
with gr.Column():
|
44 |
input_textbox = gr.Textbox(
|
45 |
label="Type your message",
|
46 |
placeholder="Ask me anything...",
|
@@ -50,52 +47,32 @@ def chat_interface():
|
|
50 |
elem_id="user-input",
|
51 |
show_label=False
|
52 |
)
|
53 |
-
with gr.Column():
|
54 |
send_button = gr.Button("Send", elem_id="send-btn")
|
55 |
|
56 |
def submit_input(user_input, chat_history):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
chat_history.append({"role": "user", "content": user_input})
|
61 |
-
return "", chat_history # Clear input field
|
62 |
|
63 |
input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
64 |
send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
65 |
|
66 |
-
def handle_response(user_input, chat_history):
|
67 |
-
# Initialize the chat history if it's empty
|
68 |
-
if not chat_history:
|
69 |
-
chat_history = []
|
70 |
-
chat_history.append({"role": "user", "content": user_input})
|
71 |
-
|
72 |
-
response_stream = get_response(user_input)
|
73 |
-
for partial_response in response_stream:
|
74 |
-
chat_history[-1] = {"role": "user", "content": user_input} # Update the last user message with content
|
75 |
-
chat_history.append({"role": "assistant", "content": partial_response}) # Add assistant's response
|
76 |
-
yield "", chat_history # Return the updated chat history progressively
|
77 |
-
|
78 |
-
input_textbox.submit(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
|
79 |
-
send_button.click(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
|
80 |
-
|
81 |
demo.css = """
|
82 |
#input-row {
|
83 |
position: absolute;
|
84 |
bottom: 10px;
|
85 |
width: 100%;
|
86 |
padding: 10px;
|
87 |
-
background-color: #
|
88 |
border-top: 1px solid #ddd;
|
89 |
}
|
90 |
#chat-box {
|
91 |
height: calc(100vh - 100px); /* Adjust the height of chat history */
|
92 |
overflow-y: scroll;
|
93 |
}
|
94 |
-
#user-input, #send-btn {
|
95 |
-
background-color: #333; /* Keep dark gray background */
|
96 |
-
}
|
97 |
"""
|
98 |
-
|
99 |
return demo
|
100 |
|
101 |
demo = chat_interface()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
|
|
4 |
|
5 |
hf_token = os.getenv("hf_token")
|
6 |
|
|
|
24 |
response = ""
|
25 |
for chunk in stream:
|
26 |
response += chunk.choices[0].delta.content
|
27 |
+
return response
|
|
|
28 |
|
29 |
def chat_interface():
|
30 |
with gr.Blocks() as demo:
|
31 |
+
with gr.Row():
|
32 |
+
with gr.Column(scale=1):
|
33 |
chat_output = gr.Chatbot(
|
34 |
elem_id="chat-box",
|
35 |
label="Xylaria 1.4 Senoa Chatbot",
|
36 |
+
show_label=False
|
|
|
37 |
)
|
38 |
|
39 |
+
with gr.Row(elem_id="input-row", scale=0.2):
|
40 |
+
with gr.Column(scale=0.8):
|
41 |
input_textbox = gr.Textbox(
|
42 |
label="Type your message",
|
43 |
placeholder="Ask me anything...",
|
|
|
47 |
elem_id="user-input",
|
48 |
show_label=False
|
49 |
)
|
50 |
+
with gr.Column(scale=0.2):
|
51 |
send_button = gr.Button("Send", elem_id="send-btn")
|
52 |
|
53 |
def submit_input(user_input, chat_history):
|
54 |
+
response = get_response(user_input)
|
55 |
+
chat_history.append((user_input, response))
|
56 |
+
return "", chat_history
|
|
|
|
|
57 |
|
58 |
input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
59 |
send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
demo.css = """
|
62 |
#input-row {
|
63 |
position: absolute;
|
64 |
bottom: 10px;
|
65 |
width: 100%;
|
66 |
padding: 10px;
|
67 |
+
background-color: #f5f5f5;
|
68 |
border-top: 1px solid #ddd;
|
69 |
}
|
70 |
#chat-box {
|
71 |
height: calc(100vh - 100px); /* Adjust the height of chat history */
|
72 |
overflow-y: scroll;
|
73 |
}
|
|
|
|
|
|
|
74 |
"""
|
75 |
+
|
76 |
return demo
|
77 |
|
78 |
demo = chat_interface()
|