-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_ui.py
85 lines (59 loc) · 1.96 KB
/
chat_ui.py
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
import asyncio
from threading import Thread
import gradio as gr
import zmq
import zmq.asyncio
USER_ID = "user"
message_queue = asyncio.Queue()
history = [
[
None,
"Hi there! I'm here to help you with any tasks you need assistance with. Just let me know what you'd like me to do, and I'll be more than happy to help!",
]
]
async def listen_for_messages(dealer):
while True:
message = await dealer.recv_string()
await message_queue.put(message)
async def zmq_listener():
await listen_for_messages(dealer)
def run_zmq_listener():
asyncio.run(zmq_listener())
async def zmq_send_message(message):
await dealer.send_multipart(
[
"LeaderGPT".encode(), # TODO: Don't hardcode this
message.encode(),
]
)
def send_message(message):
history.append([message, None])
asyncio.run(zmq_send_message(message))
def update_chat():
while not message_queue.empty():
message = message_queue.get_nowait()
history.append([None, message])
return history
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
with gr.Row():
with gr.Column(scale=9):
textbox = gr.Textbox(
show_label=False, placeholder="Enter text and press enter"
).style(container=False)
with gr.Column(scale=1, min_width=0):
send_button = gr.Button("➤", variant="primary")
textbox.submit(send_message, textbox)
textbox.submit(lambda: "", None, textbox)
send_button.click(send_message, textbox)
send_button.click(lambda: "", None, textbox)
demo.load(update_chat, None, chatbot, every=1)
if __name__ == "__main__":
context = zmq.asyncio.Context()
dealer = context.socket(zmq.DEALER)
dealer.identity = USER_ID.encode()
dealer.connect("tcp://localhost:5555")
print(f"Subprocess {USER_ID} is connected")
zmq_thread = Thread(target=run_zmq_listener)
zmq_thread.start()
demo.queue().launch()