awacke1 commited on
Commit
c6df19f
ยท
verified ยท
1 Parent(s): ee45388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +260 -297
app.py CHANGED
@@ -37,8 +37,8 @@ load_dotenv()
37
 
38
  # Add available English voices for Edge TTS
39
  EDGE_TTS_VOICES = [
40
- "en-US-AriaNeural", # Default voice
41
- "en-US-GuyNeural",
42
  "en-US-JennyNeural",
43
  "en-GB-SoniaNeural",
44
  "en-GB-RyanNeural",
@@ -48,6 +48,16 @@ EDGE_TTS_VOICES = [
48
  "en-CA-LiamNeural"
49
  ]
50
 
 
 
 
 
 
 
 
 
 
 
51
  # Initialize session state variables
52
  if 'tts_voice' not in st.session_state:
53
  st.session_state['tts_voice'] = EDGE_TTS_VOICES[0]
@@ -102,44 +112,7 @@ FILE_EMOJIS = {
102
  "wav": "๐Ÿ”Š"
103
  }
104
 
105
- # Marquee Functions
106
- def get_marquee_settings():
107
- """Get global marquee settings from sidebar controls"""
108
- st.sidebar.markdown("### ๐ŸŽฏ Marquee Settings")
109
- cols = st.sidebar.columns(2)
110
- with cols[0]:
111
- bg_color = st.color_picker("๐ŸŽจ Background", "#1E1E1E", key="bg_color_picker")
112
- text_color = st.color_picker("โœ๏ธ Text", "#FFFFFF", key="text_color_picker")
113
- with cols[1]:
114
- font_size = st.slider("๐Ÿ“ Size", 10, 24, 14, key="font_size_slider")
115
- duration = st.slider("โฑ๏ธ Speed", 1, 20, 10, key="duration_slider")
116
-
117
- return {
118
- "background": bg_color,
119
- "color": text_color,
120
- "font-size": f"{font_size}px",
121
- "animationDuration": f"{duration}s",
122
- "width": "100%",
123
- "lineHeight": "35px"
124
- }
125
-
126
- def display_marquee(text, settings, key_suffix=""):
127
- """Display marquee with given text and settings"""
128
- truncated_text = text[:280] + "..." if len(text) > 280 else text
129
- streamlit_marquee(
130
- content=truncated_text,
131
- **settings,
132
- key=f"marquee_{key_suffix}"
133
- )
134
- st.write("")
135
-
136
- def process_paper_content(paper):
137
- """Process paper content for marquee and audio"""
138
- marquee_text = f"๐Ÿ“„ {paper['title']} | ๐Ÿ‘ค {paper['authors'][:100]} | ๐Ÿ“ {paper['summary'][:100]}"
139
- audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
140
- return marquee_text, audio_text
141
-
142
- # Text Processing Functions
143
  def get_high_info_terms(text: str, top_n=10) -> list:
144
  stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
145
  words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
@@ -157,17 +130,8 @@ def clean_text_for_filename(text: str) -> str:
157
  filtered = [w for w in words if len(w) > 3 and w not in stop_short]
158
  return '_'.join(filtered)[:200]
159
 
160
- def clean_for_speech(text: str) -> str:
161
- text = text.replace("\n", " ")
162
- text = text.replace("</s>", " ")
163
- text = text.replace("#", "")
164
- text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
165
- text = re.sub(r"\s+", " ", text).strip()
166
- return text
167
-
168
- # File Operations
169
  def generate_filename(prompt, response, file_type="md"):
170
- prefix = datetime.now().strftime("%y%m_%H%M") + "_"
171
  combined = (prompt + " " + response).strip()
172
  info_terms = get_high_info_terms(combined, top_n=10)
173
  snippet = (prompt[:100] + " " + response[:100]).strip()
@@ -198,7 +162,14 @@ def get_download_link(file, file_type="zip"):
198
  else:
199
  return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
200
 
201
- # Audio Processing
 
 
 
 
 
 
 
202
  async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
203
  text = clean_for_speech(text)
204
  if not text.strip():
@@ -219,108 +190,27 @@ def play_and_download_audio(file_path, file_type="mp3"):
219
  dl_link = get_download_link(file_path, file_type=file_type)
220
  st.markdown(dl_link, unsafe_allow_html=True)
221
 
222
- # Paper Processing Functions
223
- def parse_arxiv_refs(ref_text: str):
224
- if not ref_text:
225
- return []
226
-
227
- results = []
228
- current_paper = {}
229
- lines = ref_text.split('\n')
230
-
231
- for i, line in enumerate(lines):
232
- if line.count('|') == 2:
233
- if current_paper:
234
- results.append(current_paper)
235
- if len(results) >= 20:
236
- break
237
-
238
- try:
239
- header_parts = line.strip('* ').split('|')
240
- date = header_parts[0].strip()
241
- title = header_parts[1].strip()
242
- url_match = re.search(r'(https://arxiv.org/\S+)', line)
243
- url = url_match.group(1) if url_match else f"paper_{len(results)}"
244
-
245
- current_paper = {
246
- 'date': date,
247
- 'title': title,
248
- 'url': url,
249
- 'authors': '',
250
- 'summary': '',
251
- 'content_start': i + 1
252
- }
253
- except Exception as e:
254
- st.warning(f"Error parsing paper header: {str(e)}")
255
- current_paper = {}
256
- continue
257
-
258
- elif current_paper:
259
- if not current_paper['authors']:
260
- current_paper['authors'] = line.strip('* ')
261
- else:
262
- if current_paper['summary']:
263
- current_paper['summary'] += ' ' + line.strip()
264
- else:
265
- current_paper['summary'] = line.strip()
266
 
267
- if current_paper:
268
- results.append(current_paper)
269
 
270
- return results[:20]
271
-
272
- def create_paper_audio_files(papers, input_question):
273
- for paper in papers:
274
- try:
275
- marquee_text, audio_text = process_paper_content(paper)
276
-
277
- audio_text = clean_for_speech(audio_text)
278
- file_format = st.session_state['audio_format']
279
- audio_file = speak_with_edge_tts(audio_text,
280
- voice=st.session_state['tts_voice'],
281
- file_format=file_format)
282
- paper['full_audio'] = audio_file
283
-
284
- st.write(f"### {FILE_EMOJIS.get(file_format, '')} {os.path.basename(audio_file)}")
285
- play_and_download_audio(audio_file, file_type=file_format)
286
- paper['marquee_text'] = marquee_text
287
-
288
- except Exception as e:
289
- st.warning(f"Error processing paper {paper['title']}: {str(e)}")
290
- paper['full_audio'] = None
291
- paper['marquee_text'] = None
292
-
293
- def display_papers(papers, marquee_settings):
294
- """Display papers with their audio controls and marquee summaries"""
295
- st.write("## Research Papers")
296
 
297
- papercount = 0
298
- for paper in papers:
299
- papercount += 1
300
- if papercount <= 20:
301
- # Display marquee if text exists
302
- if paper.get('marquee_text'):
303
- display_marquee(paper['marquee_text'],
304
- marquee_settings,
305
- key_suffix=f"paper_{papercount}")
306
-
307
- with st.expander(f"{papercount}. ๐Ÿ“„ {paper['title']}", expanded=True):
308
- st.markdown(f"**{paper['date']} | {paper['title']} | โฌ‡๏ธ**")
309
- st.markdown(f"*{paper['authors']}*")
310
- st.markdown(paper['summary'])
311
-
312
- if paper.get('full_audio'):
313
- st.write("๐Ÿ“š Paper Audio")
314
- file_ext = os.path.splitext(paper['full_audio'])[1].lower().strip('.')
315
- if file_ext in ['mp3', 'wav']:
316
- st.audio(paper['full_audio'])
317
 
318
  def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
319
  titles_summary=True, full_audio=False, marquee_settings=None):
320
- """Perform Arxiv search with audio generation per paper."""
321
  start = time.time()
322
 
323
- # Query the HF RAG pipeline
324
  client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
325
  refs = client.predict(q, 20, "Semantic Search",
326
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
@@ -328,11 +218,14 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
328
  r2 = client.predict(q, "mistralai/Mixtral-8x7B-Instruct-v0.1",
329
  True, api_name="/ask_llm")
330
 
331
- # Combine for final text output
332
  result = f"### ๐Ÿ”Ž {q}\n\n{r2}\n\n{refs}"
333
  st.markdown(result)
334
 
335
- # Parse and process papers
 
 
 
 
336
  papers = parse_arxiv_refs(refs)
337
  if papers:
338
  create_paper_audio_files(papers, input_question=q)
@@ -345,145 +238,96 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
345
 
346
  elapsed = time.time()-start
347
  st.write(f"**Total Elapsed:** {elapsed:.2f} s")
348
-
349
- # Save full transcript
350
- create_file(q, result, "md")
351
  return result
352
 
353
- def process_with_gpt(text):
354
- """Process text with GPT-4"""
355
- if not text:
356
- return
357
- st.session_state.messages.append({"role":"user","content":text})
358
- with st.chat_message("user"):
359
- st.markdown(text)
360
- with st.chat_message("assistant"):
361
- c = openai_client.chat.completions.create(
362
- model=st.session_state["openai_model"],
363
- messages=st.session_state.messages,
364
- stream=False
365
- )
366
- ans = c.choices[0].message.content
367
- st.write("GPT-4o: " + ans)
368
- create_file(text, ans, "md")
369
- st.session_state.messages.append({"role":"assistant","content":ans})
370
- return ans
371
-
372
- def process_with_claude(text):
373
- """Process text with Claude"""
374
- if not text:
375
  return
376
- with st.chat_message("user"):
377
- st.markdown(text)
378
- with st.chat_message("assistant"):
379
- r = claude_client.messages.create(
380
- model="claude-3-sonnet-20240229",
381
- max_tokens=1000,
382
- messages=[{"role":"user","content":text}]
383
- )
384
- ans = r.content[0].text
385
- st.write("Claude-3.5: " + ans)
386
- create_file(text, ans, "md")
387
- st.session_state.chat_history.append({"user":text,"claude":ans})
388
- return ans
389
-
390
-
391
-
392
- def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
393
- """Create zip with intelligent naming based on high-info terms"""
394
- md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
395
- all_files = md_files + mp3_files + wav_files
396
- if not all_files:
397
- return None
398
-
399
- all_content = []
400
- for f in all_files:
401
- if f.endswith('.md'):
402
- with open(f, 'r', encoding='utf-8') as file:
403
- all_content.append(file.read())
404
- elif f.endswith('.mp3') or f.endswith('.wav'):
405
- basename = os.path.splitext(os.path.basename(f))[0]
406
- words = basename.replace('_', ' ')
407
- all_content.append(words)
408
-
409
- all_content.append(input_question)
410
- combined_content = " ".join(all_content)
411
- info_terms = get_high_info_terms(combined_content, top_n=10)
412
-
413
- timestamp = datetime.now().strftime("%y%m_%H%M")
414
- name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:10])
415
- zip_name = f"{timestamp}_{name_text}.zip"
416
 
417
- with zipfile.ZipFile(zip_name, 'w') as z:
418
- for f in all_files:
419
- z.write(f)
420
 
421
- return zip_name
422
-
423
-
424
-
425
- def get_central_time():
426
- """Get current time in US Central timezone"""
427
- central = pytz.timezone('US/Central')
428
- return datetime.now(central)
429
-
430
- def format_timestamp_prefix():
431
- """Generate timestamp prefix in format MM_dd_yy_hh_mm_AM/PM"""
432
- ct = get_central_time()
433
- return ct.strftime("%m_%d_%y_%I_%M_%p")
434
-
435
- def generate_filename(prompt, response, file_type="md"):
436
- """Generate filename with central time prefix"""
437
- prefix = format_timestamp_prefix() + "_"
438
- combined = (prompt + " " + response).strip()
439
- info_terms = get_high_info_terms(combined, top_n=10)
440
- snippet = (prompt[:100] + " " + response[:100]).strip()
441
- snippet_cleaned = clean_text_for_filename(snippet)
442
- name_parts = info_terms + [snippet_cleaned]
443
- full_name = '_'.join(name_parts)
444
- if len(full_name) > 150:
445
- full_name = full_name[:150]
446
- return f"{prefix}{full_name}.{file_type}"
447
 
448
  def load_files_for_sidebar():
449
- """Load and group files based on central time prefix"""
450
  md_files = glob.glob("*.md")
451
- mp3_files = glob.glob("*.mp3")
452
  wav_files = glob.glob("*.wav")
453
 
454
  md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
455
  all_files = md_files + mp3_files + wav_files
456
 
457
  groups = defaultdict(list)
458
- prefix_length = len("MM_dd_yy_hh_mm_AP") # 17 characters
459
 
460
  for f in all_files:
461
  basename = os.path.basename(f)
462
- # Group by the timestamp prefix if it matches the pattern
463
  if len(basename) >= prefix_length and '_' in basename:
464
  group_name = basename[:prefix_length]
465
  groups[group_name].append(f)
466
  else:
467
  groups['Other'].append(f)
468
 
469
- # Sort groups by timestamp, most recent first
470
  sorted_groups = sorted(groups.items(),
471
  key=lambda x: x[0] if x[0] != 'Other' else '',
472
  reverse=True)
473
  return sorted_groups
474
 
475
  def display_file_manager_sidebar(groups_sorted):
476
- """Display file manager with central time formatted groups"""
477
  st.sidebar.title("๐ŸŽต Audio & Docs Manager")
478
-
479
- # ... [previous delete/zip buttons code remains same]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
  for group_name, files in groups_sorted:
482
  if group_name == 'Other':
483
  group_label = 'Other Files'
484
  else:
485
  try:
486
- # Parse the timestamp format MM_dd_yy_hh_mm_AM/PM
487
  timestamp_dt = datetime.strptime(group_name, "%m_%d_%y_%I_%M_%p")
488
  group_label = timestamp_dt.strftime("%b %d, %Y %I:%M %p")
489
  except ValueError:
@@ -509,21 +353,171 @@ def display_file_manager_sidebar(groups_sorted):
509
  ctime = datetime.fromtimestamp(mtime).strftime("%I:%M:%S %p")
510
  st.write(f"{emoji} **{fname}** - {ctime}")
511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
 
514
  def main():
515
- # Get marquee settings first
516
  marquee_settings = get_marquee_settings()
517
 
518
- # Initial welcome marquee
519
  display_marquee(st.session_state['marquee_content'],
520
  {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
521
  key_suffix="welcome")
522
 
523
- # Load files for sidebar
524
  groups_sorted = load_files_for_sidebar()
525
 
526
- # Update marquee content when viewing files
527
  if st.session_state.viewing_prefix:
528
  for group_name, files in groups_sorted:
529
  if group_name == st.session_state.viewing_prefix:
@@ -532,7 +526,6 @@ def main():
532
  with open(f, 'r', encoding='utf-8') as file:
533
  st.session_state['marquee_content'] = file.read()[:280]
534
 
535
- # Voice Settings
536
  st.sidebar.markdown("### ๐ŸŽค Voice Settings")
537
  selected_voice = st.sidebar.selectbox(
538
  "Select TTS Voice:",
@@ -540,7 +533,6 @@ def main():
540
  index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
541
  )
542
 
543
- # Audio Format Settings
544
  st.sidebar.markdown("### ๐Ÿ”Š Audio Format")
545
  selected_format = st.sidebar.radio(
546
  "Choose Audio Format:",
@@ -555,7 +547,6 @@ def main():
555
  st.session_state['audio_format'] = selected_format.lower()
556
  st.rerun()
557
 
558
- # Main Interface
559
  tab_main = st.radio("Action:", ["๐ŸŽค Voice", "๐Ÿ“ธ Media", "๐Ÿ” ArXiv", "๐Ÿ“ Editor"],
560
  horizontal=True)
561
 
@@ -578,30 +569,17 @@ def main():
578
  if autorun and input_changed:
579
  st.session_state.old_val = val
580
  st.session_state.last_query = edited_input
581
- if run_option == "Arxiv":
582
- perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
583
- titles_summary=True, full_audio=full_audio,
584
- marquee_settings=marquee_settings)
585
- else:
586
- if run_option == "GPT-4o":
587
- process_with_gpt(edited_input)
588
- elif run_option == "Claude-3.5":
589
- process_with_claude(edited_input)
590
  else:
591
  if st.button("โ–ถ Run"):
592
  st.session_state.old_val = val
593
  st.session_state.last_query = edited_input
594
- if run_option == "Arxiv":
595
- perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
596
- titles_summary=True, full_audio=full_audio,
597
- marquee_settings=marquee_settings)
598
- else:
599
- if run_option == "GPT-4o":
600
- process_with_gpt(edited_input)
601
- elif run_option == "Claude-3.5":
602
- process_with_claude(edited_input)
603
 
604
- # ArXiv Tab
605
  if tab_main == "๐Ÿ” ArXiv":
606
  st.subheader("๐Ÿ” Query ArXiv")
607
  q = st.text_input("๐Ÿ” Query:")
@@ -618,30 +596,20 @@ def main():
618
  result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
619
  titles_summary=titles_summary, full_audio=full_audio,
620
  marquee_settings=marquee_settings)
621
- if full_transcript:
622
- create_file(q, result, "md")
623
 
624
- # Voice Tab
625
  elif tab_main == "๐ŸŽค Voice":
626
  st.subheader("๐ŸŽค Voice Input")
627
  user_text = st.text_area("๐Ÿ’ฌ Message:", height=100)
628
  user_text = user_text.strip().replace('\n', ' ')
629
 
630
  if st.button("๐Ÿ“จ Send"):
631
- process_with_gpt(user_text)
632
 
633
  st.subheader("๐Ÿ“œ Chat History")
634
- t1, t2 = st.tabs(["Claude History", "GPT-4o History"])
635
- with t1:
636
- for c in st.session_state.chat_history:
637
- st.write("**You:**", c["user"])
638
- st.write("**Claude:**", c["claude"])
639
- with t2:
640
- for m in st.session_state.messages:
641
- with st.chat_message(m["role"]):
642
- st.markdown(m["content"])
643
-
644
- # Media Tab
645
  elif tab_main == "๐Ÿ“ธ Media":
646
  st.header("๐Ÿ“ธ Images & ๐ŸŽฅ Videos")
647
  tabs = st.tabs(["๐Ÿ–ผ Images", "๐ŸŽฅ Video"])
@@ -691,7 +659,6 @@ def main():
691
  else:
692
  st.write("No videos found.")
693
 
694
- # Editor Tab
695
  elif tab_main == "๐Ÿ“ Editor":
696
  if st.session_state.editing_file:
697
  st.subheader(f"Editing: {st.session_state.editing_file}")
@@ -705,10 +672,8 @@ def main():
705
  else:
706
  st.write("Select a file from the sidebar to edit.")
707
 
708
- # Display file manager in sidebar
709
  display_file_manager_sidebar(groups_sorted)
710
 
711
- # Display viewed group content
712
  if st.session_state.viewing_prefix and any(st.session_state.viewing_prefix == group for group, _ in groups_sorted):
713
  st.write("---")
714
  st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
@@ -730,7 +695,6 @@ def main():
730
  st.session_state.viewing_prefix = None
731
  st.session_state['marquee_content'] = "๐Ÿš€ Welcome to TalkingAIResearcher | ๐Ÿค– Your Research Assistant"
732
 
733
- # Add custom CSS
734
  st.markdown("""
735
  <style>
736
  .main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
@@ -739,10 +703,9 @@ def main():
739
  </style>
740
  """, unsafe_allow_html=True)
741
 
742
- # Handle rerun if needed
743
  if st.session_state.should_rerun:
744
  st.session_state.should_rerun = False
745
  st.rerun()
746
 
747
  if __name__ == "__main__":
748
- main()
 
37
 
38
  # Add available English voices for Edge TTS
39
  EDGE_TTS_VOICES = [
40
+ "en-US-AriaNeural",
41
+ "en-US-GuyNeural",
42
  "en-US-JennyNeural",
43
  "en-GB-SoniaNeural",
44
  "en-GB-RyanNeural",
 
48
  "en-CA-LiamNeural"
49
  ]
50
 
51
+ def get_central_time():
52
+ """Get current time in US Central timezone"""
53
+ central = pytz.timezone('US/Central')
54
+ return datetime.now(central)
55
+
56
+ def format_timestamp_prefix():
57
+ """Generate timestamp prefix in format MM_dd_yy_hh_mm_AM/PM"""
58
+ ct = get_central_time()
59
+ return ct.strftime("%m_%d_%y_%I_%M_%p")
60
+
61
  # Initialize session state variables
62
  if 'tts_voice' not in st.session_state:
63
  st.session_state['tts_voice'] = EDGE_TTS_VOICES[0]
 
112
  "wav": "๐Ÿ”Š"
113
  }
114
 
115
+ # Functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def get_high_info_terms(text: str, top_n=10) -> list:
117
  stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
118
  words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
 
130
  filtered = [w for w in words if len(w) > 3 and w not in stop_short]
131
  return '_'.join(filtered)[:200]
132
 
 
 
 
 
 
 
 
 
 
133
  def generate_filename(prompt, response, file_type="md"):
134
+ prefix = format_timestamp_prefix() + "_"
135
  combined = (prompt + " " + response).strip()
136
  info_terms = get_high_info_terms(combined, top_n=10)
137
  snippet = (prompt[:100] + " " + response[:100]).strip()
 
162
  else:
163
  return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
164
 
165
+ def clean_for_speech(text: str) -> str:
166
+ text = text.replace("\n", " ")
167
+ text = text.replace("</s>", " ")
168
+ text = text.replace("#", "")
169
+ text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
170
+ text = re.sub(r"\s+", " ", text).strip()
171
+ return text
172
+
173
  async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
174
  text = clean_for_speech(text)
175
  if not text.strip():
 
190
  dl_link = get_download_link(file_path, file_type=file_type)
191
  st.markdown(dl_link, unsafe_allow_html=True)
192
 
193
+ def save_qa_with_audio(question, answer, voice=None):
194
+ """Save Q&A to markdown and generate audio"""
195
+ if not voice:
196
+ voice = st.session_state['tts_voice']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ combined_text = f"# Question\n{question}\n\n# Answer\n{answer}"
199
+ md_file = create_file(question, answer, "md")
200
 
201
+ audio_text = f"Question: {question}\n\nAnswer: {answer}"
202
+ audio_file = speak_with_edge_tts(
203
+ audio_text,
204
+ voice=voice,
205
+ file_format=st.session_state['audio_format']
206
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
+ return md_file, audio_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
  def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
211
  titles_summary=True, full_audio=False, marquee_settings=None):
 
212
  start = time.time()
213
 
 
214
  client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
215
  refs = client.predict(q, 20, "Semantic Search",
216
  "mistralai/Mixtral-8x7B-Instruct-v0.1",
 
218
  r2 = client.predict(q, "mistralai/Mixtral-8x7B-Instruct-v0.1",
219
  True, api_name="/ask_llm")
220
 
 
221
  result = f"### ๐Ÿ”Ž {q}\n\n{r2}\n\n{refs}"
222
  st.markdown(result)
223
 
224
+ md_file, audio_file = save_qa_with_audio(q, result)
225
+
226
+ st.subheader("๐Ÿ“ Main Response Audio")
227
+ play_and_download_audio(audio_file, st.session_state['audio_format'])
228
+
229
  papers = parse_arxiv_refs(refs)
230
  if papers:
231
  create_paper_audio_files(papers, input_question=q)
 
238
 
239
  elapsed = time.time()-start
240
  st.write(f"**Total Elapsed:** {elapsed:.2f} s")
241
+
 
 
242
  return result
243
 
244
+ def process_voice_input(text):
245
+ if not text:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  return
247
+
248
+ st.subheader("๐Ÿ” Search Results")
249
+ result = perform_ai_lookup(
250
+ text,
251
+ vocal_summary=True,
252
+ extended_refs=False,
253
+ titles_summary=True,
254
+ full_audio=True,
255
+ marquee_settings=get_marquee_settings()
256
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
+ md_file, audio_file = save_qa_with_audio(text, result)
 
 
259
 
260
+ st.subheader("๐Ÿ“ Generated Files")
261
+ st.write(f"Markdown: {md_file}")
262
+ st.write(f"Audio: {audio_file}")
263
+ play_and_download_audio(audio_file, st.session_state['audio_format'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  def load_files_for_sidebar():
 
266
  md_files = glob.glob("*.md")
267
+ mp3_files = glob.glob("*.mp3")
268
  wav_files = glob.glob("*.wav")
269
 
270
  md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
271
  all_files = md_files + mp3_files + wav_files
272
 
273
  groups = defaultdict(list)
274
+ prefix_length = len("MM_dd_yy_hh_mm_AP")
275
 
276
  for f in all_files:
277
  basename = os.path.basename(f)
 
278
  if len(basename) >= prefix_length and '_' in basename:
279
  group_name = basename[:prefix_length]
280
  groups[group_name].append(f)
281
  else:
282
  groups['Other'].append(f)
283
 
 
284
  sorted_groups = sorted(groups.items(),
285
  key=lambda x: x[0] if x[0] != 'Other' else '',
286
  reverse=True)
287
  return sorted_groups
288
 
289
  def display_file_manager_sidebar(groups_sorted):
 
290
  st.sidebar.title("๐ŸŽต Audio & Docs Manager")
291
+
292
+ all_md = []
293
+ all_mp3 = []
294
+ all_wav = []
295
+ for _, files in groups_sorted:
296
+ for f in files:
297
+ if f.endswith(".md"):
298
+ all_md.append(f)
299
+ elif f.endswith(".mp3"):
300
+ all_mp3.append(f)
301
+ elif f.endswith(".wav"):
302
+ all_wav.append(f)
303
+
304
+ col1, col2, col3, col4 = st.sidebar.columns(4)
305
+ with col1:
306
+ if st.button("๐Ÿ—‘ DelMD"):
307
+ for f in all_md:
308
+ os.remove(f)
309
+ st.session_state.should_rerun = True
310
+ with col2:
311
+ if st.button("๐Ÿ—‘ DelMP3"):
312
+ for f in all_mp3:
313
+ os.remove(f)
314
+ st.session_state.should_rerun = True
315
+ with col3:
316
+ if st.button("๐Ÿ—‘ DelWAV"):
317
+ for f in all_wav:
318
+ os.remove(f)
319
+ st.session_state.should_rerun = True
320
+ with col4:
321
+ if st.button("โฌ‡๏ธ ZipAll"):
322
+ zip_name = create_zip_of_files(all_md, all_mp3, all_wav, st.session_state.get('last_query', ''))
323
+ if zip_name:
324
+ st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
325
 
326
  for group_name, files in groups_sorted:
327
  if group_name == 'Other':
328
  group_label = 'Other Files'
329
  else:
330
  try:
 
331
  timestamp_dt = datetime.strptime(group_name, "%m_%d_%y_%I_%M_%p")
332
  group_label = timestamp_dt.strftime("%b %d, %Y %I:%M %p")
333
  except ValueError:
 
353
  ctime = datetime.fromtimestamp(mtime).strftime("%I:%M:%S %p")
354
  st.write(f"{emoji} **{fname}** - {ctime}")
355
 
356
+ def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
357
+ md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
358
+ all_files = md_files + mp3_files + wav_files
359
+ if not all_files:
360
+ return None
361
+
362
+ all_content = []
363
+ for f in all_files:
364
+ if f.endswith('.md'):
365
+ with open(f, 'r', encoding='utf-8') as file:
366
+ all_content.append(file.read())
367
+ elif f.endswith('.mp3') or f.endswith('.wav'):
368
+ basename = os.path.splitext(os.path.basename(f))[0]
369
+ words = basename.replace('_', ' ')
370
+ all_content.append(words)
371
+
372
+ all_content.append(input_question)
373
+ combined_content = " ".join(all_content)
374
+ info_terms = get_high_info_terms(combined_content, top_n=10)
375
+
376
+ timestamp = format_timestamp_prefix()
377
+ name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:10])
378
+ zip_name = f"{timestamp}_{name_text}.zip"
379
+
380
+ with zipfile.ZipFile(zip_name, 'w') as z:
381
+ for f in all_files:
382
+ z.write(f)
383
+
384
+ return zip_name
385
+
386
+ def get_marquee_settings():
387
+ st.sidebar.markdown("### ๐ŸŽฏ Marquee Settings")
388
+ cols = st.sidebar.columns(2)
389
+ with cols[0]:
390
+ bg_color = st.color_picker("๐ŸŽจ Background", "#1E1E1E", key="bg_color_picker")
391
+ text_color = st.color_picker("โœ๏ธ Text", "#FFFFFF", key="text_color_picker")
392
+ with cols[1]:
393
+ font_size = st.slider("๐Ÿ“ Size", 10, 24, 14, key="font_size_slider")
394
+ duration = st.slider("โฑ๏ธ Speed", 1, 20, 10, key="duration_slider")
395
+
396
+ return {
397
+ "background": bg_color,
398
+ "color": text_color,
399
+ "font-size": f"{font_size}px",
400
+ "animationDuration": f"{duration}s",
401
+ "width": "100%",
402
+ "lineHeight": "35px"
403
+ }
404
+
405
+ def display_marquee(text, settings, key_suffix=""):
406
+ truncated_text = text[:280] + "..." if len(text) > 280 else text
407
+ streamlit_marquee(
408
+ content=truncated_text,
409
+ **settings,
410
+ key=f"marquee_{key_suffix}"
411
+ )
412
+ st.write("")
413
+
414
+ def parse_arxiv_refs(ref_text: str):
415
+ if not ref_text:
416
+ return []
417
+
418
+ results = []
419
+ current_paper = {}
420
+ lines = ref_text.split('\n')
421
+
422
+ for i, line in enumerate(lines):
423
+ if line.count('|') == 2:
424
+ if current_paper:
425
+ results.append(current_paper)
426
+ if len(results) >= 20:
427
+ break
428
+
429
+ try:
430
+ header_parts = line.strip('* ').split('|')
431
+ date = header_parts[0].strip()
432
+ title = header_parts[1].strip()
433
+ url_match = re.search(r'(https://arxiv.org/\S+)', line)
434
+ url = url_match.group(1) if url_match else f"paper_{len(results)}"
435
+
436
+ current_paper = {
437
+ 'date': date,
438
+ 'title': title,
439
+ 'url': url,
440
+ 'authors': '',
441
+ 'summary': '',
442
+ 'content_start': i + 1
443
+ }
444
+ except Exception as e:
445
+ st.warning(f"Error parsing paper header: {str(e)}")
446
+ current_paper = {}
447
+ continue
448
+
449
+ elif current_paper:
450
+ if not current_paper['authors']:
451
+ current_paper['authors'] = line.strip('* ')
452
+ else:
453
+ if current_paper['summary']:
454
+ current_paper['summary'] += ' ' + line.strip()
455
+ else:
456
+ current_paper['summary'] = line.strip()
457
+
458
+ if current_paper:
459
+ results.append(current_paper)
460
+
461
+ return results[:20]
462
+
463
+ def process_paper_content(paper):
464
+ marquee_text = f"๐Ÿ“„ {paper['title']} | ๐Ÿ‘ค {paper['authors'][:100]} | ๐Ÿ“ {paper['summary'][:100]}"
465
+ audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
466
+ return marquee_text, audio_text
467
 
468
+ def create_paper_audio_files(papers, input_question):
469
+ for paper in papers:
470
+ try:
471
+ marquee_text, audio_text = process_paper_content(paper)
472
+
473
+ audio_text = clean_for_speech(audio_text)
474
+ file_format = st.session_state['audio_format']
475
+ audio_file = speak_with_edge_tts(audio_text,
476
+ voice=st.session_state['tts_voice'],
477
+ file_format=file_format)
478
+ paper['full_audio'] = audio_file
479
+
480
+ st.write(f"### {FILE_EMOJIS.get(file_format, '')} {os.path.basename(audio_file)}")
481
+ play_and_download_audio(audio_file, file_type=file_format)
482
+ paper['marquee_text'] = marquee_text
483
+
484
+ except Exception as e:
485
+ st.warning(f"Error processing paper {paper['title']}: {str(e)}")
486
+ paper['full_audio'] = None
487
+ paper['marquee_text'] = None
488
+
489
+ def display_papers(papers, marquee_settings):
490
+ st.write("## Research Papers")
491
+
492
+ papercount = 0
493
+ for paper in papers:
494
+ papercount += 1
495
+ if papercount <= 20:
496
+ if paper.get('marquee_text'):
497
+ display_marquee(paper['marquee_text'],
498
+ marquee_settings,
499
+ key_suffix=f"paper_{papercount}")
500
+
501
+ with st.expander(f"{papercount}. ๐Ÿ“„ {paper['title']}", expanded=True):
502
+ st.markdown(f"**{paper['date']} | {paper['title']} | โฌ‡๏ธ**")
503
+ st.markdown(f"*{paper['authors']}*")
504
+ st.markdown(paper['summary'])
505
+
506
+ if paper.get('full_audio'):
507
+ st.write("๐Ÿ“š Paper Audio")
508
+ file_ext = os.path.splitext(paper['full_audio'])[1].lower().strip('.')
509
+ if file_ext in ['mp3', 'wav']:
510
+ st.audio(paper['full_audio'])
511
 
512
  def main():
 
513
  marquee_settings = get_marquee_settings()
514
 
 
515
  display_marquee(st.session_state['marquee_content'],
516
  {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
517
  key_suffix="welcome")
518
 
 
519
  groups_sorted = load_files_for_sidebar()
520
 
 
521
  if st.session_state.viewing_prefix:
522
  for group_name, files in groups_sorted:
523
  if group_name == st.session_state.viewing_prefix:
 
526
  with open(f, 'r', encoding='utf-8') as file:
527
  st.session_state['marquee_content'] = file.read()[:280]
528
 
 
529
  st.sidebar.markdown("### ๐ŸŽค Voice Settings")
530
  selected_voice = st.sidebar.selectbox(
531
  "Select TTS Voice:",
 
533
  index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
534
  )
535
 
 
536
  st.sidebar.markdown("### ๐Ÿ”Š Audio Format")
537
  selected_format = st.sidebar.radio(
538
  "Choose Audio Format:",
 
547
  st.session_state['audio_format'] = selected_format.lower()
548
  st.rerun()
549
 
 
550
  tab_main = st.radio("Action:", ["๐ŸŽค Voice", "๐Ÿ“ธ Media", "๐Ÿ” ArXiv", "๐Ÿ“ Editor"],
551
  horizontal=True)
552
 
 
569
  if autorun and input_changed:
570
  st.session_state.old_val = val
571
  st.session_state.last_query = edited_input
572
+ result = perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
573
+ titles_summary=True, full_audio=full_audio,
574
+ marquee_settings=marquee_settings)
 
 
 
 
 
 
575
  else:
576
  if st.button("โ–ถ Run"):
577
  st.session_state.old_val = val
578
  st.session_state.last_query = edited_input
579
+ result = perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
580
+ titles_summary=True, full_audio=full_audio,
581
+ marquee_settings=marquee_settings)
 
 
 
 
 
 
582
 
 
583
  if tab_main == "๐Ÿ” ArXiv":
584
  st.subheader("๐Ÿ” Query ArXiv")
585
  q = st.text_input("๐Ÿ” Query:")
 
596
  result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
597
  titles_summary=titles_summary, full_audio=full_audio,
598
  marquee_settings=marquee_settings)
 
 
599
 
 
600
  elif tab_main == "๐ŸŽค Voice":
601
  st.subheader("๐ŸŽค Voice Input")
602
  user_text = st.text_area("๐Ÿ’ฌ Message:", height=100)
603
  user_text = user_text.strip().replace('\n', ' ')
604
 
605
  if st.button("๐Ÿ“จ Send"):
606
+ process_voice_input(user_text)
607
 
608
  st.subheader("๐Ÿ“œ Chat History")
609
+ for c in st.session_state.chat_history:
610
+ st.write("**You:**", c["user"])
611
+ st.write("**Response:**", c["claude"])
612
+
 
 
 
 
 
 
 
613
  elif tab_main == "๐Ÿ“ธ Media":
614
  st.header("๐Ÿ“ธ Images & ๐ŸŽฅ Videos")
615
  tabs = st.tabs(["๐Ÿ–ผ Images", "๐ŸŽฅ Video"])
 
659
  else:
660
  st.write("No videos found.")
661
 
 
662
  elif tab_main == "๐Ÿ“ Editor":
663
  if st.session_state.editing_file:
664
  st.subheader(f"Editing: {st.session_state.editing_file}")
 
672
  else:
673
  st.write("Select a file from the sidebar to edit.")
674
 
 
675
  display_file_manager_sidebar(groups_sorted)
676
 
 
677
  if st.session_state.viewing_prefix and any(st.session_state.viewing_prefix == group for group, _ in groups_sorted):
678
  st.write("---")
679
  st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
 
695
  st.session_state.viewing_prefix = None
696
  st.session_state['marquee_content'] = "๐Ÿš€ Welcome to TalkingAIResearcher | ๐Ÿค– Your Research Assistant"
697
 
 
698
  st.markdown("""
699
  <style>
700
  .main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
 
703
  </style>
704
  """, unsafe_allow_html=True)
705
 
 
706
  if st.session_state.should_rerun:
707
  st.session_state.should_rerun = False
708
  st.rerun()
709
 
710
  if __name__ == "__main__":
711
+ main()