awacke1 commited on
Commit
4ffd976
Β·
verified Β·
1 Parent(s): c565db3

Create backup18.app.py

Browse files
Files changed (1) hide show
  1. backup18.app.py +801 -0
backup18.app.py ADDED
@@ -0,0 +1,801 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # ─────────────────────────────────────────────────────────
25
+ # 1. CORE CONFIGURATION & SETUP
26
+ # ─────────────────────────────────────────────────────────
27
+ st.set_page_config(
28
+ page_title="🚲TalkingAIResearcherπŸ†",
29
+ page_icon="πŸš²πŸ†",
30
+ layout="wide",
31
+ initial_sidebar_state="auto",
32
+ menu_items={
33
+ 'Get Help': 'https://huggingface.co/awacke1',
34
+ 'Report a bug': 'https://huggingface.co/spaces/awacke1',
35
+ 'About': "🚲TalkingAIResearcherπŸ†"
36
+ }
37
+ )
38
+ load_dotenv()
39
+
40
+ # Available English voices for Edge TTS
41
+ EDGE_TTS_VOICES = [
42
+ "en-US-AriaNeural",
43
+ "en-US-GuyNeural",
44
+ "en-US-JennyNeural",
45
+ "en-GB-SoniaNeural",
46
+ "en-GB-RyanNeural",
47
+ "en-AU-NatashaNeural",
48
+ "en-AU-WilliamNeural",
49
+ "en-CA-ClaraNeural",
50
+ "en-CA-LiamNeural"
51
+ ]
52
+
53
+ # Session state variables
54
+ if 'marquee_settings' not in st.session_state:
55
+ st.session_state['marquee_settings'] = {
56
+ "background": "#1E1E1E",
57
+ "color": "#FFFFFF",
58
+ "font-size": "14px",
59
+ "animationDuration": "20s",
60
+ "width": "100%",
61
+ "lineHeight": "35px"
62
+ }
63
+
64
+ if 'tts_voice' not in st.session_state:
65
+ st.session_state['tts_voice'] = EDGE_TTS_VOICES[0]
66
+
67
+ if 'audio_format' not in st.session_state:
68
+ st.session_state['audio_format'] = 'mp3'
69
+
70
+ if 'transcript_history' not in st.session_state:
71
+ st.session_state['transcript_history'] = []
72
+
73
+ if 'chat_history' not in st.session_state:
74
+ st.session_state['chat_history'] = []
75
+
76
+ if 'openai_model' not in st.session_state:
77
+ st.session_state['openai_model'] = "gpt-4o-2024-05-13"
78
+
79
+ if 'messages' not in st.session_state:
80
+ st.session_state['messages'] = []
81
+
82
+ if 'last_voice_input' not in st.session_state:
83
+ st.session_state['last_voice_input'] = ""
84
+
85
+ if 'editing_file' not in st.session_state:
86
+ st.session_state['editing_file'] = None
87
+
88
+ if 'edit_new_name' not in st.session_state:
89
+ st.session_state['edit_new_name'] = ""
90
+
91
+ if 'edit_new_content' not in st.session_state:
92
+ st.session_state['edit_new_content'] = ""
93
+
94
+ if 'viewing_prefix' not in st.session_state:
95
+ st.session_state['viewing_prefix'] = None
96
+
97
+ if 'should_rerun' not in st.session_state:
98
+ st.session_state['should_rerun'] = False
99
+
100
+ if 'old_val' not in st.session_state:
101
+ st.session_state['old_val'] = None
102
+
103
+ if 'last_query' not in st.session_state:
104
+ st.session_state['last_query'] = ""
105
+
106
+ if 'marquee_content' not in st.session_state:
107
+ st.session_state['marquee_content'] = "πŸš€ Welcome to TalkingAIResearcher | πŸ€– Your Research Assistant"
108
+
109
+ # API Keys
110
+ openai_api_key = os.getenv('OPENAI_API_KEY', "")
111
+ anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
112
+ xai_key = os.getenv('xai',"")
113
+ if 'OPENAI_API_KEY' in st.secrets:
114
+ openai_api_key = st.secrets['OPENAI_API_KEY']
115
+ if 'ANTHROPIC_API_KEY' in st.secrets:
116
+ anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
117
+
118
+ openai.api_key = openai_api_key
119
+ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID'))
120
+ HF_KEY = os.getenv('HF_KEY')
121
+ API_URL = os.getenv('API_URL')
122
+
123
+ # Helper constants
124
+ FILE_EMOJIS = {
125
+ "md": "πŸ“",
126
+ "mp3": "🎡",
127
+ "wav": "πŸ”Š"
128
+ }
129
+
130
+ # ─────────────────────────────────────────────────────────
131
+ # 2. HELPER FUNCTIONS
132
+ # ─────────────────────────────────────────────────────────
133
+
134
+ def get_central_time():
135
+ """Get current time in US Central timezone."""
136
+ central = pytz.timezone('US/Central')
137
+ return datetime.now(central)
138
+
139
+ def format_timestamp_prefix():
140
+ """Generate timestamp prefix in format MM_dd_yy_hh_mm_AM/PM."""
141
+ ct = get_central_time()
142
+ return ct.strftime("%m_%d_%y_%I_%M_%p")
143
+
144
+ def initialize_marquee_settings():
145
+ if 'marquee_settings' not in st.session_state:
146
+ st.session_state['marquee_settings'] = {
147
+ "background": "#1E1E1E",
148
+ "color": "#FFFFFF",
149
+ "font-size": "14px",
150
+ "animationDuration": "20s",
151
+ "width": "100%",
152
+ "lineHeight": "35px"
153
+ }
154
+
155
+ def get_marquee_settings():
156
+ initialize_marquee_settings()
157
+ return st.session_state['marquee_settings']
158
+
159
+ def update_marquee_settings_ui():
160
+ """Add color pickers & sliders for marquee config in sidebar."""
161
+ st.sidebar.markdown("### 🎯 Marquee Settings")
162
+ cols = st.sidebar.columns(2)
163
+ with cols[0]:
164
+ bg_color = st.color_picker("🎨 Background",
165
+ st.session_state['marquee_settings']["background"],
166
+ key="bg_color_picker")
167
+ text_color = st.color_picker("✍️ Text",
168
+ st.session_state['marquee_settings']["color"],
169
+ key="text_color_picker")
170
+ with cols[1]:
171
+ font_size = st.slider("πŸ“ Size", 10, 24, 14, key="font_size_slider")
172
+ duration = st.slider("⏱️ Speed", 1, 20, 20, key="duration_slider")
173
+
174
+ st.session_state['marquee_settings'].update({
175
+ "background": bg_color,
176
+ "color": text_color,
177
+ "font-size": f"{font_size}px",
178
+ "animationDuration": f"{duration}s"
179
+ })
180
+
181
+ def display_marquee(text, settings, key_suffix=""):
182
+ """Show marquee text with style from settings."""
183
+ truncated_text = text[:280] + "..." if len(text) > 280 else text
184
+ streamlit_marquee(
185
+ content=truncated_text,
186
+ **settings,
187
+ key=f"marquee_{key_suffix}"
188
+ )
189
+ st.write("")
190
+
191
+ def get_high_info_terms(text: str, top_n=10) -> list:
192
+ """Extract top_n freq words or bigrams (excluding stopwords)."""
193
+ stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
194
+ words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
195
+ bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
196
+ combined = words + bi_grams
197
+ filtered = [term for term in combined if term not in stop_words and len(term.split()) <= 2]
198
+ counter = Counter(filtered)
199
+ return [term for term, freq in counter.most_common(top_n)]
200
+
201
+ def clean_text_for_filename(text: str) -> str:
202
+ """Remove special chars, short words, etc. for filenames."""
203
+ text = text.lower()
204
+ text = re.sub(r'[^\w\s-]', '', text)
205
+ words = text.split()
206
+ # remove short or unhelpful words
207
+ stop_short = set(['the', 'and', 'for', 'with', 'this', 'that', 'ai', 'library'])
208
+ filtered = [w for w in words if len(w) > 3 and w not in stop_short]
209
+ return '_'.join(filtered)[:200]
210
+
211
+ def generate_filename(prompt, response, file_type="md", max_length=200):
212
+ """
213
+ Generate a shortened filename by:
214
+ 1) extracting high-info terms,
215
+ 2) snippet from prompt+response,
216
+ 3) remove duplicates,
217
+ 4) truncate if needed.
218
+ """
219
+ prefix = format_timestamp_prefix() + "_"
220
+ combined_text = (prompt + " " + response)[:200]
221
+ info_terms = get_high_info_terms(combined_text, top_n=5)
222
+ snippet = (prompt[:40] + " " + response[:40]).strip()
223
+ snippet_cleaned = clean_text_for_filename(snippet)
224
+
225
+ # remove duplicates
226
+ name_parts = info_terms + [snippet_cleaned]
227
+ seen = set()
228
+ unique_parts = []
229
+ for part in name_parts:
230
+ if part not in seen:
231
+ seen.add(part)
232
+ unique_parts.append(part)
233
+
234
+ full_name = '_'.join(unique_parts).strip('_')
235
+ leftover_chars = max_length - len(prefix) - len(file_type) - 1
236
+ if len(full_name) > leftover_chars:
237
+ full_name = full_name[:leftover_chars]
238
+
239
+ return f"{prefix}{full_name}.{file_type}"
240
+
241
+ def create_file(prompt, response, file_type="md"):
242
+ """Create a text file from prompt + response with sanitized filename."""
243
+ filename = generate_filename(prompt.strip(), response.strip(), file_type)
244
+ with open(filename, 'w', encoding='utf-8') as f:
245
+ f.write(prompt + "\n\n" + response)
246
+ return filename
247
+
248
+ def get_download_link(file, file_type="zip"):
249
+ """
250
+ Convert a file to base64 and return an HTML link for download.
251
+ """
252
+ with open(file, "rb") as f:
253
+ b64 = base64.b64encode(f.read()).decode()
254
+ if file_type == "zip":
255
+ return f'<a href="data:application/zip;base64,{b64}" download="{os.path.basename(file)}">πŸ“‚ Download {os.path.basename(file)}</a>'
256
+ elif file_type == "mp3":
257
+ return f'<a href="data:audio/mpeg;base64,{b64}" download="{os.path.basename(file)}">🎡 Download {os.path.basename(file)}</a>'
258
+ elif file_type == "wav":
259
+ return f'<a href="data:audio/wav;base64,{b64}" download="{os.path.basename(file)}">πŸ”Š Download {os.path.basename(file)}</a>'
260
+ elif file_type == "md":
261
+ return f'<a href="data:text/markdown;base64,{b64}" download="{os.path.basename(file)}">πŸ“ Download {os.path.basename(file)}</a>'
262
+ else:
263
+ return f'<a href="data:application/octet-stream;base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
264
+
265
+ def clean_for_speech(text: str) -> str:
266
+ """Clean up text for TTS output."""
267
+ text = text.replace("\n", " ")
268
+ text = text.replace("</s>", " ")
269
+ text = text.replace("#", "")
270
+ text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
271
+ text = re.sub(r"\s+", " ", text).strip()
272
+ return text
273
+
274
+ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
275
+ """Async TTS generation with edge-tts library."""
276
+ text = clean_for_speech(text)
277
+ if not text.strip():
278
+ return None
279
+ rate_str = f"{rate:+d}%"
280
+ pitch_str = f"{pitch:+d}Hz"
281
+ communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
282
+ out_fn = generate_filename(text, text, file_type=file_format)
283
+ await communicate.save(out_fn)
284
+ return out_fn
285
+
286
+ def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0, file_format="mp3"):
287
+ """Wrapper for the async TTS generate call."""
288
+ return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch, file_format))
289
+
290
+ def play_and_download_audio(file_path, file_type="mp3"):
291
+ """Streamlit audio + a quick download link."""
292
+ if file_path and os.path.exists(file_path):
293
+ st.audio(file_path)
294
+ dl_link = get_download_link(file_path, file_type=file_type)
295
+ st.markdown(dl_link, unsafe_allow_html=True)
296
+
297
+ def save_qa_with_audio(question, answer, voice=None):
298
+ """Save Q&A to markdown and also generate audio."""
299
+ if not voice:
300
+ voice = st.session_state['tts_voice']
301
+
302
+ combined_text = f"# Question\n{question}\n\n# Answer\n{answer}"
303
+ md_file = create_file(question, answer, "md")
304
+ audio_text = f"{question}\n\nAnswer: {answer}"
305
+ audio_file = speak_with_edge_tts(
306
+ audio_text,
307
+ voice=voice,
308
+ file_format=st.session_state['audio_format']
309
+ )
310
+ return md_file, audio_file
311
+
312
+ # ─────────────────────────────────────────────────────────
313
+ # 3. PAPER PARSING & DISPLAY
314
+ # ─────────────────────────────────────────────────────────
315
+
316
+ def parse_arxiv_refs(ref_text: str):
317
+ """
318
+ Given a multi-line markdown with arxiv references, parse them into
319
+ a list of dicts: {date, title, url, authors, summary, ...}.
320
+ """
321
+ if not ref_text:
322
+ return []
323
+
324
+ results = []
325
+ current_paper = {}
326
+ lines = ref_text.split('\n')
327
+
328
+ for i, line in enumerate(lines):
329
+ if line.count('|') == 2:
330
+ # Found a new paper line
331
+ if current_paper:
332
+ results.append(current_paper)
333
+ if len(results) >= 20:
334
+ break
335
+ try:
336
+ header_parts = line.strip('* ').split('|')
337
+ date = header_parts[0].strip()
338
+ title = header_parts[1].strip()
339
+ url_match = re.search(r'(https://arxiv.org/\S+)', line)
340
+ url = url_match.group(1) if url_match else f"paper_{len(results)}"
341
+
342
+ current_paper = {
343
+ 'date': date,
344
+ 'title': title,
345
+ 'url': url,
346
+ 'authors': '',
347
+ 'summary': '',
348
+ 'full_audio': None,
349
+ 'download_base64': '',
350
+ }
351
+ except Exception as e:
352
+ st.warning(f"Error parsing paper header: {str(e)}")
353
+ current_paper = {}
354
+ continue
355
+
356
+ elif current_paper:
357
+ # If authors not set, fill it; otherwise, fill summary
358
+ if not current_paper['authors']:
359
+ current_paper['authors'] = line.strip('* ')
360
+ else:
361
+ if current_paper['summary']:
362
+ current_paper['summary'] += ' ' + line.strip()
363
+ else:
364
+ current_paper['summary'] = line.strip()
365
+
366
+ if current_paper:
367
+ results.append(current_paper)
368
+
369
+ return results[:20]
370
+
371
+ def create_paper_links_md(papers):
372
+ """Creates a minimal .md content linking to each paper's arxiv URL."""
373
+ lines = ["# Paper Links\n"]
374
+ for i, p in enumerate(papers, start=1):
375
+ lines.append(f"{i}. **{p['title']}** β€” [Arxiv]({p['url']})")
376
+ return "\n".join(lines)
377
+
378
+ def create_paper_audio_files(papers, input_question):
379
+ """
380
+ For each paper, generate TTS audio summary, store the path in `paper['full_audio']`,
381
+ and also store a base64 link for stable downloading.
382
+ """
383
+ for paper in papers:
384
+ try:
385
+ audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
386
+ audio_text = clean_for_speech(audio_text)
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
+ if audio_file:
396
+ with open(audio_file, "rb") as af:
397
+ b64_data = base64.b64encode(af.read()).decode()
398
+ download_filename = os.path.basename(audio_file)
399
+ mime_type = "mpeg" if file_format == "mp3" else "wav"
400
+ paper['download_base64'] = (
401
+ f'<a href="data:audio/{mime_type};base64,{b64_data}" '
402
+ f'download="{download_filename}">🎡 Download {download_filename}</a>'
403
+ )
404
+
405
+ except Exception as e:
406
+ st.warning(f"Error processing paper {paper['title']}: {str(e)}")
407
+ paper['full_audio'] = None
408
+ paper['download_base64'] = ''
409
+
410
+ def display_papers(papers, marquee_settings):
411
+ """Display paper info in the main area with marquee + expanders + audio."""
412
+ st.write("## Research Papers")
413
+ for i, paper in enumerate(papers, start=1):
414
+ marquee_text = f"πŸ“„ {paper['title']} | πŸ‘€ {paper['authors'][:120]} | πŸ“ {paper['summary'][:200]}"
415
+ display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
416
+
417
+ with st.expander(f"{i}. πŸ“„ {paper['title']}", expanded=True):
418
+ st.markdown(f"**{paper['date']} | {paper['title']}** β€” [Arxiv Link]({paper['url']})")
419
+ st.markdown(f"*Authors:* {paper['authors']}")
420
+ st.markdown(paper['summary'])
421
+ if paper.get('full_audio'):
422
+ st.write("πŸ“š Paper Audio")
423
+ st.audio(paper['full_audio'])
424
+ if paper['download_base64']:
425
+ st.markdown(paper['download_base64'], unsafe_allow_html=True)
426
+
427
+ def display_papers_in_sidebar(papers):
428
+ """Mirrors the paper listing in the sidebar with expanders, audio, etc."""
429
+ st.sidebar.title("🎢 Papers & Audio")
430
+ for i, paper in enumerate(papers, start=1):
431
+ with st.sidebar.expander(f"{i}. {paper['title']}"):
432
+ st.markdown(f"**Arxiv:** [Link]({paper['url']})")
433
+ if paper['full_audio']:
434
+ st.audio(paper['full_audio'])
435
+ if paper['download_base64']:
436
+ st.markdown(paper['download_base64'], unsafe_allow_html=True)
437
+ st.markdown(f"**Authors:** {paper['authors']}")
438
+ if paper['summary']:
439
+ st.markdown(f"**Summary:** {paper['summary'][:300]}...")
440
+
441
+ # ─────────────────────────────────────────────────────────
442
+ # 4. ZIP FUNCTION
443
+ # ─────────────────────────────────────────────────────────
444
+
445
+ def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
446
+ """
447
+ Zip up all relevant files, limiting the final zip name to ~20 chars
448
+ to avoid overly long base64 strings.
449
+ """
450
+ md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
451
+ all_files = md_files + mp3_files + wav_files
452
+ if not all_files:
453
+ return None
454
+
455
+ all_content = []
456
+ for f in all_files:
457
+ if f.endswith('.md'):
458
+ with open(f, 'r', encoding='utf-8') as file:
459
+ all_content.append(file.read())
460
+ elif f.endswith('.mp3') or f.endswith('.wav'):
461
+ basename = os.path.splitext(os.path.basename(f))[0]
462
+ words = basename.replace('_', ' ')
463
+ all_content.append(words)
464
+
465
+ all_content.append(input_question)
466
+ combined_content = " ".join(all_content)
467
+ info_terms = get_high_info_terms(combined_content, top_n=10)
468
+
469
+ timestamp = format_timestamp_prefix()
470
+ name_text = '-'.join(term for term in info_terms[:5])
471
+ short_zip_name = (timestamp + "_" + name_text)[:20] + ".zip"
472
+
473
+ with zipfile.ZipFile(short_zip_name, 'w') as z:
474
+ for f in all_files:
475
+ z.write(f)
476
+ return short_zip_name
477
+
478
+ # ─────────────────────────────────────────────────────────
479
+ # 5. MAIN LOGIC: AI LOOKUP & VOICE INPUT
480
+ # ─────────────────────────────────────────────────────────
481
+
482
+ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
483
+ titles_summary=True, full_audio=False):
484
+ """Main routine that uses Anthropic (Claude) + Gradio ArXiv RAG pipeline."""
485
+ start = time.time()
486
+ ai_constitution = """
487
+ You are a talented AI coder and songwriter...
488
+ """
489
+
490
+ # --- 1) Claude API
491
+ client = anthropic.Anthropic(api_key=anthropic_key)
492
+ user_input = q
493
+ response = client.messages.create(
494
+ model="claude-3-sonnet-20240229",
495
+ max_tokens=1000,
496
+ messages=[
497
+ {"role": "user", "content": user_input}
498
+ ])
499
+ st.write("Claude's reply 🧠:")
500
+ st.markdown(response.content[0].text)
501
+
502
+ # Save & produce audio
503
+ result = response.content[0].text
504
+ create_file(q, result)
505
+ md_file, audio_file = save_qa_with_audio(q, result)
506
+ st.subheader("πŸ“ Main Response Audio")
507
+ play_and_download_audio(audio_file, st.session_state['audio_format'])
508
+
509
+ # --- 2) Arxiv RAG
510
+ st.write("Arxiv's AI this Evening is Mixtral 8x7B...")
511
+ client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
512
+ refs = client.predict(
513
+ q,
514
+ 20,
515
+ "Semantic Search",
516
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
517
+ api_name="/update_with_rag_md"
518
+ )[0]
519
+
520
+ r2 = client.predict(
521
+ q,
522
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
523
+ True,
524
+ api_name="/ask_llm"
525
+ )
526
+
527
+ result = f"### πŸ”Ž {q}\n\n{r2}\n\n{refs}"
528
+ md_file, audio_file = save_qa_with_audio(q, result)
529
+ st.subheader("πŸ“ Main Response Audio")
530
+ play_and_download_audio(audio_file, st.session_state['audio_format'])
531
+
532
+ # --- 3) Parse + handle papers
533
+ papers = parse_arxiv_refs(refs)
534
+ if papers:
535
+ # Create minimal links page first
536
+ paper_links = create_paper_links_md(papers)
537
+ links_file = create_file(q, paper_links, "md")
538
+ st.markdown(paper_links)
539
+
540
+ # Then create audio for each paper
541
+ create_paper_audio_files(papers, input_question=q)
542
+ display_papers(papers, get_marquee_settings())
543
+ display_papers_in_sidebar(papers)
544
+ else:
545
+ st.warning("No papers found in the response.")
546
+
547
+ elapsed = time.time() - start
548
+ st.write(f"**Total Elapsed:** {elapsed:.2f} s")
549
+ return result
550
+
551
+ def process_voice_input(text):
552
+ """When user sends voice query, we run the AI lookup + Q&A with audio."""
553
+ if not text:
554
+ return
555
+ st.subheader("πŸ” Search Results")
556
+ result = perform_ai_lookup(
557
+ text,
558
+ vocal_summary=True,
559
+ extended_refs=False,
560
+ titles_summary=True,
561
+ full_audio=True
562
+ )
563
+ md_file, audio_file = save_qa_with_audio(text, result)
564
+ st.subheader("πŸ“ Generated Files")
565
+ st.write(f"Markdown: {md_file}")
566
+ st.write(f"Audio: {audio_file}")
567
+ play_and_download_audio(audio_file, st.session_state['audio_format'])
568
+
569
+ # ─────────────────────────────────────────────────────────
570
+ # 6. FILE HISTORY SIDEBAR
571
+ # ─────────────────────────────────────────────────────────
572
+
573
+ def display_file_history_in_sidebar():
574
+ """
575
+ Shows a history of each local .md, .mp3, .wav file in descending
576
+ order of modification time, with quick icons and optional download links.
577
+ """
578
+ st.sidebar.markdown("---")
579
+ st.sidebar.markdown("### πŸ“‚ File History")
580
+
581
+ # Gather all files
582
+ md_files = glob.glob("*.md")
583
+ mp3_files = glob.glob("*.mp3")
584
+ wav_files = glob.glob("*.wav")
585
+ all_files = md_files + mp3_files + wav_files
586
+
587
+ if not all_files:
588
+ st.sidebar.write("No files found.")
589
+ return
590
+
591
+ # Sort newest first
592
+ all_files = sorted(all_files, key=os.path.getmtime, reverse=True)
593
+
594
+ for f in all_files:
595
+ fname = os.path.basename(f)
596
+ ext = os.path.splitext(fname)[1].lower().strip('.')
597
+ emoji = FILE_EMOJIS.get(ext, 'πŸ“¦')
598
+ time_str = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
599
+
600
+ with st.sidebar.expander(f"{emoji} {fname}"):
601
+ st.write(f"**Modified:** {time_str}")
602
+ if ext == "md":
603
+ with open(f, "r", encoding="utf-8") as file_in:
604
+ snippet = file_in.read(200).replace("\n", " ")
605
+ if len(snippet) == 200:
606
+ snippet += "..."
607
+ st.write(snippet)
608
+ st.markdown(get_download_link(f, file_type="md"), unsafe_allow_html=True)
609
+ elif ext in ["mp3","wav"]:
610
+ st.audio(f)
611
+ st.markdown(get_download_link(f, file_type=ext), unsafe_allow_html=True)
612
+ else:
613
+ st.markdown(get_download_link(f), unsafe_allow_html=True)
614
+
615
+ # ─────────────────────────────────────────────────────────
616
+ # 7. MAIN APP
617
+ # ─────────────────────────────────────────────────────────
618
+
619
+ def main():
620
+ # 1) Setup marquee UI in the sidebar
621
+ update_marquee_settings_ui()
622
+ marquee_settings = get_marquee_settings()
623
+
624
+ # 2) Display the marquee welcome
625
+ display_marquee(st.session_state['marquee_content'],
626
+ {**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
627
+ key_suffix="welcome")
628
+
629
+ # 3) Main action tabs
630
+ tab_main = st.radio("Action:", ["🎀 Voice", "πŸ“Έ Media", "πŸ” ArXiv", "πŸ“ Editor"],
631
+ horizontal=True)
632
+
633
+ # Example custom component usage
634
+ mycomponent = components.declare_component("mycomponent", path="mycomponent")
635
+ val = mycomponent(my_input_value="Hello")
636
+
637
+ if val:
638
+ val_stripped = val.replace('\\n', ' ')
639
+ edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
640
+ run_option = st.selectbox("Model:", ["Arxiv"])
641
+ col1, col2 = st.columns(2)
642
+ with col1:
643
+ autorun = st.checkbox("βš™ AutoRun", value=True)
644
+ with col2:
645
+ full_audio = st.checkbox("πŸ“šFullAudio", value=False)
646
+
647
+ input_changed = (val != st.session_state.old_val)
648
+
649
+ if autorun and input_changed:
650
+ st.session_state.old_val = val
651
+ st.session_state.last_query = edited_input
652
+ perform_ai_lookup(edited_input,
653
+ vocal_summary=True,
654
+ extended_refs=False,
655
+ titles_summary=True,
656
+ full_audio=full_audio)
657
+ else:
658
+ if st.button("β–Ά Run"):
659
+ st.session_state.old_val = val
660
+ st.session_state.last_query = edited_input
661
+ perform_ai_lookup(edited_input,
662
+ vocal_summary=True,
663
+ extended_refs=False,
664
+ titles_summary=True,
665
+ full_audio=full_audio)
666
+
667
+ # ─────────────────────────────────────────────────────────
668
+ # TAB: ArXiv
669
+ # ─────────────────────────────────────────────────────────
670
+ if tab_main == "πŸ” ArXiv":
671
+ st.subheader("πŸ” Query ArXiv")
672
+ q = st.text_input("πŸ” Query:", key="arxiv_query")
673
+
674
+ st.markdown("### πŸŽ› Options")
675
+ vocal_summary = st.checkbox("πŸŽ™ShortAudio", value=True, key="option_vocal_summary")
676
+ extended_refs = st.checkbox("πŸ“œLongRefs", value=False, key="option_extended_refs")
677
+ titles_summary = st.checkbox("πŸ”–TitlesOnly", value=True, key="option_titles_summary")
678
+ full_audio = st.checkbox("πŸ“šFullAudio", value=False, key="option_full_audio")
679
+ full_transcript = st.checkbox("🧾FullTranscript", value=False, key="option_full_transcript")
680
+
681
+ if q and st.button("πŸ”Run"):
682
+ st.session_state.last_query = q
683
+ result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
684
+ titles_summary=titles_summary, full_audio=full_audio)
685
+ if full_transcript:
686
+ create_file(q, result, "md")
687
+
688
+ # ─────────────────────────────────────────────────────────
689
+ # TAB: Voice
690
+ # ─────────────────────────────────────────────────────────
691
+ elif tab_main == "🎀 Voice":
692
+ st.subheader("🎀 Voice Input")
693
+
694
+ st.markdown("### 🎀 Voice Settings")
695
+ selected_voice = st.selectbox(
696
+ "Select TTS Voice:",
697
+ options=EDGE_TTS_VOICES,
698
+ index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
699
+ )
700
+
701
+ st.markdown("### πŸ”Š Audio Format")
702
+ selected_format = st.radio(
703
+ "Choose Audio Format:",
704
+ options=["MP3", "WAV"],
705
+ index=0
706
+ )
707
+
708
+ # Update session state if voice/format changes
709
+ if selected_voice != st.session_state['tts_voice']:
710
+ st.session_state['tts_voice'] = selected_voice
711
+ st.rerun()
712
+ if selected_format.lower() != st.session_state['audio_format']:
713
+ st.session_state['audio_format'] = selected_format.lower()
714
+ st.rerun()
715
+
716
+ # Input text
717
+ user_text = st.text_area("πŸ’¬ Message:", height=100)
718
+ user_text = user_text.strip().replace('\n', ' ')
719
+
720
+ if st.button("πŸ“¨ Send"):
721
+ process_voice_input(user_text)
722
+
723
+ st.subheader("πŸ“œ Chat History")
724
+ for c in st.session_state.chat_history:
725
+ st.write("**You:**", c["user"])
726
+ st.write("**Response:**", c["claude"])
727
+
728
+ # ─────────────────────────────────────────────────────────
729
+ # TAB: Media
730
+ # ─────────────────────────────────────────────────────────
731
+ elif tab_main == "πŸ“Έ Media":
732
+ st.header("πŸ“Έ Media Gallery")
733
+
734
+ # By default, show audio first
735
+ tabs = st.tabs(["🎡 Audio", "πŸ–Ό Images", "πŸŽ₯ Video"])
736
+
737
+ # AUDIO sub-tab
738
+ with tabs[0]:
739
+ st.subheader("🎡 Audio Files")
740
+ audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
741
+ if audio_files:
742
+ for a in audio_files:
743
+ with st.expander(os.path.basename(a)):
744
+ st.audio(a)
745
+ ext = os.path.splitext(a)[1].replace('.', '')
746
+ dl_link = get_download_link(a, file_type=ext)
747
+ st.markdown(dl_link, unsafe_allow_html=True)
748
+ else:
749
+ st.write("No audio files found.")
750
+
751
+ # IMAGES sub-tab
752
+ with tabs[1]:
753
+ st.subheader("πŸ–Ό Image Files")
754
+ imgs = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
755
+ if imgs:
756
+ c = st.slider("Cols", 1, 5, 3, key="cols_images")
757
+ cols = st.columns(c)
758
+ for i, f in enumerate(imgs):
759
+ with cols[i % c]:
760
+ st.image(Image.open(f), use_container_width=True)
761
+ else:
762
+ st.write("No images found.")
763
+
764
+ # VIDEO sub-tab
765
+ with tabs[2]:
766
+ st.subheader("πŸŽ₯ Video Files")
767
+ vids = glob.glob("*.mp4") + glob.glob("*.mov") + glob.glob("*.avi")
768
+ if vids:
769
+ for v in vids:
770
+ with st.expander(os.path.basename(v)):
771
+ st.video(v)
772
+ else:
773
+ st.write("No videos found.")
774
+
775
+ # ─────────────────────────────────────────────────────────
776
+ # TAB: Editor
777
+ # ─────────────────────────────────────────────────────────
778
+ elif tab_main == "πŸ“ Editor":
779
+ st.write("Select or create a file to edit. (Currently minimal demo)")
780
+
781
+ # ─────────────────────────────────────────────────────────
782
+ # SIDEBAR: FILE HISTORY
783
+ # ─────────────────────────────────────────────────────────
784
+ display_file_history_in_sidebar()
785
+
786
+ # Some light CSS styling
787
+ st.markdown("""
788
+ <style>
789
+ .main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
790
+ .stMarkdown { font-family: 'Helvetica Neue', sans-serif; }
791
+ .stButton>button { margin-right: 0.5rem; }
792
+ </style>
793
+ """, unsafe_allow_html=True)
794
+
795
+ # Rerun if needed
796
+ if st.session_state.should_rerun:
797
+ st.session_state.should_rerun = False
798
+ st.rerun()
799
+
800
+ if __name__ == "__main__":
801
+ main()