Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,53 +149,55 @@ async def start_client():
|
|
| 149 |
await ws.send(client_response)
|
| 150 |
return server_message
|
| 151 |
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
with gr.Blocks() as demo:
|
| 157 |
-
with gr.Column(scale=1, min_width=600):
|
| 158 |
-
with gr.Row():
|
| 159 |
-
input_msg1 = gr.Textbox(lines=15, max_lines=130, label="inputs", interactive=False)
|
| 160 |
-
response_msg1 = gr.Textbox(lines=15, max_lines=130, label="Client responses", interactive=False)
|
| 161 |
-
with gr.Row():
|
| 162 |
-
user_input1 = gr.Textbox(label="User Input")
|
| 163 |
-
with gr.Row():
|
| 164 |
-
bot1 = gr.Button("Ask Agent1")
|
| 165 |
-
bot2 = gr.Button("Ask Agent2")
|
| 166 |
-
with gr.Row():
|
| 167 |
-
websocket_port = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
| 168 |
-
start_client = gr.Button("Start WebSocket client")
|
| 169 |
-
stop_client = gr.Button("Stop WebSocket client")
|
| 170 |
-
with gr.Row():
|
| 171 |
-
port_in_use = gr.Textbox()
|
| 172 |
-
with gr.Row():
|
| 173 |
-
chat = gr.ChatInterface(handle_message, inputs=input_msg1, outputs=[response_msg1])
|
| 174 |
-
start_client.click(start_client, inputs=websocket_port, outputs=[port_in_use]).then(handle_message, inputs=None, outputs=chat)
|
| 175 |
-
bot1.click(ask_question, inputs=user_input1, outputs=response_msg1)
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=8765)
|
|
|
|
| 149 |
await ws.send(client_response)
|
| 150 |
return server_message
|
| 151 |
await asyncio.sleep(0.1)
|
| 152 |
+
async def start_websocket_server():
|
| 153 |
+
server = await websockets.serve(websocket_server, "localhost", 8765) # Set your desired WebSocket server parameters
|
| 154 |
+
await server.wait_closed()
|
| 155 |
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
demo = gr.Interface.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
with gr.Blocks() as demo:
|
| 160 |
+
with gr.Column(scale=1, min_width=600):
|
| 161 |
+
with gr.Row():
|
| 162 |
+
input_msg1 = gr.Textbox(lines=15, max_lines=130, label="inputs", interactive=False)
|
| 163 |
+
response_msg1 = gr.Textbox(lines=15, max_lines=130, label="Client responses", interactive=False)
|
| 164 |
+
with gr.Row():
|
| 165 |
+
user_input1 = gr.Textbox(label="User Input")
|
| 166 |
+
with gr.Row():
|
| 167 |
+
bot1 = gr.Button("Ask Agent1")
|
| 168 |
+
bot2 = gr.Button("Ask Agent2")
|
| 169 |
+
with gr.Row():
|
| 170 |
+
websocket_port = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
| 171 |
+
start_client = gr.Button("Start WebSocket client")
|
| 172 |
+
stop_client = gr.Button("Stop WebSocket client")
|
| 173 |
+
with gr.Row():
|
| 174 |
+
port_in_use = gr.Textbox()
|
| 175 |
+
with gr.Row():
|
| 176 |
+
chat = gr.ChatInterface(handle_message, inputs=input_msg1, outputs=[response_msg1])
|
| 177 |
+
start_client.click(start_client, inputs=websocket_port, outputs=[port_in_use]).then(handle_message, inputs=None, outputs=chat)
|
| 178 |
+
bot1.click(ask_question, inputs=user_input1, outputs=response_msg1)
|
| 179 |
+
|
| 180 |
+
chatbot = gr.Chatbot()
|
| 181 |
+
msg = gr.Textbox()
|
| 182 |
+
clear = gr.Button("Clear")
|
| 183 |
+
|
| 184 |
+
def user(user_message, history):
|
| 185 |
+
return "", history + [[user_message, None]]
|
| 186 |
+
|
| 187 |
+
def bot(history):
|
| 188 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
| 189 |
+
history[-1][1] = ""
|
| 190 |
+
for character in bot_message:
|
| 191 |
+
history[-1][1] += character
|
| 192 |
+
time.sleep(0.05)
|
| 193 |
+
yield history
|
| 194 |
+
|
| 195 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 196 |
+
bot, chatbot, chatbot
|
| 197 |
+
)
|
| 198 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 199 |
|
| 200 |
+
demo.queue()
|
| 201 |
+
demo.launch(share=True, server_name="0.0.0.0", server_port=8765)
|
| 202 |
|
| 203 |
+
asyncio.run(start_websocket_server())
|
|
|