Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
import anthropic
|
3 |
-
import openai
|
4 |
-
import base64
|
5 |
-
import cv2
|
6 |
-
import glob
|
7 |
-
import json
|
8 |
-
import math
|
9 |
-
import os
|
10 |
-
import pytz
|
11 |
-
import random
|
12 |
-
import re
|
13 |
-
import requests
|
14 |
-
import textract
|
15 |
-
import time
|
16 |
-
import zipfile
|
17 |
import plotly.graph_objects as go
|
18 |
import streamlit.components.v1 as components
|
19 |
from datetime import datetime
|
@@ -34,13 +20,10 @@ from streamlit.runtime.scriptrunner import get_script_run_ctx
|
|
34 |
import asyncio
|
35 |
import edge_tts
|
36 |
from streamlit_marquee import streamlit_marquee
|
37 |
-
from typing import Tuple, Optional
|
38 |
-
import pandas as pd
|
39 |
|
40 |
# ─────────────────────────────────────────────────────────
|
41 |
# 1. CORE CONFIGURATION & SETUP
|
42 |
# ─────────────────────────────────────────────────────────
|
43 |
-
|
44 |
st.set_page_config(
|
45 |
page_title="🚲TalkingAIResearcher🏆",
|
46 |
page_icon="🚲🏆",
|
@@ -54,7 +37,7 @@ st.set_page_config(
|
|
54 |
)
|
55 |
load_dotenv()
|
56 |
|
57 |
-
#
|
58 |
EDGE_TTS_VOICES = [
|
59 |
"en-US-AriaNeural",
|
60 |
"en-US-GuyNeural",
|
@@ -67,7 +50,7 @@ EDGE_TTS_VOICES = [
|
|
67 |
"en-CA-LiamNeural"
|
68 |
]
|
69 |
|
70 |
-
#
|
71 |
if 'marquee_settings' not in st.session_state:
|
72 |
st.session_state['marquee_settings'] = {
|
73 |
"background": "#1E1E1E",
|
@@ -77,50 +60,53 @@ if 'marquee_settings' not in st.session_state:
|
|
77 |
"width": "100%",
|
78 |
"lineHeight": "35px"
|
79 |
}
|
|
|
80 |
if 'tts_voice' not in st.session_state:
|
81 |
st.session_state['tts_voice'] = EDGE_TTS_VOICES[0]
|
|
|
82 |
if 'audio_format' not in st.session_state:
|
83 |
st.session_state['audio_format'] = 'mp3'
|
|
|
84 |
if 'transcript_history' not in st.session_state:
|
85 |
st.session_state['transcript_history'] = []
|
|
|
86 |
if 'chat_history' not in st.session_state:
|
87 |
st.session_state['chat_history'] = []
|
|
|
88 |
if 'openai_model' not in st.session_state:
|
89 |
st.session_state['openai_model'] = "gpt-4o-2024-05-13"
|
|
|
90 |
if 'messages' not in st.session_state:
|
91 |
st.session_state['messages'] = []
|
|
|
92 |
if 'last_voice_input' not in st.session_state:
|
93 |
st.session_state['last_voice_input'] = ""
|
|
|
94 |
if 'editing_file' not in st.session_state:
|
95 |
st.session_state['editing_file'] = None
|
|
|
96 |
if 'edit_new_name' not in st.session_state:
|
97 |
st.session_state['edit_new_name'] = ""
|
|
|
98 |
if 'edit_new_content' not in st.session_state:
|
99 |
st.session_state['edit_new_content'] = ""
|
|
|
100 |
if 'viewing_prefix' not in st.session_state:
|
101 |
st.session_state['viewing_prefix'] = None
|
|
|
102 |
if 'should_rerun' not in st.session_state:
|
103 |
st.session_state['should_rerun'] = False
|
|
|
104 |
if 'old_val' not in st.session_state:
|
105 |
st.session_state['old_val'] = None
|
|
|
106 |
if 'last_query' not in st.session_state:
|
107 |
st.session_state['last_query'] = ""
|
|
|
108 |
if 'marquee_content' not in st.session_state:
|
109 |
st.session_state['marquee_content'] = "🚀 Welcome to TalkingAIResearcher | 🤖 Your Research Assistant"
|
110 |
|
111 |
-
#
|
112 |
-
if 'audio_cache' not in st.session_state:
|
113 |
-
st.session_state['audio_cache'] = {}
|
114 |
-
if 'download_link_cache' not in st.session_state:
|
115 |
-
st.session_state['download_link_cache'] = {}
|
116 |
-
if 'operation_timings' not in st.session_state:
|
117 |
-
st.session_state['operation_timings'] = {}
|
118 |
-
if 'performance_metrics' not in st.session_state:
|
119 |
-
st.session_state['performance_metrics'] = defaultdict(list)
|
120 |
-
if 'enable_audio' not in st.session_state:
|
121 |
-
st.session_state['enable_audio'] = True # Turn TTS on/off
|
122 |
-
|
123 |
-
# ▶ API Keys
|
124 |
openai_api_key = os.getenv('OPENAI_API_KEY', "")
|
125 |
anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
|
126 |
xai_key = os.getenv('xai',"")
|
@@ -134,7 +120,7 @@ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_OR
|
|
134 |
HF_KEY = os.getenv('HF_KEY')
|
135 |
API_URL = os.getenv('API_URL')
|
136 |
|
137 |
-
#
|
138 |
FILE_EMOJIS = {
|
139 |
"md": "📝",
|
140 |
"mp3": "🎵",
|
@@ -142,77 +128,20 @@ FILE_EMOJIS = {
|
|
142 |
}
|
143 |
|
144 |
# ─────────────────────────────────────────────────────────
|
145 |
-
# 2.
|
146 |
-
# ─────────────────────────────────────────────────────────
|
147 |
-
|
148 |
-
class PerformanceTimer:
|
149 |
-
"""
|
150 |
-
⏱️ A context manager for timing operations with automatic logging.
|
151 |
-
Usage:
|
152 |
-
with PerformanceTimer("my_operation"):
|
153 |
-
# do something
|
154 |
-
The duration is stored into `st.session_state['operation_timings']`
|
155 |
-
and appended to the `performance_metrics` list.
|
156 |
-
"""
|
157 |
-
def __init__(self, operation_name: str):
|
158 |
-
self.operation_name = operation_name
|
159 |
-
self.start_time = None
|
160 |
-
|
161 |
-
def __enter__(self):
|
162 |
-
self.start_time = time.time()
|
163 |
-
return self
|
164 |
-
|
165 |
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
166 |
-
if not exc_type: # Only log if no exception occurred
|
167 |
-
duration = time.time() - self.start_time
|
168 |
-
st.session_state['operation_timings'][self.operation_name] = duration
|
169 |
-
st.session_state['performance_metrics'][self.operation_name].append(duration)
|
170 |
-
|
171 |
-
def log_performance_metrics():
|
172 |
-
"""
|
173 |
-
📈 Display performance metrics in the sidebar, including a timing breakdown
|
174 |
-
and a small bar chart of average times.
|
175 |
-
"""
|
176 |
-
st.sidebar.markdown("### ⏱️ Performance Metrics")
|
177 |
-
|
178 |
-
metrics = st.session_state['operation_timings']
|
179 |
-
if metrics:
|
180 |
-
total_time = sum(metrics.values())
|
181 |
-
st.sidebar.write(f"**Total Processing Time:** {total_time:.2f}s")
|
182 |
-
|
183 |
-
# Break down each operation time
|
184 |
-
for operation, duration in metrics.items():
|
185 |
-
percentage = (duration / total_time) * 100
|
186 |
-
st.sidebar.write(f"**{operation}:** {duration:.2f}s ({percentage:.1f}%)")
|
187 |
-
|
188 |
-
# Show timing history chart
|
189 |
-
history_data = []
|
190 |
-
for op, times in st.session_state['performance_metrics'].items():
|
191 |
-
if times: # Only if we have data
|
192 |
-
avg_time = sum(times) / len(times)
|
193 |
-
history_data.append({"Operation": op, "Avg Time (s)": avg_time})
|
194 |
-
|
195 |
-
if history_data:
|
196 |
-
st.sidebar.markdown("### 📊 Timing History (Avg)")
|
197 |
-
chart_data = pd.DataFrame(history_data)
|
198 |
-
st.sidebar.bar_chart(chart_data.set_index("Operation"))
|
199 |
-
|
200 |
-
# ─────────────────────────────────────────────────────────
|
201 |
-
# 3. HELPER FUNCTIONS (FILENAMES, LINKS, MARQUEE, ETC.)
|
202 |
# ─────────────────────────────────────────────────────────
|
203 |
|
204 |
def get_central_time():
|
205 |
-
"""
|
206 |
central = pytz.timezone('US/Central')
|
207 |
return datetime.now(central)
|
208 |
|
209 |
def format_timestamp_prefix():
|
210 |
-
"""
|
211 |
ct = get_central_time()
|
212 |
return ct.strftime("%m_%d_%y_%I_%M_%p")
|
213 |
|
214 |
def initialize_marquee_settings():
|
215 |
-
"""🌈 Initialize marquee defaults if needed."""
|
216 |
if 'marquee_settings' not in st.session_state:
|
217 |
st.session_state['marquee_settings'] = {
|
218 |
"background": "#1E1E1E",
|
@@ -224,12 +153,11 @@ def initialize_marquee_settings():
|
|
224 |
}
|
225 |
|
226 |
def get_marquee_settings():
|
227 |
-
"""🔧 Retrieve marquee settings from session."""
|
228 |
initialize_marquee_settings()
|
229 |
return st.session_state['marquee_settings']
|
230 |
|
231 |
def update_marquee_settings_ui():
|
232 |
-
"""
|
233 |
st.sidebar.markdown("### 🎯 Marquee Settings")
|
234 |
cols = st.sidebar.columns(2)
|
235 |
with cols[0]:
|
@@ -241,7 +169,7 @@ def update_marquee_settings_ui():
|
|
241 |
key="text_color_picker")
|
242 |
with cols[1]:
|
243 |
font_size = st.slider("📏 Size", 10, 24, 14, key="font_size_slider")
|
244 |
-
duration = st.slider("⏱️ Speed
|
245 |
|
246 |
st.session_state['marquee_settings'].update({
|
247 |
"background": bg_color,
|
@@ -251,10 +179,7 @@ def update_marquee_settings_ui():
|
|
251 |
})
|
252 |
|
253 |
def display_marquee(text, settings, key_suffix=""):
|
254 |
-
"""
|
255 |
-
🎉 Show a marquee text with style from the marquee settings.
|
256 |
-
Automatically truncates text to ~280 chars to avoid overflow.
|
257 |
-
"""
|
258 |
truncated_text = text[:280] + "..." if len(text) > 280 else text
|
259 |
streamlit_marquee(
|
260 |
content=truncated_text,
|
@@ -264,10 +189,7 @@ def display_marquee(text, settings, key_suffix=""):
|
|
264 |
st.write("")
|
265 |
|
266 |
def get_high_info_terms(text: str, top_n=10) -> list:
|
267 |
-
"""
|
268 |
-
📌 Extract top_n frequent words & bigrams (excluding common stopwords).
|
269 |
-
Useful for generating short descriptive keywords from Q/A content.
|
270 |
-
"""
|
271 |
stop_words = set(['the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'])
|
272 |
words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
|
273 |
bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
|
@@ -277,24 +199,22 @@ def get_high_info_terms(text: str, top_n=10) -> list:
|
|
277 |
return [term for term, freq in counter.most_common(top_n)]
|
278 |
|
279 |
def clean_text_for_filename(text: str) -> str:
|
280 |
-
"""
|
281 |
-
🏷️ Remove special chars & short unhelpful words from text for safer filenames.
|
282 |
-
Returns a lowercased, underscore-joined token string.
|
283 |
-
"""
|
284 |
text = text.lower()
|
285 |
text = re.sub(r'[^\w\s-]', '', text)
|
286 |
words = text.split()
|
|
|
287 |
stop_short = set(['the', 'and', 'for', 'with', 'this', 'that', 'ai', 'library'])
|
288 |
filtered = [w for w in words if len(w) > 3 and w not in stop_short]
|
289 |
return '_'.join(filtered)[:200]
|
290 |
|
291 |
def generate_filename(prompt, response, file_type="md", max_length=200):
|
292 |
"""
|
293 |
-
|
294 |
-
1)
|
295 |
-
2)
|
296 |
-
3)
|
297 |
-
4)
|
298 |
"""
|
299 |
prefix = format_timestamp_prefix() + "_"
|
300 |
combined_text = (prompt + " " + response)[:200]
|
@@ -302,7 +222,7 @@ def generate_filename(prompt, response, file_type="md", max_length=200):
|
|
302 |
snippet = (prompt[:40] + " " + response[:40]).strip()
|
303 |
snippet_cleaned = clean_text_for_filename(snippet)
|
304 |
|
305 |
-
#
|
306 |
name_parts = info_terms + [snippet_cleaned]
|
307 |
seen = set()
|
308 |
unique_parts = []
|
@@ -319,160 +239,88 @@ def generate_filename(prompt, response, file_type="md", max_length=200):
|
|
319 |
return f"{prefix}{full_name}.{file_type}"
|
320 |
|
321 |
def create_file(prompt, response, file_type="md"):
|
322 |
-
"""
|
323 |
-
📝 Create a text file from prompt + response with a sanitized filename.
|
324 |
-
Returns the created filename.
|
325 |
-
"""
|
326 |
filename = generate_filename(prompt.strip(), response.strip(), file_type)
|
327 |
with open(filename, 'w', encoding='utf-8') as f:
|
328 |
f.write(prompt + "\n\n" + response)
|
329 |
return filename
|
330 |
|
331 |
-
|
332 |
-
# 4. OPTIMIZED AUDIO GENERATION (ASYNC TTS + CACHING)
|
333 |
-
# ─────────────────────────────────────────────────────────
|
334 |
-
|
335 |
-
def clean_for_speech(text: str) -> str:
|
336 |
-
"""
|
337 |
-
🔉 Clean up text for TTS output with enhanced cleaning.
|
338 |
-
Removes markdown, code blocks, links, etc.
|
339 |
-
"""
|
340 |
-
with PerformanceTimer("text_cleaning"):
|
341 |
-
# Remove markdown headers
|
342 |
-
text = re.sub(r'#+ ', '', text)
|
343 |
-
# Remove link formats [text](url)
|
344 |
-
text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
|
345 |
-
# Remove emphasis markers (*, _, ~, `)
|
346 |
-
text = re.sub(r'[*_~`]', '', text)
|
347 |
-
# Remove code blocks
|
348 |
-
text = re.sub(r'```[\s\S]*?```', '', text)
|
349 |
-
text = re.sub(r'`[^`]*`', '', text)
|
350 |
-
# Remove excess whitespace
|
351 |
-
text = re.sub(r'\s+', ' ', text).replace("\n", " ")
|
352 |
-
# Remove hidden S tokens
|
353 |
-
text = text.replace("</s>", " ")
|
354 |
-
# Remove URLs
|
355 |
-
text = re.sub(r'https?://\S+', '', text)
|
356 |
-
text = re.sub(r'\(https?://[^\)]+\)', '', text)
|
357 |
-
text = text.strip()
|
358 |
-
return text
|
359 |
-
|
360 |
-
async def async_edge_tts_generate(
|
361 |
-
text: str,
|
362 |
-
voice: str,
|
363 |
-
rate: int = 0,
|
364 |
-
pitch: int = 0,
|
365 |
-
file_format: str = "mp3"
|
366 |
-
) -> Tuple[Optional[str], float]:
|
367 |
-
"""
|
368 |
-
🎶 Asynchronous TTS generation with caching and performance tracking.
|
369 |
-
Returns (filename, generation_time).
|
370 |
-
"""
|
371 |
-
with PerformanceTimer("tts_generation") as timer:
|
372 |
-
# ▶ Clean & validate text
|
373 |
-
text = clean_for_speech(text)
|
374 |
-
if not text.strip():
|
375 |
-
return None, 0
|
376 |
-
|
377 |
-
# ▶ Check cache (avoid regenerating the same TTS)
|
378 |
-
cache_key = f"{text[:100]}_{voice}_{rate}_{pitch}_{file_format}"
|
379 |
-
if cache_key in st.session_state['audio_cache']:
|
380 |
-
return st.session_state['audio_cache'][cache_key], 0
|
381 |
-
|
382 |
-
try:
|
383 |
-
# ▶ Generate audio
|
384 |
-
rate_str = f"{rate:+d}%"
|
385 |
-
pitch_str = f"{pitch:+d}Hz"
|
386 |
-
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
387 |
-
|
388 |
-
# ▶ Generate unique filename
|
389 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
390 |
-
filename = f"audio_{timestamp}_{random.randint(1000, 9999)}.{file_format}"
|
391 |
-
|
392 |
-
# ▶ Save audio file
|
393 |
-
await communicate.save(filename)
|
394 |
-
|
395 |
-
# ▶ Store in cache
|
396 |
-
st.session_state['audio_cache'][cache_key] = filename
|
397 |
-
|
398 |
-
# ▶ Return path + timing
|
399 |
-
return filename, time.time() - timer.start_time
|
400 |
-
|
401 |
-
except Exception as e:
|
402 |
-
st.error(f"❌ Error generating audio: {str(e)}")
|
403 |
-
return None, 0
|
404 |
-
|
405 |
-
async def async_save_qa_with_audio(
|
406 |
-
question: str,
|
407 |
-
answer: str,
|
408 |
-
voice: Optional[str] = None
|
409 |
-
) -> Tuple[str, Optional[str], float, float]:
|
410 |
"""
|
411 |
-
|
412 |
-
Returns (md_file, audio_file, md_time, audio_time).
|
413 |
"""
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
audio_text = f"{question}\n\nAnswer: {answer}"
|
427 |
-
audio_file, audio_time = await async_edge_tts_generate(
|
428 |
-
audio_text,
|
429 |
-
voice=voice,
|
430 |
-
file_format=st.session_state['audio_format']
|
431 |
-
)
|
432 |
-
|
433 |
-
return md_file, audio_file, md_time, audio_time
|
434 |
|
435 |
-
def
|
436 |
-
"""
|
437 |
-
|
438 |
-
"""
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
464 |
|
465 |
# ─────────────────────────────────────────────────────────
|
466 |
-
#
|
467 |
# ─────────────────────────────────────────────────────────
|
468 |
|
469 |
def parse_arxiv_refs(ref_text: str):
|
470 |
"""
|
471 |
-
|
472 |
-
|
473 |
"""
|
474 |
if not ref_text:
|
475 |
return []
|
|
|
476 |
results = []
|
477 |
current_paper = {}
|
478 |
lines = ref_text.split('\n')
|
@@ -501,9 +349,10 @@ def parse_arxiv_refs(ref_text: str):
|
|
501 |
'download_base64': '',
|
502 |
}
|
503 |
except Exception as e:
|
504 |
-
st.warning(f"
|
505 |
current_paper = {}
|
506 |
continue
|
|
|
507 |
elif current_paper:
|
508 |
# If authors not set, fill it; otherwise, fill summary
|
509 |
if not current_paper['authors']:
|
@@ -520,85 +369,197 @@ def parse_arxiv_refs(ref_text: str):
|
|
520 |
return results[:20]
|
521 |
|
522 |
def create_paper_links_md(papers):
|
523 |
-
"""
|
524 |
-
🔗 Create a minimal .md content linking to each paper's Arxiv URL.
|
525 |
-
"""
|
526 |
lines = ["# Paper Links\n"]
|
527 |
for i, p in enumerate(papers, start=1):
|
528 |
lines.append(f"{i}. **{p['title']}** — [Arxiv]({p['url']})")
|
529 |
return "\n".join(lines)
|
530 |
|
531 |
-
|
532 |
"""
|
533 |
-
|
534 |
-
|
535 |
"""
|
536 |
for paper in papers:
|
537 |
try:
|
538 |
audio_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
|
539 |
audio_text = clean_for_speech(audio_text)
|
540 |
file_format = st.session_state['audio_format']
|
541 |
-
audio_file
|
542 |
audio_text,
|
543 |
voice=st.session_state['tts_voice'],
|
544 |
file_format=file_format
|
545 |
)
|
546 |
paper['full_audio'] = audio_file
|
547 |
-
|
548 |
if audio_file:
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
|
|
|
|
|
|
|
|
553 |
|
554 |
except Exception as e:
|
555 |
-
st.warning(f"
|
556 |
paper['full_audio'] = None
|
557 |
paper['download_base64'] = ''
|
558 |
|
559 |
-
|
|
|
560 |
"""
|
561 |
-
|
562 |
"""
|
563 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
for i, paper in enumerate(papers, start=1):
|
565 |
-
marquee_text = f"📄 {paper['title']} | 👤 {paper['authors'][:120]}
|
566 |
display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
|
567 |
|
568 |
with st.expander(f"{i}. 📄 {paper['title']}", expanded=True):
|
569 |
-
|
|
|
|
|
|
|
|
|
|
|
570 |
st.markdown(f"*Authors:* {paper['authors']}")
|
571 |
st.markdown(paper['summary'])
|
572 |
if paper.get('full_audio'):
|
573 |
-
st.write("📚
|
574 |
st.audio(paper['full_audio'])
|
575 |
if paper['download_base64']:
|
576 |
st.markdown(paper['download_base64'], unsafe_allow_html=True)
|
577 |
|
578 |
def display_papers_in_sidebar(papers):
|
579 |
-
"""
|
580 |
-
🔎 Mirrors the paper listing in the sidebar with expanders, audio, etc.
|
581 |
-
"""
|
582 |
st.sidebar.title("🎶 Papers & Audio")
|
583 |
for i, paper in enumerate(papers, start=1):
|
|
|
|
|
|
|
|
|
584 |
with st.sidebar.expander(f"{i}. {paper['title']}"):
|
585 |
-
|
586 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
st.audio(paper['full_audio'])
|
588 |
if paper['download_base64']:
|
589 |
st.markdown(paper['download_base64'], unsafe_allow_html=True)
|
590 |
-
|
591 |
-
if paper['summary']:
|
592 |
-
st.markdown(f"**Summary:** {paper['summary'][:300]}...")
|
593 |
-
|
594 |
# ─────────────────────────────────────────────────────────
|
595 |
-
#
|
596 |
# ─────────────────────────────────────────────────────────
|
597 |
|
598 |
def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
|
599 |
"""
|
600 |
-
|
601 |
-
|
602 |
"""
|
603 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
604 |
all_files = md_files + mp3_files + wav_files
|
@@ -608,7 +569,7 @@ def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
|
|
608 |
all_content = []
|
609 |
for f in all_files:
|
610 |
if f.endswith('.md'):
|
611 |
-
with open(f,
|
612 |
all_content.append(file.read())
|
613 |
elif f.endswith('.mp3') or f.endswith('.wav'):
|
614 |
basename = os.path.splitext(os.path.basename(f))[0]
|
@@ -629,60 +590,98 @@ def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
|
|
629 |
return short_zip_name
|
630 |
|
631 |
# ─────────────────────────────────────────────────────────
|
632 |
-
#
|
633 |
# ─────────────────────────────────────────────────────────
|
634 |
|
635 |
-
def perform_ai_lookup(
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
681 |
if not text:
|
682 |
return
|
683 |
st.subheader("🔍 Search Results")
|
684 |
-
|
685 |
-
# ▶ Call AI
|
686 |
result = perform_ai_lookup(
|
687 |
text,
|
688 |
vocal_summary=True,
|
@@ -690,98 +689,25 @@ async def process_voice_input(text):
|
|
690 |
titles_summary=True,
|
691 |
full_audio=True
|
692 |
)
|
693 |
-
|
694 |
-
# ▶ Save Q&A as Markdown + audio (async)
|
695 |
-
md_file, audio_file, md_time, audio_time = await async_save_qa_with_audio(text, result)
|
696 |
-
|
697 |
st.subheader("📝 Generated Files")
|
698 |
-
st.write(f"
|
699 |
-
|
700 |
-
|
701 |
-
st.audio(audio_file)
|
702 |
-
dl_link = create_download_link_with_cache(audio_file, file_type=st.session_state['audio_format'])
|
703 |
-
st.markdown(dl_link, unsafe_allow_html=True)
|
704 |
-
|
705 |
-
def display_voice_tab():
|
706 |
-
"""
|
707 |
-
🎙️ Display the voice input tab with TTS settings and real-time usage.
|
708 |
-
"""
|
709 |
-
st.subheader("🎤 Voice Input")
|
710 |
-
|
711 |
-
# ▶ Voice Settings
|
712 |
-
st.markdown("### 🎤 Voice Settings")
|
713 |
-
caption_female = 'Top: 🌸 **Aria** – 🎶 **Jenny** – 🌺 **Sonia** – 🌌 **Natasha** – 🌷 **Clara**'
|
714 |
-
caption_male = 'Bottom: 🌟 **Guy** – 🛠️ **Ryan** – 🎻 **William** – 🌟 **Liam**'
|
715 |
-
|
716 |
-
# Optionally, replace with your own local image or comment out
|
717 |
-
# st.sidebar.image('Group Picture - Voices.png', caption=caption_female + ' | ' + caption_male)
|
718 |
-
|
719 |
-
st.sidebar.markdown("""
|
720 |
-
# 🎙️ Voice Character Agent Selector 🎭
|
721 |
-
*Female Voices*:
|
722 |
-
- 🌸 **Aria** – Elegant, creative storytelling
|
723 |
-
- 🎶 **Jenny** – Friendly, conversational
|
724 |
-
- 🌺 **Sonia** – Bold, confident
|
725 |
-
- 🌌 **Natasha** – Sophisticated, mysterious
|
726 |
-
- 🌷 **Clara** – Cheerful, empathetic
|
727 |
-
|
728 |
-
*Male Voices*:
|
729 |
-
- 🌟 **Guy** – Authoritative, versatile
|
730 |
-
- 🛠️ **Ryan** – Approachable, casual
|
731 |
-
- 🎻 **William** – Classic, scholarly
|
732 |
-
- 🌟 **Liam** – Energetic, engaging
|
733 |
-
""")
|
734 |
-
|
735 |
-
selected_voice = st.selectbox(
|
736 |
-
"👄 Select TTS Voice:",
|
737 |
-
options=EDGE_TTS_VOICES,
|
738 |
-
index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
|
739 |
-
)
|
740 |
-
|
741 |
-
# ▶ Audio Format
|
742 |
-
st.markdown("### 🔊 Audio Format")
|
743 |
-
selected_format = st.radio(
|
744 |
-
"Choose Audio Format:",
|
745 |
-
options=["MP3", "WAV"],
|
746 |
-
index=0
|
747 |
-
)
|
748 |
-
|
749 |
-
# ▶ Update session state if changed
|
750 |
-
if selected_voice != st.session_state['tts_voice']:
|
751 |
-
st.session_state['tts_voice'] = selected_voice
|
752 |
-
st.experimental_rerun()
|
753 |
-
if selected_format.lower() != st.session_state['audio_format']:
|
754 |
-
st.session_state['audio_format'] = selected_format.lower()
|
755 |
-
st.experimental_rerun()
|
756 |
-
|
757 |
-
# ▶ Text Input
|
758 |
-
user_text = st.text_area("💬 Message:", height=100)
|
759 |
-
user_text = user_text.strip().replace('\n', ' ')
|
760 |
-
|
761 |
-
# ▶ Send Button
|
762 |
-
if st.button("📨 Send"):
|
763 |
-
# Run our process_voice_input as an async function
|
764 |
-
asyncio.run(process_voice_input(user_text))
|
765 |
-
|
766 |
-
# ▶ Chat History
|
767 |
-
st.subheader("📜 Chat History")
|
768 |
-
for c in st.session_state.chat_history:
|
769 |
-
st.write("**You:**", c["user"])
|
770 |
-
st.write("**Response:**", c["claude"])
|
771 |
|
772 |
# ─────────────────────────────────────────────────────────
|
773 |
-
# FILE HISTORY SIDEBAR
|
774 |
# ─────────────────────────────────────────────────────────
|
775 |
|
776 |
def display_file_history_in_sidebar():
|
777 |
"""
|
778 |
-
|
779 |
-
with quick icons and optional download links.
|
780 |
"""
|
781 |
st.sidebar.markdown("---")
|
782 |
st.sidebar.markdown("### 📂 File History")
|
783 |
|
784 |
-
#
|
785 |
md_files = glob.glob("*.md")
|
786 |
mp3_files = glob.glob("*.mp3")
|
787 |
wav_files = glob.glob("*.wav")
|
@@ -791,7 +717,24 @@ def display_file_history_in_sidebar():
|
|
791 |
st.sidebar.write("No files found.")
|
792 |
return
|
793 |
|
794 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
795 |
all_files = sorted(all_files, key=os.path.getmtime, reverse=True)
|
796 |
|
797 |
for f in all_files:
|
@@ -808,44 +751,39 @@ def display_file_history_in_sidebar():
|
|
808 |
if len(snippet) == 200:
|
809 |
snippet += "..."
|
810 |
st.write(snippet)
|
811 |
-
|
812 |
-
st.markdown(dl_link, unsafe_allow_html=True)
|
813 |
elif ext in ["mp3","wav"]:
|
814 |
st.audio(f)
|
815 |
-
|
816 |
-
st.markdown(dl_link, unsafe_allow_html=True)
|
817 |
else:
|
818 |
-
|
819 |
-
st.markdown(dl_link, unsafe_allow_html=True)
|
820 |
|
821 |
# ─────────────────────────────────────────────────────────
|
822 |
-
# MAIN APP
|
823 |
# ─────────────────────────────────────────────────────────
|
824 |
|
825 |
def main():
|
826 |
-
#
|
827 |
update_marquee_settings_ui()
|
828 |
marquee_settings = get_marquee_settings()
|
829 |
|
830 |
-
#
|
831 |
-
display_marquee(
|
832 |
-
|
833 |
-
|
834 |
-
key_suffix="welcome"
|
835 |
-
)
|
836 |
|
837 |
-
#
|
838 |
tab_main = st.radio("Action:", ["🎤 Voice", "📸 Media", "🔍 ArXiv", "📝 Editor"],
|
839 |
horizontal=True)
|
840 |
|
841 |
-
#
|
842 |
mycomponent = components.declare_component("mycomponent", path="mycomponent")
|
843 |
-
val = mycomponent(my_input_value="Hello
|
844 |
|
845 |
if val:
|
846 |
val_stripped = val.replace('\\n', ' ')
|
847 |
edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
|
848 |
-
run_option = st.selectbox("Model:", ["Arxiv"
|
849 |
col1, col2 = st.columns(2)
|
850 |
with col1:
|
851 |
autorun = st.checkbox("⚙ AutoRun", value=True)
|
@@ -871,7 +809,7 @@ def main():
|
|
871 |
extended_refs=False,
|
872 |
titles_summary=True,
|
873 |
full_audio=full_audio)
|
874 |
-
|
875 |
# ─────────────────────────────────────────────────────────
|
876 |
# TAB: ArXiv
|
877 |
# ─────────────────────────────────────────────────────────
|
@@ -888,11 +826,8 @@ def main():
|
|
888 |
|
889 |
if q and st.button("🔍Run"):
|
890 |
st.session_state.last_query = q
|
891 |
-
result = perform_ai_lookup(q,
|
892 |
-
|
893 |
-
extended_refs=extended_refs,
|
894 |
-
titles_summary=titles_summary,
|
895 |
-
full_audio=full_audio)
|
896 |
if full_transcript:
|
897 |
create_file(q, result, "md")
|
898 |
|
@@ -900,16 +835,52 @@ def main():
|
|
900 |
# TAB: Voice
|
901 |
# ─────────────────────────────────────────────────────────
|
902 |
elif tab_main == "🎤 Voice":
|
903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
904 |
|
905 |
# ─────────────────────────────────────────────────────────
|
906 |
# TAB: Media
|
907 |
# ─────────────────────────────────────────────────────────
|
908 |
elif tab_main == "📸 Media":
|
909 |
st.header("📸 Media Gallery")
|
|
|
|
|
910 |
tabs = st.tabs(["🎵 Audio", "🖼 Images", "🎥 Video"])
|
911 |
|
912 |
-
#
|
913 |
with tabs[0]:
|
914 |
st.subheader("🎵 Audio Files")
|
915 |
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
@@ -918,12 +889,12 @@ def main():
|
|
918 |
with st.expander(os.path.basename(a)):
|
919 |
st.audio(a)
|
920 |
ext = os.path.splitext(a)[1].replace('.', '')
|
921 |
-
dl_link =
|
922 |
st.markdown(dl_link, unsafe_allow_html=True)
|
923 |
else:
|
924 |
st.write("No audio files found.")
|
925 |
|
926 |
-
#
|
927 |
with tabs[1]:
|
928 |
st.subheader("🖼 Image Files")
|
929 |
imgs = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
|
@@ -936,7 +907,7 @@ def main():
|
|
936 |
else:
|
937 |
st.write("No images found.")
|
938 |
|
939 |
-
#
|
940 |
with tabs[2]:
|
941 |
st.subheader("🎥 Video Files")
|
942 |
vids = glob.glob("*.mp4") + glob.glob("*.mov") + glob.glob("*.avi")
|
@@ -951,16 +922,14 @@ def main():
|
|
951 |
# TAB: Editor
|
952 |
# ─────────────────────────────────────────────────────────
|
953 |
elif tab_main == "📝 Editor":
|
954 |
-
st.write("
|
955 |
-
st.write("Select or create a file to edit. More advanced features can be added as needed.")
|
956 |
|
957 |
# ─────────────────────────────────────────────────────────
|
958 |
-
# SIDEBAR: FILE HISTORY
|
959 |
# ─────────────────────────────────────────────────────────
|
960 |
display_file_history_in_sidebar()
|
961 |
-
log_performance_metrics()
|
962 |
|
963 |
-
#
|
964 |
st.markdown("""
|
965 |
<style>
|
966 |
.main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
|
@@ -969,14 +938,10 @@ def main():
|
|
969 |
</style>
|
970 |
""", unsafe_allow_html=True)
|
971 |
|
972 |
-
#
|
973 |
if st.session_state.should_rerun:
|
974 |
st.session_state.should_rerun = False
|
975 |
-
st.
|
976 |
-
|
977 |
-
# ─────────────────────────────────────────────────────────
|
978 |
-
# 8. RUN APP
|
979 |
-
# ─────────────────────────────────────────────────────────
|
980 |
|
981 |
if __name__ == "__main__":
|
982 |
main()
|
|
|
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
|
|
|
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="🚲🏆",
|
|
|
37 |
)
|
38 |
load_dotenv()
|
39 |
|
40 |
+
# Available English voices for Edge TTS
|
41 |
EDGE_TTS_VOICES = [
|
42 |
"en-US-AriaNeural",
|
43 |
"en-US-GuyNeural",
|
|
|
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",
|
|
|
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',"")
|
|
|
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": "🎵",
|
|
|
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",
|
|
|
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]:
|
|
|
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,
|
|
|
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,
|
|
|
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:])]
|
|
|
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]
|
|
|
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 = []
|
|
|
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')
|
|
|
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']:
|
|
|
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 |
+
|
411 |
+
def display_file_history_in_sidebar():
|
412 |
"""
|
413 |
+
Shows a history of files grouped by query, with lazy loading of audio and content.
|
414 |
"""
|
415 |
+
st.sidebar.markdown("---")
|
416 |
+
st.sidebar.markdown("### 📂 File History")
|
417 |
+
|
418 |
+
# Gather all files
|
419 |
+
md_files = glob.glob("*.md")
|
420 |
+
mp3_files = glob.glob("*.mp3")
|
421 |
+
wav_files = glob.glob("*.wav")
|
422 |
+
all_files = md_files + mp3_files + wav_files
|
423 |
+
|
424 |
+
if not all_files:
|
425 |
+
st.sidebar.write("No files found.")
|
426 |
+
return
|
427 |
+
|
428 |
+
# Group files by their query prefix (timestamp_query)
|
429 |
+
grouped_files = {}
|
430 |
+
for f in all_files:
|
431 |
+
fname = os.path.basename(f)
|
432 |
+
prefix = '_'.join(fname.split('_')[:6]) # Get timestamp part
|
433 |
+
if prefix not in grouped_files:
|
434 |
+
grouped_files[prefix] = {'md': [], 'audio': [], 'loaded': False}
|
435 |
+
|
436 |
+
ext = os.path.splitext(fname)[1].lower()
|
437 |
+
if ext == '.md':
|
438 |
+
grouped_files[prefix]['md'].append(f)
|
439 |
+
elif ext in ['.mp3', '.wav']:
|
440 |
+
grouped_files[prefix]['audio'].append(f)
|
441 |
+
|
442 |
+
# Sort groups by timestamp (newest first)
|
443 |
+
sorted_groups = sorted(grouped_files.items(), key=lambda x: x[0], reverse=True)
|
444 |
+
|
445 |
+
# 🗑⬇️ Sidebar delete all and zip all download
|
446 |
+
col1, col4 = st.sidebar.columns(2)
|
447 |
+
with col1:
|
448 |
+
if st.button("🗑 Delete All"):
|
449 |
+
for f in all_files:
|
450 |
+
os.remove(f)
|
451 |
+
st.session_state.should_rerun = True
|
452 |
+
with col4:
|
453 |
+
if st.button("⬇️ Zip All"):
|
454 |
+
zip_name = create_zip_of_files(md_files, mp3_files, wav_files,
|
455 |
+
st.session_state.get('last_query', ''))
|
456 |
+
if zip_name:
|
457 |
+
st.sidebar.markdown(get_download_link(zip_name, "zip"),
|
458 |
+
unsafe_allow_html=True)
|
459 |
+
|
460 |
+
# Display grouped files
|
461 |
+
for prefix, files in sorted_groups:
|
462 |
+
# Get a preview of content from first MD file
|
463 |
+
preview = ""
|
464 |
+
if files['md']:
|
465 |
+
with open(files['md'][0], "r", encoding="utf-8") as f:
|
466 |
+
preview = f.read(200).replace("\n", " ")
|
467 |
+
if len(preview) > 200:
|
468 |
+
preview += "..."
|
469 |
+
|
470 |
+
# Create unique key for this group
|
471 |
+
group_key = f"group_{prefix}"
|
472 |
+
if group_key not in st.session_state:
|
473 |
+
st.session_state[group_key] = False
|
474 |
+
|
475 |
+
# Display group expander
|
476 |
+
with st.sidebar.expander(f"📑 Query Group: {prefix}"):
|
477 |
+
st.write("**Preview:**")
|
478 |
+
st.write(preview)
|
479 |
+
|
480 |
+
# Load full content button
|
481 |
+
if st.button("📖 View Full Content", key=f"btn_{prefix}"):
|
482 |
+
st.session_state[group_key] = True
|
483 |
+
|
484 |
+
# Only show full content and audio if button was clicked
|
485 |
+
if st.session_state[group_key]:
|
486 |
+
# Display markdown files
|
487 |
+
for md_file in files['md']:
|
488 |
+
with open(md_file, "r", encoding="utf-8") as f:
|
489 |
+
content = f.read()
|
490 |
+
st.markdown("**Full Content:**")
|
491 |
+
st.markdown(content)
|
492 |
+
st.markdown(get_download_link(md_file, file_type="md"),
|
493 |
+
unsafe_allow_html=True)
|
494 |
+
|
495 |
+
# Display audio files
|
496 |
+
usePlaySidebar=False
|
497 |
+
if usePlaySidebar:
|
498 |
+
for audio_file in files['audio']:
|
499 |
+
ext = os.path.splitext(audio_file)[1].replace('.', '')
|
500 |
+
st.audio(audio_file)
|
501 |
+
st.markdown(get_download_link(audio_file, file_type=ext),
|
502 |
+
unsafe_allow_html=True)
|
503 |
+
|
504 |
+
def display_papers(papers, marquee_settings):
|
505 |
+
"""Display paper info with both abs and PDF links."""
|
506 |
+
st.write("## Research Papers")
|
507 |
for i, paper in enumerate(papers, start=1):
|
508 |
+
marquee_text = f"📄 {paper['title']} | 👤 {paper['authors'][:120]}"
|
509 |
display_marquee(marquee_text, marquee_settings, key_suffix=f"paper_{i}")
|
510 |
|
511 |
with st.expander(f"{i}. 📄 {paper['title']}", expanded=True):
|
512 |
+
# Create PDF link by replacing 'abs' with 'pdf' in arxiv URL
|
513 |
+
pdf_url = paper['url'].replace('/abs/', '/pdf/')
|
514 |
+
st.markdown(f"""
|
515 |
+
**{paper['date']} | {paper['title']}**
|
516 |
+
📄 [Abstract]({paper['url']}) | 📑 [PDF]({pdf_url})
|
517 |
+
""")
|
518 |
st.markdown(f"*Authors:* {paper['authors']}")
|
519 |
st.markdown(paper['summary'])
|
520 |
if paper.get('full_audio'):
|
521 |
+
st.write("📚 Paper Audio")
|
522 |
st.audio(paper['full_audio'])
|
523 |
if paper['download_base64']:
|
524 |
st.markdown(paper['download_base64'], unsafe_allow_html=True)
|
525 |
|
526 |
def display_papers_in_sidebar(papers):
|
527 |
+
"""Mirrors the paper listing in sidebar with lazy loading."""
|
|
|
|
|
528 |
st.sidebar.title("🎶 Papers & Audio")
|
529 |
for i, paper in enumerate(papers, start=1):
|
530 |
+
paper_key = f"paper_{paper['url']}"
|
531 |
+
if paper_key not in st.session_state:
|
532 |
+
st.session_state[paper_key] = False
|
533 |
+
|
534 |
with st.sidebar.expander(f"{i}. {paper['title']}"):
|
535 |
+
# Create PDF link
|
536 |
+
pdf_url = paper['url'].replace('/abs/', '/pdf/')
|
537 |
+
st.markdown(f"📄 [Abstract]({paper['url']}) | 📑 [PDF]({pdf_url})")
|
538 |
+
|
539 |
+
# Preview of authors and summary
|
540 |
+
st.markdown(f"**Authors:** {paper['authors'][:100]}...")
|
541 |
+
if paper['summary']:
|
542 |
+
st.markdown(f"**Summary:** {paper['summary'][:200]}...")
|
543 |
+
|
544 |
+
# Load audio button
|
545 |
+
if paper['full_audio'] and st.button("🎵 Load Audio",
|
546 |
+
key=f"btn_{paper_key}"):
|
547 |
+
st.session_state[paper_key] = True
|
548 |
+
|
549 |
+
# Show audio player and download only if requested
|
550 |
+
if st.session_state[paper_key] and paper['full_audio']:
|
551 |
st.audio(paper['full_audio'])
|
552 |
if paper['download_base64']:
|
553 |
st.markdown(paper['download_base64'], unsafe_allow_html=True)
|
554 |
+
|
|
|
|
|
|
|
555 |
# ─────────────────────────────────────────────────────────
|
556 |
+
# 4. ZIP FUNCTION
|
557 |
# ─────────────────────────────────────────────────────────
|
558 |
|
559 |
def create_zip_of_files(md_files, mp3_files, wav_files, input_question):
|
560 |
"""
|
561 |
+
Zip up all relevant files, limiting the final zip name to ~20 chars
|
562 |
+
to avoid overly long base64 strings.
|
563 |
"""
|
564 |
md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
|
565 |
all_files = md_files + mp3_files + wav_files
|
|
|
569 |
all_content = []
|
570 |
for f in all_files:
|
571 |
if f.endswith('.md'):
|
572 |
+
with open(f, 'r', encoding='utf-8') as file:
|
573 |
all_content.append(file.read())
|
574 |
elif f.endswith('.mp3') or f.endswith('.wav'):
|
575 |
basename = os.path.splitext(os.path.basename(f))[0]
|
|
|
590 |
return short_zip_name
|
591 |
|
592 |
# ─────────────────────────────────────────────────────────
|
593 |
+
# 5. MAIN LOGIC: AI LOOKUP & VOICE INPUT
|
594 |
# ─────────────────────────────────────────────────────────
|
595 |
|
596 |
+
def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
|
597 |
+
titles_summary=True, full_audio=False):
|
598 |
+
"""Main routine that uses Anthropic (Claude) + Gradio ArXiv RAG pipeline."""
|
599 |
+
start = time.time()
|
600 |
+
ai_constitution = """
|
601 |
+
You are a talented AI coder and songwriter...
|
602 |
+
"""
|
603 |
+
|
604 |
+
# --- 1) Claude API
|
605 |
+
client = anthropic.Anthropic(api_key=anthropic_key)
|
606 |
+
user_input = q
|
607 |
+
response = client.messages.create(
|
608 |
+
model="claude-3-sonnet-20240229",
|
609 |
+
max_tokens=1000,
|
610 |
+
messages=[
|
611 |
+
{"role": "user", "content": user_input}
|
612 |
+
])
|
613 |
+
st.write("Claude's reply 🧠:")
|
614 |
+
st.markdown(response.content[0].text)
|
615 |
+
|
616 |
+
# Save & produce audio
|
617 |
+
result = response.content[0].text
|
618 |
+
create_file(q, result)
|
619 |
+
md_file, audio_file = save_qa_with_audio(q, result)
|
620 |
+
st.subheader("📝 Main Response Audio")
|
621 |
+
play_and_download_audio(audio_file, st.session_state['audio_format'])
|
622 |
+
|
623 |
+
# --- 2) Arxiv RAG
|
624 |
+
#st.write("Arxiv's AI this Evening is Mixtral 8x7B...")
|
625 |
+
st.write('Running Arxiv RAG with Claude inputs.')
|
626 |
+
client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
|
627 |
+
refs = client.predict(
|
628 |
+
q,
|
629 |
+
10,
|
630 |
+
"Semantic Search",
|
631 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
632 |
+
api_name="/update_with_rag_md"
|
633 |
+
)[0]
|
634 |
+
|
635 |
+
#r2 = client.predict(
|
636 |
+
# q,
|
637 |
+
# "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
638 |
+
# True,
|
639 |
+
# api_name="/ask_llm"
|
640 |
+
#)
|
641 |
+
|
642 |
+
# --- 3) Claude API with arxiv list of papers to app.py
|
643 |
+
client = anthropic.Anthropic(api_key=anthropic_key)
|
644 |
+
user_input = q + '\n\n' + 'Use the paper list below to answer the question thinking through step by step how to create a streamlit app.py and requirements.txt for the solution that answers the questions with a working app to demonstrate.'+ '\n\n'
|
645 |
+
response = client.messages.create(
|
646 |
+
model="claude-3-sonnet-20240229",
|
647 |
+
max_tokens=1000,
|
648 |
+
messages=[
|
649 |
+
{"role": "user", "content": user_input}
|
650 |
+
])
|
651 |
+
r2 = response.content[0].text
|
652 |
+
st.write("Claude's reply 🧠:")
|
653 |
+
st.markdown(r2)
|
654 |
+
|
655 |
+
#result = f"### 🔎 {q}\n\n{r2}\n\n{refs}"
|
656 |
+
result = f"🔎 {r2}\n\n{refs}"
|
657 |
+
md_file, audio_file = save_qa_with_audio(q, result)
|
658 |
+
st.subheader("📝 Main Response Audio")
|
659 |
+
play_and_download_audio(audio_file, st.session_state['audio_format'])
|
660 |
+
|
661 |
+
# --- 3) Parse + handle papers
|
662 |
+
papers = parse_arxiv_refs(refs)
|
663 |
+
if papers:
|
664 |
+
# Create minimal links page first
|
665 |
+
paper_links = create_paper_links_md(papers)
|
666 |
+
links_file = create_file(q, paper_links, "md")
|
667 |
+
st.markdown(paper_links)
|
668 |
+
|
669 |
+
# Then create audio for each paper
|
670 |
+
create_paper_audio_files(papers, input_question=q)
|
671 |
+
display_papers(papers, get_marquee_settings())
|
672 |
+
display_papers_in_sidebar(papers)
|
673 |
+
else:
|
674 |
+
st.warning("No papers found in the response.")
|
675 |
+
|
676 |
+
elapsed = time.time() - start
|
677 |
+
st.write(f"**Total Elapsed:** {elapsed:.2f} s")
|
678 |
+
return result
|
679 |
+
|
680 |
+
def process_voice_input(text):
|
681 |
+
"""When user sends voice query, we run the AI lookup + Q&A with audio."""
|
682 |
if not text:
|
683 |
return
|
684 |
st.subheader("🔍 Search Results")
|
|
|
|
|
685 |
result = perform_ai_lookup(
|
686 |
text,
|
687 |
vocal_summary=True,
|
|
|
689 |
titles_summary=True,
|
690 |
full_audio=True
|
691 |
)
|
692 |
+
md_file, audio_file = save_qa_with_audio(text, result)
|
|
|
|
|
|
|
693 |
st.subheader("📝 Generated Files")
|
694 |
+
st.write(f"Markdown: {md_file}")
|
695 |
+
st.write(f"Audio: {audio_file}")
|
696 |
+
play_and_download_audio(audio_file, st.session_state['audio_format'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
697 |
|
698 |
# ─────────────────────────────────────────────────────────
|
699 |
+
# 6. FILE HISTORY SIDEBAR
|
700 |
# ─────────────────────────────────────────────────────────
|
701 |
|
702 |
def display_file_history_in_sidebar():
|
703 |
"""
|
704 |
+
Shows a history of each local .md, .mp3, .wav file in descending
|
705 |
+
order of modification time, with quick icons and optional download links.
|
706 |
"""
|
707 |
st.sidebar.markdown("---")
|
708 |
st.sidebar.markdown("### 📂 File History")
|
709 |
|
710 |
+
# Gather all files
|
711 |
md_files = glob.glob("*.md")
|
712 |
mp3_files = glob.glob("*.mp3")
|
713 |
wav_files = glob.glob("*.wav")
|
|
|
717 |
st.sidebar.write("No files found.")
|
718 |
return
|
719 |
|
720 |
+
# 🗑⬇️ Sidebar delete all and zip all download
|
721 |
+
col1, col4 = st.sidebar.columns(2)
|
722 |
+
with col1:
|
723 |
+
if st.button("🗑 Delete All"):
|
724 |
+
for f in all_md:
|
725 |
+
os.remove(f)
|
726 |
+
for f in all_mp3:
|
727 |
+
os.remove(f)
|
728 |
+
for f in all_wav:
|
729 |
+
os.remove(f)
|
730 |
+
st.session_state.should_rerun = True
|
731 |
+
with col4:
|
732 |
+
if st.button("⬇️ Zip All"):
|
733 |
+
zip_name = create_zip_of_files(md_files, mp3_files, wav_files, st.session_state.get('last_query', ''))
|
734 |
+
if zip_name:
|
735 |
+
st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
|
736 |
+
|
737 |
+
# Sort newest first
|
738 |
all_files = sorted(all_files, key=os.path.getmtime, reverse=True)
|
739 |
|
740 |
for f in all_files:
|
|
|
751 |
if len(snippet) == 200:
|
752 |
snippet += "..."
|
753 |
st.write(snippet)
|
754 |
+
st.markdown(get_download_link(f, file_type="md"), unsafe_allow_html=True)
|
|
|
755 |
elif ext in ["mp3","wav"]:
|
756 |
st.audio(f)
|
757 |
+
st.markdown(get_download_link(f, file_type=ext), unsafe_allow_html=True)
|
|
|
758 |
else:
|
759 |
+
st.markdown(get_download_link(f), unsafe_allow_html=True)
|
|
|
760 |
|
761 |
# ─────────────────────────────────────────────────────────
|
762 |
+
# 7. MAIN APP
|
763 |
# ─────────────────────────────────────────────────────────
|
764 |
|
765 |
def main():
|
766 |
+
# 1) Setup marquee UI in the sidebar
|
767 |
update_marquee_settings_ui()
|
768 |
marquee_settings = get_marquee_settings()
|
769 |
|
770 |
+
# 2) Display the marquee welcome
|
771 |
+
display_marquee(st.session_state['marquee_content'],
|
772 |
+
{**marquee_settings, "font-size": "28px", "lineHeight": "50px"},
|
773 |
+
key_suffix="welcome")
|
|
|
|
|
774 |
|
775 |
+
# 3) Main action tabs
|
776 |
tab_main = st.radio("Action:", ["🎤 Voice", "📸 Media", "🔍 ArXiv", "📝 Editor"],
|
777 |
horizontal=True)
|
778 |
|
779 |
+
# Example custom component usage
|
780 |
mycomponent = components.declare_component("mycomponent", path="mycomponent")
|
781 |
+
val = mycomponent(my_input_value="Hello")
|
782 |
|
783 |
if val:
|
784 |
val_stripped = val.replace('\\n', ' ')
|
785 |
edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
|
786 |
+
run_option = st.selectbox("Model:", ["Arxiv"])
|
787 |
col1, col2 = st.columns(2)
|
788 |
with col1:
|
789 |
autorun = st.checkbox("⚙ AutoRun", value=True)
|
|
|
809 |
extended_refs=False,
|
810 |
titles_summary=True,
|
811 |
full_audio=full_audio)
|
812 |
+
|
813 |
# ─────────────────────────────────────────────────────────
|
814 |
# TAB: ArXiv
|
815 |
# ─────────────────────────────────────────────────────────
|
|
|
826 |
|
827 |
if q and st.button("🔍Run"):
|
828 |
st.session_state.last_query = q
|
829 |
+
result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
|
830 |
+
titles_summary=titles_summary, full_audio=full_audio)
|
|
|
|
|
|
|
831 |
if full_transcript:
|
832 |
create_file(q, result, "md")
|
833 |
|
|
|
835 |
# TAB: Voice
|
836 |
# ─────────────────────────────────────────────────────────
|
837 |
elif tab_main == "🎤 Voice":
|
838 |
+
st.subheader("🎤 Voice Input")
|
839 |
+
|
840 |
+
st.markdown("### 🎤 Voice Settings")
|
841 |
+
selected_voice = st.selectbox(
|
842 |
+
"Select TTS Voice:",
|
843 |
+
options=EDGE_TTS_VOICES,
|
844 |
+
index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
|
845 |
+
)
|
846 |
+
|
847 |
+
st.markdown("### 🔊 Audio Format")
|
848 |
+
selected_format = st.radio(
|
849 |
+
"Choose Audio Format:",
|
850 |
+
options=["MP3", "WAV"],
|
851 |
+
index=0
|
852 |
+
)
|
853 |
+
|
854 |
+
# Update session state if voice/format changes
|
855 |
+
if selected_voice != st.session_state['tts_voice']:
|
856 |
+
st.session_state['tts_voice'] = selected_voice
|
857 |
+
st.rerun()
|
858 |
+
if selected_format.lower() != st.session_state['audio_format']:
|
859 |
+
st.session_state['audio_format'] = selected_format.lower()
|
860 |
+
st.rerun()
|
861 |
+
|
862 |
+
# Input text
|
863 |
+
user_text = st.text_area("💬 Message:", height=100)
|
864 |
+
user_text = user_text.strip().replace('\n', ' ')
|
865 |
+
|
866 |
+
if st.button("📨 Send"):
|
867 |
+
process_voice_input(user_text)
|
868 |
+
|
869 |
+
st.subheader("📜 Chat History")
|
870 |
+
for c in st.session_state.chat_history:
|
871 |
+
st.write("**You:**", c["user"])
|
872 |
+
st.write("**Response:**", c["claude"])
|
873 |
|
874 |
# ─────────────────────────────────────────────────────────
|
875 |
# TAB: Media
|
876 |
# ─────────────────────────────────────────────────────────
|
877 |
elif tab_main == "📸 Media":
|
878 |
st.header("📸 Media Gallery")
|
879 |
+
|
880 |
+
# By default, show audio first
|
881 |
tabs = st.tabs(["🎵 Audio", "🖼 Images", "🎥 Video"])
|
882 |
|
883 |
+
# AUDIO sub-tab
|
884 |
with tabs[0]:
|
885 |
st.subheader("🎵 Audio Files")
|
886 |
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
|
|
889 |
with st.expander(os.path.basename(a)):
|
890 |
st.audio(a)
|
891 |
ext = os.path.splitext(a)[1].replace('.', '')
|
892 |
+
dl_link = get_download_link(a, file_type=ext)
|
893 |
st.markdown(dl_link, unsafe_allow_html=True)
|
894 |
else:
|
895 |
st.write("No audio files found.")
|
896 |
|
897 |
+
# IMAGES sub-tab
|
898 |
with tabs[1]:
|
899 |
st.subheader("🖼 Image Files")
|
900 |
imgs = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
|
|
|
907 |
else:
|
908 |
st.write("No images found.")
|
909 |
|
910 |
+
# VIDEO sub-tab
|
911 |
with tabs[2]:
|
912 |
st.subheader("🎥 Video Files")
|
913 |
vids = glob.glob("*.mp4") + glob.glob("*.mov") + glob.glob("*.avi")
|
|
|
922 |
# TAB: Editor
|
923 |
# ─────────────────────────────────────────────────────────
|
924 |
elif tab_main == "📝 Editor":
|
925 |
+
st.write("Select or create a file to edit. (Currently minimal demo)")
|
|
|
926 |
|
927 |
# ─────────────────────────────────────────────────────────
|
928 |
+
# SIDEBAR: FILE HISTORY
|
929 |
# ─────────────────────────────────────────────────────────
|
930 |
display_file_history_in_sidebar()
|
|
|
931 |
|
932 |
+
# Some light CSS styling
|
933 |
st.markdown("""
|
934 |
<style>
|
935 |
.main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
|
|
|
938 |
</style>
|
939 |
""", unsafe_allow_html=True)
|
940 |
|
941 |
+
# Rerun if needed
|
942 |
if st.session_state.should_rerun:
|
943 |
st.session_state.should_rerun = False
|
944 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
945 |
|
946 |
if __name__ == "__main__":
|
947 |
main()
|