awacke1 commited on
Commit
076f5ce
·
verified ·
1 Parent(s): 72e2bb4

Create backup10-almostperfect-60seccondtoallaudio20papers-app.py

Browse files
backup10-almostperfect-60seccondtoallaudio20papers-app.py ADDED
@@ -0,0 +1,774 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import anthropic, openai, base64, cv2, glob, json, math, os, pytz, random, re, requests, textract, time, zipfile
3
+ import plotly.graph_objects as go
4
+ import streamlit.components.v1 as components
5
+ from datetime import datetime
6
+ from audio_recorder_streamlit import audio_recorder
7
+ from bs4 import BeautifulSoup
8
+ from collections import defaultdict, deque
9
+ from dotenv import load_dotenv
10
+ from gradio_client import Client
11
+ from huggingface_hub import InferenceClient
12
+ from io import BytesIO
13
+ from PIL import Image
14
+ from PyPDF2 import PdfReader
15
+ from urllib.parse import quote
16
+ from xml.etree import ElementTree as ET
17
+ from openai import OpenAI
18
+ import extra_streamlit_components as stx
19
+ from streamlit.runtime.scriptrunner import get_script_run_ctx
20
+ import asyncio
21
+ import edge_tts
22
+
23
+ # 🎯 1. Core Configuration & Setup
24
+ st.set_page_config(
25
+ page_title="🚲TalkingAIResearcher🏆",
26
+ page_icon="🚲🏆",
27
+ layout="wide",
28
+ initial_sidebar_state="auto",
29
+ menu_items={
30
+ 'Get Help': 'https://huggingface.co/awacke1',
31
+ 'Report a bug': 'https://huggingface.co/spaces/awacke1',
32
+ 'About': "🚲TalkingAIResearcher🏆"
33
+ }
34
+ )
35
+ load_dotenv()
36
+
37
+ # Add available English voices for Edge TTS
38
+ EDGE_TTS_VOICES = [
39
+ "en-US-AriaNeural", # Default voice
40
+ "en-US-GuyNeural",
41
+ "en-US-JennyNeural",
42
+ "en-GB-SoniaNeural",
43
+ "en-GB-RyanNeural",
44
+ "en-AU-NatashaNeural",
45
+ "en-AU-WilliamNeural",
46
+ "en-CA-ClaraNeural",
47
+ "en-CA-LiamNeural"
48
+ ]
49
+
50
+ # Add this to your session state initialization section:
51
+ if 'tts_voice' not in st.session_state:
52
+ st.session_state['tts_voice'] = EDGE_TTS_VOICES[0] # Default voice
53
+
54
+
55
+ # 🔑 2. API Setup & Clients
56
+ openai_api_key = os.getenv('OPENAI_API_KEY', "")
57
+ anthropic_key = os.getenv('ANTHROPIC_API_KEY_3', "")
58
+ xai_key = os.getenv('xai',"")
59
+ if 'OPENAI_API_KEY' in st.secrets:
60
+ openai_api_key = st.secrets['OPENAI_API_KEY']
61
+ if 'ANTHROPIC_API_KEY' in st.secrets:
62
+ anthropic_key = st.secrets["ANTHROPIC_API_KEY"]
63
+
64
+ openai.api_key = openai_api_key
65
+ claude_client = anthropic.Anthropic(api_key=anthropic_key)
66
+ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_ORG_ID'))
67
+ HF_KEY = os.getenv('HF_KEY')
68
+ API_URL = os.getenv('API_URL')
69
+
70
+ # 📝 3. Session State Management
71
+ if 'transcript_history' not in st.session_state:
72
+ st.session_state['transcript_history'] = []
73
+ if 'chat_history' not in st.session_state:
74
+ st.session_state['chat_history'] = []
75
+ if 'openai_model' not in st.session_state:
76
+ st.session_state['openai_model'] = "gpt-4o-2024-05-13"
77
+ if 'messages' not in st.session_state:
78
+ st.session_state['messages'] = []
79
+ if 'last_voice_input' not in st.session_state:
80
+ st.session_state['last_voice_input'] = ""
81
+ if 'editing_file' not in st.session_state:
82
+ st.session_state['editing_file'] = None
83
+ if 'edit_new_name' not in st.session_state:
84
+ st.session_state['edit_new_name'] = ""
85
+ if 'edit_new_content' not in st.session_state:
86
+ st.session_state['edit_new_content'] = ""
87
+ if 'viewing_prefix' not in st.session_state:
88
+ st.session_state['viewing_prefix'] = None
89
+ if 'should_rerun' not in st.session_state:
90
+ st.session_state['should_rerun'] = False
91
+ if 'old_val' not in st.session_state:
92
+ st.session_state['old_val'] = None
93
+
94
+ # 🎨 4. Custom CSS
95
+ st.markdown("""
96
+ <style>
97
+ .main { background: linear-gradient(to right, #1a1a1a, #2d2d2d); color: #fff; }
98
+ .stMarkdown { font-family: 'Helvetica Neue', sans-serif; }
99
+ .stButton>button {
100
+ margin-right: 0.5rem;
101
+ }
102
+ </style>
103
+ """, unsafe_allow_html=True)
104
+
105
+ FILE_EMOJIS = {
106
+ "md": "📝",
107
+ "mp3": "🎵",
108
+ }
109
+
110
+ # 🧠 5. High-Information Content Extraction
111
+ def get_high_info_terms(text: str) -> list:
112
+ """Extract high-information terms from text, including key phrases."""
113
+ stop_words = set([
114
+ 'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with',
115
+ 'by', 'from', 'up', 'about', 'into', 'over', 'after', 'is', 'are', 'was', 'were',
116
+ 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would',
117
+ 'should', 'could', 'might', 'must', 'shall', 'can', 'may', 'this', 'that', 'these',
118
+ 'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which', 'who',
119
+ 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most',
120
+ 'other', 'some', 'such', 'than', 'too', 'very', 'just', 'there'
121
+ ])
122
+
123
+ key_phrases = [
124
+ 'artificial intelligence', 'machine learning', 'deep learning', 'neural network',
125
+ 'personal assistant', 'natural language', 'computer vision', 'data science',
126
+ 'reinforcement learning', 'knowledge graph', 'semantic search', 'time series',
127
+ 'large language model', 'transformer model', 'attention mechanism',
128
+ 'autonomous system', 'edge computing', 'quantum computing', 'blockchain technology',
129
+ 'cognitive science', 'human computer', 'decision making', 'arxiv search',
130
+ 'research paper', 'scientific study', 'empirical analysis'
131
+ ]
132
+
133
+ # Identify key phrases
134
+ #preserved_phrases = []
135
+ #lower_text = text.lower()
136
+ #for phrase in key_phrases:
137
+ # if phrase in lower_text:
138
+ # preserved_phrases.append(phrase)
139
+ # text = text.replace(phrase, '')
140
+
141
+ # Extract individual words
142
+ words = re.findall(r'\b\w+(?:-\w+)*\b', text)
143
+ high_info_words = [
144
+ word.lower() for word in words
145
+ if len(word) > 3
146
+ and word.lower() not in stop_words
147
+ and not word.isdigit()
148
+ and any(c.isalpha() for c in word)
149
+ ]
150
+
151
+ #all_terms = preserved_phrases + high_info_words
152
+ all_terms = high_info_words
153
+ seen = set()
154
+ unique_terms = []
155
+ for term in all_terms:
156
+ if term not in seen:
157
+ seen.add(term)
158
+ unique_terms.append(term)
159
+
160
+ max_terms = 5
161
+ return unique_terms[:max_terms]
162
+
163
+ def clean_text_for_filename(text: str) -> str:
164
+ """Remove punctuation and short filler words, return a compact string."""
165
+ text = text.lower()
166
+ text = re.sub(r'[^\w\s-]', '', text)
167
+ words = text.split()
168
+ stop_short = set(['the','and','for','with','this','that','from','just','very','then','been','only','also','about'])
169
+ filtered = [w for w in words if len(w)>3 and w not in stop_short]
170
+ return '_'.join(filtered)[:200]
171
+
172
+ # 📁 6. File Operations
173
+ def generate_filename(prompt, response, file_type="md"):
174
+ """
175
+ Generate filename with meaningful terms and short dense clips from prompt & response.
176
+ The filename should be about 150 chars total, include high-info terms, and a clipped snippet.
177
+ """
178
+ prefix = datetime.now().strftime("%y%m_%H%M") + "_"
179
+ combined = (prompt + " " + response).strip()
180
+ info_terms = get_high_info_terms(combined)
181
+
182
+ # Include a short snippet from prompt and response
183
+ snippet = (prompt[:100] + " " + response[:100]).strip()
184
+ snippet_cleaned = clean_text_for_filename(snippet)
185
+
186
+ # Combine info terms and snippet
187
+ name_parts = info_terms + [snippet_cleaned]
188
+ full_name = '_'.join(name_parts)
189
+
190
+ # Trim to ~150 chars
191
+ if len(full_name) > 150:
192
+ full_name = full_name[:150]
193
+
194
+ filename = f"{prefix}{full_name}.{file_type}"
195
+ return filename
196
+
197
+ def create_file(prompt, response, file_type="md"):
198
+ """Create file with intelligent naming"""
199
+ filename = generate_filename(prompt.strip(), response.strip(), file_type)
200
+ with open(filename, 'w', encoding='utf-8') as f:
201
+ f.write(prompt + "\n\n" + response)
202
+ return filename
203
+
204
+ def get_download_link(file):
205
+ """Generate download link for file"""
206
+ with open(file, "rb") as f:
207
+ b64 = base64.b64encode(f.read()).decode()
208
+ return f'<a href="data:file/zip;base64,{b64}" download="{os.path.basename(file)}">📂 Download {os.path.basename(file)}</a>'
209
+
210
+ # 🔊 7. Audio Processing
211
+ def clean_for_speech(text: str) -> str:
212
+ """Clean text for speech synthesis"""
213
+ text = text.replace("\n", " ")
214
+ text = text.replace("</s>", " ")
215
+ text = text.replace("#", "")
216
+ text = re.sub(r"\(https?:\/\/[^\)]+\)", "", text)
217
+ text = re.sub(r"\s+", " ", text).strip()
218
+ return text
219
+
220
+ @st.cache_resource
221
+ def speech_synthesis_html(result):
222
+ """Create HTML for speech synthesis"""
223
+ html_code = f"""
224
+ <html><body>
225
+ <script>
226
+ var msg = new SpeechSynthesisUtterance("{result.replace('"', '')}");
227
+ window.speechSynthesis.speak(msg);
228
+ </script>
229
+ </body></html>
230
+ """
231
+ components.html(html_code, height=0)
232
+
233
+ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0):
234
+ """Generate audio using Edge TTS"""
235
+ text = clean_for_speech(text)
236
+ if not text.strip():
237
+ return None
238
+ rate_str = f"{rate:+d}%"
239
+ pitch_str = f"{pitch:+d}Hz"
240
+ communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
241
+ out_fn = generate_filename(text, text, "mp3")
242
+ await communicate.save(out_fn)
243
+ return out_fn
244
+
245
+ def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
246
+ """Wrapper for edge TTS generation"""
247
+ return asyncio.run(edge_tts_generate_audio(text, voice, rate, pitch))
248
+
249
+ def play_and_download_audio(file_path):
250
+ """Play and provide download link for audio"""
251
+ if file_path and os.path.exists(file_path):
252
+ st.audio(file_path)
253
+ dl_link = f'<a href="data:audio/mpeg;base64,{base64.b64encode(open(file_path,"rb").read()).decode()}" download="{os.path.basename(file_path)}">Download {os.path.basename(file_path)}</a>'
254
+ st.markdown(dl_link, unsafe_allow_html=True)
255
+
256
+ # 🎬 8. Media Processing
257
+ def process_image(image_path, user_prompt):
258
+ """Process image with GPT-4V"""
259
+ with open(image_path, "rb") as imgf:
260
+ image_data = imgf.read()
261
+ b64img = base64.b64encode(image_data).decode("utf-8")
262
+ resp = openai_client.chat.completions.create(
263
+ model=st.session_state["openai_model"],
264
+ messages=[
265
+ {"role": "system", "content": "You are a helpful assistant."},
266
+ {"role": "user", "content": [
267
+ {"type": "text", "text": user_prompt},
268
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64img}"}}
269
+ ]}
270
+ ],
271
+ temperature=0.0,
272
+ )
273
+ return resp.choices[0].message.content
274
+
275
+ def process_audio(audio_path):
276
+ """Process audio with Whisper"""
277
+ with open(audio_path, "rb") as f:
278
+ transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
279
+ st.session_state.messages.append({"role": "user", "content": transcription.text})
280
+ return transcription.text
281
+
282
+ def process_video(video_path, seconds_per_frame=1):
283
+ """Extract frames from video"""
284
+ vid = cv2.VideoCapture(video_path)
285
+ total = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
286
+ fps = vid.get(cv2.CAP_PROP_FPS)
287
+ skip = int(fps*seconds_per_frame)
288
+ frames_b64 = []
289
+ for i in range(0, total, skip):
290
+ vid.set(cv2.CAP_PROP_POS_FRAMES, i)
291
+ ret, frame = vid.read()
292
+ if not ret:
293
+ break
294
+ _, buf = cv2.imencode(".jpg", frame)
295
+ frames_b64.append(base64.b64encode(buf).decode("utf-8"))
296
+ vid.release()
297
+ return frames_b64
298
+
299
+ def process_video_with_gpt(video_path, prompt):
300
+ """Analyze video frames with GPT-4V"""
301
+ frames = process_video(video_path)
302
+ resp = openai_client.chat.completions.create(
303
+ model=st.session_state["openai_model"],
304
+ messages=[
305
+ {"role":"system","content":"Analyze video frames."},
306
+ {"role":"user","content":[
307
+ {"type":"text","text":prompt},
308
+ *[{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{fr}"}} for fr in frames]
309
+ ]}
310
+ ]
311
+ )
312
+ return resp.choices[0].message.content
313
+
314
+ # 🤖 9. AI Model Integration
315
+
316
+ def save_full_transcript(query, text):
317
+ """Save full transcript of Arxiv results as a file."""
318
+ create_file(query, text, "md")
319
+
320
+
321
+
322
+ def parse_arxiv_refs(ref_text: str):
323
+ """
324
+ Parse papers by finding lines with two pipe characters as title lines.
325
+ Returns list of paper dictionaries with audio files.
326
+ """
327
+ if not ref_text:
328
+ return []
329
+
330
+ results = []
331
+ current_paper = {}
332
+ lines = ref_text.split('\n')
333
+
334
+ for i, line in enumerate(lines):
335
+ # Check if this is a title line (contains exactly 2 pipe characters)
336
+ if line.count('|') == 2:
337
+ # If we have a previous paper, add it to results
338
+ if current_paper:
339
+ results.append(current_paper)
340
+ if len(results) >= 20: # Limit to 20 papers
341
+ break
342
+
343
+ # Parse new paper header
344
+ try:
345
+ # Remove ** and split by |
346
+ header_parts = line.strip('* ').split('|')
347
+ date = header_parts[0].strip()
348
+ title = header_parts[1].strip()
349
+ # Extract arXiv URL if present
350
+ url_match = re.search(r'(https://arxiv.org/\S+)', line)
351
+ url = url_match.group(1) if url_match else f"paper_{len(results)}"
352
+
353
+ current_paper = {
354
+ 'date': date,
355
+ 'title': title,
356
+ 'url': url,
357
+ 'authors': '',
358
+ 'summary': '',
359
+ 'content_start': i + 1 # Track where content begins
360
+ }
361
+ except Exception as e:
362
+ st.warning(f"Error parsing paper header: {str(e)}")
363
+ current_paper = {}
364
+ continue
365
+
366
+ # If we have a current paper and this isn't a title line, add to content
367
+ elif current_paper:
368
+ if not current_paper['authors']: # First line after title is authors
369
+ current_paper['authors'] = line.strip('* ')
370
+ else: # Rest is summary
371
+ if current_paper['summary']:
372
+ current_paper['summary'] += ' ' + line.strip()
373
+ else:
374
+ current_paper['summary'] = line.strip()
375
+
376
+ # Don't forget the last paper
377
+ if current_paper:
378
+ results.append(current_paper)
379
+
380
+ return results[:20] # Ensure we return maximum 20 papers
381
+
382
+
383
+
384
+ def create_paper_audio_files(papers):
385
+ """
386
+ Create audio files for each paper's content and add file paths to paper dict.
387
+ Only generates full audio file since it includes the title.
388
+ """
389
+ for paper in papers:
390
+ try:
391
+ # Generate audio for full content only
392
+ full_text = f"{paper['title']} by {paper['authors']}. {paper['summary']}"
393
+ full_text = clean_for_speech(full_text)
394
+ full_file = speak_with_edge_tts(full_text, voice=st.session_state['tts_voice'])
395
+ paper['full_audio'] = full_file
396
+
397
+ except Exception as e:
398
+ st.warning(f"Error generating audio for paper {paper['title']}: {str(e)}")
399
+ paper['full_audio'] = None
400
+
401
+
402
+ def display_papers(papers):
403
+ """
404
+ Display papers with their audio controls using URLs as unique keys.
405
+ """
406
+ st.write("## Research Papers")
407
+
408
+ for idx, paper in enumerate(papers):
409
+ with st.expander(f"📄 {paper['title']}", expanded=True):
410
+ st.markdown(f"**{paper['date']} | {paper['title']} | ⬇️**")
411
+ st.markdown(f"*{paper['authors']}*")
412
+ st.markdown(paper['summary'])
413
+
414
+ # Single audio control for full content
415
+ if paper.get('full_audio'):
416
+ st.write("📚 Paper Audio")
417
+ st.audio(paper['full_audio'])
418
+
419
+
420
+ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False,
421
+ titles_summary=True, full_audio=False):
422
+ """Perform Arxiv search with audio generation per paper."""
423
+ start = time.time()
424
+
425
+ # Query the HF RAG pipeline
426
+ client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
427
+ refs = client.predict(q, 20, "Semantic Search",
428
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
429
+ api_name="/update_with_rag_md")[0]
430
+ r2 = client.predict(q, "mistralai/Mixtral-8x7B-Instruct-v0.1",
431
+ True, api_name="/ask_llm")
432
+
433
+ # Combine for final text output
434
+ result = f"### 🔎 {q}\n\n{r2}\n\n{refs}"
435
+ st.markdown(result)
436
+
437
+ # Parse and process papers
438
+ papers = parse_arxiv_refs(refs)
439
+ if papers:
440
+ create_paper_audio_files(papers)
441
+ display_papers(papers)
442
+ else:
443
+ st.warning("No papers found in the response.")
444
+
445
+ elapsed = time.time()-start
446
+ st.write(f"**Total Elapsed:** {elapsed:.2f} s")
447
+
448
+ # Save full transcript
449
+ create_file(q, result, "md")
450
+ return result
451
+
452
+
453
+
454
+
455
+ def process_with_gpt(text):
456
+ """Process text with GPT-4"""
457
+ if not text:
458
+ return
459
+ st.session_state.messages.append({"role":"user","content":text})
460
+ with st.chat_message("user"):
461
+ st.markdown(text)
462
+ with st.chat_message("assistant"):
463
+ c = openai_client.chat.completions.create(
464
+ model=st.session_state["openai_model"],
465
+ messages=st.session_state.messages,
466
+ stream=False
467
+ )
468
+ ans = c.choices[0].message.content
469
+ st.write("GPT-4o: " + ans)
470
+ create_file(text, ans, "md")
471
+ st.session_state.messages.append({"role":"assistant","content":ans})
472
+ return ans
473
+
474
+ def process_with_claude(text):
475
+ """Process text with Claude"""
476
+ if not text:
477
+ return
478
+ with st.chat_message("user"):
479
+ st.markdown(text)
480
+ with st.chat_message("assistant"):
481
+ r = claude_client.messages.create(
482
+ model="claude-3-sonnet-20240229",
483
+ max_tokens=1000,
484
+ messages=[{"role":"user","content":text}]
485
+ )
486
+ ans = r.content[0].text
487
+ st.write("Claude-3.5: " + ans)
488
+ create_file(text, ans, "md")
489
+ st.session_state.chat_history.append({"user":text,"claude":ans})
490
+ return ans
491
+
492
+ # 📂 10. File Management
493
+ def create_zip_of_files(md_files, mp3_files):
494
+ """Create zip with intelligent naming"""
495
+ md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
496
+ all_files = md_files + mp3_files
497
+ if not all_files:
498
+ return None
499
+
500
+ # Collect content for high-info term extraction
501
+ all_content = []
502
+ for f in all_files:
503
+ if f.endswith('.md'):
504
+ with open(f, 'r', encoding='utf-8') as file:
505
+ all_content.append(file.read())
506
+ elif f.endswith('.mp3'):
507
+ all_content.append(os.path.basename(f))
508
+
509
+ combined_content = " ".join(all_content)
510
+ info_terms = get_high_info_terms(combined_content)
511
+
512
+ timestamp = datetime.now().strftime("%y%m_%H%M")
513
+ name_text = '_'.join(term.replace(' ', '-') for term in info_terms[:3])
514
+ zip_name = f"{timestamp}_{name_text}.zip"
515
+
516
+ with zipfile.ZipFile(zip_name,'w') as z:
517
+ for f in all_files:
518
+ z.write(f)
519
+
520
+ return zip_name
521
+
522
+ def load_files_for_sidebar():
523
+ """Load and group files for sidebar display"""
524
+ md_files = glob.glob("*.md")
525
+ mp3_files = glob.glob("*.mp3")
526
+
527
+ md_files = [f for f in md_files if os.path.basename(f).lower() != 'readme.md']
528
+ all_files = md_files + mp3_files
529
+
530
+ groups = defaultdict(list)
531
+ for f in all_files:
532
+ fname = os.path.basename(f)
533
+ prefix = fname[:10]
534
+ groups[prefix].append(f)
535
+
536
+ for prefix in groups:
537
+ groups[prefix].sort(key=lambda x: os.path.getmtime(x), reverse=True)
538
+
539
+ sorted_prefixes = sorted(groups.keys(),
540
+ key=lambda pre: max(os.path.getmtime(x) for x in groups[pre]),
541
+ reverse=True)
542
+ return groups, sorted_prefixes
543
+
544
+ def extract_keywords_from_md(files):
545
+ """Extract keywords from markdown files"""
546
+ text = ""
547
+ for f in files:
548
+ if f.endswith(".md"):
549
+ c = open(f,'r',encoding='utf-8').read()
550
+ text += " " + c
551
+ return get_high_info_terms(text)
552
+
553
+ def display_file_manager_sidebar(groups, sorted_prefixes):
554
+ """Display file manager in sidebar"""
555
+ st.sidebar.title("🎵 Audio & Docs Manager")
556
+
557
+ all_md = []
558
+ all_mp3 = []
559
+ for prefix in groups:
560
+ for f in groups[prefix]:
561
+ if f.endswith(".md"):
562
+ all_md.append(f)
563
+ elif f.endswith(".mp3"):
564
+ all_mp3.append(f)
565
+
566
+ top_bar = st.sidebar.columns(3)
567
+ with top_bar[0]:
568
+ if st.button("🗑 DelAllMD"):
569
+ for f in all_md:
570
+ os.remove(f)
571
+ st.session_state.should_rerun = True
572
+ with top_bar[1]:
573
+ if st.button("🗑 DelAllMP3"):
574
+ for f in all_mp3:
575
+ os.remove(f)
576
+ st.session_state.should_rerun = True
577
+ with top_bar[2]:
578
+ if st.button("⬇️ ZipAll"):
579
+ z = create_zip_of_files(all_md, all_mp3)
580
+ if z:
581
+ st.sidebar.markdown(get_download_link(z),unsafe_allow_html=True)
582
+
583
+ for prefix in sorted_prefixes:
584
+ files = groups[prefix]
585
+ kw = extract_keywords_from_md(files)
586
+ keywords_str = " ".join(kw) if kw else "No Keywords"
587
+ with st.sidebar.expander(f"{prefix} Files ({len(files)}) - KW: {keywords_str}", expanded=True):
588
+ c1,c2 = st.columns(2)
589
+ with c1:
590
+ if st.button("👀ViewGrp", key="view_group_"+prefix):
591
+ st.session_state.viewing_prefix = prefix
592
+ with c2:
593
+ if st.button("🗑DelGrp", key="del_group_"+prefix):
594
+ for f in files:
595
+ os.remove(f)
596
+ st.success(f"Deleted group {prefix}!")
597
+ st.session_state.should_rerun = True
598
+
599
+ for f in files:
600
+ fname = os.path.basename(f)
601
+ ctime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
602
+ st.write(f"**{fname}** - {ctime}")
603
+
604
+ # 🎯 11. Main Application
605
+ def main():
606
+ st.sidebar.markdown("### 🚲BikeAI🏆 Multi-Agent Research")
607
+
608
+ # Add voice selector to sidebar
609
+ st.sidebar.markdown("### 🎤 Voice Settings")
610
+ selected_voice = st.sidebar.selectbox(
611
+ "Select TTS Voice:",
612
+ options=EDGE_TTS_VOICES,
613
+ index=EDGE_TTS_VOICES.index(st.session_state['tts_voice'])
614
+ )
615
+
616
+ # Update session state if voice changes
617
+ if selected_voice != st.session_state['tts_voice']:
618
+ st.session_state['tts_voice'] = selected_voice
619
+ st.rerun()
620
+
621
+
622
+ tab_main = st.radio("Action:",["🎤 Voice","📸 Media","🔍 ArXiv","📝 Editor"],horizontal=True)
623
+
624
+ mycomponent = components.declare_component("mycomponent", path="mycomponent")
625
+ val = mycomponent(my_input_value="Hello")
626
+
627
+ # Show input in a text box for editing if detected
628
+ if val:
629
+ val_stripped = val.replace('\\n', ' ')
630
+ edited_input = st.text_area("✏️ Edit Input:", value=val_stripped, height=100)
631
+ #edited_input = edited_input.replace('\n', ' ')
632
+
633
+ run_option = st.selectbox("Model:", ["Arxiv", "GPT-4o", "Claude-3.5"])
634
+ col1, col2 = st.columns(2)
635
+ with col1:
636
+ autorun = st.checkbox("⚙ AutoRun", value=True)
637
+ with col2:
638
+ full_audio = st.checkbox("📚FullAudio", value=False,
639
+ help="Generate full audio response")
640
+
641
+ input_changed = (val != st.session_state.old_val)
642
+
643
+ if autorun and input_changed:
644
+ st.session_state.old_val = val
645
+ if run_option == "Arxiv":
646
+ perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
647
+ titles_summary=True, full_audio=full_audio)
648
+ else:
649
+ if run_option == "GPT-4o":
650
+ process_with_gpt(edited_input)
651
+ elif run_option == "Claude-3.5":
652
+ process_with_claude(edited_input)
653
+ else:
654
+ if st.button("▶ Run"):
655
+ st.session_state.old_val = val
656
+ if run_option == "Arxiv":
657
+ perform_ai_lookup(edited_input, vocal_summary=True, extended_refs=False,
658
+ titles_summary=True, full_audio=full_audio)
659
+ else:
660
+ if run_option == "GPT-4o":
661
+ process_with_gpt(edited_input)
662
+ elif run_option == "Claude-3.5":
663
+ process_with_claude(edited_input)
664
+
665
+ if tab_main == "🔍 ArXiv":
666
+ st.subheader("🔍 Query ArXiv")
667
+ q = st.text_input("🔍 Query:")
668
+
669
+ st.markdown("### 🎛 Options")
670
+ vocal_summary = st.checkbox("🎙ShortAudio", value=True)
671
+ extended_refs = st.checkbox("📜LongRefs", value=False)
672
+ titles_summary = st.checkbox("🔖TitlesOnly", value=True)
673
+ full_audio = st.checkbox("📚FullAudio", value=False,
674
+ help="Full audio of results")
675
+ full_transcript = st.checkbox("🧾FullTranscript", value=False,
676
+ help="Generate a full transcript file")
677
+
678
+ if q and st.button("🔍Run"):
679
+ result = perform_ai_lookup(q, vocal_summary=vocal_summary, extended_refs=extended_refs,
680
+ titles_summary=titles_summary, full_audio=full_audio)
681
+ if full_transcript:
682
+ save_full_transcript(q, result)
683
+
684
+ st.markdown("### Change Prompt & Re-Run")
685
+ q_new = st.text_input("🔄 Modify Query:")
686
+ if q_new and st.button("🔄 Re-Run with Modified Query"):
687
+ result = perform_ai_lookup(q_new, vocal_summary=vocal_summary, extended_refs=extended_refs,
688
+ titles_summary=titles_summary, full_audio=full_audio)
689
+ if full_transcript:
690
+ save_full_transcript(q_new, result)
691
+
692
+ elif tab_main == "🎤 Voice":
693
+ st.subheader("🎤 Voice Input")
694
+ user_text = st.text_area("💬 Message:", height=100)
695
+ user_text = user_text.strip().replace('\n', ' ')
696
+ if st.button("📨 Send"):
697
+ process_with_gpt(user_text)
698
+ st.subheader("📜 Chat History")
699
+ t1,t2=st.tabs(["Claude History","GPT-4o History"])
700
+ with t1:
701
+ for c in st.session_state.chat_history:
702
+ st.write("**You:**", c["user"])
703
+ st.write("**Claude:**", c["claude"])
704
+ with t2:
705
+ for m in st.session_state.messages:
706
+ with st.chat_message(m["role"]):
707
+ st.markdown(m["content"])
708
+
709
+ elif tab_main == "📸 Media":
710
+ st.header("📸 Images & 🎥 Videos")
711
+ tabs = st.tabs(["🖼 Images", "🎥 Video"])
712
+ with tabs[0]:
713
+ imgs = glob.glob("*.png")+glob.glob("*.jpg")
714
+ if imgs:
715
+ c = st.slider("Cols",1,5,3)
716
+ cols = st.columns(c)
717
+ for i,f in enumerate(imgs):
718
+ with cols[i%c]:
719
+ st.image(Image.open(f),use_container_width=True)
720
+ if st.button(f"👀 Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
721
+ a = process_image(f,"Describe this image.")
722
+ st.markdown(a)
723
+ else:
724
+ st.write("No images found.")
725
+ with tabs[1]:
726
+ vids = glob.glob("*.mp4")
727
+ if vids:
728
+ for v in vids:
729
+ with st.expander(f"🎥 {os.path.basename(v)}"):
730
+ st.video(v)
731
+ if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
732
+ a = process_video_with_gpt(v,"Describe video.")
733
+ st.markdown(a)
734
+ else:
735
+ st.write("No videos found.")
736
+
737
+ elif tab_main == "📝 Editor":
738
+ if getattr(st.session_state,'current_file',None):
739
+ st.subheader(f"Editing: {st.session_state.current_file}")
740
+ new_text = st.text_area("✏️ Content:", st.session_state.file_content, height=300)
741
+ if st.button("💾 Save"):
742
+ with open(st.session_state.current_file,'w',encoding='utf-8') as f:
743
+ f.write(new_text)
744
+ st.success("Updated!")
745
+ st.session_state.should_rerun = True
746
+ else:
747
+ st.write("Select a file from the sidebar to edit.")
748
+
749
+ groups, sorted_prefixes = load_files_for_sidebar()
750
+ display_file_manager_sidebar(groups, sorted_prefixes)
751
+
752
+ if st.session_state.viewing_prefix and st.session_state.viewing_prefix in groups:
753
+ st.write("---")
754
+ st.write(f"**Viewing Group:** {st.session_state.viewing_prefix}")
755
+ for f in groups[st.session_state.viewing_prefix]:
756
+ fname = os.path.basename(f)
757
+ ext = os.path.splitext(fname)[1].lower().strip('.')
758
+ st.write(f"### {fname}")
759
+ if ext == "md":
760
+ content = open(f,'r',encoding='utf-8').read()
761
+ st.markdown(content)
762
+ elif ext == "mp3":
763
+ st.audio(f)
764
+ else:
765
+ st.markdown(get_download_link(f), unsafe_allow_html=True)
766
+ if st.button("❌ Close"):
767
+ st.session_state.viewing_prefix = None
768
+
769
+ if st.session_state.should_rerun:
770
+ st.session_state.should_rerun = False
771
+ st.rerun()
772
+
773
+ if __name__=="__main__":
774
+ main()