Update app.py
Browse files
app.py
CHANGED
@@ -64,7 +64,7 @@ VOTE_DIR = "vote_logs"
|
|
64 |
STATE_FILE = "user_state.txt"
|
65 |
AUDIO_DIR = "audio_logs"
|
66 |
HISTORY_DIR = "history_logs"
|
67 |
-
MEDIA_DIR = "media_files"
|
68 |
os.makedirs(CHAT_DIR, exist_ok=True)
|
69 |
os.makedirs(VOTE_DIR, exist_ok=True)
|
70 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
@@ -111,6 +111,10 @@ if 'active_connections' not in st.session_state:
|
|
111 |
st.session_state.active_connections = {}
|
112 |
if 'media_notifications' not in st.session_state:
|
113 |
st.session_state.media_notifications = []
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# Timestamp wizardry - clock ticks with flair! β°π©
|
116 |
def format_timestamp_prefix():
|
@@ -158,6 +162,7 @@ async def save_chat_entry(username, message):
|
|
158 |
with open(HISTORY_FILE, 'a') as f:
|
159 |
f.write(f"[{timestamp}] {username}: Audio generated - {audio_file}\n")
|
160 |
await broadcast_message(f"{username}|{message}", "chat") # Notify all clients
|
|
|
161 |
|
162 |
# Chat loader - history unleashed! ππ
|
163 |
async def load_chat():
|
@@ -366,8 +371,6 @@ def create_streamlit_interface():
|
|
366 |
st.session_state.message_text = ""
|
367 |
if 'audio_cache' not in st.session_state:
|
368 |
st.session_state.audio_cache = {}
|
369 |
-
if 'chat_history' not in st.session_state:
|
370 |
-
st.session_state.chat_history = []
|
371 |
|
372 |
st.markdown("""
|
373 |
<style>
|
@@ -389,43 +392,52 @@ def create_streamlit_interface():
|
|
389 |
await process_voice_input(audio_bytes)
|
390 |
st.rerun()
|
391 |
|
|
|
392 |
st.subheader(f"{START_ROOM} Chat π¬")
|
393 |
chat_content = await load_chat()
|
394 |
chat_lines = chat_content.split('\n')
|
395 |
chat_votes = await load_votes(QUOTE_VOTES_FILE)
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
if st.
|
415 |
-
st.session_state.
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
|
430 |
if 'quote_line' in st.session_state:
|
431 |
st.markdown(f"### Quoting: {st.session_state.quote_line}")
|
|
|
64 |
STATE_FILE = "user_state.txt"
|
65 |
AUDIO_DIR = "audio_logs"
|
66 |
HISTORY_DIR = "history_logs"
|
67 |
+
MEDIA_DIR = "media_files"
|
68 |
os.makedirs(CHAT_DIR, exist_ok=True)
|
69 |
os.makedirs(VOTE_DIR, exist_ok=True)
|
70 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
|
|
111 |
st.session_state.active_connections = {}
|
112 |
if 'media_notifications' not in st.session_state:
|
113 |
st.session_state.media_notifications = []
|
114 |
+
if 'last_chat_update' not in st.session_state:
|
115 |
+
st.session_state.last_chat_update = 0
|
116 |
+
if 'displayed_chat_lines' not in st.session_state:
|
117 |
+
st.session_state.displayed_chat_lines = []
|
118 |
|
119 |
# Timestamp wizardry - clock ticks with flair! β°π©
|
120 |
def format_timestamp_prefix():
|
|
|
162 |
with open(HISTORY_FILE, 'a') as f:
|
163 |
f.write(f"[{timestamp}] {username}: Audio generated - {audio_file}\n")
|
164 |
await broadcast_message(f"{username}|{message}", "chat") # Notify all clients
|
165 |
+
st.session_state.last_chat_update = time.time() # Update timestamp for new chat
|
166 |
|
167 |
# Chat loader - history unleashed! ππ
|
168 |
async def load_chat():
|
|
|
371 |
st.session_state.message_text = ""
|
372 |
if 'audio_cache' not in st.session_state:
|
373 |
st.session_state.audio_cache = {}
|
|
|
|
|
374 |
|
375 |
st.markdown("""
|
376 |
<style>
|
|
|
392 |
await process_voice_input(audio_bytes)
|
393 |
st.rerun()
|
394 |
|
395 |
+
# Load chat only if thereβs an update
|
396 |
st.subheader(f"{START_ROOM} Chat π¬")
|
397 |
chat_content = await load_chat()
|
398 |
chat_lines = chat_content.split('\n')
|
399 |
chat_votes = await load_votes(QUOTE_VOTES_FILE)
|
400 |
+
|
401 |
+
# Only add new lines that havenβt been displayed yet
|
402 |
+
current_time = time.time()
|
403 |
+
if current_time - st.session_state.last_chat_update > 1 or not st.session_state.displayed_chat_lines:
|
404 |
+
new_lines = [line for line in chat_lines if line.strip() and ': ' in line and line not in st.session_state.displayed_chat_lines]
|
405 |
+
st.session_state.displayed_chat_lines.extend(new_lines)
|
406 |
+
st.session_state.last_chat_update = current_time
|
407 |
+
|
408 |
+
# Display chat lines from session state
|
409 |
+
for i, line in enumerate(st.session_state.displayed_chat_lines):
|
410 |
+
col1, col2, col3, col4 = st.columns([3, 1, 1, 2])
|
411 |
+
with col1:
|
412 |
+
st.markdown(line)
|
413 |
+
with col2:
|
414 |
+
vote_count = chat_votes.get(line.split('. ')[1] if '. ' in line else line, 0)
|
415 |
+
if st.button(f"π {vote_count}", key=f"chat_vote_{i}"):
|
416 |
+
comment = st.session_state.message_text
|
417 |
+
await save_vote(QUOTE_VOTES_FILE, line.split('. ')[1] if '. ' in line else line, await generate_user_hash(), st.session_state.username, comment)
|
418 |
+
if st.session_state.pasted_image_data:
|
419 |
+
filename = await save_pasted_image(st.session_state.pasted_image_data, st.session_state.username)
|
420 |
+
if filename:
|
421 |
+
await save_chat_entry(st.session_state.username, f"Pasted image: {filename}")
|
422 |
+
st.session_state.pasted_image_data = None
|
423 |
+
st.session_state.message_text = ''
|
424 |
+
st.rerun()
|
425 |
+
with col3:
|
426 |
+
if st.button("π’ Quote", key=f"quote_{i}"):
|
427 |
+
st.session_state.quote_line = line
|
428 |
+
st.rerun()
|
429 |
+
with col4:
|
430 |
+
username = line.split(': ')[1].split(' ')[0]
|
431 |
+
audio_file = None
|
432 |
+
cache_key = f"{line}_{FUN_USERNAMES.get(username, 'en-US-AriaNeural')}"
|
433 |
+
if cache_key in st.session_state.audio_cache:
|
434 |
+
audio_file = st.session_state.audio_cache[cache_key]
|
435 |
+
else:
|
436 |
+
cleaned_text = clean_text_for_tts(line.split(': ', 1)[1])
|
437 |
+
audio_file = await async_edge_tts_generate(cleaned_text, FUN_USERNAMES.get(username, "en-US-AriaNeural"))
|
438 |
+
st.session_state.audio_cache[cache_key] = audio_file
|
439 |
+
if audio_file:
|
440 |
+
play_and_download_audio(audio_file)
|
441 |
|
442 |
if 'quote_line' in st.session_state:
|
443 |
st.markdown(f"### Quoting: {st.session_state.quote_line}")
|