awacke1 commited on
Commit
bbd4a78
·
verified ·
1 Parent(s): b1fe0dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -150,12 +150,17 @@ async def start_websocket_server(host='0.0.0.0', port=8765):
150
  logger.info(f"WebSocket server started on ws://{host}:{port}")
151
  return server
152
 
 
 
 
 
153
  def send_message(message, username, room_id):
154
  """Function to send a message from the Gradio interface."""
155
  if not message.strip():
156
  return None
157
 
158
- loop = asyncio.get_event_loop()
 
159
  msg_data = {
160
  "type": "chat",
161
  "content": message,
@@ -163,9 +168,10 @@ def send_message(message, username, room_id):
163
  "room_id": room_id
164
  }
165
 
166
- loop.create_task(broadcast_message(msg_data, room_id))
 
167
 
168
- # Format the message for display
169
  formatted_msg = f"{username}: {message}"
170
  return formatted_msg
171
 
@@ -244,14 +250,35 @@ def create_gradio_interface():
244
 
245
  return interface
246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  async def main():
248
  """Main function to start the application."""
249
- global NODE_NAME
250
  NODE_NAME, port = get_node_name()
251
 
 
 
 
252
  # Start WebSocket server
253
  server = await start_websocket_server()
254
 
 
 
 
255
  # Create and launch Gradio interface
256
  interface = create_gradio_interface()
257
 
@@ -283,10 +310,11 @@ async def main():
283
 
284
  logger.info(f"Starting Gradio interface on http://0.0.0.0:{port} with node name '{NODE_NAME}'")
285
 
286
- await server.serve()
 
287
 
288
- # Keep the WebSocket server running
289
- await asyncio.Future()
290
 
291
  if __name__ == "__main__":
292
  asyncio.run(main())
 
150
  logger.info(f"WebSocket server started on ws://{host}:{port}")
151
  return server
152
 
153
+ # Global variables for event loop and queue
154
+ main_event_loop = None
155
+ message_queue = []
156
+
157
  def send_message(message, username, room_id):
158
  """Function to send a message from the Gradio interface."""
159
  if not message.strip():
160
  return None
161
 
162
+ global message_queue
163
+
164
  msg_data = {
165
  "type": "chat",
166
  "content": message,
 
168
  "room_id": room_id
169
  }
170
 
171
+ # Add to queue for processing by the main loop
172
+ message_queue.append(msg_data)
173
 
174
+ # Format the message for display in the UI
175
  formatted_msg = f"{username}: {message}"
176
  return formatted_msg
177
 
 
250
 
251
  return interface
252
 
253
+ async def process_message_queue():
254
+ """Process messages in the queue and broadcast them."""
255
+ global message_queue
256
+
257
+ while True:
258
+ # Check if there are messages to process
259
+ if message_queue:
260
+ # Get the oldest message
261
+ msg_data = message_queue.pop(0)
262
+ # Broadcast it
263
+ await broadcast_message(msg_data, msg_data["room_id"])
264
+
265
+ # Sleep to avoid busy-waiting
266
+ await asyncio.sleep(0.1)
267
+
268
  async def main():
269
  """Main function to start the application."""
270
+ global NODE_NAME, main_event_loop
271
  NODE_NAME, port = get_node_name()
272
 
273
+ # Store the main event loop for later use
274
+ main_event_loop = asyncio.get_running_loop()
275
+
276
  # Start WebSocket server
277
  server = await start_websocket_server()
278
 
279
+ # Start message queue processor
280
+ asyncio.create_task(process_message_queue())
281
+
282
  # Create and launch Gradio interface
283
  interface = create_gradio_interface()
284
 
 
310
 
311
  logger.info(f"Starting Gradio interface on http://0.0.0.0:{port} with node name '{NODE_NAME}'")
312
 
313
+ # Start message processor
314
+ logger.info("Starting message queue processor")
315
 
316
+ # Run the server and keep it running
317
+ await server.serve()
318
 
319
  if __name__ == "__main__":
320
  asyncio.run(main())