Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
|
|
4 |
|
5 |
hf_token = os.getenv("hf_token")
|
6 |
|
@@ -24,20 +25,22 @@ def get_response(user_input):
|
|
24 |
response = ""
|
25 |
for chunk in stream:
|
26 |
response += chunk.choices[0].delta.content
|
27 |
-
|
|
|
28 |
|
29 |
def chat_interface():
|
30 |
with gr.Blocks() as demo:
|
31 |
-
with gr.Row():
|
32 |
-
with gr.Column(
|
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"
|
40 |
-
with gr.Column(
|
41 |
input_textbox = gr.Textbox(
|
42 |
label="Type your message",
|
43 |
placeholder="Ask me anything...",
|
@@ -47,17 +50,33 @@ def chat_interface():
|
|
47 |
elem_id="user-input",
|
48 |
show_label=False
|
49 |
)
|
50 |
-
with gr.Column(
|
51 |
send_button = gr.Button("Send", elem_id="send-btn")
|
52 |
|
53 |
def submit_input(user_input, chat_history):
|
54 |
-
|
55 |
-
|
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;
|
@@ -72,7 +91,7 @@ def chat_interface():
|
|
72 |
overflow-y: scroll;
|
73 |
}
|
74 |
"""
|
75 |
-
|
76 |
return demo
|
77 |
|
78 |
demo = chat_interface()
|
|
|
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 |
response = ""
|
26 |
for chunk in stream:
|
27 |
response += chunk.choices[0].delta.content
|
28 |
+
yield response # Yielding progressively as the model generates output
|
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(): # No 'min_width' argument here
|
34 |
+
with gr.Column(): # No 'min_width' argument here
|
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(): # No 'min_width' argument here
|
44 |
input_textbox = gr.Textbox(
|
45 |
label="Type your message",
|
46 |
placeholder="Ask me anything...",
|
|
|
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 |
+
chat_history.append({"role": "user", "content": user_input})
|
58 |
+
return "", chat_history # Clear input field
|
|
|
59 |
|
60 |
input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
61 |
send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
|
62 |
|
63 |
+
def handle_response(user_input, chat_history):
|
64 |
+
# Initialize chat_history if it's empty
|
65 |
+
if not chat_history:
|
66 |
+
chat_history = [{"role": "system", "content": "you are xylaria 1.4 senoa, developed by sk md saad amin"}]
|
67 |
+
|
68 |
+
# Add user input
|
69 |
+
chat_history.append({"role": "user", "content": user_input})
|
70 |
+
|
71 |
+
# Get response from model
|
72 |
+
response_stream = get_response(user_input)
|
73 |
+
for partial_response in response_stream:
|
74 |
+
chat_history[-1] = {"role": "assistant", "content": partial_response}
|
75 |
+
yield "", chat_history # Return the updated chat history progressively
|
76 |
+
|
77 |
+
input_textbox.submit(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
|
78 |
+
send_button.click(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
|
79 |
+
|
80 |
demo.css = """
|
81 |
#input-row {
|
82 |
position: absolute;
|
|
|
91 |
overflow-y: scroll;
|
92 |
}
|
93 |
"""
|
94 |
+
|
95 |
return demo
|
96 |
|
97 |
demo = chat_interface()
|