awacke1 commited on
Commit
ee45388
·
verified ·
1 Parent(s): 1e4a8c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +748 -0
app.py ADDED
@@ -0,0 +1,748 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, textract, time, zipfile
3
+ import plotly.graph_objects as go
4
+ import streamlit.components.v1 as components
5
+ from datetime import datetime
6
+ from audio_recorder_streamlit import audio_recorder
7
+ from bs4 import BeautifulSoup
8
+ from collections import defaultdict, deque, Counter
9
+ from dotenv import load_dotenv
10
+ from gradio_client import Client
11
+ from huggingface_hub import InferenceClient
12
+ from io import BytesIO
13
+ from PIL import Image
14
+ from PyPDF2 import PdfReader
15
+ from urllib.parse import quote
16
+ from xml.etree import ElementTree as ET
17
+ from openai import OpenAI
18
+ import extra_streamlit_components as stx
19
+ from streamlit.runtime.scriptrunner import get_script_run_ctx
20
+ import asyncio
21
+ import edge_tts
22
+ from streamlit_marquee import streamlit_marquee
23
+
24
+ # 🎯 1. Core Configuration & Setup
25
+ st.set_page_config(
26
+ page_title="🚲TalkingAIResearcher🏆",
27
+ page_icon="🚲🏆",
28
+ layout="wide",
29
+ initial_sidebar_state="auto",
30
+ menu_items={
31
+ 'Get Help': 'https://huggingface.co/awacke1',
32
+ 'Report a bug': 'https://huggingface.co/spaces/awacke1',
33
+ 'About': "🚲TalkingAIResearcher🏆"
34
+ }
35
+ )
36
+ 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",
45
+ "en-AU-NatashaNeural",
46
+ "en-AU-WilliamNeural",
47
+ "en-CA-ClaraNeural",
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]
54
+ if 'audio_format' not in st.session_state:
55
+ st.session_state['audio_format'] = 'mp3'
56
+ if 'transcript_history' not in st.session_state:
57
+ st.session_state['transcript_history'] = []
58
+ if 'chat_history' not in st.session_state:
59
+ st.session_state['chat_history'] = []
60
+ if 'openai_model' not in st.session_state:
61
+ st.session_state['openai_model'] = "gpt-4o-2024-05-13"
62
+ if 'messages' not in st.session_state:
63
+ st.session_state['messages'] = []
64
+ if 'last_voice_input' not in st.session_state:
65
+ st.session_state['last_voice_input'] = ""
66
+ if 'editing_file' not in st.session_state:
67
+ st.session_state['editing_file'] = None
68
+ if 'edit_new_name' not in st.session_state:
69
+ st.session_state['edit_new_name'] = ""
70
+ if 'edit_new_content' not in st.session_state:
71
+ st.session_state['edit_new_content'] = ""
72
+ if 'viewing_prefix' not in st.session_state:
73
+ st.session_state['viewing_prefix'] = None
74
+ if 'should_rerun' not in st.session_state:
75
+ st.session_state['should_rerun'] = False
76
+ if 'old_val' not in st.session_state:
77
+ st.session_state['old_val'] = None
78
+ if 'last_query' not in st.session_state:
79
+ st.session_state['last_query'] = ""
80
+ if 'marquee_content' not in st.session_state:
81
+ st.session_state['marquee_content'] = "🚀 Welcome to TalkingAIResearcher | 🤖 Your Research Assistant"
82
+
83
+ # 🔑 2. API Setup & Clients
84
+ openai_api_key = os.getenv('OPENAI_API_KEY', "")
85
+ anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
86
+ xai_key = os.getenv('xai',"")
87
+ if 'OPENAI_API_KEY' in st.secrets:
88
+ openai_api_key = st.secrets['OPENAI_API_KEY']
89
+ if 'ANTHROPIC_API_KEY' in st.secrets:
90
+ anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
91
+
92
+ openai.api_key = openai_api_key
93
+ claude_client = anthropic.Anthropic(api_key=anthropic_key)
94
+ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID'))
95
+ HF_KEY = os.getenv('HF_KEY')
96
+ API_URL = os.getenv('API_URL')
97
+
98
+ # Constants
99
+ FILE_EMOJIS = {
100
+ "md": "📝",
101
+ "mp3": "🎵",
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())
146
+ bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
147
+ combined = words + bi_grams
148
+ filtered = [term for term in combined if term not in stop_words and len(term.split()) <= 2]
149
+ counter = Counter(filtered)
150
+ return [term for term, freq in counter.most_common(top_n)]
151
+
152
+ def clean_text_for_filename(text: str) -> str:
153
+ text = text.lower()
154
+ text = re.sub(r'[^\w\s-]', '', text)
155
+ words = text.split()
156
+ stop_short = set(['the', 'and', 'for', 'with', 'this', 'that'])
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()
174
+ snippet_cleaned = clean_text_for_filename(snippet)
175
+ name_parts = info_terms + [snippet_cleaned]
176
+ full_name = '_'.join(name_parts)
177
+ if len(full_name) > 150:
178
+ full_name = full_name[:150]
179
+ return f"{prefix}{full_name}.{file_type}"
180
+
181
+ def create_file(prompt, response, file_type="md"):
182
+ filename = generate_filename(prompt.strip(), response.strip(), file_type)
183
+ with open(filename, 'w', encoding='utf-8') as f:
184
+ f.write(prompt + "\n\n" + response)
185
+ return filename
186
+
187
+ def get_download_link(file, file_type="zip"):
188
+ with open(file, "rb") as f:
189
+ b64 = base64.b64encode(f.read()).decode()
190
+ if file_type == "zip":
191
+ return f'<a href="data:application/zip;base64,{b64}" download="{os.path.basename(file)}">📂 Download {os.path.basename(file)}</a>'
192
+ elif file_type == "mp3":
193
+ return f'<a href="data:audio/mpeg;base64,{b64}" download="{os.path.basename(file)}">🎵 Download {os.path.basename(file)}</a>'
194
+ elif file_type == "wav":
195
+ return f'<a href="data:audio/wav;base64,{b64}" download="{os.path.basename(file)}">🔊 Download {os.path.basename(file)}</a>'
196
+ elif file_type == "md":
197
+ return f'<a href="data:text/markdown;base64,{b64}" download="{os.path.basename(file)}">📝 Download {os.path.basename(file)}</a>'
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():
205
+ return None
206
+ rate_str = f"{rate:+d}%"
207
+ pitch_str = f"{pitch:+d}Hz"
208
+ communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
209
+ out_fn = generate_filename(text, text, file_type=file_format)
210
+ await communicate.save(out_fn)
211
+ return out_fn
212
+
213
+ def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
214
+ return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
215
+
216
+ def play_and_download_audio(file_path, file_type="mp3"):
217
+ if file_path and os.path.exists(file_path):
218
+ st.audio(file_path)
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",
327
+ api_name="/update_with_rag_md")[0]
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)
339
+ if marquee_settings:
340
+ display_papers(papers, marquee_settings)
341
+ else:
342
+ display_papers(papers, get_marquee_settings())
343
+ else:
344
+ st.warning("No papers found in the response.")
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:
490
+ group_label = group_name
491
+
492
+ with st.sidebar.expander(f"📁 {group_label} ({len(files)})", expanded=True):
493
+ c1, c2 = st.columns(2)
494
+ with c1:
495
+ if st.button("👀 View", key=f"view_group_{group_name}"):
496
+ st.session_state.viewing_prefix = group_name
497
+ with c2:
498
+ if st.button("🗑 Del", key=f"del_group_{group_name}"):
499
+ for f in files:
500
+ os.remove(f)
501
+ st.success(f"Deleted group {group_label}!")
502
+ st.session_state.should_rerun = True
503
+
504
+ for f in files:
505
+ fname = os.path.basename(f)
506
+ ext = os.path.splitext(fname)[1].lower()
507
+ emoji = FILE_EMOJIS.get(ext.strip('.'), '')
508
+ mtime = os.path.getmtime(f)
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:
530
+ for f in files:
531
+ if f.endswith('.md'):
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:",
539
+ options=EDGE_TTS_VOICES,
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:",
547
+ options=["MP3", "WAV"],
548
+ index=0
549
+ )
550
+
551
+ if selected_voice != st.session_state['tts_voice']:
552
+ st.session_state['tts_voice'] = selected_voice
553
+ st.rerun()
554
+ if selected_format.lower() != st.session_state['audio_format']:
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
+
562
+ mycomponent = components.declare_component("mycomponent", path="mycomponent")
563
+ val = mycomponent(my_input_value="Hello")
564
+
565
+ if val:
566
+ val_stripped = val.replace('\\n', ' ')
567
+ edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
568
+
569
+ run_option = st.selectbox("Model:", ["Arxiv", "GPT-4o", "Claude-3.5"])
570
+ col1, col2 = st.columns(2)
571
+ with col1:
572
+ autorun = st.checkbox("⚙ AutoRun", value=True)
573
+ with col2:
574
+ full_audio = st.checkbox("📚FullAudio", value=False)
575
+
576
+ input_changed = (val != st.session_state.old_val)
577
+
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:")
608
+
609
+ st.markdown("### 🎛 Options")
610
+ vocal_summary = st.checkbox("🎙ShortAudio", value=True)
611
+ extended_refs = st.checkbox("📜LongRefs", value=False)
612
+ titles_summary = st.checkbox("🔖TitlesOnly", value=True)
613
+ full_audio = st.checkbox("📚FullAudio", value=False)
614
+ full_transcript = st.checkbox("🧾FullTranscript", value=False)
615
+
616
+ if q and st.button("🔍Run"):
617
+ st.session_state.last_query = q
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"])
648
+ with tabs[0]:
649
+ imgs = glob.glob("*.png") + glob.glob("*.jpg")
650
+ if imgs:
651
+ c = st.slider("Cols", 1, 5, 3)
652
+ cols = st.columns(c)
653
+ for i, f in enumerate(imgs):
654
+ with cols[i % c]:
655
+ st.image(Image.open(f), use_container_width=True)
656
+ if st.button(f"👀 Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
657
+ response = openai_client.chat.completions.create(
658
+ model=st.session_state["openai_model"],
659
+ messages=[
660
+ {"role": "system", "content": "Analyze the image content."},
661
+ {"role": "user", "content": [
662
+ {"type": "image_url",
663
+ "image_url": {"url": f"data:image/jpeg;base64,{base64.b64encode(open(f, 'rb').read()).decode()}"}}
664
+ ]}
665
+ ]
666
+ )
667
+ st.markdown(response.choices[0].message.content)
668
+ else:
669
+ st.write("No images found.")
670
+
671
+ with tabs[1]:
672
+ vids = glob.glob("*.mp4")
673
+ if vids:
674
+ for v in vids:
675
+ with st.expander(f"🎥 {os.path.basename(v)}"):
676
+ st.video(v)
677
+ if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
678
+ frames = process_video(v)
679
+ response = openai_client.chat.completions.create(
680
+ model=st.session_state["openai_model"],
681
+ messages=[
682
+ {"role": "system", "content": "Analyze video frames."},
683
+ {"role": "user", "content": [
684
+ {"type": "image_url",
685
+ "image_url": {"url": f"data:image/jpeg;base64,{frame}"}}
686
+ for frame in frames
687
+ ]}
688
+ ]
689
+ )
690
+ st.markdown(response.choices[0].message.content)
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}")
698
+ new_text = st.text_area("✏️ Content:", st.session_state.edit_new_content, height=300)
699
+ if st.button("💾 Save"):
700
+ with open(st.session_state.editing_file, 'w', encoding='utf-8') as f:
701
+ f.write(new_text)
702
+ st.success("File updated successfully!")
703
+ st.session_state.should_rerun = True
704
+ st.session_state.editing_file = None
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}")
715
+ for group_name, files in groups_sorted:
716
+ if group_name == st.session_state.viewing_prefix:
717
+ for f in files:
718
+ fname = os.path.basename(f)
719
+ ext = os.path.splitext(fname)[1].lower().strip('.')
720
+ st.write(f"### {fname}")
721
+ if ext == "md":
722
+ content = open(f, 'r', encoding='utf-8').read()
723
+ st.markdown(content)
724
+ elif ext in ["mp3", "wav"]:
725
+ st.audio(f)
726
+ else:
727
+ st.markdown(get_download_link(f), unsafe_allow_html=True)
728
+ break
729
+ if st.button("❌ Close"):
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; }
737
+ .stMarkdown { font-family: 'Helvetica Neue', sans-serif; }
738
+ .stButton>button { margin-right: 0.5rem; }
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()