awacke1 commited on
Commit
26b513b
Β·
verified Β·
1 Parent(s): 6c9f5a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
app.py CHANGED
@@ -258,6 +258,8 @@ async def save_chat_entry(username, message, is_markdown=False, quote_line=None)
258
  f.write(f"[{timestamp}] {username} ({voice}): Audio generated - {audio_filename}\n")
259
  with open(user_history_file, 'a') as f:
260
  f.write(f"{indent}[{timestamp}] Audio: {audio_filename}\n")
 
 
261
 
262
  # Embed audio and image in global chat if applicable
263
  if message.startswith("Pasted image:") or message.startswith("Uploaded media:") or message.startswith("Uploaded PDF:"):
@@ -449,7 +451,12 @@ async def get_audio_html(audio_path, width="100%"):
449
  username = st.session_state.get('username', 'System 🌟')
450
  await asyncio.to_thread(log_action, username, "🎢✈️ - Audio renderer - sounds soar!")
451
  audio_url = f"data:audio/mpeg;base64,{base64.b64encode(await asyncio.to_thread(open, audio_path, 'rb').read()).decode()}"
452
- return f'<audio controls style="width: {width};"><source src="{audio_url}" type="audio/mpeg">Your browser does not support the audio element.</audio>'
 
 
 
 
 
453
 
454
  # Websocket handler - chat links up! πŸŒπŸ”—
455
  async def websocket_handler(websocket, path):
@@ -590,15 +597,13 @@ def main():
590
  elif file_path.endswith('.pdf'):
591
  st.write(f"PDF: {os.path.basename(file_path)}")
592
  with col_audio:
593
- username = line.split(': ')[1].split(' ')[0] if ': ' in line else "Unknown"
594
- cache_key = f"{line}_{FUN_USERNAMES.get(username, 'en-US-AriaNeural')}"
595
- if cache_key not in st.session_state.audio_cache and "Audio:" in line:
596
  audio_ref = line.split("Audio: ")[-1].strip()
597
  if os.path.exists(audio_ref):
 
 
 
598
  st.session_state.audio_cache[cache_key] = audio_ref
599
- audio_file = st.session_state.audio_cache.get(cache_key)
600
- if audio_file:
601
- play_and_download_audio(audio_file)
602
 
603
  if st.session_state.quote_line:
604
  st.markdown(f"### Quoting: {st.session_state.quote_line}")
@@ -714,33 +719,30 @@ def main():
714
  time.sleep(1)
715
  st.rerun()
716
 
717
- # Gallery with Adjustable Tiles
718
- st.subheader("Media Gallery πŸŽ¨πŸŽΆπŸ“œπŸŽ₯")
719
- gallery_columns = st.slider("Number of Gallery Tiles", 1, 20, st.session_state.gallery_columns)
720
- st.session_state.gallery_columns = gallery_columns
721
- media_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.mp4") + glob.glob("*.pdf")
722
- if media_files:
723
- media_votes = await load_votes(MEDIA_VOTES_FILE)
724
- seen_files = set()
725
- cols = st.columns(gallery_columns)
726
- col_idx = 0
727
- for media_file in sorted(media_files, key=os.path.getmtime, reverse=True):
728
- if media_file not in seen_files:
729
- seen_files.add(media_file)
730
- with cols[col_idx]:
731
- filename = os.path.basename(media_file)
732
- vote_count = media_votes.get(media_file, 0)
733
- st.markdown(f"**{filename}**")
734
- if media_file.endswith(('.png', '.jpg')):
735
- st.image(media_file, use_container_width=True)
736
- elif media_file.endswith('.mp4'):
737
- st.markdown(get_video_html(media_file), unsafe_allow_html=True)
738
- elif media_file.endswith('.pdf'):
739
- st.write(f"PDF: {filename}")
740
- if st.button(f"πŸ‘ {vote_count}", key=f"media_vote_{media_file}"):
741
- await save_vote(MEDIA_VOTES_FILE, media_file, await generate_user_hash(), st.session_state.username)
742
- st.rerun()
743
- col_idx = (col_idx + 1) % gallery_columns
744
 
745
  # Full Log at End
746
  st.subheader("Full Chat Log πŸ“œ")
 
258
  f.write(f"[{timestamp}] {username} ({voice}): Audio generated - {audio_filename}\n")
259
  with open(user_history_file, 'a') as f:
260
  f.write(f"{indent}[{timestamp}] Audio: {audio_filename}\n")
261
+ with open(CHAT_FILE, 'a') as f:
262
+ f.write(f"{indent}[{timestamp}] Audio: {audio_filename}\n")
263
 
264
  # Embed audio and image in global chat if applicable
265
  if message.startswith("Pasted image:") or message.startswith("Uploaded media:") or message.startswith("Uploaded PDF:"):
 
451
  username = st.session_state.get('username', 'System 🌟')
452
  await asyncio.to_thread(log_action, username, "🎢✈️ - Audio renderer - sounds soar!")
453
  audio_url = f"data:audio/mpeg;base64,{base64.b64encode(await asyncio.to_thread(open, audio_path, 'rb').read()).decode()}"
454
+ return f'''
455
+ <audio controls style="width: {width};">
456
+ <source src="{audio_url}" type="audio/mpeg">
457
+ Your browser does not support the audio element.
458
+ </audio>
459
+ '''
460
 
461
  # Websocket handler - chat links up! πŸŒπŸ”—
462
  async def websocket_handler(websocket, path):
 
597
  elif file_path.endswith('.pdf'):
598
  st.write(f"PDF: {os.path.basename(file_path)}")
599
  with col_audio:
600
+ if "Audio:" in line:
 
 
601
  audio_ref = line.split("Audio: ")[-1].strip()
602
  if os.path.exists(audio_ref):
603
+ play_and_download_audio(audio_ref)
604
+ username = line.split(': ')[1].split(' ')[0] if ': ' in line else "Unknown"
605
+ cache_key = f"{line}_{FUN_USERNAMES.get(username, 'en-US-AriaNeural')}"
606
  st.session_state.audio_cache[cache_key] = audio_ref
 
 
 
607
 
608
  if st.session_state.quote_line:
609
  st.markdown(f"### Quoting: {st.session_state.quote_line}")
 
719
  time.sleep(1)
720
  st.rerun()
721
 
722
+ # Gallery with Adjustable Tiles - Consistent Three-Set from Provided Code
723
+ st.subheader("Image Gallery πŸ–Ό")
724
+ image_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
725
+ image_cols = st.slider("Image Gallery Columns πŸ–Ό", min_value=1, max_value=15, value=5)
726
+ cols = st.columns(image_cols)
727
+ for idx, image_file in enumerate(image_files):
728
+ with cols[idx % image_cols]:
729
+ st.image(image_file, use_container_width=True)
730
+
731
+ st.subheader("Video Gallery πŸŽ₯")
732
+ video_files = glob.glob("*.mp4")
733
+ video_cols = st.slider("Video Gallery Columns 🎬", min_value=1, max_value=5, value=3)
734
+ cols = st.columns(video_cols)
735
+ for idx, video_file in enumerate(video_files):
736
+ with cols[idx % video_cols]:
737
+ st.markdown(get_video_html(video_file, width="100%"), unsafe_allow_html=True)
738
+
739
+ st.subheader("Audio Gallery 🎧")
740
+ audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
741
+ audio_cols = st.slider("Audio Gallery Columns 🎢", min_value=1, max_value=15, value=5)
742
+ cols = st.columns(audio_cols)
743
+ for idx, audio_file in enumerate(audio_files):
744
+ with cols[idx % audio_cols]:
745
+ st.markdown(await get_audio_html(audio_file, width="100%"), unsafe_allow_html=True)
 
 
 
746
 
747
  # Full Log at End
748
  st.subheader("Full Chat Log πŸ“œ")