awacke1 commited on
Commit
5f86351
·
verified ·
1 Parent(s): 2454e63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +221 -351
app.py CHANGED
@@ -129,7 +129,7 @@ def initialize_marquee_settings():
129
  "background": "#1E1E1E",
130
  "color": "#FFFFFF",
131
  "font-size": "14px",
132
- "animationDuration": "20s", # ensure 20s stays
133
  "width": "100%",
134
  "lineHeight": "35px"
135
  }
@@ -153,7 +153,6 @@ def update_marquee_settings_ui():
153
  key="text_color_picker")
154
  with cols[1]:
155
  font_size = st.slider("📏 Size", 10, 24, 14, key="font_size_slider")
156
- # The default is now 20, not 10
157
  duration = st.slider("⏱️ Speed", 1, 20, 20, key="duration_slider")
158
 
159
  st.session_state['marquee_settings'].update({
@@ -174,6 +173,9 @@ def display_marquee(text, settings, key_suffix=""):
174
  st.write("")
175
 
176
  def get_high_info_terms(text: str, top_n=10) -> list:
 
 
 
177
  stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
178
  words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
179
  bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
@@ -183,30 +185,43 @@ def get_high_info_terms(text: str, top_n=10) -> list:
183
  return [term for term, freq in counter.most_common(top_n)]
184
 
185
  def clean_text_for_filename(text: str) -> str:
 
 
 
186
  text = text.lower()
187
  text = re.sub(r'[^\w\s-]', '', text)
188
  words = text.split()
189
- stop_short = set(['the', 'and', 'for', 'with', 'this', 'that'])
 
190
  filtered = [w for w in words if len(w) > 3 and w not in stop_short]
191
  return '_'.join(filtered)[:200]
192
 
193
 
194
-
195
  def generate_filename(prompt, response, file_type="md", max_length=200):
196
  """
197
  Generate a shortened filename by:
198
  1. Extracting high-info terms
199
  2. Creating a smaller snippet
200
  3. Cleaning & joining them
201
- 4. Truncating if needed
 
202
  """
203
  prefix = format_timestamp_prefix() + "_"
204
- combined_text = (prompt + " " + response)[:200] # limit huge text input
205
  info_terms = get_high_info_terms(combined_text, top_n=5)
206
  snippet = (prompt[:40] + " " + response[:40]).strip()
207
  snippet_cleaned = clean_text_for_filename(snippet)
 
 
208
  name_parts = info_terms + [snippet_cleaned]
209
- full_name = '_'.join(name_parts).strip('_')
 
 
 
 
 
 
 
210
  leftover_chars = max_length - len(prefix) - len(file_type) - 1
211
  if len(full_name) > leftover_chars:
212
  full_name = full_name[:leftover_chars]
@@ -223,31 +238,10 @@ def create_file(prompt, response, file_type="md"):
223
  f.write(prompt + "\n\n" + response)
224
  return filename
225
 
226
-
227
-
228
-
229
-
230
-
231
-
232
- def generate_filename_old(prompt, response, file_type="md"):
233
- prefix = format_timestamp_prefix() + "_"
234
- combined = (prompt + " " + response).strip()
235
- info_terms = get_high_info_terms(combined, top_n=10)
236
- snippet = (prompt[:100] + " " + response[:100]).strip()
237
- snippet_cleaned = clean_text_for_filename(snippet)
238
- name_parts = info_terms + [snippet_cleaned]
239
- full_name = '_'.join(name_parts)
240
- if len(full_name) > 150:
241
- full_name = full_name[:150]
242
- return f"{prefix}{full_name}.{file_type}"
243
-
244
- def create_file_old(prompt, response, file_type="md"):
245
- filename = generate_filename(prompt.strip(), response.strip(), file_type)
246
- with open(filename, 'w', encoding='utf-8') as f:
247
- f.write(prompt + "\n\n" + response)
248
- return filename
249
-
250
  def get_download_link(file, file_type="zip"):
 
 
 
251
  with open(file, "rb") as f:
252
  b64 = base64.b64encode(f.read()).decode()
253
  if file_type == "zip":
@@ -262,6 +256,9 @@ def get_download_link(file, file_type="zip"):
262
  return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
263
 
264
  def clean_for_speech(text: str) -> str:
 
 
 
265
  text = text.replace("\n", " ")
266
  text = text.replace("</s>", " ")
267
  text = text.replace("#", "")
@@ -284,13 +281,14 @@ def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_fo
284
  return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
285
 
286
  def play_and_download_audio(file_path, file_type="mp3"):
 
287
  if file_path and os.path.exists(file_path):
288
  st.audio(file_path)
289
  dl_link = get_download_link(file_path, file_type=file_type)
290
  st.markdown(dl_link, unsafe_allow_html=True)
291
 
292
  def save_qa_with_audio(question, answer, voice=None):
293
- """Save Q&A to markdown and generate audio"""
294
  if not voice:
295
  voice = st.session_state['tts_voice']
296
 
@@ -308,56 +306,11 @@ def save_qa_with_audio(question, answer, voice=None):
308
 
309
  return md_file, audio_file
310
 
311
- def process_paper_content(paper):
312
- marquee_text = f"📄 {paper['title']} | 👤 {paper['authors'][:100]} | 📝 {paper['summary'][:500]}"
313
- audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
314
- return marquee_text, audio_text
315
-
316
- def create_paper_audio_files(papers, input_question):
317
- for paper in papers:
318
- try:
319
- marquee_text, audio_text = process_paper_content(paper)
320
-
321
- audio_text = clean_for_speech(audio_text)
322
- file_format = st.session_state['audio_format']
323
- audio_file = speak_with_edge_tts(audio_text,
324
- voice=st.session_state['tts_voice'],
325
- file_format=file_format)
326
- paper['full_audio'] = audio_file
327
-
328
- st.write(f"### {FILE_EMOJIS.get(file_format, '')} {os.path.basename(audio_file)}")
329
- play_and_download_audio(audio_file, file_type=file_format)
330
- paper['marquee_text'] = marquee_text
331
-
332
- except Exception as e:
333
- st.warning(f"Error processing paper {paper['title']}: {str(e)}")
334
- paper['full_audio'] = None
335
- paper['marquee_text'] = None
336
-
337
- def display_papers(papers, marquee_settings):
338
- st.write("## Research Papers")
339
-
340
- papercount = 0
341
- for paper in papers:
342
- papercount += 1
343
- if papercount <= 20:
344
- if paper.get('marquee_text'):
345
- display_marquee(paper['marquee_text'],
346
- marquee_settings,
347
- key_suffix=f"paper_{papercount}")
348
-
349
- with st.expander(f"{papercount}. 📄 {paper['title']}", expanded=True):
350
- st.markdown(f"**{paper['date']} | {paper['title']} | ⬇️**")
351
- st.markdown(f"*{paper['authors']}*")
352
- st.markdown(paper['summary'])
353
-
354
- if paper.get('full_audio'):
355
- st.write("📚 Paper Audio")
356
- file_ext = os.path.splitext(paper['full_audio'])[1].lower().strip('.')
357
- if file_ext in ['mp3', 'wav']:
358
- st.audio(paper['full_audio'])
359
-
360
  def parse_arxiv_refs(ref_text: str):
 
 
 
 
361
  if not ref_text:
362
  return []
363
 
@@ -367,6 +320,7 @@ def parse_arxiv_refs(ref_text: str):
367
 
368
  for i, line in enumerate(lines):
369
  if line.count('|') == 2:
 
370
  if current_paper:
371
  results.append(current_paper)
372
  if len(results) >= 20:
@@ -385,7 +339,8 @@ def parse_arxiv_refs(ref_text: str):
385
  'url': url,
386
  'authors': '',
387
  'summary': '',
388
- 'content_start': i + 1
 
389
  }
390
  except Exception as e:
391
  st.warning(f"Error parsing paper header: {str(e)}")
@@ -393,6 +348,7 @@ def parse_arxiv_refs(ref_text: str):
393
  continue
394
 
395
  elif current_paper:
 
396
  if not current_paper['authors']:
397
  current_paper['authors'] = line.strip('* ')
398
  else:
@@ -406,65 +362,140 @@ def parse_arxiv_refs(ref_text: str):
406
 
407
  return results[:20]
408
 
 
 
 
 
 
 
 
 
 
 
409
 
410
- # ---------------------------- Edit 1/11/2025 - add a constitution to my arxiv system templating to build configurable character and personality of IO.
411
-
412
- def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
413
- titles_summary=True, full_audio=False):
414
- start = time.time()
415
-
416
- ai_constitution = """
417
- You are a talented AI coder and songwriter with a unique ability to explain scientific concepts through music with code easter eggs.. Your task is to create a song that not only entertains but also educates listeners about a specific science problem and its potential solutions.
418
-
419
- 1. First, carefully read and analyze the problem provided:
420
- <science_problem>
421
- {{q}}
422
- </science_problem>
423
 
424
- 2. Next, consider the style requested:
425
- <song_style>
426
- {{SONG_STYLE}}
427
- </song_style>
 
 
 
428
 
429
- 3. Follow these steps to create your output:
 
 
 
 
 
 
 
 
 
 
430
 
431
- 1. Analyze the problem:
432
- - Identify the key issues and challenges
433
- - Note any potential solutions or technologies mentioned, especially in AI
434
- - Consider how these concepts can be simplified for a general audience
435
 
436
- 2. Plan your structure. Document and enumerate in markdown outlines with emojis.:
437
- - Decide on a format that fits the style
438
- - Plan to introduce the problem
439
- - Highlight key points or solutions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
 
441
- 3. Write.:
442
- - Begin with an attention-grabbing opening line
443
- - Use metaphors and analogies to explain complex concepts
444
- - Ensure the flow naturally fits the rhythm of the chosen style
445
- - Include scientific terminology, but explain it in simple terms within
 
 
 
 
 
 
 
 
 
 
 
 
446
 
447
- 4. Incorporate scientific explanations.:
448
- - Weave factual information throughout the verses
449
- - Use the chorus to reinforce main ideas or solutions
450
- - Ensure that the scientific content is accurate and up-to-date
 
 
 
 
451
 
452
- 5. Match the requested style.:
453
- - Adapt your word choice and phrasing to fit the genre
454
- - Consider the typical rhythm and structure of this style
455
- - If applicable, include style-specific elements
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
 
457
- 6. Review and refine, add useful paper titles, keywords, descriptions of topics and concepts.:
458
- - Check that effectively communicates the problem and solutions
459
- - Ensure catchy and memorable
460
- - Verify maintains the requested style throughout
461
- """
462
 
 
463
 
 
 
 
464
 
 
 
465
 
466
-
467
- # Claude then Arxiv..
468
 
469
  # Claude:
470
  client = anthropic.Anthropic(api_key=anthropic_key)
@@ -480,19 +511,14 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
480
  st.write("Claude's reply 🧠:")
481
  st.markdown(response.content[0].text)
482
 
483
- # Render audio track for Claude Response
484
- #filename = generate_filename(q, response.content[0].text)
485
- result = response.content[0].text
486
- create_file(q, result)
487
  # Save and produce audio for Claude response
 
 
488
  md_file, audio_file = save_qa_with_audio(q, result)
489
  st.subheader("📝 Main Response Audio")
490
  play_and_download_audio(audio_file, st.session_state['audio_format'])
491
 
492
 
493
-
494
-
495
-
496
  # Arxiv:
497
  st.write("Arxiv's AI this Evening is Mixtral 8x7B MoE Instruct with 9 English Voices 🧠:")
498
 
@@ -506,7 +532,7 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
506
 
507
  result = f"### 🔎 {q}\n\n{r2}\n\n{refs}"
508
 
509
- # Save and produce audio
510
  md_file, audio_file = save_qa_with_audio(q, result)
511
 
512
  st.subheader("📝 Main Response Audio")
@@ -514,14 +540,22 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
514
 
515
  papers = parse_arxiv_refs(refs)
516
  if papers:
 
 
 
 
 
 
517
  create_paper_audio_files(papers, input_question=q)
518
  display_papers(papers, get_marquee_settings())
 
 
 
519
  else:
520
  st.warning("No papers found in the response.")
521
 
522
- elapsed = time.time()-start
523
  st.write(f"**Total Elapsed:** {elapsed:.2f} s")
524
-
525
  return result
526
 
527
  def process_voice_input(text):
@@ -537,6 +571,7 @@ def process_voice_input(text):
537
  full_audio=True
538
  )
539
 
 
540
  md_file, audio_file = save_qa_with_audio(text, result)
541
 
542
  st.subheader("📝 Generated Files")
@@ -544,153 +579,25 @@ def process_voice_input(text):
544
  st.write(f"Audio: {audio_file}")
545
  play_and_download_audio(audio_file, st.session_state['audio_format'])
546
 
547
- def load_files_for_sidebar():
548
- md_files = glob.glob("*.md")
549
- mp3_files = glob.glob("*.mp3")
550
- wav_files = glob.glob("*.wav")
551
-
552
- md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
553
- all_files = md_files + mp3_files + wav_files
554
-
555
- groups = defaultdict(list)
556
- prefix_length = len("MM_dd_yy_hh_mm_AP")
557
-
558
- for f in all_files:
559
- basename = os.path.basename(f)
560
- if len(basename) >= prefix_length and '_' in basename:
561
- group_name = basename[:prefix_length]
562
- groups[group_name].append(f)
563
- else:
564
- groups['Other'].append(f)
565
-
566
- sorted_groups = sorted(groups.items(),
567
- key=lambda x: x[0] if x[0] != 'Other' else '',
568
- reverse=True)
569
- return sorted_groups
570
-
571
- def display_file_manager_sidebar(groups_sorted):
572
- st.sidebar.title("🎵 Audio & Docs Manager")
573
-
574
- all_md = []
575
- all_mp3 = []
576
- all_wav = []
577
- for _, files in groups_sorted:
578
- for f in files:
579
- if f.endswith(".md"):
580
- all_md.append(f)
581
- elif f.endswith(".mp3"):
582
- all_mp3.append(f)
583
- elif f.endswith(".wav"):
584
- all_wav.append(f)
585
-
586
- col1, col4 = st.sidebar.columns(2)
587
- with col1:
588
- if st.button("🗑 Delete All"):
589
- for f in all_md:
590
- os.remove(f)
591
- for f in all_mp3:
592
- os.remove(f)
593
- for f in all_wav:
594
- os.remove(f)
595
- st.session_state.should_rerun = True
596
- with col4:
597
- if st.button("⬇️ Zip All"):
598
- zip_name = create_zip_of_files(all_md, all_mp3, all_wav, st.session_state.get('last_query', ''))
599
- if zip_name:
600
- st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
601
-
602
- for group_name, files in groups_sorted:
603
- if group_name == 'Other':
604
- group_label = 'Other Files'
605
- else:
606
- try:
607
- timestamp_dt = datetime.strptime(group_name, "%m_%d_%y_%I_%M_%p")
608
- group_label = timestamp_dt.strftime("%b %d, %Y %I:%M %p")
609
- except ValueError:
610
- group_label = group_name
611
-
612
- with st.sidebar.expander(f"📁 {group_label} ({len(files)})", expanded=True):
613
- c1, c2 = st.columns(2)
614
- with c1:
615
- if st.button("👀 View", key=f"view_group_{group_name}"):
616
- st.session_state.viewing_prefix = group_name
617
- with c2:
618
- if st.button("🗑 Del", key=f"del_group_{group_name}"):
619
- for f in files:
620
- os.remove(f)
621
- st.success(f"Deleted group {group_label}!")
622
- st.session_state.should_rerun = True
623
-
624
- for f in files:
625
- fname = os.path.basename(f)
626
- ext = os.path.splitext(fname)[1].lower()
627
- emoji = FILE_EMOJIS.get(ext.strip('.'), '')
628
- mtime = os.path.getmtime(f)
629
- ctime = datetime.fromtimestamp(mtime).strftime("%I:%M:%S %p")
630
- st.write(f"{emoji} **{fname}** - {ctime}")
631
-
632
- def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
633
- md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
634
- all_files = md_files + mp3_files + wav_files
635
- if not all_files:
636
- return None
637
-
638
- all_content = []
639
- for f in all_files:
640
- if f.endswith('.md'):
641
- with open(f, 'r', encoding='utf-8') as file:
642
- all_content.append(file.read())
643
- elif f.endswith('.mp3') or f.endswith('.wav'):
644
- basename = os.path.splitext(os.path.basename(f))[0]
645
- words = basename.replace('_', ' ')
646
- all_content.append(words)
647
-
648
- all_content.append(input_question)
649
- combined_content = " ".join(all_content)
650
- info_terms = get_high_info_terms(combined_content, top_n=10)
651
-
652
- timestamp = format_timestamp_prefix()
653
- name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:10])
654
- zip_name = f"{timestamp}_{name_text}.zip"
655
-
656
- with zipfile.ZipFile(zip_name, 'w') as z:
657
- for f in all_files:
658
- z.write(f)
659
-
660
- return zip_name
661
-
662
  def main():
663
- # Update marquee settings UI first
664
  update_marquee_settings_ui()
665
  marquee_settings = get_marquee_settings()
666
-
667
  # Initial welcome marquee
668
  display_marquee(st.session_state['marquee_content'],
669
- {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
670
- key_suffix="welcome")
671
 
672
- # Load files for sidebar
673
- groups_sorted = load_files_for_sidebar()
674
-
675
- # Update marquee content when viewing files
676
- if st.session_state.viewing_prefix:
677
- for group_name, files in groups_sorted:
678
- if group_name == st.session_state.viewing_prefix:
679
- for f in files:
680
- if f.endswith('.md'):
681
- with open(f, 'r', encoding='utf-8') as file:
682
- st.session_state['marquee_content'] = file.read()[:280]
683
-
684
- # Instead of putting voice settings in the sidebar,
685
- # we will handle them in the "🎤 Voice" tab below.
686
-
687
- # Main Interface
688
  tab_main = st.radio("Action:", ["🎤 Voice", "📸 Media", "🔍 ArXiv", "📝 Editor"],
689
- horizontal=True)
690
 
 
691
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
692
  val = mycomponent(my_input_value="Hello")
693
 
 
694
  if val:
695
  val_stripped = val.replace('\\n', ' ')
696
  edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
@@ -707,14 +614,20 @@ def main():
707
  if autorun and input_changed:
708
  st.session_state.old_val = val
709
  st.session_state.last_query = edited_input
710
- result = perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
711
- titles_summary=True, full_audio=full_audio)
 
 
 
712
  else:
713
  if st.button("▶ Run"):
714
  st.session_state.old_val = val
715
  st.session_state.last_query = edited_input
716
- result = perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
717
- titles_summary=True, full_audio=full_audio)
 
 
 
718
 
719
  # --- Tab: ArXiv
720
  if tab_main == "🔍 ArXiv":
@@ -739,7 +652,7 @@ def main():
739
  elif tab_main == "🎤 Voice":
740
  st.subheader("🎤 Voice Input")
741
 
742
- # Move voice selection here:
743
  st.markdown("### 🎤 Voice Settings")
744
  selected_voice = st.selectbox(
745
  "Select TTS Voice:",
@@ -747,7 +660,6 @@ def main():
747
  index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
748
  )
749
 
750
- # Audio Format Settings below the voice selection
751
  st.markdown("### 🔊 Audio Format")
752
  selected_format = st.radio(
753
  "Choose Audio Format:",
@@ -762,7 +674,7 @@ def main():
762
  st.session_state['audio_format'] = selected_format.lower()
763
  st.rerun()
764
 
765
- # Now the text area to enter your message
766
  user_text = st.text_area("💬 Message:", height=100)
767
  user_text = user_text.strip().replace('\n', ' ')
768
 
@@ -776,92 +688,49 @@ def main():
776
 
777
  # --- Tab: Media
778
  elif tab_main == "📸 Media":
779
- st.header("📸 Images & 🎥 Videos")
780
- tabs = st.tabs(["🖼 Images", "🎥 Video"])
 
781
  with tabs[0]:
782
- imgs = glob.glob("*.png") + glob.glob("*.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
  if imgs:
784
- c = st.slider("Cols", 1, 5, 3)
785
  cols = st.columns(c)
786
  for i, f in enumerate(imgs):
787
  with cols[i % c]:
788
  st.image(Image.open(f), use_container_width=True)
789
- if st.button(f"👀 Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
790
- response = openai_client.chat.completions.create(
791
- model=st.session_state["openai_model"],
792
- messages=[
793
- {"role": "system", "content": "Analyze the image content."},
794
- {"role": "user", "content": [
795
- {"type": "image_url",
796
- "image_url": {"url": f"data:image/jpeg;base64,{base64.b64encode(open(f, 'rb').read()).decode()}"}}
797
- ]}
798
- ]
799
- )
800
- st.markdown(response.choices[0].message.content)
801
  else:
802
  st.write("No images found.")
803
-
804
- with tabs[1]:
805
- vids = glob.glob("*.mp4")
 
 
806
  if vids:
807
  for v in vids:
808
- with st.expander(f"🎥 {os.path.basename(v)}"):
809
  st.video(v)
810
- if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
811
- frames = process_video(v)
812
- response = openai_client.chat.completions.create(
813
- model=st.session_state["openai_model"],
814
- messages=[
815
- {"role": "system", "content": "Analyze video frames."},
816
- {"role": "user", "content": [
817
- {"type": "image_url",
818
- "image_url": {"url": f"data:image/jpeg;base64,{frame}"}}
819
- for frame in frames
820
- ]}
821
- ]
822
- )
823
- st.markdown(response.choices[0].message.content)
824
  else:
825
  st.write("No videos found.")
826
 
827
  # --- Tab: Editor
828
  elif tab_main == "📝 Editor":
829
- if st.session_state.editing_file:
830
- st.subheader(f"Editing: {st.session_state.editing_file}")
831
- new_text = st.text_area("✏️ Content:", st.session_state.edit_new_content, height=300)
832
- if st.button("💾 Save"):
833
- with open(st.session_state.editing_file, 'w', encoding='utf-8') as f:
834
- f.write(new_text)
835
- st.success("File updated successfully!")
836
- st.session_state.should_rerun = True
837
- st.session_state.editing_file = None
838
- else:
839
- st.write("Select a file from the sidebar to edit.")
840
-
841
- # Display file manager in sidebar
842
- display_file_manager_sidebar(groups_sorted)
843
-
844
- # Display viewed group content
845
- if st.session_state.viewing_prefix and any(st.session_state.viewing_prefix == group for group, _ in groups_sorted):
846
- st.write("---")
847
- st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
848
- for group_name, files in groups_sorted:
849
- if group_name == st.session_state.viewing_prefix:
850
- for f in files:
851
- fname = os.path.basename(f)
852
- ext = os.path.splitext(fname)[1].lower().strip('.')
853
- st.write(f"### {fname}")
854
- if ext == "md":
855
- content = open(f, 'r', encoding='utf-8').read()
856
- st.markdown(content)
857
- elif ext in ["mp3", "wav"]:
858
- st.audio(f)
859
- else:
860
- st.markdown(get_download_link(f), unsafe_allow_html=True)
861
- break
862
- if st.button("❌ Close"):
863
- st.session_state.viewing_prefix = None
864
- st.session_state['marquee_content'] = "🚀 Welcome to Deep Research Evaluator | 🤖 Your Talking Research Assistant"
865
 
866
  st.markdown("""
867
  <style>
@@ -875,5 +744,6 @@ def main():
875
  st.session_state.should_rerun = False
876
  st.rerun()
877
 
 
878
  if __name__ == "__main__":
879
  main()
 
129
  "background": "#1E1E1E",
130
  "color": "#FFFFFF",
131
  "font-size": "14px",
132
+ "animationDuration": "20s",
133
  "width": "100%",
134
  "lineHeight": "35px"
135
  }
 
153
  key="text_color_picker")
154
  with cols[1]:
155
  font_size = st.slider("📏 Size", 10, 24, 14, key="font_size_slider")
 
156
  duration = st.slider("⏱️ Speed", 1, 20, 20, key="duration_slider")
157
 
158
  st.session_state['marquee_settings'].update({
 
173
  st.write("")
174
 
175
  def get_high_info_terms(text: str, top_n=10) -> list:
176
+ """
177
+ Finds the top_n frequent words or bigrams (excluding some common stopwords).
178
+ """
179
  stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
180
  words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
181
  bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
 
185
  return [term for term, freq in counter.most_common(top_n)]
186
 
187
  def clean_text_for_filename(text: str) -> str:
188
+ """
189
+ Cleans a text so it can be used in a filename.
190
+ """
191
  text = text.lower()
192
  text = re.sub(r'[^\w\s-]', '', text)
193
  words = text.split()
194
+ # remove short or unhelpful words
195
+ stop_short = set(['the', 'and', 'for', 'with', 'this', 'that', 'ai', 'library'])
196
  filtered = [w for w in words if len(w) > 3 and w not in stop_short]
197
  return '_'.join(filtered)[:200]
198
 
199
 
 
200
  def generate_filename(prompt, response, file_type="md", max_length=200):
201
  """
202
  Generate a shortened filename by:
203
  1. Extracting high-info terms
204
  2. Creating a smaller snippet
205
  3. Cleaning & joining them
206
+ 4. Removing duplicates
207
+ 5. Truncating if needed
208
  """
209
  prefix = format_timestamp_prefix() + "_"
210
+ combined_text = (prompt + " " + response)[:200]
211
  info_terms = get_high_info_terms(combined_text, top_n=5)
212
  snippet = (prompt[:40] + " " + response[:40]).strip()
213
  snippet_cleaned = clean_text_for_filename(snippet)
214
+
215
+ # Combine info terms + snippet, remove duplicates
216
  name_parts = info_terms + [snippet_cleaned]
217
+ seen = set()
218
+ unique_parts = []
219
+ for part in name_parts:
220
+ if part not in seen:
221
+ seen.add(part)
222
+ unique_parts.append(part)
223
+ full_name = '_'.join(unique_parts).strip('_')
224
+
225
  leftover_chars = max_length - len(prefix) - len(file_type) - 1
226
  if len(full_name) > leftover_chars:
227
  full_name = full_name[:leftover_chars]
 
238
  f.write(prompt + "\n\n" + response)
239
  return filename
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  def get_download_link(file, file_type="zip"):
242
+ """
243
+ Returns an HTML anchor tag for downloading the specified file (base64-encoded).
244
+ """
245
  with open(file, "rb") as f:
246
  b64 = base64.b64encode(f.read()).decode()
247
  if file_type == "zip":
 
256
  return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
257
 
258
  def clean_for_speech(text: str) -> str:
259
+ """
260
+ Cleans text to make TTS output more coherent.
261
+ """
262
  text = text.replace("\n", " ")
263
  text = text.replace("</s>", " ")
264
  text = text.replace("#", "")
 
281
  return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
282
 
283
  def play_and_download_audio(file_path, file_type="mp3"):
284
+ """Play audio and show a direct download link in the main area."""
285
  if file_path and os.path.exists(file_path):
286
  st.audio(file_path)
287
  dl_link = get_download_link(file_path, file_type=file_type)
288
  st.markdown(dl_link, unsafe_allow_html=True)
289
 
290
  def save_qa_with_audio(question, answer, voice=None):
291
+ """Save Q&A to markdown and generate audio file."""
292
  if not voice:
293
  voice = st.session_state['tts_voice']
294
 
 
306
 
307
  return md_file, audio_file
308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  def parse_arxiv_refs(ref_text: str):
310
+ """
311
+ Given a multi-line markdown with arxiv references, parse them into
312
+ a structure: [{date, title, url, authors, summary}, ...]
313
+ """
314
  if not ref_text:
315
  return []
316
 
 
320
 
321
  for i, line in enumerate(lines):
322
  if line.count('|') == 2:
323
+ # We found a new paper header line
324
  if current_paper:
325
  results.append(current_paper)
326
  if len(results) >= 20:
 
339
  'url': url,
340
  'authors': '',
341
  'summary': '',
342
+ 'full_audio': None,
343
+ 'download_base64': '',
344
  }
345
  except Exception as e:
346
  st.warning(f"Error parsing paper header: {str(e)}")
 
348
  continue
349
 
350
  elif current_paper:
351
+ # Fill authors if empty, else fill summary
352
  if not current_paper['authors']:
353
  current_paper['authors'] = line.strip('* ')
354
  else:
 
362
 
363
  return results[:20]
364
 
365
+ def create_paper_links_md(papers):
366
+ """
367
+ Creates a minimal markdown list of paper titles + arxiv links
368
+ (and if you store PDF links, you could also include them).
369
+ """
370
+ lines = ["# Paper Links\n"]
371
+ for i, p in enumerate(papers, start=1):
372
+ # Basic link
373
+ lines.append(f"{i}. **{p['title']}** — [Arxiv]({p['url']})")
374
+ return "\n".join(lines)
375
 
376
+ def create_paper_audio_files(papers, input_question):
377
+ """
378
+ Generate TTS audio for each paper, store base64 link for stable download,
379
+ and attach to each paper dict.
380
+ """
381
+ for paper in papers:
382
+ try:
383
+ # Just a short version for TTS
384
+ audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
385
+ audio_text = clean_for_speech(audio_text)
 
 
 
386
 
387
+ file_format = st.session_state['audio_format']
388
+ audio_file = speak_with_edge_tts(
389
+ audio_text,
390
+ voice=st.session_state['tts_voice'],
391
+ file_format=file_format
392
+ )
393
+ paper['full_audio'] = audio_file
394
 
395
+ # Store a base64 link with consistent name
396
+ if audio_file:
397
+ with open(audio_file, "rb") as af:
398
+ b64_data = base64.b64encode(af.read()).decode()
399
+ # We'll keep the original file's name as the stable download name
400
+ download_filename = os.path.basename(audio_file)
401
+ mime_type = "mpeg" if file_format == "mp3" else "wav"
402
+ paper['download_base64'] = (
403
+ f'<a href="data:audio/{mime_type};base64,{b64_data}" '
404
+ f'download="{download_filename}">🎵 Download {download_filename}</a>'
405
+ )
406
 
407
+ except Exception as e:
408
+ st.warning(f"Error processing paper {paper['title']}: {str(e)}")
409
+ paper['full_audio'] = None
410
+ paper['download_base64'] = ''
411
 
412
+ def display_papers(papers, marquee_settings):
413
+ """
414
+ Display the papers in the main area with marquee + expanders + audio.
415
+ """
416
+ st.write("## Research Papers")
417
+
418
+ for i, paper in enumerate(papers, start=1):
419
+ # Show marquee
420
+ marquee_text = f"📄 {paper['title']} | 👤 {paper['authors'][:120]} | 📝 {paper['summary'][:200]}"
421
+ display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
422
+
423
+ with st.expander(f"{i}. 📄 {paper['title']}", expanded=True):
424
+ st.markdown(f"**{paper['date']} | {paper['title']} |** [Arxiv Link]({paper['url']})")
425
+ st.markdown(f"*Authors:* {paper['authors']}")
426
+ st.markdown(paper['summary'])
427
+
428
+ if paper.get('full_audio'):
429
+ st.write("📚 Paper Audio")
430
+ st.audio(paper['full_audio'])
431
+ if paper['download_base64']:
432
+ st.markdown(paper['download_base64'], unsafe_allow_html=True)
433
 
434
+ def display_papers_in_sidebar(papers):
435
+ """
436
+ New approach: in the sidebar, mirror the paper listing
437
+ with expanders for each paper, link to arxiv, st.audio, etc.
438
+ """
439
+ st.sidebar.title("🎶 Papers & Audio")
440
+ for i, paper in enumerate(papers, start=1):
441
+ with st.sidebar.expander(f"{i}. {paper['title']}"):
442
+ st.markdown(f"**Arxiv:** [Link]({paper['url']})")
443
+ if paper['full_audio']:
444
+ st.audio(paper['full_audio'])
445
+ if paper['download_base64']:
446
+ st.markdown(paper['download_base64'], unsafe_allow_html=True)
447
+ # Show minimal text if desired:
448
+ st.markdown(f"**Authors:** {paper['authors']}")
449
+ if paper['summary']:
450
+ st.markdown(f"**Summary:** {paper['summary'][:300]}...")
451
 
452
+ def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
453
+ """
454
+ Zip up all relevant files, but limit final zip name to 20 chars.
455
+ """
456
+ md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
457
+ all_files = md_files + mp3_files + wav_files
458
+ if not all_files:
459
+ return None
460
 
461
+ all_content = []
462
+ for f in all_files:
463
+ if f.endswith('.md'):
464
+ with open(f, 'r', encoding='utf-8') as file:
465
+ all_content.append(file.read())
466
+ elif f.endswith('.mp3') or f.endswith('.wav'):
467
+ # Add some text representation
468
+ basename = os.path.splitext(os.path.basename(f))[0]
469
+ words = basename.replace('_', ' ')
470
+ all_content.append(words)
471
+
472
+ all_content.append(input_question)
473
+ combined_content = " ".join(all_content)
474
+ info_terms = get_high_info_terms(combined_content, top_n=10)
475
+
476
+ timestamp = format_timestamp_prefix()
477
+ name_text = '-'.join(term for term in info_terms[:5]) # shorter
478
+ # Limit the final name to 20 chars (excluding .zip)
479
+ short_zip_name = (timestamp + "_" + name_text)[:20] + ".zip"
480
+
481
+ with zipfile.ZipFile(short_zip_name, 'w') as z:
482
+ for f in all_files:
483
+ z.write(f)
484
+
485
+ return short_zip_name
486
 
 
 
 
 
 
487
 
488
+ # ---------------------------- 1/11/2025 - add a constitution to my arxiv system templating to build configurable personality
489
 
490
+ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
491
+ titles_summary=True, full_audio=False):
492
+ start = time.time()
493
 
494
+ ai_constitution = """
495
+ You are a talented AI coder and songwriter with a unique ability to explain scientific concepts through music with code easter eggs.. Your task is to create a song that not only entertains but also educates listeners about a specific science problem and its potential solutions.
496
 
497
+ (Omitted extra instructions for brevity...)
498
+ """
499
 
500
  # Claude:
501
  client = anthropic.Anthropic(api_key=anthropic_key)
 
511
  st.write("Claude's reply 🧠:")
512
  st.markdown(response.content[0].text)
513
 
 
 
 
 
514
  # Save and produce audio for Claude response
515
+ result = response.content[0].text
516
+ create_file(q, result) # MD file
517
  md_file, audio_file = save_qa_with_audio(q, result)
518
  st.subheader("📝 Main Response Audio")
519
  play_and_download_audio(audio_file, st.session_state['audio_format'])
520
 
521
 
 
 
 
522
  # Arxiv:
523
  st.write("Arxiv's AI this Evening is Mixtral 8x7B MoE Instruct with 9 English Voices 🧠:")
524
 
 
532
 
533
  result = f"### 🔎 {q}\n\n{r2}\n\n{refs}"
534
 
535
+ # Save and produce audio for second response
536
  md_file, audio_file = save_qa_with_audio(q, result)
537
 
538
  st.subheader("📝 Main Response Audio")
 
540
 
541
  papers = parse_arxiv_refs(refs)
542
  if papers:
543
+ # 4) Create & show a minimal markdown links page before generating audio
544
+ paper_links = create_paper_links_md(papers)
545
+ links_file = create_file(q, paper_links, "md")
546
+ st.markdown(paper_links)
547
+
548
+ # Now produce audio for each paper
549
  create_paper_audio_files(papers, input_question=q)
550
  display_papers(papers, get_marquee_settings())
551
+
552
+ # Also display in the sidebar as requested
553
+ display_papers_in_sidebar(papers)
554
  else:
555
  st.warning("No papers found in the response.")
556
 
557
+ elapsed = time.time() - start
558
  st.write(f"**Total Elapsed:** {elapsed:.2f} s")
 
559
  return result
560
 
561
  def process_voice_input(text):
 
571
  full_audio=True
572
  )
573
 
574
+ # Save final Q&A with audio
575
  md_file, audio_file = save_qa_with_audio(text, result)
576
 
577
  st.subheader("📝 Generated Files")
 
579
  st.write(f"Audio: {audio_file}")
580
  play_and_download_audio(audio_file, st.session_state['audio_format'])
581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  def main():
583
+ # Update marquee settings UI
584
  update_marquee_settings_ui()
585
  marquee_settings = get_marquee_settings()
586
+
587
  # Initial welcome marquee
588
  display_marquee(st.session_state['marquee_content'],
589
+ {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
590
+ key_suffix="welcome")
591
 
592
+ # Main action tabs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  tab_main = st.radio("Action:", ["🎤 Voice", "📸 Media", "🔍 ArXiv", "📝 Editor"],
594
+ horizontal=True)
595
 
596
+ # Simple example usage of a Streamlit component (placeholder)
597
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
598
  val = mycomponent(my_input_value="Hello")
599
 
600
+ # Quick example - if the component returns text:
601
  if val:
602
  val_stripped = val.replace('\\n', ' ')
603
  edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
 
614
  if autorun and input_changed:
615
  st.session_state.old_val = val
616
  st.session_state.last_query = edited_input
617
+ perform_ai_lookup(edited_input,
618
+ vocal_summary=True,
619
+ extended_refs=False,
620
+ titles_summary=True,
621
+ full_audio=full_audio)
622
  else:
623
  if st.button("▶ Run"):
624
  st.session_state.old_val = val
625
  st.session_state.last_query = edited_input
626
+ perform_ai_lookup(edited_input,
627
+ vocal_summary=True,
628
+ extended_refs=False,
629
+ titles_summary=True,
630
+ full_audio=full_audio)
631
 
632
  # --- Tab: ArXiv
633
  if tab_main == "🔍 ArXiv":
 
652
  elif tab_main == "🎤 Voice":
653
  st.subheader("🎤 Voice Input")
654
 
655
+ # Voice and format settings
656
  st.markdown("### 🎤 Voice Settings")
657
  selected_voice = st.selectbox(
658
  "Select TTS Voice:",
 
660
  index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
661
  )
662
 
 
663
  st.markdown("### 🔊 Audio Format")
664
  selected_format = st.radio(
665
  "Choose Audio Format:",
 
674
  st.session_state['audio_format'] = selected_format.lower()
675
  st.rerun()
676
 
677
+ # User text
678
  user_text = st.text_area("💬 Message:", height=100)
679
  user_text = user_text.strip().replace('\n', ' ')
680
 
 
688
 
689
  # --- Tab: Media
690
  elif tab_main == "📸 Media":
691
+ st.header("📸 Media Gallery")
692
+ tabs = st.tabs(["🎵 Audio", "🖼 Images", "🎥 Video"]) # audio first = default
693
+ # --- Audio Tab
694
  with tabs[0]:
695
+ st.subheader("🎵 Audio Files")
696
+ audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
697
+ if audio_files:
698
+ for a in audio_files:
699
+ with st.expander(os.path.basename(a)):
700
+ st.audio(a)
701
+ ext = os.path.splitext(a)[1].replace('.', '')
702
+ dl_link = get_download_link(a, file_type=ext)
703
+ st.markdown(dl_link, unsafe_allow_html=True)
704
+ else:
705
+ st.write("No audio files found.")
706
+
707
+ # --- Images Tab
708
+ with tabs[1]:
709
+ st.subheader("🖼 Image Files")
710
+ imgs = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
711
  if imgs:
712
+ c = st.slider("Cols", 1, 5, 3, key="cols_images")
713
  cols = st.columns(c)
714
  for i, f in enumerate(imgs):
715
  with cols[i % c]:
716
  st.image(Image.open(f), use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
717
  else:
718
  st.write("No images found.")
719
+
720
+ # --- Video Tab
721
+ with tabs[2]:
722
+ st.subheader("🎥 Video Files")
723
+ vids = glob.glob("*.mp4") + glob.glob("*.mov") + glob.glob("*.avi")
724
  if vids:
725
  for v in vids:
726
+ with st.expander(os.path.basename(v)):
727
  st.video(v)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  else:
729
  st.write("No videos found.")
730
 
731
  # --- Tab: Editor
732
  elif tab_main == "📝 Editor":
733
+ st.write("Select or create a file to edit. (Currently minimal demo)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734
 
735
  st.markdown("""
736
  <style>
 
744
  st.session_state.should_rerun = False
745
  st.rerun()
746
 
747
+
748
  if __name__ == "__main__":
749
  main()