awacke1 commited on
Commit
b41266a
·
verified ·
1 Parent(s): 5fa7729

Create backup14.SpeechInWorks.app.py

Browse files
Files changed (1) hide show
  1. backup14.SpeechInWorks.app.py +738 -0
backup14.SpeechInWorks.app.py ADDED
@@ -0,0 +1,738 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import asyncio
3
+ import websockets
4
+ import uuid
5
+ from datetime import datetime
6
+ import os
7
+ import random
8
+ import time
9
+ import hashlib
10
+ from PIL import Image
11
+ import glob
12
+ import base64
13
+ import io
14
+ import streamlit.components.v1 as components
15
+ import edge_tts
16
+ from audio_recorder_streamlit import audio_recorder
17
+ import nest_asyncio
18
+ import re
19
+ from streamlit_paste_button import paste_image_button
20
+ import pytz
21
+ import shutil
22
+ import anthropic
23
+ import openai
24
+ from PyPDF2 import PdfReader
25
+ import threading
26
+ import json
27
+ import zipfile
28
+ from gradio_client import Client
29
+ from dotenv import load_dotenv
30
+ from streamlit_marquee import streamlit_marquee
31
+ from collections import defaultdict, Counter
32
+ import pandas as pd
33
+
34
+ # 🛠️ Patch asyncio for nesting
35
+ nest_asyncio.apply()
36
+
37
+ # 🎨 Page Config
38
+ st.set_page_config(
39
+ page_title="🚲TalkingAIResearcher🏆",
40
+ page_icon="🚲🏆",
41
+ layout="wide",
42
+ initial_sidebar_state="auto"
43
+ )
44
+
45
+ # 🌟 Static Config
46
+ icons = '🤖🧠🔬📝'
47
+ Site_Name = '🤖🧠Chat & Quote Node📝🔬'
48
+ START_ROOM = "Sector 🌌"
49
+ FUN_USERNAMES = {
50
+ "CosmicJester 🌌": "en-US-AriaNeural",
51
+ "PixelPanda 🐼": "en-US-JennyNeural",
52
+ "QuantumQuack 🦆": "en-GB-SoniaNeural",
53
+ "StellarSquirrel 🐿️": "en-AU-NatashaNeural",
54
+ "GizmoGuru ⚙️": "en-CA-ClaraNeural",
55
+ "NebulaNinja 🌠": "en-US-GuyNeural",
56
+ "ByteBuster 💾": "en-GB-RyanNeural",
57
+ "GalacticGopher 🌍": "en-AU-WilliamNeural",
58
+ "RocketRaccoon 🚀": "en-CA-LiamNeural",
59
+ "EchoElf 🧝": "en-US-AnaNeural",
60
+ "PhantomFox 🦊": "en-US-BrandonNeural",
61
+ "WittyWizard 🧙": "en-GB-ThomasNeural",
62
+ "LunarLlama 🌙": "en-AU-FreyaNeural",
63
+ "SolarSloth ☀️": "en-CA-LindaNeural",
64
+ "AstroAlpaca 🦙": "en-US-ChristopherNeural",
65
+ "CyberCoyote 🐺": "en-GB-ElliotNeural",
66
+ "MysticMoose 🦌": "en-AU-JamesNeural",
67
+ "GlitchGnome 🧚": "en-CA-EthanNeural",
68
+ "VortexViper 🐍": "en-US-AmberNeural",
69
+ "ChronoChimp 🐒": "en-GB-LibbyNeural"
70
+ }
71
+ EDGE_TTS_VOICES = list(set(FUN_USERNAMES.values()))
72
+ FILE_EMOJIS = {"md": "📝", "mp3": "🎵", "png": "🖼️", "mp4": "🎥"}
73
+
74
+ # 📁 Directories (Media at Root)
75
+ for d in ["chat_logs", "vote_logs", "audio_logs", "history_logs", "audio_cache"]:
76
+ os.makedirs(d, exist_ok=True)
77
+
78
+ CHAT_DIR = "chat_logs"
79
+ VOTE_DIR = "vote_logs"
80
+ MEDIA_DIR = "."
81
+ AUDIO_CACHE_DIR = "audio_cache"
82
+ AUDIO_DIR = "audio_logs"
83
+ STATE_FILE = "user_state.txt"
84
+
85
+ CHAT_FILE = os.path.join(CHAT_DIR, "global_chat.md")
86
+ QUOTE_VOTES_FILE = os.path.join(VOTE_DIR, "quote_votes.md")
87
+ IMAGE_VOTES_FILE = os.path.join(VOTE_DIR, "image_votes.md")
88
+ HISTORY_FILE = os.path.join(VOTE_DIR, "vote_history.md")
89
+
90
+ # 🔑 API Keys
91
+ load_dotenv()
92
+ anthropic_key = os.getenv('ANTHROPIC_API_KEY', st.secrets.get('ANTHROPIC_API_KEY', ""))
93
+ openai_api_key = os.getenv('OPENAI_API_KEY', st.secrets.get('OPENAI_API_KEY', ""))
94
+ openai_client = openai.OpenAI(api_key=openai_api_key)
95
+
96
+ # 🕒 Timestamp Helper
97
+ def format_timestamp_prefix(username=""):
98
+ central = pytz.timezone('US/Central')
99
+ now = datetime.now(central)
100
+ return f"{now.strftime('%Y%m%d_%H%M%S')}-by-{username}"
101
+
102
+ # 📈 Performance Timer
103
+ class PerformanceTimer:
104
+ def __init__(self, name):
105
+ self.name, self.start = name, None
106
+ def __enter__(self):
107
+ self.start = time.time()
108
+ return self
109
+ def __exit__(self, *args):
110
+ duration = time.time() - self.start
111
+ st.session_state['operation_timings'][self.name] = duration
112
+ st.session_state['performance_metrics'][self.name].append(duration)
113
+
114
+ # 🎛️ Session State Init
115
+ def init_session_state():
116
+ defaults = {
117
+ 'server_running': False, 'server_task': None, 'active_connections': {},
118
+ 'media_notifications': [], 'last_chat_update': 0, 'displayed_chat_lines': [],
119
+ 'message_text': "", 'audio_cache': {}, 'pasted_image_data': None,
120
+ 'quote_line': None, 'refresh_rate': 5, 'base64_cache': {},
121
+ 'transcript_history': [], 'last_transcript': "", 'image_hashes': set(),
122
+ 'tts_voice': "en-US-AriaNeural", 'chat_history': [], 'marquee_settings': {
123
+ "background": "#1E1E1E", "color": "#FFFFFF", "font-size": "14px",
124
+ "animationDuration": "20s", "width": "100%", "lineHeight": "35px"
125
+ }, 'operation_timings': {}, 'performance_metrics': defaultdict(list),
126
+ 'enable_audio': True, 'download_link_cache': {}, 'username': None,
127
+ 'autosend': True, 'autosearch': True, 'last_message': "", 'last_query': "",
128
+ 'mp3_files': {}, 'timer_start': time.time(), 'quote_index': 0,
129
+ 'quote_source': "famous", 'last_sent_transcript': "", 'old_val': None
130
+ }
131
+ for k, v in defaults.items():
132
+ if k not in st.session_state:
133
+ st.session_state[k] = v
134
+
135
+ # 🖌️ Marquee Helpers
136
+ def update_marquee_settings_ui():
137
+ st.sidebar.markdown("### 🎯 Marquee Settings")
138
+ cols = st.sidebar.columns(2)
139
+ with cols[0]:
140
+ st.session_state['marquee_settings']['background'] = st.color_picker("🎨 Background", "#1E1E1E")
141
+ st.session_state['marquee_settings']['color'] = st.color_picker("✍️ Text", "#FFFFFF")
142
+ with cols[1]:
143
+ st.session_state['marquee_settings']['font-size'] = f"{st.slider('📏 Size', 10, 24, 14)}px"
144
+ st.session_state['marquee_settings']['animationDuration'] = f"{st.slider('⏱️ Speed', 1, 20, 20)}s"
145
+
146
+ def display_marquee(text, settings, key_suffix=""):
147
+ truncated = text[:280] + "..." if len(text) > 280 else text
148
+ streamlit_marquee(content=truncated, **settings, key=f"marquee_{key_suffix}")
149
+ st.write("")
150
+
151
+ # 📝 Text & File Helpers
152
+ def clean_text_for_tts(text):
153
+ return re.sub(r'[#*!\[\]]+', '', ' '.join(text.split()))[:200] or "No text"
154
+
155
+ def clean_text_for_filename(text):
156
+ return '_'.join(re.sub(r'[^\w\s-]', '', text.lower()).split())[:200]
157
+
158
+ def get_high_info_terms(text, top_n=10):
159
+ stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'}
160
+ words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower())
161
+ bi_grams = [' '.join(pair) for pair in zip(words, words[1:])]
162
+ filtered = [t for t in words + bi_grams if t not in stop_words and len(t.split()) <= 2]
163
+ return [t for t, _ in Counter(filtered).most_common(top_n)]
164
+
165
+ def generate_filename(prompt, username, file_type="md"):
166
+ timestamp = format_timestamp_prefix(username)
167
+ hash_val = hashlib.md5(prompt.encode()).hexdigest()[:8]
168
+ return f"{timestamp}-{hash_val}.{file_type}"
169
+
170
+ def create_file(prompt, username, file_type="md"):
171
+ filename = generate_filename(prompt, username, file_type)
172
+ with open(filename, 'w', encoding='utf-8') as f:
173
+ f.write(prompt)
174
+ return filename
175
+
176
+ def get_download_link(file, file_type="mp3"):
177
+ cache_key = f"dl_{file}"
178
+ if cache_key not in st.session_state['download_link_cache']:
179
+ with open(file, "rb") as f:
180
+ b64 = base64.b64encode(f.read()).decode()
181
+ mime_types = {"mp3": "audio/mpeg", "png": "image/png", "mp4": "video/mp4", "md": "text/markdown"}
182
+ st.session_state['download_link_cache'][cache_key] = f'<a href="data:{mime_types.get(file_type, "application/octet-stream")};base64,{b64}" download="{os.path.basename(file)}">{FILE_EMOJIS.get(file_type, "Download")} Download {os.path.basename(file)}</a>'
183
+ return st.session_state['download_link_cache'][cache_key]
184
+
185
+ def save_username(username):
186
+ try:
187
+ with open(STATE_FILE, 'w') as f:
188
+ f.write(username)
189
+ except Exception as e:
190
+ print(f"Failed to save username: {e}")
191
+
192
+ def load_username():
193
+ if os.path.exists(STATE_FILE):
194
+ try:
195
+ with open(STATE_FILE, 'r') as f:
196
+ return f.read().strip()
197
+ except Exception as e:
198
+ print(f"Failed to load username: {e}")
199
+ return None
200
+
201
+ def concatenate_markdown_files():
202
+ md_files = sorted(glob.glob("*.md"), key=os.path.getmtime, reverse=True)
203
+ all_md_content = ""
204
+ for md_file in md_files:
205
+ with open(md_file, 'r', encoding='utf-8') as f:
206
+ all_md_content += f.read() + "\n\n---\n\n"
207
+ return all_md_content.strip()
208
+
209
+ # 🎶 Audio Processing
210
+ async def async_edge_tts_generate(text, voice, username, rate=0, pitch=0, file_format="mp3"):
211
+ cache_key = f"{text[:100]}_{voice}_{rate}_{pitch}_{file_format}"
212
+ if cache_key in st.session_state['audio_cache']:
213
+ return st.session_state['audio_cache'][cache_key], 0
214
+ start_time = time.time()
215
+ text = clean_text_for_tts(text)
216
+ if not text or text == "No text":
217
+ print(f"Skipping audio generation for empty/invalid text: '{text}'")
218
+ return None, 0
219
+ filename = f"{format_timestamp_prefix(username)}-{hashlib.md5(text.encode()).hexdigest()[:8]}.{file_format}"
220
+ try:
221
+ communicate = edge_tts.Communicate(text, voice, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz")
222
+ await communicate.save(filename)
223
+ st.session_state['audio_cache'][cache_key] = filename
224
+ return filename, time.time() - start_time
225
+ except edge_tts.exceptions.NoAudioReceived as e:
226
+ print(f"No audio received for text: '{text}' with voice: {voice}. Error: {e}")
227
+ return None, 0
228
+ except Exception as e:
229
+ print(f"Error generating audio for text: '{text}' with voice: {voice}. Error: {e}")
230
+ return None, 0
231
+
232
+ def play_and_download_audio(file_path):
233
+ if file_path and os.path.exists(file_path):
234
+ st.audio(file_path)
235
+ st.markdown(get_download_link(file_path), unsafe_allow_html=True)
236
+
237
+ def load_mp3_viewer():
238
+ mp3_files = sorted(glob.glob(f"*.mp3"), key=os.path.getmtime, reverse=True)
239
+ for mp3 in mp3_files:
240
+ filename = os.path.basename(mp3)
241
+ if filename not in st.session_state['mp3_files']:
242
+ st.session_state['mp3_files'][filename] = mp3
243
+
244
+ async def save_chat_entry(username, message, is_markdown=False):
245
+ central = pytz.timezone('US/Central')
246
+ timestamp = datetime.now(central).strftime("%Y-%m-%d %H:%M:%S")
247
+ entry = f"[{timestamp}] {username}: {message}" if not is_markdown else f"[{timestamp}] {username}:\n```markdown\n{message}\n```"
248
+ with open(CHAT_FILE, 'a') as f:
249
+ f.write(f"{entry}\n")
250
+ voice = FUN_USERNAMES.get(username, "en-US-AriaNeural")
251
+ audio_file, _ = await async_edge_tts_generate(message, voice, username)
252
+ if audio_file:
253
+ with open(HISTORY_FILE, 'a') as f:
254
+ f.write(f"[{timestamp}] {username}: Audio - {audio_file}\n")
255
+ st.session_state['mp3_files'][os.path.basename(audio_file)] = audio_file
256
+ else:
257
+ print(f"No audio generated for message: '{message}' by {username}")
258
+ await broadcast_message(f"{username}|{message}", "chat")
259
+ st.session_state.last_chat_update = time.time()
260
+ st.session_state.chat_history.append(entry)
261
+ return audio_file
262
+
263
+ async def load_chat():
264
+ if not os.path.exists(CHAT_FILE):
265
+ with open(CHAT_FILE, 'a') as f:
266
+ f.write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub! 🎤\n")
267
+ with open(CHAT_FILE, 'r') as f:
268
+ content = f.read().strip()
269
+ lines = content.split('\n')
270
+ numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(lines) if line.strip())
271
+ return numbered_content
272
+
273
+ async def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False, useArxiv=True, useArxivAudio=False):
274
+ start = time.time()
275
+ client = anthropic.Anthropic(api_key=anthropic_key)
276
+ response = client.messages.create(
277
+ model="claude-3-sonnet-20240229",
278
+ max_tokens=1000,
279
+ messages=[{"role": "user", "content": q}]
280
+ )
281
+ st.write("Claude's reply 🧠:")
282
+ st.markdown(response.content[0].text)
283
+
284
+ result = response.content[0].text
285
+ md_file = create_file(q, result, "System")
286
+ audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
287
+ st.subheader("📝 Main Response Audio")
288
+ play_and_download_audio(audio_file)
289
+
290
+ if useArxiv:
291
+ q = q + result
292
+ st.write('Running Arxiv RAG with Claude inputs.')
293
+ gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
294
+ refs = gradio_client.predict(
295
+ q, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md"
296
+ )[0]
297
+ result = f"🔎 {q}\n\n{refs}"
298
+ md_file = create_file(q, result, "System")
299
+ audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
300
+ st.subheader("📝 ArXiv Response Audio")
301
+ play_and_download_audio(audio_file)
302
+
303
+ papers = parse_arxiv_refs(refs)
304
+ if papers and useArxivAudio:
305
+ await create_paper_audio_files(papers, q)
306
+ return result, papers
307
+ elapsed = time.time() - start
308
+ st.write(f"**Total Elapsed:** {elapsed:.2f} s")
309
+ return result, []
310
+
311
+ # 🌐 WebSocket Handling
312
+ async def websocket_handler(websocket, path):
313
+ client_id = str(uuid.uuid4())
314
+ room_id = "chat"
315
+ if room_id not in st.session_state.active_connections:
316
+ st.session_state.active_connections[room_id] = {}
317
+ st.session_state.active_connections[room_id][client_id] = websocket
318
+ username = st.session_state.get('username', random.choice(list(FUN_USERNAMES.keys())))
319
+ chat_content = await load_chat()
320
+ if not any(f"Client-{client_id}" in line for line in chat_content.split('\n')):
321
+ await save_chat_entry("System 🌟", f"{username} has joined {START_ROOM}!")
322
+ try:
323
+ async for message in websocket:
324
+ if '|' in message:
325
+ username, content = message.split('|', 1)
326
+ await save_chat_entry(username, content)
327
+ else:
328
+ await websocket.send("ERROR|Message format: username|content")
329
+ except websockets.ConnectionClosed:
330
+ await save_chat_entry("System 🌟", f"{username} has left {START_ROOM}!")
331
+ finally:
332
+ if room_id in st.session_state.active_connections and client_id in st.session_state.active_connections[room_id]:
333
+ del st.session_state.active_connections[room_id][client_id]
334
+
335
+ async def broadcast_message(message, room_id):
336
+ if room_id in st.session_state.active_connections:
337
+ disconnected = []
338
+ for client_id, ws in st.session_state.active_connections[room_id].items():
339
+ try:
340
+ await ws.send(message)
341
+ except websockets.ConnectionClosed:
342
+ disconnected.append(client_id)
343
+ for client_id in disconnected:
344
+ if client_id in st.session_state.active_connections[room_id]:
345
+ del st.session_state.active_connections[room_id][client_id]
346
+
347
+ async def run_websocket_server():
348
+ if not st.session_state.server_running:
349
+ server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
350
+ st.session_state.server_running = True
351
+ await server.wait_closed()
352
+
353
+ def start_websocket_server():
354
+ asyncio.run(run_websocket_server())
355
+
356
+ # 📚 PDF to Audio
357
+ class AudioProcessor:
358
+ def __init__(self):
359
+ self.cache_dir = AUDIO_CACHE_DIR
360
+ os.makedirs(self.cache_dir, exist_ok=True)
361
+ self.metadata = json.load(open(f"{self.cache_dir}/metadata.json")) if os.path.exists(f"{self.cache_dir}/metadata.json") else {}
362
+
363
+ def _save_metadata(self):
364
+ with open(f"{self.cache_dir}/metadata.json", 'w') as f:
365
+ json.dump(self.metadata, f)
366
+
367
+ async def create_audio(self, text, voice='en-US-AriaNeural'):
368
+ cache_key = hashlib.md5(f"{text}:{voice}".encode()).hexdigest()
369
+ cache_path = f"{self.cache_dir}/{cache_key}.mp3"
370
+ if cache_key in self.metadata and os.path.exists(cache_path):
371
+ return cache_path
372
+ text = clean_text_for_tts(text)
373
+ if not text:
374
+ return None
375
+ communicate = edge_tts.Communicate(text, voice)
376
+ await communicate.save(cache_path)
377
+ self.metadata[cache_key] = {'timestamp': datetime.now().isoformat(), 'text_length': len(text), 'voice': voice}
378
+ self._save_metadata()
379
+ return cache_path
380
+
381
+ def process_pdf(pdf_file, max_pages, voice, audio_processor):
382
+ reader = PdfReader(pdf_file)
383
+ total_pages = min(len(reader.pages), max_pages)
384
+ texts, audios = [], {}
385
+ async def process_page(i, text):
386
+ audio_path = await audio_processor.create_audio(text, voice)
387
+ if audio_path:
388
+ audios[i] = audio_path
389
+ for i in range(total_pages):
390
+ text = reader.pages[i].extract_text()
391
+ texts.append(text)
392
+ threading.Thread(target=lambda: asyncio.run(process_page(i, text))).start()
393
+ return texts, audios, total_pages
394
+
395
+ # 🔍 ArXiv & AI Lookup
396
+ def parse_arxiv_refs(ref_text):
397
+ if not ref_text:
398
+ return []
399
+ papers = []
400
+ current = {}
401
+ for line in ref_text.split('\n'):
402
+ if line.count('|') == 2:
403
+ if current:
404
+ papers.append(current)
405
+ date, title, *_ = line.strip('* ').split('|')
406
+ url = re.search(r'(https://arxiv.org/\S+)', line).group(1) if re.search(r'(https://arxiv.org/\S+)', line) else f"paper_{len(papers)}"
407
+ current = {'date': date, 'title': title, 'url': url, 'authors': '', 'summary': '', 'full_audio': None, 'download_base64': ''}
408
+ elif current:
409
+ if not current['authors']:
410
+ current['authors'] = line.strip('* ')
411
+ else:
412
+ current['summary'] += ' ' + line.strip() if current['summary'] else line.strip()
413
+ if current:
414
+ papers.append(current)
415
+ return papers[:20]
416
+
417
+ def generate_5min_feature_markdown(paper):
418
+ title, summary, authors, date, url = paper['title'], paper['summary'], paper['authors'], paper['date'], paper['url']
419
+ pdf_url = url.replace("abs", "pdf") + (".pdf" if not url.endswith(".pdf") else "")
420
+ wct, sw = len(title.split()), len(summary.split())
421
+ terms = get_high_info_terms(summary, 15)
422
+ rouge = round((len(terms) / max(sw, 1)) * 100, 2)
423
+ mermaid = "```mermaid\nflowchart TD\n" + "\n".join(f' T{i+1}["{t}"] --> T{i+2}["{terms[i+1]}"]' for i in range(len(terms)-1)) + "\n```"
424
+ return f"""
425
+ ## 📄 {title}
426
+ **Authors:** {authors} | **Date:** {date} | **Words:** Title: {wct}, Summary: {sw}
427
+ **Links:** [Abstract]({url}) | [PDF]({pdf_url})
428
+ **Terms:** {', '.join(terms)} | **ROUGE:** {rouge}%
429
+ ### 🎤 TTF Read Aloud
430
+ - **Title:** {title} | **Terms:** {', '.join(terms)} | **ROUGE:** {rouge}%
431
+ #### Concepts Graph
432
+ {mermaid}
433
+ ---
434
+ """
435
+
436
+ def create_detailed_paper_md(papers):
437
+ return "# Detailed Summary\n" + "\n".join(generate_5min_feature_markdown(p) for p in papers)
438
+
439
+ async def create_paper_audio_files(papers, query):
440
+ for p in papers:
441
+ audio_text = clean_text_for_tts(f"{p['title']} by {p['authors']}. {p['summary']}")
442
+ p['full_audio'], _ = await async_edge_tts_generate(audio_text, st.session_state['tts_voice'], p['authors'])
443
+ if p['full_audio']:
444
+ p['download_base64'] = get_download_link(p['full_audio'])
445
+
446
+ def save_vote(file, item, user_hash):
447
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
448
+ entry = f"[{timestamp}] {user_hash} voted for {item}"
449
+ try:
450
+ with open(file, 'a') as f:
451
+ f.write(f"{entry}\n")
452
+ with open(HISTORY_FILE, 'a') as f:
453
+ f.write(f"- {timestamp} - User {user_hash} voted for {item}\n")
454
+ return True
455
+ except Exception as e:
456
+ print(f"Vote save flop: {e}")
457
+ return False
458
+
459
+ def load_votes(file):
460
+ if not os.path.exists(file):
461
+ with open(file, 'w') as f:
462
+ f.write("# Vote Tally\n\nNo votes yet - get clicking! 🖱️\n")
463
+ try:
464
+ with open(file, 'r') as f:
465
+ lines = f.read().strip().split('\n')
466
+ votes = {}
467
+ for line in lines[2:]: # Skip header
468
+ if line.strip() and 'voted for' in line:
469
+ item = line.split('voted for ')[1]
470
+ votes[item] = votes.get(item, 0) + 1
471
+ return votes
472
+ except Exception as e:
473
+ print(f"Vote load oopsie: {e}")
474
+ return {}
475
+
476
+ def generate_user_hash():
477
+ if 'user_hash' not in st.session_state:
478
+ session_id = str(random.getrandbits(128))
479
+ hash_object = hashlib.md5(session_id.encode())
480
+ st.session_state['user_hash'] = hash_object.hexdigest()[:8]
481
+ return st.session_state['user_hash']
482
+
483
+ async def save_pasted_image(image, username):
484
+ img_hash = hashlib.md5(image.tobytes()).hexdigest()[:8]
485
+ if img_hash in st.session_state.image_hashes:
486
+ return None
487
+ timestamp = format_timestamp_prefix(username)
488
+ filename = f"{timestamp}-{img_hash}.png"
489
+ filepath = filename
490
+ image.save(filepath, "PNG")
491
+ st.session_state.image_hashes.add(img_hash)
492
+ return filepath
493
+
494
+ # 📦 Zip Files
495
+ def create_zip_of_files(md_files, mp3_files, png_files, mp4_files, query):
496
+ all_files = md_files + mp3_files + png_files + mp4_files
497
+ if not all_files:
498
+ return None
499
+ terms = get_high_info_terms(" ".join([open(f, 'r', encoding='utf-8').read() if f.endswith('.md') else os.path.splitext(os.path.basename(f))[0].replace('_', ' ') for f in all_files] + [query]), 5)
500
+ zip_name = f"{format_timestamp_prefix()}_{'-'.join(terms)[:20]}.zip"
501
+ with zipfile.ZipFile(zip_name, 'w') as z:
502
+ [z.write(f) for f in all_files]
503
+ return zip_name
504
+
505
+ # 🎮 Main Interface
506
+ def main():
507
+ init_session_state()
508
+ load_mp3_viewer()
509
+ saved_username = load_username()
510
+ if saved_username and saved_username in FUN_USERNAMES:
511
+ st.session_state.username = saved_username
512
+ if not st.session_state.username:
513
+ available = [n for n in FUN_USERNAMES if not any(f"{n} has joined" in l for l in asyncio.run(load_chat()).split('\n'))]
514
+ st.session_state.username = random.choice(available or list(FUN_USERNAMES.keys()))
515
+ st.session_state.tts_voice = FUN_USERNAMES[st.session_state.username]
516
+ asyncio.run(save_chat_entry("System 🌟", f"{st.session_state.username} has joined {START_ROOM}!"))
517
+ save_username(st.session_state.username)
518
+
519
+ st.title(f"{Site_Name} for {st.session_state.username}")
520
+ update_marquee_settings_ui()
521
+ display_marquee(f"🚀 Welcome to {START_ROOM} | 🤖 {st.session_state.username}", st.session_state['marquee_settings'], "welcome")
522
+
523
+ # Speech Component at Top Level
524
+ mycomponent = components.declare_component("mycomponent", path="mycomponent")
525
+ val = mycomponent(my_input_value="Hello from MyComponent")
526
+ if val:
527
+ val_stripped = val.replace('\\n', ' ')
528
+ edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100, key="speech_input")
529
+ run_option = st.selectbox("Model:", ["Chat", "Arxiv"], key="model_select")
530
+ col1, col2 = st.columns(2)
531
+ with col1:
532
+ st.checkbox("⚙ AutoRun", value=True, key="autorun") # Let Streamlit manage autorun state
533
+ with col2:
534
+ full_audio = st.checkbox("📚 FullAudio", value=False, key="full_audio")
535
+
536
+ input_changed = (val != st.session_state.old_val)
537
+
538
+ if st.session_state.autorun and input_changed:
539
+ st.session_state.old_val = val
540
+ st.session_state.last_query = edited_input
541
+ if run_option == "Chat":
542
+ asyncio.run(save_chat_entry(st.session_state.username, edited_input, True))
543
+ elif run_option == "Arxiv":
544
+ asyncio.run(perform_ai_lookup(edited_input, useArxiv=True, useArxivAudio=full_audio))
545
+ elif st.button("▶ Run", key="run_button"):
546
+ st.session_state.old_val = val
547
+ st.session_state.last_query = edited_input
548
+ if run_option == "Chat":
549
+ asyncio.run(save_chat_entry(st.session_state.username, edited_input, True))
550
+ elif run_option == "Arxiv":
551
+ asyncio.run(perform_ai_lookup(edited_input, useArxiv=True, useArxivAudio=full_audio))
552
+
553
+ tab_main = st.radio("Action:", ["🎤 Chat & Voice", "📸 Media", "🔍 ArXiv", "📚 PDF to Audio"], horizontal=True, key="tab_main")
554
+ useArxiv = st.checkbox("Search ArXiv", True, key="use_arxiv")
555
+ useArxivAudio = st.checkbox("ArXiv Audio", False, key="use_arxiv_audio")
556
+ # Use st.checkbox without reassigning to st.session_state
557
+ st.checkbox("Autosend Chat", value=True, key="autosend")
558
+ st.checkbox("Autosearch ArXiv", value=True, key="autosearch")
559
+
560
+ # 🎤 Chat & Voice
561
+ if tab_main == "🎤 Chat & Voice":
562
+ st.subheader(f"{START_ROOM} Chat 💬")
563
+ chat_content = asyncio.run(load_chat())
564
+ chat_container = st.container()
565
+ with chat_container:
566
+ lines = chat_content.split('\n')
567
+ for i, line in enumerate(lines):
568
+ if line.strip():
569
+ col1, col2 = st.columns([5, 1])
570
+ with col1:
571
+ st.markdown(line)
572
+ for mp3_name, mp3_path in st.session_state['mp3_files'].items():
573
+ if st.session_state.username in mp3_name and any(word in mp3_name for word in line.split()):
574
+ st.audio(mp3_path)
575
+ break
576
+ with col2:
577
+ if st.button(f"👍", key=f"chat_vote_{i}"):
578
+ user_hash = generate_user_hash()
579
+ save_vote(QUOTE_VOTES_FILE, line, user_hash)
580
+ st.session_state.timer_start = time.time()
581
+ save_username(st.session_state.username)
582
+ st.rerun()
583
+
584
+ message = st.text_input(f"Message as {st.session_state.username}", key="message_input")
585
+ paste_result = paste_image_button("📋 Paste Image or Text", key="paste_button_msg")
586
+ if paste_result.image_data is not None:
587
+ if isinstance(paste_result.image_data, str):
588
+ st.session_state.message_text = paste_result.image_data
589
+ message = st.text_input(f"Message as {st.session_state.username}", key="message_input_paste", value=st.session_state.message_text)
590
+ else:
591
+ st.image(paste_result.image_data, caption="Pasted Image")
592
+ filename = asyncio.run(save_pasted_image(paste_result.image_data, st.session_state.username))
593
+ if filename:
594
+ st.session_state.pasted_image_data = filename
595
+ if (message and message != st.session_state.last_message) or st.session_state.pasted_image_data:
596
+ st.session_state.last_message = message
597
+ if st.session_state.autosend or st.button("Send 🚀", key="send_button"): # Use st.session_state.autosend directly
598
+ if message.strip():
599
+ asyncio.run(save_chat_entry(st.session_state.username, message, True))
600
+ if st.session_state.pasted_image_data:
601
+ asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {st.session_state.pasted_image_data}"))
602
+ st.session_state.pasted_image_data = None
603
+ st.session_state.timer_start = time.time()
604
+ save_username(st.session_state.username)
605
+ st.rerun()
606
+
607
+ # 📸 Media
608
+ elif tab_main == "📸 Media":
609
+ st.header("📸 Media Gallery")
610
+ all_files = sorted(glob.glob("*.md") + glob.glob("*.mp3") + glob.glob("*.png") + glob.glob("*.mp4"), key=os.path.getmtime, reverse=True)
611
+ md_files = [f for f in all_files if f.endswith('.md')]
612
+ mp3_files = [f for f in all_files if f.endswith('.mp3')]
613
+ png_files = [f for f in all_files if f.endswith('.png')]
614
+ mp4_files = [f for f in all_files if f.endswith('.mp4')]
615
+
616
+ st.subheader("All Submitted Text")
617
+ all_md_content = concatenate_markdown_files()
618
+ st.markdown(all_md_content)
619
+
620
+ st.subheader("🎵 Audio (MP3)")
621
+ for mp3 in mp3_files:
622
+ with st.expander(os.path.basename(mp3)):
623
+ st.audio(mp3)
624
+ st.markdown(get_download_link(mp3, "mp3"), unsafe_allow_html=True)
625
+
626
+ st.subheader("🖼️ Images (PNG)")
627
+ for png in png_files:
628
+ with st.expander(os.path.basename(png)):
629
+ st.image(png, use_container_width=True)
630
+ st.markdown(get_download_link(png, "png"), unsafe_allow_html=True)
631
+
632
+ st.subheader("🎥 Videos (MP4)")
633
+ for mp4 in mp4_files:
634
+ with st.expander(os.path.basename(mp4)):
635
+ st.video(mp4)
636
+ st.markdown(get_download_link(mp4, "mp4"), unsafe_allow_html=True)
637
+
638
+ uploaded_file = st.file_uploader("Upload Media", type=['png', 'mp4', 'mp3'], key="media_upload")
639
+ if uploaded_file:
640
+ filename = f"{format_timestamp_prefix(st.session_state.username)}-{hashlib.md5(uploaded_file.getbuffer()).hexdigest()[:8]}.{uploaded_file.name.split('.')[-1]}"
641
+ with open(filename, 'wb') as f:
642
+ f.write(uploaded_file.getbuffer())
643
+ asyncio.run(save_chat_entry(st.session_state.username, f"Uploaded: {filename}"))
644
+ st.session_state.timer_start = time.time()
645
+ save_username(st.session_state.username)
646
+ st.rerun()
647
+
648
+ # 🔍 ArXiv
649
+ elif tab_main == "🔍 ArXiv":
650
+ st.subheader("🔍 Query ArXiv")
651
+ q = st.text_input("🔍 Query:", key="arxiv_query")
652
+ if q and q != st.session_state.last_query:
653
+ st.session_state.last_query = q
654
+ if st.session_state.autosearch or st.button("🔍 Run", key="arxiv_run"): # Use st.session_state.autosearch directly
655
+ result, papers = asyncio.run(perform_ai_lookup(q, useArxiv=useArxiv, useArxivAudio=useArxivAudio))
656
+ for i, p in enumerate(papers, 1):
657
+ with st.expander(f"{i}. 📄 {p['title']}"):
658
+ st.markdown(f"**{p['date']} | {p['title']}** — [Link]({p['url']})")
659
+ st.markdown(generate_5min_feature_markdown(p))
660
+ if p.get('full_audio'):
661
+ play_and_download_audio(p['full_audio'])
662
+
663
+ # 📚 PDF to Audio
664
+ elif tab_main == "📚 PDF to Audio":
665
+ audio_processor = AudioProcessor()
666
+ pdf_file = st.file_uploader("Choose PDF", "pdf", key="pdf_upload")
667
+ max_pages = st.slider('Pages', 1, 100, 10, key="pdf_pages")
668
+ if pdf_file:
669
+ with st.spinner('Processing...'):
670
+ texts, audios, total = process_pdf(pdf_file, max_pages, st.session_state['tts_voice'], audio_processor)
671
+ for i, text in enumerate(texts):
672
+ with st.expander(f"Page {i+1}"):
673
+ st.markdown(text)
674
+ while i not in audios:
675
+ time.sleep(0.1)
676
+ if audios.get(i):
677
+ st.audio(audios[i])
678
+ st.markdown(get_download_link(audios[i], "mp3"), unsafe_allow_html=True)
679
+ asyncio.run(save_chat_entry(st.session_state.username, f"PDF Page {i+1} converted to audio: {audios[i]}"))
680
+
681
+ # 🗂️ Sidebar with Dialog and Audio
682
+ st.sidebar.subheader("Voice Settings")
683
+ new_username = st.sidebar.selectbox("Change Name/Voice", list(FUN_USERNAMES.keys()), index=list(FUN_USERNAMES.keys()).index(st.session_state.username), key="username_select")
684
+ if new_username != st.session_state.username:
685
+ asyncio.run(save_chat_entry("System 🌟", f"{st.session_state.username} changed to {new_username}"))
686
+ st.session_state.username, st.session_state.tts_voice = new_username, FUN_USERNAMES[new_username]
687
+ st.session_state.timer_start = time.time()
688
+ save_username(st.session_state.username)
689
+ st.rerun()
690
+
691
+ st.sidebar.markdown("### 💬 Chat Dialog & Media")
692
+ chat_content = asyncio.run(load_chat())
693
+ lines = chat_content.split('\n')
694
+ all_files = sorted(glob.glob("*.md") + glob.glob("*.mp3") + glob.glob("*.png") + glob.glob("*.mp4"), key=os.path.getmtime, reverse=True)
695
+ for line in lines[-10:]:
696
+ if line.strip():
697
+ st.sidebar.markdown(f"**{line}**")
698
+ for f in all_files:
699
+ f_name = os.path.basename(f)
700
+ if st.session_state.username in f_name and any(word in f_name for word in line.split()):
701
+ if f.endswith('.mp3'):
702
+ st.sidebar.audio(f)
703
+ st.sidebar.markdown(get_download_link(f, "mp3"), unsafe_allow_html=True)
704
+ elif f.endswith('.png'):
705
+ st.sidebar.image(f, use_container_width=True)
706
+ st.sidebar.markdown(get_download_link(f, "png"), unsafe_allow_html=True)
707
+ elif f.endswith('.mp4'):
708
+ st.sidebar.video(f)
709
+ st.sidebar.markdown(get_download_link(f, "mp4"), unsafe_allow_html=True)
710
+ break
711
+
712
+ st.sidebar.subheader("Vote Totals")
713
+ chat_votes = load_votes(QUOTE_VOTES_FILE)
714
+ image_votes = load_votes(IMAGE_VOTES_FILE)
715
+ for item, count in chat_votes.items():
716
+ st.sidebar.write(f"{item}: {count} votes")
717
+ for image, count in image_votes.items():
718
+ st.sidebar.write(f"{image}: {count} votes")
719
+
720
+ md_files = [f for f in all_files if f.endswith('.md')]
721
+ mp3_files = [f for f in all_files if f.endswith('.mp3')]
722
+ png_files = [f for f in all_files if f.endswith('.png')]
723
+ mp4_files = [f for f in all_files if f.endswith('.mp4')]
724
+ st.sidebar.markdown("### 📂 File History")
725
+ for f in all_files[:10]:
726
+ st.sidebar.write(f"{FILE_EMOJIS.get(f.split('.')[-1], '📄')} {os.path.basename(f)}")
727
+ if st.sidebar.button("⬇️ Zip All", key="zip_all"):
728
+ zip_name = create_zip_of_files(md_files, mp3_files, png_files, mp4_files, "latest_query")
729
+ if zip_name:
730
+ st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)
731
+
732
+ # Start WebSocket server in a separate thread
733
+ if not st.session_state.server_running and not st.session_state.server_task:
734
+ st.session_state.server_task = threading.Thread(target=start_websocket_server, daemon=True)
735
+ st.session_state.server_task.start()
736
+
737
+ if __name__ == "__main__":
738
+ main()