awacke1 commited on
Commit
030c313
·
verified ·
1 Parent(s): 014b782

Create backup8.app.py

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