awacke1 commited on
Commit
25c5d30
Β·
verified Β·
1 Parent(s): 6f5ebb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -58,15 +58,15 @@ UNICODE_FONTS = [
58
  server_running = False
59
  server_task = None
60
 
61
- async def get_node_name():
62
  """🌐 - Naming Node with Code - Spins a name, oh so bold!"""
63
  action = "🌐 - Naming Node with Code - Spins a name, oh so bold!"
64
  username = st.session_state.get('username', 'System 🌟')
65
- await asyncio.to_thread(log_action, username, action)
66
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
67
  parser.add_argument('--node-name', type=str, default=None)
68
  parser.add_argument('--port', type=int, default=8501)
69
- args = await asyncio.to_thread(parser.parse_args)
70
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
71
 
72
  def log_action(username, action):
@@ -99,13 +99,13 @@ async def load_chat():
99
  await asyncio.to_thread(lambda: open(CHAT_FILE, 'w').write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub - start chatting! 🎀\n"))
100
  with open(CHAT_FILE, 'r') as f:
101
  content = await asyncio.to_thread(f.read)
102
- return content # Return the raw string, splitting handled where needed
103
 
104
- async def get_user_list(chat_content):
105
  """πŸ‘₯ - Crew Clue Brew - Spots who’s who in the crew!"""
106
  action = "πŸ‘₯ - Crew Clue Brew - Spots who’s who in the crew!"
107
  username = st.session_state.get('username', 'System 🌟')
108
- await asyncio.to_thread(log_action, username, action)
109
  users = set()
110
  for line in chat_content.split('\n'):
111
  if line.strip() and ': ' in line:
@@ -269,11 +269,11 @@ async def run_websocket_server():
269
  server_running = True
270
  await server.wait_closed()
271
 
272
- async def create_streamlit_interface():
273
  """🎨 - UI Brew Crew - Builds the view, so new!"""
274
  action = "🎨 - UI Brew Crew - Builds the view, so new!"
275
  username = st.session_state.get('username', 'System 🌟')
276
- await asyncio.to_thread(log_action, username, action)
277
 
278
  # Dynamic title based on username
279
  if 'username' in st.session_state:
@@ -298,7 +298,7 @@ async def create_streamlit_interface():
298
  if 'client_id' not in st.session_state:
299
  st.session_state.client_id = str(uuid.uuid4())
300
  if 'username' not in st.session_state:
301
- chat_content = await load_chat() # Await the coroutine here
302
  available_names = [name for name in FUN_USERNAMES if not any(f"{name} has joined" in line for line in chat_content.split('\n'))]
303
  st.session_state.username = random.choice(available_names) if available_names else random.choice(FUN_USERNAMES)
304
 
@@ -308,7 +308,7 @@ async def create_streamlit_interface():
308
  if 'timer_start' not in st.session_state:
309
  st.session_state.timer_start = time.time()
310
  if 'quote_index' not in st.session_state:
311
- quotes = await load_quotes("famous")
312
  st.session_state.quote_index = random.randint(0, max(0, len(quotes) - 1)) if quotes else 0
313
  if 'quote_source' not in st.session_state:
314
  st.session_state.quote_source = "famous"
@@ -349,17 +349,17 @@ async def create_streamlit_interface():
349
  # Handle pasted image data
350
  pasted_image = st.session_state.get('pasted_image_data')
351
  if pasted_image:
352
- filename = await save_pasted_image(pasted_image)
353
  if filename:
354
- await save_chat_entry(st.session_state.username, f"Pasted image: {filename}")
355
  st.session_state.pasted_image_data = None
356
  st.rerun()
357
 
358
  # Chat section with quoting
359
  st.subheader(f"{START_ROOM} Chat πŸ’¬")
360
- chat_content = await load_chat()
361
  chat_lines = chat_content.split('\n')
362
- chat_votes = await load_votes(QUOTE_VOTES_FILE)
363
  for i, line in enumerate(chat_lines):
364
  if line.strip() and ': ' in line:
365
  col1, col2, col3 = st.columns([4, 1, 1])
@@ -369,11 +369,11 @@ async def create_streamlit_interface():
369
  vote_count = chat_votes.get(line.split('. ')[1] if '. ' in line else line, 0)
370
  if st.button(f"πŸ‘ {vote_count}", key=f"chat_vote_{i}"):
371
  comment = st.session_state.message_text
372
- await save_vote(QUOTE_VOTES_FILE, line.split('. ')[1] if '. ' in line else line, await generate_user_hash(), st.session_state.username, comment)
373
  if st.session_state.pasted_image_data:
374
- filename = await save_pasted_image(st.session_state.pasted_image_data)
375
  if filename:
376
- await save_chat_entry(st.session_state.username, f"Pasted image: {filename}")
377
  st.session_state.pasted_image_data = None
378
  st.session_state.message_text = ''
379
  st.rerun()
@@ -387,18 +387,17 @@ async def create_streamlit_interface():
387
  st.markdown(f"### Quoting: {st.session_state.quote_line}")
388
  quote_response = st.text_area("Add your response", key="quote_response")
389
  if st.button("Send Quote πŸš€", key="send_quote"):
390
- async def process_quote():
391
- """πŸ“’ - Quote Float Boat - Echoes chat, we gloat!"""
392
  action = "πŸ“’ - Quote Float Boat - Echoes chat, we gloat!"
393
- await asyncio.to_thread(log_action, st.session_state.username, action)
394
  markdown_response = f"### Quote Response\n- **Original**: {st.session_state.quote_line}\n- **{st.session_state.username} Replies**: {quote_response}"
395
  if st.session_state.pasted_image_data:
396
- filename = await save_pasted_image(st.session_state.pasted_image_data)
397
  if filename:
398
  markdown_response += f"\n- **Image**: ![Pasted Image]({filename})"
399
  st.session_state.pasted_image_data = None
400
- await save_chat_entry(st.session_state.username, markdown_response)
401
- await process_quote()
402
  del st.session_state.quote_line
403
  st.session_state.message_text = ''
404
  st.rerun()
@@ -406,18 +405,18 @@ async def create_streamlit_interface():
406
  # Username change dropdown
407
  new_username = st.selectbox("Change Name", [""] + FUN_USERNAMES, index=0)
408
  if new_username and new_username != st.session_state.username:
409
- await save_chat_entry("System 🌟", f"{st.session_state.username} changed name to {new_username}")
410
  st.session_state.username = new_username
411
  st.rerun()
412
 
413
  # Message input
414
  message = st.text_input(f"Message as {st.session_state.username}", key="message_input", value=st.session_state.message_text, on_change=lambda: st.session_state.update(message_text=st.session_state.message_input))
415
  if st.button("Send πŸš€", key="send_button") and message.strip():
416
- await save_chat_entry(st.session_state.username, message)
417
  if st.session_state.pasted_image_data:
418
- filename = await save_pasted_image(st.session_state.pasted_image_data)
419
  if filename:
420
- await save_chat_entry(st.session_state.username, f"Pasted image: {filename}")
421
  st.session_state.pasted_image_data = None
422
  st.session_state.message_text = ''
423
  st.rerun()
@@ -427,13 +426,14 @@ async def create_streamlit_interface():
427
  uploaded_file = st.file_uploader("Upload Media", type=['png', 'jpg', 'mp3', 'mp4'])
428
  if uploaded_file:
429
  file_path = os.path.join('./', uploaded_file.name)
430
- await asyncio.to_thread(lambda: open(file_path, 'wb').write(uploaded_file.getbuffer()))
 
431
  st.success(f"Uploaded {uploaded_file.name}")
432
 
433
  media_files = glob.glob("./*.png") + glob.glob("./*.jpg") + glob.glob("./*.mp3") + glob.glob("./*.mp4")
434
  if media_files:
435
  cols = st.columns(3)
436
- media_votes = await load_votes(MEDIA_VOTES_FILE)
437
  for idx, media_file in enumerate(media_files):
438
  vote_count = media_votes.get(media_file, 0)
439
  if vote_count > 0:
@@ -441,24 +441,24 @@ async def create_streamlit_interface():
441
  if media_file.endswith(('.png', '.jpg')):
442
  st.image(media_file, use_container_width=True)
443
  elif media_file.endswith('.mp3'):
444
- st.markdown(await get_audio_html(media_file), unsafe_allow_html=True)
445
  elif media_file.endswith('.mp4'):
446
- st.markdown(await get_video_html(media_file), unsafe_allow_html=True)
447
  col1, col2 = st.columns(2)
448
  with col1:
449
  if st.button(f"πŸ‘ {vote_count}", key=f"media_vote_{idx}"):
450
  comment = st.session_state.message_text
451
- await save_vote(MEDIA_VOTES_FILE, media_file, await generate_user_hash(), st.session_state.username, comment)
452
  if st.session_state.pasted_image_data:
453
- filename = await save_pasted_image(st.session_state.pasted_image_data)
454
  if filename:
455
- await save_chat_entry(st.session_state.username, f"Pasted image: {filename}")
456
  st.session_state.pasted_image_data = None
457
  st.session_state.message_text = ''
458
  st.rerun()
459
  with col2:
460
  if st.button("πŸ—‘οΈ", key=f"media_delete_{idx}"):
461
- await asyncio.to_thread(os.remove, media_file)
462
  st.rerun()
463
 
464
  # Refresh timer
@@ -470,13 +470,13 @@ async def create_streamlit_interface():
470
  font_name, font_func = random.choice(UNICODE_FONTS)
471
  countdown_str = "".join(UNICODE_DIGITS[int(d)] for d in str(i)) if i < 10 else font_func(str(i))
472
  timer_placeholder.markdown(f"<p class='timer'>⏳ {font_func('Refresh in:')} {countdown_str}</p>", unsafe_allow_html=True)
473
- await asyncio.sleep(1)
474
  st.rerun()
475
 
476
  # Sidebar vote stats
477
  st.sidebar.subheader("Vote Counts")
478
- chat_votes = await load_votes(QUOTE_VOTES_FILE)
479
- media_votes = await load_votes(MEDIA_VOTES_FILE)
480
  for item, count in {**chat_votes, **media_votes}.items():
481
  if count > 0:
482
  st.sidebar.write(f"{item}: {count} votes")
@@ -487,10 +487,10 @@ async def main():
487
  username = st.session_state.get('username', 'System 🌟')
488
  await asyncio.to_thread(log_action, username, action)
489
  global NODE_NAME, server_task
490
- NODE_NAME, port = await get_node_name()
491
  if server_task is None:
492
  server_task = asyncio.create_task(run_websocket_server())
493
- await create_streamlit_interface()
494
 
495
  if __name__ == "__main__":
496
  asyncio.run(main())
 
58
  server_running = False
59
  server_task = None
60
 
61
+ def get_node_name():
62
  """🌐 - Naming Node with Code - Spins a name, oh so bold!"""
63
  action = "🌐 - Naming Node with Code - Spins a name, oh so bold!"
64
  username = st.session_state.get('username', 'System 🌟')
65
+ log_action(username, action)
66
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
67
  parser.add_argument('--node-name', type=str, default=None)
68
  parser.add_argument('--port', type=int, default=8501)
69
+ args = parser.parse_args()
70
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
71
 
72
  def log_action(username, action):
 
99
  await asyncio.to_thread(lambda: open(CHAT_FILE, 'w').write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub - start chatting! 🎀\n"))
100
  with open(CHAT_FILE, 'r') as f:
101
  content = await asyncio.to_thread(f.read)
102
+ return content
103
 
104
+ def get_user_list(chat_content):
105
  """πŸ‘₯ - Crew Clue Brew - Spots who’s who in the crew!"""
106
  action = "πŸ‘₯ - Crew Clue Brew - Spots who’s who in the crew!"
107
  username = st.session_state.get('username', 'System 🌟')
108
+ log_action(username, action)
109
  users = set()
110
  for line in chat_content.split('\n'):
111
  if line.strip() and ': ' in line:
 
269
  server_running = True
270
  await server.wait_closed()
271
 
272
+ def create_streamlit_interface():
273
  """🎨 - UI Brew Crew - Builds the view, so new!"""
274
  action = "🎨 - UI Brew Crew - Builds the view, so new!"
275
  username = st.session_state.get('username', 'System 🌟')
276
+ log_action(username, action) # Sync call here since we're in Streamlit context
277
 
278
  # Dynamic title based on username
279
  if 'username' in st.session_state:
 
298
  if 'client_id' not in st.session_state:
299
  st.session_state.client_id = str(uuid.uuid4())
300
  if 'username' not in st.session_state:
301
+ chat_content = asyncio.run(load_chat()) # Run async function synchronously
302
  available_names = [name for name in FUN_USERNAMES if not any(f"{name} has joined" in line for line in chat_content.split('\n'))]
303
  st.session_state.username = random.choice(available_names) if available_names else random.choice(FUN_USERNAMES)
304
 
 
308
  if 'timer_start' not in st.session_state:
309
  st.session_state.timer_start = time.time()
310
  if 'quote_index' not in st.session_state:
311
+ quotes = asyncio.run(load_quotes("famous"))
312
  st.session_state.quote_index = random.randint(0, max(0, len(quotes) - 1)) if quotes else 0
313
  if 'quote_source' not in st.session_state:
314
  st.session_state.quote_source = "famous"
 
349
  # Handle pasted image data
350
  pasted_image = st.session_state.get('pasted_image_data')
351
  if pasted_image:
352
+ filename = asyncio.run(save_pasted_image(pasted_image))
353
  if filename:
354
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {filename}"))
355
  st.session_state.pasted_image_data = None
356
  st.rerun()
357
 
358
  # Chat section with quoting
359
  st.subheader(f"{START_ROOM} Chat πŸ’¬")
360
+ chat_content = asyncio.run(load_chat())
361
  chat_lines = chat_content.split('\n')
362
+ chat_votes = asyncio.run(load_votes(QUOTE_VOTES_FILE))
363
  for i, line in enumerate(chat_lines):
364
  if line.strip() and ': ' in line:
365
  col1, col2, col3 = st.columns([4, 1, 1])
 
369
  vote_count = chat_votes.get(line.split('. ')[1] if '. ' in line else line, 0)
370
  if st.button(f"πŸ‘ {vote_count}", key=f"chat_vote_{i}"):
371
  comment = st.session_state.message_text
372
+ asyncio.run(save_vote(QUOTE_VOTES_FILE, line.split('. ')[1] if '. ' in line else line, asyncio.run(generate_user_hash()), st.session_state.username, comment))
373
  if st.session_state.pasted_image_data:
374
+ filename = asyncio.run(save_pasted_image(st.session_state.pasted_image_data))
375
  if filename:
376
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {filename}"))
377
  st.session_state.pasted_image_data = None
378
  st.session_state.message_text = ''
379
  st.rerun()
 
387
  st.markdown(f"### Quoting: {st.session_state.quote_line}")
388
  quote_response = st.text_area("Add your response", key="quote_response")
389
  if st.button("Send Quote πŸš€", key="send_quote"):
390
+ def process_quote():
 
391
  action = "πŸ“’ - Quote Float Boat - Echoes chat, we gloat!"
392
+ log_action(st.session_state.username, action)
393
  markdown_response = f"### Quote Response\n- **Original**: {st.session_state.quote_line}\n- **{st.session_state.username} Replies**: {quote_response}"
394
  if st.session_state.pasted_image_data:
395
+ filename = asyncio.run(save_pasted_image(st.session_state.pasted_image_data))
396
  if filename:
397
  markdown_response += f"\n- **Image**: ![Pasted Image]({filename})"
398
  st.session_state.pasted_image_data = None
399
+ asyncio.run(save_chat_entry(st.session_state.username, markdown_response))
400
+ process_quote()
401
  del st.session_state.quote_line
402
  st.session_state.message_text = ''
403
  st.rerun()
 
405
  # Username change dropdown
406
  new_username = st.selectbox("Change Name", [""] + FUN_USERNAMES, index=0)
407
  if new_username and new_username != st.session_state.username:
408
+ asyncio.run(save_chat_entry("System 🌟", f"{st.session_state.username} changed name to {new_username}"))
409
  st.session_state.username = new_username
410
  st.rerun()
411
 
412
  # Message input
413
  message = st.text_input(f"Message as {st.session_state.username}", key="message_input", value=st.session_state.message_text, on_change=lambda: st.session_state.update(message_text=st.session_state.message_input))
414
  if st.button("Send πŸš€", key="send_button") and message.strip():
415
+ asyncio.run(save_chat_entry(st.session_state.username, message))
416
  if st.session_state.pasted_image_data:
417
+ filename = asyncio.run(save_pasted_image(st.session_state.pasted_image_data))
418
  if filename:
419
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {filename}"))
420
  st.session_state.pasted_image_data = None
421
  st.session_state.message_text = ''
422
  st.rerun()
 
426
  uploaded_file = st.file_uploader("Upload Media", type=['png', 'jpg', 'mp3', 'mp4'])
427
  if uploaded_file:
428
  file_path = os.path.join('./', uploaded_file.name)
429
+ with open(file_path, 'wb') as f:
430
+ f.write(uploaded_file.getbuffer())
431
  st.success(f"Uploaded {uploaded_file.name}")
432
 
433
  media_files = glob.glob("./*.png") + glob.glob("./*.jpg") + glob.glob("./*.mp3") + glob.glob("./*.mp4")
434
  if media_files:
435
  cols = st.columns(3)
436
+ media_votes = asyncio.run(load_votes(MEDIA_VOTES_FILE))
437
  for idx, media_file in enumerate(media_files):
438
  vote_count = media_votes.get(media_file, 0)
439
  if vote_count > 0:
 
441
  if media_file.endswith(('.png', '.jpg')):
442
  st.image(media_file, use_container_width=True)
443
  elif media_file.endswith('.mp3'):
444
+ st.markdown(asyncio.run(get_audio_html(media_file)), unsafe_allow_html=True)
445
  elif media_file.endswith('.mp4'):
446
+ st.markdown(asyncio.run(get_video_html(media_file)), unsafe_allow_html=True)
447
  col1, col2 = st.columns(2)
448
  with col1:
449
  if st.button(f"πŸ‘ {vote_count}", key=f"media_vote_{idx}"):
450
  comment = st.session_state.message_text
451
+ asyncio.run(save_vote(MEDIA_VOTES_FILE, media_file, asyncio.run(generate_user_hash()), st.session_state.username, comment))
452
  if st.session_state.pasted_image_data:
453
+ filename = asyncio.run(save_pasted_image(st.session_state.pasted_image_data))
454
  if filename:
455
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {filename}"))
456
  st.session_state.pasted_image_data = None
457
  st.session_state.message_text = ''
458
  st.rerun()
459
  with col2:
460
  if st.button("πŸ—‘οΈ", key=f"media_delete_{idx}"):
461
+ os.remove(media_file)
462
  st.rerun()
463
 
464
  # Refresh timer
 
470
  font_name, font_func = random.choice(UNICODE_FONTS)
471
  countdown_str = "".join(UNICODE_DIGITS[int(d)] for d in str(i)) if i < 10 else font_func(str(i))
472
  timer_placeholder.markdown(f"<p class='timer'>⏳ {font_func('Refresh in:')} {countdown_str}</p>", unsafe_allow_html=True)
473
+ time.sleep(1) # Sync sleep here since it's UI-driven
474
  st.rerun()
475
 
476
  # Sidebar vote stats
477
  st.sidebar.subheader("Vote Counts")
478
+ chat_votes = asyncio.run(load_votes(QUOTE_VOTES_FILE))
479
+ media_votes = asyncio.run(load_votes(MEDIA_VOTES_FILE))
480
  for item, count in {**chat_votes, **media_votes}.items():
481
  if count > 0:
482
  st.sidebar.write(f"{item}: {count} votes")
 
487
  username = st.session_state.get('username', 'System 🌟')
488
  await asyncio.to_thread(log_action, username, action)
489
  global NODE_NAME, server_task
490
+ NODE_NAME, port = get_node_name() # Sync call since it's CLI parsing
491
  if server_task is None:
492
  server_task = asyncio.create_task(run_websocket_server())
493
+ create_streamlit_interface() # Call synchronously
494
 
495
  if __name__ == "__main__":
496
  asyncio.run(main())