awacke1 commited on
Commit
a0abcd6
·
verified ·
1 Parent(s): 7d0ce53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -87
app.py CHANGED
@@ -126,7 +126,7 @@ def init_session_state():
126
  'enable_audio': True, 'download_link_cache': {}, 'username': None,
127
  'autosend': True, 'autosearch': True, 'last_message': "", 'last_query': "",
128
  'mp3_files': {}, 'timer_start': time.time(), 'quote_index': 0,
129
- 'quote_source': "famous", 'last_sent_transcript': ""
130
  }
131
  for k, v in defaults.items():
132
  if k not in st.session_state:
@@ -270,6 +270,44 @@ async def load_chat():
270
  numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(lines) if line.strip())
271
  return numbered_content
272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  # 🌐 WebSocket Handling
274
  async def websocket_handler(websocket, path):
275
  client_id = str(uuid.uuid4())
@@ -402,28 +440,6 @@ async def create_paper_audio_files(papers, query):
402
  if p['full_audio']:
403
  p['download_base64'] = get_download_link(p['full_audio'])
404
 
405
- async def perform_ai_lookup(q, useArxiv=True, useArxivAudio=False):
406
- client = anthropic.Anthropic(api_key=anthropic_key)
407
- response = client.messages.create(model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role": "user", "content": q}])
408
- result = response.content[0].text
409
- st.markdown("### Claude's Reply 🧠\n" + result)
410
- md_file = create_file(result, "System", "md")
411
- audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
412
- play_and_download_audio(audio_file)
413
-
414
- if useArxiv:
415
- q += result
416
- gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
417
- refs = gradio_client.predict(q, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md")[0]
418
- result = f"🔎 {q}\n\n{refs}"
419
- md_file, audio_file = create_file(result, "System", "md"), (await async_edge_tts_generate(result, st.session_state['tts_voice'], "System"))[0]
420
- play_and_download_audio(audio_file)
421
- papers = parse_arxiv_refs(refs)
422
- if papers and useArxivAudio:
423
- await create_paper_audio_files(papers, q)
424
- return result, papers
425
- return result, []
426
-
427
  def save_vote(file, item, user_hash):
428
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
429
  entry = f"[{timestamp}] {user_hash} voted for {item}"
@@ -484,35 +500,63 @@ def create_zip_of_files(md_files, mp3_files, png_files, mp4_files, query):
484
  return zip_name
485
 
486
  # 🎮 Main Interface
487
- async def async_interface():
488
  init_session_state()
489
  load_mp3_viewer()
490
  saved_username = load_username()
491
  if saved_username and saved_username in FUN_USERNAMES:
492
  st.session_state.username = saved_username
493
  if not st.session_state.username:
494
- available = [n for n in FUN_USERNAMES if not any(f"{n} has joined" in l for l in (await load_chat()).split('\n'))]
495
  st.session_state.username = random.choice(available or list(FUN_USERNAMES.keys()))
496
  st.session_state.tts_voice = FUN_USERNAMES[st.session_state.username]
497
- await save_chat_entry("System 🌟", f"{st.session_state.username} has joined {START_ROOM}!")
498
  save_username(st.session_state.username)
499
 
500
  st.title(f"{Site_Name} for {st.session_state.username}")
501
  update_marquee_settings_ui()
502
  display_marquee(f"🚀 Welcome to {START_ROOM} | 🤖 {st.session_state.username}", st.session_state['marquee_settings'], "welcome")
503
 
504
- if not st.session_state.server_task:
505
- st.session_state.server_task = asyncio.create_task(run_websocket_server())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
 
507
  tab_main = st.radio("Action:", ["🎤 Chat & Voice", "📸 Media", "🔍 ArXiv", "📚 PDF to Audio"], horizontal=True)
508
- useArxiv, useArxivAudio = st.checkbox("Search ArXiv", True), st.checkbox("ArXiv Audio", False)
509
- st.session_state.autosend = st.checkbox("Autosend Chat", value=True)
510
- st.session_state.autosearch = st.checkbox("Autosearch ArXiv", value=True)
 
511
 
512
  # 🎤 Chat & Voice
513
  if tab_main == "🎤 Chat & Voice":
514
  st.subheader(f"{START_ROOM} Chat 💬")
515
- chat_content = await load_chat()
516
  chat_container = st.container()
517
  with chat_container:
518
  lines = chat_content.split('\n')
@@ -541,59 +585,21 @@ async def async_interface():
541
  message = st.text_input(f"Message as {st.session_state.username}", key="message_input_paste", value=st.session_state.message_text)
542
  else:
543
  st.image(paste_result.image_data, caption="Pasted Image")
544
- filename = await save_pasted_image(paste_result.image_data, st.session_state.username)
545
  if filename:
546
  st.session_state.pasted_image_data = filename
547
  if (message and message != st.session_state.last_message) or st.session_state.pasted_image_data:
548
  st.session_state.last_message = message
549
- if st.session_state.autosend or st.button("Send 🚀"):
550
  if message.strip():
551
- await save_chat_entry(st.session_state.username, message, True)
552
  if st.session_state.pasted_image_data:
553
- await save_chat_entry(st.session_state.username, f"Pasted image: {st.session_state.pasted_image_data}")
554
  st.session_state.pasted_image_data = None
555
  st.session_state.timer_start = time.time()
556
  save_username(st.session_state.username)
557
  st.rerun()
558
 
559
- st.subheader("🎤 Speech-to-Chat")
560
- from mycomponent import mycomponent
561
-
562
- # Invoke the speech component
563
- transcript_data = mycomponent(default_value=st.session_state.get('last_transcript', ''), key="speech_input")
564
-
565
- # Extract transcript, defaulting to empty string if None or invalid
566
- transcript = transcript_data['value'].strip().replace('\n', ' ') if transcript_data and 'value' in transcript_data else st.session_state.get('last_transcript', '')
567
-
568
- # Update session state only if new transcript differs
569
- if transcript and transcript != st.session_state.get('last_transcript', ''):
570
- st.session_state.last_transcript = transcript
571
-
572
- # Text area for editing transcript
573
- edited_transcript = st.text_area("✏️ Edit Transcript:", value=transcript, height=100, key="transcript_input")
574
-
575
- # Display raw transcript for feedback
576
- if transcript:
577
- st.write(f"🎙️ You said: {transcript}")
578
-
579
- # Send edited transcript to chat
580
- if edited_transcript and edited_transcript != st.session_state.get('last_sent_transcript', ''):
581
- if st.session_state.autosend:
582
- await save_chat_entry(st.session_state.username, edited_transcript, True)
583
- st.session_state.last_sent_transcript = edited_transcript
584
- st.session_state.timer_start = time.time()
585
- save_username(st.session_state.username)
586
- with chat_container:
587
- st.markdown(await load_chat())
588
- elif st.button("Send to Chat", key="send_transcript"):
589
- await save_chat_entry(st.session_state.username, edited_transcript, True)
590
- st.session_state.last_sent_transcript = edited_transcript
591
- st.session_state.timer_start = time.time()
592
- save_username(st.session_state.username)
593
- st.rerun()
594
- if not transcript:
595
- st.write("🎙️ Speak to transcribe your message...")
596
-
597
  # 📸 Media
598
  elif tab_main == "📸 Media":
599
  st.header("📸 Media Gallery")
@@ -625,23 +631,24 @@ async def async_interface():
625
  st.video(mp4)
626
  st.markdown(get_download_link(mp4, "mp4"), unsafe_allow_html=True)
627
 
628
- uploaded_file = st.file_uploader("Upload Media", type=['png', 'mp4', 'mp3'])
629
  if uploaded_file:
630
  filename = f"{format_timestamp_prefix(st.session_state.username)}-{hashlib.md5(uploaded_file.getbuffer()).hexdigest()[:8]}.{uploaded_file.name.split('.')[-1]}"
631
  with open(filename, 'wb') as f:
632
  f.write(uploaded_file.getbuffer())
633
- await save_chat_entry(st.session_state.username, f"Uploaded: {filename}")
634
  st.session_state.timer_start = time.time()
635
  save_username(st.session_state.username)
636
  st.rerun()
637
 
638
  # 🔍 ArXiv
639
  elif tab_main == "🔍 ArXiv":
 
640
  q = st.text_input("🔍 Query:", key="arxiv_query")
641
  if q and q != st.session_state.last_query:
642
  st.session_state.last_query = q
643
- if st.session_state.autosearch or st.button("🔍 Run"):
644
- result, papers = await perform_ai_lookup(q, useArxiv, useArxivAudio)
645
  for i, p in enumerate(papers, 1):
646
  with st.expander(f"{i}. 📄 {p['title']}"):
647
  st.markdown(f"**{p['date']} | {p['title']}** — [Link]({p['url']})")
@@ -652,8 +659,8 @@ async def async_interface():
652
  # 📚 PDF to Audio
653
  elif tab_main == "📚 PDF to Audio":
654
  audio_processor = AudioProcessor()
655
- pdf_file = st.file_uploader("Choose PDF", "pdf")
656
- max_pages = st.slider('Pages', 1, 100, 10)
657
  if pdf_file:
658
  with st.spinner('Processing...'):
659
  texts, audios, total = process_pdf(pdf_file, max_pages, st.session_state['tts_voice'], audio_processor)
@@ -665,20 +672,20 @@ async def async_interface():
665
  if audios.get(i):
666
  st.audio(audios[i])
667
  st.markdown(get_download_link(audios[i], "mp3"), unsafe_allow_html=True)
668
- await save_chat_entry(st.session_state.username, f"PDF Page {i+1} converted to audio: {audios[i]}")
669
 
670
  # 🗂️ Sidebar with Dialog and Audio
671
  st.sidebar.subheader("Voice Settings")
672
- new_username = st.sidebar.selectbox("Change Name/Voice", list(FUN_USERNAMES.keys()), index=list(FUN_USERNAMES.keys()).index(st.session_state.username))
673
  if new_username != st.session_state.username:
674
- await save_chat_entry("System 🌟", f"{st.session_state.username} changed to {new_username}")
675
  st.session_state.username, st.session_state.tts_voice = new_username, FUN_USERNAMES[new_username]
676
  st.session_state.timer_start = time.time()
677
  save_username(st.session_state.username)
678
  st.rerun()
679
 
680
  st.sidebar.markdown("### 💬 Chat Dialog & Media")
681
- chat_content = await load_chat()
682
  lines = chat_content.split('\n')
683
  all_files = sorted(glob.glob("*.md") + glob.glob("*.mp3") + glob.glob("*.png") + glob.glob("*.mp4"), key=os.path.getmtime, reverse=True)
684
  for line in lines[-10:]:
@@ -713,13 +720,13 @@ async def async_interface():
713
  st.sidebar.markdown("### 📂 File History")
714
  for f in all_files[:10]:
715
  st.sidebar.write(f"{FILE_EMOJIS.get(f.split('.')[-1], '📄')} {os.path.basename(f)}")
716
- if st.sidebar.button("⬇️ Zip All"):
717
  zip_name = create_zip_of_files(md_files, mp3_files, png_files, mp4_files, "latest_query")
718
  if zip_name:
719
  st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
720
 
721
- def main():
722
- asyncio.run(async_interface())
723
 
724
  if __name__ == "__main__":
725
  main()
 
126
  'enable_audio': True, 'download_link_cache': {}, 'username': None,
127
  'autosend': True, 'autosearch': True, 'last_message': "", 'last_query': "",
128
  'mp3_files': {}, 'timer_start': time.time(), 'quote_index': 0,
129
+ 'quote_source': "famous", 'last_sent_transcript': "", 'old_val': None
130
  }
131
  for k, v in defaults.items():
132
  if k not in st.session_state:
 
270
  numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(lines) if line.strip())
271
  return numbered_content
272
 
273
+ async def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False, useArxiv=True, useArxivAudio=False):
274
+ start = time.time()
275
+ client = anthropic.Anthropic(api_key=anthropic_key)
276
+ response = client.messages.create(
277
+ model="claude-3-sonnet-20240229",
278
+ max_tokens=1000,
279
+ messages=[{"role": "user", "content": q}]
280
+ )
281
+ st.write("Claude's reply 🧠:")
282
+ st.markdown(response.content[0].text)
283
+
284
+ result = response.content[0].text
285
+ md_file = create_file(q, result, "System")
286
+ audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
287
+ st.subheader("📝 Main Response Audio")
288
+ play_and_download_audio(audio_file)
289
+
290
+ if useArxiv:
291
+ q = q + result
292
+ st.write('Running Arxiv RAG with Claude inputs.')
293
+ gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
294
+ refs = gradio_client.predict(
295
+ q, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md"
296
+ )[0]
297
+ result = f"🔎 {q}\n\n{refs}"
298
+ md_file = create_file(q, result, "System")
299
+ audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
300
+ st.subheader("📝 ArXiv Response Audio")
301
+ play_and_download_audio(audio_file)
302
+
303
+ papers = parse_arxiv_refs(refs)
304
+ if papers and useArxivAudio:
305
+ await create_paper_audio_files(papers, q)
306
+ return result, papers
307
+ elapsed = time.time() - start
308
+ st.write(f"**Total Elapsed:** {elapsed:.2f} s")
309
+ return result, []
310
+
311
  # 🌐 WebSocket Handling
312
  async def websocket_handler(websocket, path):
313
  client_id = str(uuid.uuid4())
 
440
  if p['full_audio']:
441
  p['download_base64'] = get_download_link(p['full_audio'])
442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
  def save_vote(file, item, user_hash):
444
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
445
  entry = f"[{timestamp}] {user_hash} voted for {item}"
 
500
  return zip_name
501
 
502
  # 🎮 Main Interface
503
+ def main():
504
  init_session_state()
505
  load_mp3_viewer()
506
  saved_username = load_username()
507
  if saved_username and saved_username in FUN_USERNAMES:
508
  st.session_state.username = saved_username
509
  if not st.session_state.username:
510
+ available = [n for n in FUN_USERNAMES if not any(f"{n} has joined" in l for l in asyncio.run(load_chat()).split('\n'))]
511
  st.session_state.username = random.choice(available or list(FUN_USERNAMES.keys()))
512
  st.session_state.tts_voice = FUN_USERNAMES[st.session_state.username]
513
+ asyncio.run(save_chat_entry("System 🌟", f"{st.session_state.username} has joined {START_ROOM}!"))
514
  save_username(st.session_state.username)
515
 
516
  st.title(f"{Site_Name} for {st.session_state.username}")
517
  update_marquee_settings_ui()
518
  display_marquee(f"🚀 Welcome to {START_ROOM} | 🤖 {st.session_state.username}", st.session_state['marquee_settings'], "welcome")
519
 
520
+ # Speech Component at Top Level
521
+ mycomponent = components.declare_component("mycomponent", path="mycomponent")
522
+ val = mycomponent(my_input_value="Hello from MyComponent")
523
+ if val:
524
+ val_stripped = val.replace('\\n', ' ')
525
+ edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100, key="speech_input")
526
+ run_option = st.selectbox("Model:", ["Chat", "Arxiv"], key="model_select")
527
+ col1, col2 = st.columns(2)
528
+ with col1:
529
+ autorun = st.checkbox("⚙ AutoRun", value=True, key="autorun")
530
+ with col2:
531
+ full_audio = st.checkbox("📚 FullAudio", value=False, key="full_audio")
532
+
533
+ input_changed = (val != st.session_state.old_val)
534
+
535
+ if autorun and input_changed:
536
+ st.session_state.old_val = val
537
+ st.session_state.last_query = edited_input
538
+ if run_option == "Chat":
539
+ asyncio.run(save_chat_entry(st.session_state.username, edited_input, True))
540
+ elif run_option == "Arxiv":
541
+ asyncio.run(perform_ai_lookup(edited_input, useArxiv=True, useArxivAudio=full_audio))
542
+ elif st.button("▶ Run", key="run_button"):
543
+ st.session_state.old_val = val
544
+ st.session_state.last_query = edited_input
545
+ if run_option == "Chat":
546
+ asyncio.run(save_chat_entry(st.session_state.username, edited_input, True))
547
+ elif run_option == "Arxiv":
548
+ asyncio.run(perform_ai_lookup(edited_input, useArxiv=True, useArxivAudio=full_audio))
549
 
550
  tab_main = st.radio("Action:", ["🎤 Chat & Voice", "📸 Media", "🔍 ArXiv", "📚 PDF to Audio"], horizontal=True)
551
+ useArxiv = st.checkbox("Search ArXiv", True, key="use_arxiv")
552
+ useArxivAudio = st.checkbox("ArXiv Audio", False, key="use_arxiv_audio")
553
+ st.session_state.autosend = st.checkbox("Autosend Chat", value=True, key="autosend")
554
+ st.session_state.autosearch = st.checkbox("Autosearch ArXiv", value=True, key="autosearch")
555
 
556
  # 🎤 Chat & Voice
557
  if tab_main == "🎤 Chat & Voice":
558
  st.subheader(f"{START_ROOM} Chat 💬")
559
+ chat_content = asyncio.run(load_chat())
560
  chat_container = st.container()
561
  with chat_container:
562
  lines = chat_content.split('\n')
 
585
  message = st.text_input(f"Message as {st.session_state.username}", key="message_input_paste", value=st.session_state.message_text)
586
  else:
587
  st.image(paste_result.image_data, caption="Pasted Image")
588
+ filename = asyncio.run(save_pasted_image(paste_result.image_data, st.session_state.username))
589
  if filename:
590
  st.session_state.pasted_image_data = filename
591
  if (message and message != st.session_state.last_message) or st.session_state.pasted_image_data:
592
  st.session_state.last_message = message
593
+ if st.session_state.autosend or st.button("Send 🚀", key="send_button"):
594
  if message.strip():
595
+ asyncio.run(save_chat_entry(st.session_state.username, message, True))
596
  if st.session_state.pasted_image_data:
597
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {st.session_state.pasted_image_data}"))
598
  st.session_state.pasted_image_data = None
599
  st.session_state.timer_start = time.time()
600
  save_username(st.session_state.username)
601
  st.rerun()
602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
  # 📸 Media
604
  elif tab_main == "📸 Media":
605
  st.header("📸 Media Gallery")
 
631
  st.video(mp4)
632
  st.markdown(get_download_link(mp4, "mp4"), unsafe_allow_html=True)
633
 
634
+ uploaded_file = st.file_uploader("Upload Media", type=['png', 'mp4', 'mp3'], key="media_upload")
635
  if uploaded_file:
636
  filename = f"{format_timestamp_prefix(st.session_state.username)}-{hashlib.md5(uploaded_file.getbuffer()).hexdigest()[:8]}.{uploaded_file.name.split('.')[-1]}"
637
  with open(filename, 'wb') as f:
638
  f.write(uploaded_file.getbuffer())
639
+ asyncio.run(save_chat_entry(st.session_state.username, f"Uploaded: {filename}"))
640
  st.session_state.timer_start = time.time()
641
  save_username(st.session_state.username)
642
  st.rerun()
643
 
644
  # 🔍 ArXiv
645
  elif tab_main == "🔍 ArXiv":
646
+ st.subheader("🔍 Query ArXiv")
647
  q = st.text_input("🔍 Query:", key="arxiv_query")
648
  if q and q != st.session_state.last_query:
649
  st.session_state.last_query = q
650
+ if st.session_state.autosearch or st.button("🔍 Run", key="arxiv_run"):
651
+ result, papers = asyncio.run(perform_ai_lookup(q, useArxiv=useArxiv, useArxivAudio=useArxivAudio))
652
  for i, p in enumerate(papers, 1):
653
  with st.expander(f"{i}. 📄 {p['title']}"):
654
  st.markdown(f"**{p['date']} | {p['title']}** — [Link]({p['url']})")
 
659
  # 📚 PDF to Audio
660
  elif tab_main == "📚 PDF to Audio":
661
  audio_processor = AudioProcessor()
662
+ pdf_file = st.file_uploader("Choose PDF", "pdf", key="pdf_upload")
663
+ max_pages = st.slider('Pages', 1, 100, 10, key="pdf_pages")
664
  if pdf_file:
665
  with st.spinner('Processing...'):
666
  texts, audios, total = process_pdf(pdf_file, max_pages, st.session_state['tts_voice'], audio_processor)
 
672
  if audios.get(i):
673
  st.audio(audios[i])
674
  st.markdown(get_download_link(audios[i], "mp3"), unsafe_allow_html=True)
675
+ asyncio.run(save_chat_entry(st.session_state.username, f"PDF Page {i+1} converted to audio: {audios[i]}"))
676
 
677
  # 🗂️ Sidebar with Dialog and Audio
678
  st.sidebar.subheader("Voice Settings")
679
+ new_username = st.sidebar.selectbox("Change Name/Voice", list(FUN_USERNAMES.keys()), index=list(FUN_USERNAMES.keys()).index(st.session_state.username), key="username_select")
680
  if new_username != st.session_state.username:
681
+ asyncio.run(save_chat_entry("System 🌟", f"{st.session_state.username} changed to {new_username}"))
682
  st.session_state.username, st.session_state.tts_voice = new_username, FUN_USERNAMES[new_username]
683
  st.session_state.timer_start = time.time()
684
  save_username(st.session_state.username)
685
  st.rerun()
686
 
687
  st.sidebar.markdown("### 💬 Chat Dialog & Media")
688
+ chat_content = asyncio.run(load_chat())
689
  lines = chat_content.split('\n')
690
  all_files = sorted(glob.glob("*.md") + glob.glob("*.mp3") + glob.glob("*.png") + glob.glob("*.mp4"), key=os.path.getmtime, reverse=True)
691
  for line in lines[-10:]:
 
720
  st.sidebar.markdown("### 📂 File History")
721
  for f in all_files[:10]:
722
  st.sidebar.write(f"{FILE_EMOJIS.get(f.split('.')[-1], '📄')} {os.path.basename(f)}")
723
+ if st.sidebar.button("⬇️ Zip All", key="zip_all"):
724
  zip_name = create_zip_of_files(md_files, mp3_files, png_files, mp4_files, "latest_query")
725
  if zip_name:
726
  st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
727
 
728
+ if not st.session_state.server_task:
729
+ st.session_state.server_task = asyncio.create_task(run_websocket_server())
730
 
731
  if __name__ == "__main__":
732
  main()