Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ 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 |
# π§ Config & Setup
|
24 |
st.set_page_config(
|
@@ -45,6 +45,10 @@ st.session_state.setdefault('chat_history', [])
|
|
45 |
st.session_state.setdefault('openai_model', "gpt-4o-2024-05-13")
|
46 |
st.session_state.setdefault('messages', [])
|
47 |
st.session_state.setdefault('last_voice_input', "")
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# π¨ Minimal Custom CSS
|
50 |
st.markdown("""
|
@@ -57,7 +61,11 @@ st.markdown("""
|
|
57 |
</style>
|
58 |
""", unsafe_allow_html=True)
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
def generate_filename(prompt, file_type="md"):
|
62 |
ctz = pytz.timezone('US/Central')
|
63 |
date_str = datetime.now(ctz).strftime("%m%d_%H%M")
|
@@ -68,7 +76,7 @@ def generate_filename(prompt, file_type="md"):
|
|
68 |
def create_file(filename, prompt, response):
|
69 |
with open(filename, 'w', encoding='utf-8') as f:
|
70 |
f.write(prompt + "\n\n" + response)
|
71 |
-
st.
|
72 |
|
73 |
def get_download_link(file):
|
74 |
with open(file, "rb") as f:
|
@@ -87,8 +95,6 @@ def speech_synthesis_html(result):
|
|
87 |
"""
|
88 |
components.html(html_code, height=0)
|
89 |
|
90 |
-
#------------add EdgeTTS
|
91 |
-
# --- NEW FUNCTIONS FOR EDGE TTS ---
|
92 |
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
93 |
if not text.strip():
|
94 |
return None
|
@@ -97,7 +103,7 @@ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=
|
|
97 |
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
98 |
out_fn = generate_filename(text,"mp3")
|
99 |
await communicate.save(out_fn)
|
100 |
-
st.
|
101 |
return out_fn
|
102 |
|
103 |
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
@@ -129,7 +135,7 @@ def process_audio(audio_path):
|
|
129 |
with open(audio_path, "rb") as f:
|
130 |
transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
|
131 |
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
132 |
-
st.
|
133 |
return transcription.text
|
134 |
|
135 |
def process_video(video_path, seconds_per_frame=1):
|
@@ -182,26 +188,21 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary
|
|
182 |
|
183 |
st.markdown(result)
|
184 |
|
185 |
-
#
|
186 |
if vocal_summary:
|
187 |
-
start_main_part = time.time()
|
188 |
audio_file_main = speak_with_edge_tts(r2, voice="en-US-AriaNeural", rate=0, pitch=0)
|
189 |
st.write("### ποΈ Vocal Summary (Short Answer)")
|
190 |
play_and_download_audio(audio_file_main)
|
191 |
-
st.write(f"**Elapsed (Short Answer):** {time.time() - start_main_part:.2f} s")
|
192 |
|
193 |
-
# Extended
|
194 |
if extended_refs:
|
195 |
-
start_refs_part = time.time()
|
196 |
summaries_text = "Here are the summaries from the references: " + refs.replace('"','')
|
197 |
audio_file_refs = speak_with_edge_tts(summaries_text, voice="en-US-AriaNeural", rate=0, pitch=0)
|
198 |
st.write("### π Extended References & Summaries")
|
199 |
play_and_download_audio(audio_file_refs)
|
200 |
-
st.write(f"**Elapsed (Extended References):** {time.time() - start_refs_part:.2f} s")
|
201 |
|
202 |
-
#
|
203 |
if titles_summary:
|
204 |
-
start_titles_part = time.time()
|
205 |
titles = []
|
206 |
for line in refs.split('\n'):
|
207 |
m = re.search(r"\[([^\]]+)\]", line)
|
@@ -212,7 +213,6 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary
|
|
212 |
audio_file_titles = speak_with_edge_tts(titles_text, voice="en-US-AriaNeural", rate=0, pitch=0)
|
213 |
st.write("### π Paper Titles")
|
214 |
play_and_download_audio(audio_file_titles)
|
215 |
-
st.write(f"**Elapsed (Titles):** {time.time() - start_titles_part:.2f} s")
|
216 |
|
217 |
elapsed = time.time()-start
|
218 |
st.write(f"**Total Elapsed:** {elapsed:.2f} s")
|
@@ -235,8 +235,7 @@ def process_with_gpt(text):
|
|
235 |
st.write("GPT-4o: " + ans)
|
236 |
create_file(generate_filename(text,"md"),text,ans)
|
237 |
st.session_state.messages.append({"role":"assistant","content":ans})
|
238 |
-
|
239 |
-
return ans
|
240 |
|
241 |
def process_with_claude(text):
|
242 |
if not text: return
|
@@ -252,18 +251,17 @@ def process_with_claude(text):
|
|
252 |
st.write("Claude: " + ans)
|
253 |
create_file(generate_filename(text,"md"),text,ans)
|
254 |
st.session_state.chat_history.append({"user":text,"claude":ans})
|
255 |
-
|
256 |
-
return ans
|
257 |
|
258 |
def create_zip_of_files():
|
259 |
-
md_files = glob.glob("
|
260 |
-
mp3_files = glob.glob("
|
261 |
all_files = md_files + mp3_files
|
262 |
zip_name = "all_files.zip"
|
263 |
with zipfile.ZipFile(zip_name,'w') as z:
|
264 |
for f in all_files:
|
265 |
z.write(f)
|
266 |
-
st.
|
267 |
return zip_name
|
268 |
|
269 |
def get_media_html(p,typ="video",w="100%"):
|
@@ -273,109 +271,109 @@ def get_media_html(p,typ="video",w="100%"):
|
|
273 |
else:
|
274 |
return f'<audio controls style="width:{w};"><source src="data:audio/mpeg;base64,{d}" type="audio/mpeg"></audio>'
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
with
|
325 |
-
|
326 |
-
|
327 |
-
st.
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
with
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
if st.
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
# Update content
|
371 |
-
with open(
|
372 |
-
f.write(
|
373 |
-
|
374 |
-
st.experimental_rerun()
|
375 |
-
if st.sidebar.button("Cancel"):
|
376 |
-
del st.session_state.editing_md
|
377 |
st.experimental_rerun()
|
378 |
-
|
|
|
|
|
|
|
379 |
|
380 |
def main():
|
381 |
st.sidebar.markdown("### π²BikeAIπ Multi-Agent Research AI")
|
@@ -469,36 +467,42 @@ def main():
|
|
469 |
st.header("π¬ Media Gallery - Images and Videos")
|
470 |
tabs = st.tabs(["πΌοΈ Images", "π₯ Video"])
|
471 |
with tabs[0]:
|
472 |
-
imgs = glob.glob("
|
473 |
if imgs:
|
474 |
c = st.slider("Cols",1,5,3)
|
475 |
cols = st.columns(c)
|
476 |
for i,f in enumerate(imgs):
|
477 |
with cols[i%c]:
|
478 |
st.image(Image.open(f),use_container_width=True)
|
479 |
-
if st.button(f"π Analyze {os.path.basename(f)}"
|
480 |
a = process_image(f,"Describe this image.")
|
481 |
st.markdown(a)
|
482 |
else:
|
483 |
st.write("No images found.")
|
484 |
with tabs[1]:
|
485 |
-
vids = glob.glob("
|
486 |
if vids:
|
487 |
for v in vids:
|
488 |
with st.expander(f"π₯ {os.path.basename(v)}"):
|
489 |
st.markdown(get_media_html(v,"video"),unsafe_allow_html=True)
|
490 |
-
if st.button(f"Analyze {os.path.basename(v)}"
|
491 |
a = process_video_with_gpt(v,"Describe video.")
|
492 |
st.markdown(a)
|
493 |
else:
|
494 |
st.write("No videos found.")
|
495 |
|
496 |
elif tab_main == "π File Editor":
|
497 |
-
st.
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
502 |
|
503 |
if __name__=="__main__":
|
504 |
main()
|
|
|
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 |
# π§ Config & Setup
|
24 |
st.set_page_config(
|
|
|
45 |
st.session_state.setdefault('openai_model', "gpt-4o-2024-05-13")
|
46 |
st.session_state.setdefault('messages', [])
|
47 |
st.session_state.setdefault('last_voice_input', "")
|
48 |
+
# For editing .md files
|
49 |
+
st.session_state.setdefault('editing_file', None)
|
50 |
+
st.session_state.setdefault('edit_new_name', "")
|
51 |
+
st.session_state.setdefault('edit_new_content', "")
|
52 |
|
53 |
# π¨ Minimal Custom CSS
|
54 |
st.markdown("""
|
|
|
61 |
</style>
|
62 |
""", unsafe_allow_html=True)
|
63 |
|
64 |
+
FILE_EMOJIS = {
|
65 |
+
"md": "π",
|
66 |
+
"mp3": "π΅",
|
67 |
+
}
|
68 |
+
|
69 |
def generate_filename(prompt, file_type="md"):
|
70 |
ctz = pytz.timezone('US/Central')
|
71 |
date_str = datetime.now(ctz).strftime("%m%d_%H%M")
|
|
|
76 |
def create_file(filename, prompt, response):
|
77 |
with open(filename, 'w', encoding='utf-8') as f:
|
78 |
f.write(prompt + "\n\n" + response)
|
79 |
+
st.rerun()
|
80 |
|
81 |
def get_download_link(file):
|
82 |
with open(file, "rb") as f:
|
|
|
95 |
"""
|
96 |
components.html(html_code, height=0)
|
97 |
|
|
|
|
|
98 |
async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
99 |
if not text.strip():
|
100 |
return None
|
|
|
103 |
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
104 |
out_fn = generate_filename(text,"mp3")
|
105 |
await communicate.save(out_fn)
|
106 |
+
st.rerun()
|
107 |
return out_fn
|
108 |
|
109 |
def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
|
|
|
135 |
with open(audio_path, "rb") as f:
|
136 |
transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
|
137 |
st.session_state.messages.append({"role": "user", "content": transcription.text})
|
138 |
+
st.rerun()
|
139 |
return transcription.text
|
140 |
|
141 |
def process_video(video_path, seconds_per_frame=1):
|
|
|
188 |
|
189 |
st.markdown(result)
|
190 |
|
191 |
+
# Vocal summary
|
192 |
if vocal_summary:
|
|
|
193 |
audio_file_main = speak_with_edge_tts(r2, voice="en-US-AriaNeural", rate=0, pitch=0)
|
194 |
st.write("### ποΈ Vocal Summary (Short Answer)")
|
195 |
play_and_download_audio(audio_file_main)
|
|
|
196 |
|
197 |
+
# Extended refs
|
198 |
if extended_refs:
|
|
|
199 |
summaries_text = "Here are the summaries from the references: " + refs.replace('"','')
|
200 |
audio_file_refs = speak_with_edge_tts(summaries_text, voice="en-US-AriaNeural", rate=0, pitch=0)
|
201 |
st.write("### π Extended References & Summaries")
|
202 |
play_and_download_audio(audio_file_refs)
|
|
|
203 |
|
204 |
+
# Titles only
|
205 |
if titles_summary:
|
|
|
206 |
titles = []
|
207 |
for line in refs.split('\n'):
|
208 |
m = re.search(r"\[([^\]]+)\]", line)
|
|
|
213 |
audio_file_titles = speak_with_edge_tts(titles_text, voice="en-US-AriaNeural", rate=0, pitch=0)
|
214 |
st.write("### π Paper Titles")
|
215 |
play_and_download_audio(audio_file_titles)
|
|
|
216 |
|
217 |
elapsed = time.time()-start
|
218 |
st.write(f"**Total Elapsed:** {elapsed:.2f} s")
|
|
|
235 |
st.write("GPT-4o: " + ans)
|
236 |
create_file(generate_filename(text,"md"),text,ans)
|
237 |
st.session_state.messages.append({"role":"assistant","content":ans})
|
238 |
+
return ans
|
|
|
239 |
|
240 |
def process_with_claude(text):
|
241 |
if not text: return
|
|
|
251 |
st.write("Claude: " + ans)
|
252 |
create_file(generate_filename(text,"md"),text,ans)
|
253 |
st.session_state.chat_history.append({"user":text,"claude":ans})
|
254 |
+
return ans
|
|
|
255 |
|
256 |
def create_zip_of_files():
|
257 |
+
md_files = glob.glob("*.md")
|
258 |
+
mp3_files = glob.glob("*.mp3")
|
259 |
all_files = md_files + mp3_files
|
260 |
zip_name = "all_files.zip"
|
261 |
with zipfile.ZipFile(zip_name,'w') as z:
|
262 |
for f in all_files:
|
263 |
z.write(f)
|
264 |
+
st.rerun()
|
265 |
return zip_name
|
266 |
|
267 |
def get_media_html(p,typ="video",w="100%"):
|
|
|
271 |
else:
|
272 |
return f'<audio controls style="width:{w};"><source src="data:audio/mpeg;base64,{d}" type="audio/mpeg"></audio>'
|
273 |
|
274 |
+
def display_file_manager():
|
275 |
+
st.sidebar.title("π΅ Audio & Document Manager")
|
276 |
+
st.sidebar.markdown("Lists .mp3 and .md files with emojis and sorted by file type count and mod time.")
|
277 |
+
|
278 |
+
# Gather all md and mp3 files
|
279 |
+
md_files = glob.glob("*.md")
|
280 |
+
mp3_files = glob.glob("*.mp3")
|
281 |
+
|
282 |
+
# Group by extension
|
283 |
+
files_by_ext = defaultdict(list)
|
284 |
+
for f in md_files:
|
285 |
+
ext = "md"
|
286 |
+
files_by_ext[ext].append(f)
|
287 |
+
for f in mp3_files:
|
288 |
+
ext = "mp3"
|
289 |
+
files_by_ext[ext].append(f)
|
290 |
+
|
291 |
+
# Sort each extension group by modification time descending
|
292 |
+
for ext in files_by_ext:
|
293 |
+
files_by_ext[ext].sort(key=lambda x: os.path.getmtime(x), reverse=True)
|
294 |
+
|
295 |
+
# Sort extensions by number of files descending
|
296 |
+
sorted_ext = sorted(files_by_ext.keys(), key=lambda x: len(files_by_ext[x]), reverse=True)
|
297 |
+
|
298 |
+
# Delete all buttons
|
299 |
+
del_col = st.sidebar.columns(2)
|
300 |
+
with del_col[0]:
|
301 |
+
if st.button("π Delete All MD"):
|
302 |
+
for f in md_files:
|
303 |
+
os.remove(f)
|
304 |
+
st.rerun()
|
305 |
+
with del_col[1]:
|
306 |
+
if st.button("π Delete All MP3"):
|
307 |
+
for f in mp3_files:
|
308 |
+
os.remove(f)
|
309 |
+
st.rerun()
|
310 |
+
|
311 |
+
# Show groups
|
312 |
+
for ext in sorted_ext:
|
313 |
+
emoji = FILE_EMOJIS.get(ext, "π¦")
|
314 |
+
count = len(files_by_ext[ext])
|
315 |
+
with st.sidebar.expander(f"{emoji} {ext.upper()} Files ({count})"):
|
316 |
+
for f in files_by_ext[ext]:
|
317 |
+
fname = os.path.basename(f)
|
318 |
+
ctime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M:%S")
|
319 |
+
col1, col2, col3, col4 = st.columns([2,1,1,1])
|
320 |
+
with col1:
|
321 |
+
st.write(f"**{fname}** - {ctime}")
|
322 |
+
with col2:
|
323 |
+
# View button
|
324 |
+
if ext == "md":
|
325 |
+
if st.button("π", key="view_"+f):
|
326 |
+
content = open(f,'r',encoding='utf-8').read()
|
327 |
+
st.write("**Viewing file content:**")
|
328 |
+
st.markdown(content)
|
329 |
+
else: # mp3
|
330 |
+
if st.button("π", key="view_"+f):
|
331 |
+
st.write(f"Playing: {fname}")
|
332 |
+
st.audio(f)
|
333 |
+
with col3:
|
334 |
+
# Edit button for MD
|
335 |
+
if ext == "md":
|
336 |
+
if st.button("βοΈ", key="edit_"+f):
|
337 |
+
st.session_state.editing_file = f
|
338 |
+
st.session_state.edit_new_name = fname.replace(".md","")
|
339 |
+
st.session_state.edit_new_content = open(f,'r',encoding='utf-8').read()
|
340 |
+
st.rerun()
|
341 |
+
else:
|
342 |
+
# No edit for mp3
|
343 |
+
pass
|
344 |
+
with col4:
|
345 |
+
# Delete button
|
346 |
+
if st.button("π", key="del_"+f):
|
347 |
+
os.remove(f)
|
348 |
+
st.rerun()
|
349 |
+
|
350 |
+
# Download all as zip
|
351 |
+
if (len(md_files) > 0 or len(mp3_files) > 0) and st.sidebar.button("β¬οΈ Download All (.md and .mp3)"):
|
352 |
+
z = create_zip_of_files()
|
353 |
+
st.sidebar.markdown(get_download_link(z),unsafe_allow_html=True)
|
354 |
+
|
355 |
+
# If editing an md file
|
356 |
+
if st.session_state.editing_file and os.path.exists(st.session_state.editing_file):
|
357 |
+
st.sidebar.subheader(f"Editing: {os.path.basename(st.session_state.editing_file)}")
|
358 |
+
st.session_state.edit_new_name = st.sidebar.text_input("New name (without extension):", value=st.session_state.edit_new_name)
|
359 |
+
st.session_state.edit_new_content = st.sidebar.text_area("Content:", st.session_state.edit_new_content, height=200)
|
360 |
+
c1,c2 = st.sidebar.columns(2)
|
361 |
+
with c1:
|
362 |
+
if st.button("Save Changes"):
|
363 |
+
old_path = st.session_state.editing_file
|
364 |
+
new_path = st.session_state.edit_new_name + ".md"
|
365 |
+
# Rename file if name changed
|
366 |
+
if new_path != os.path.basename(old_path):
|
367 |
+
os.rename(old_path, new_path)
|
368 |
# Update content
|
369 |
+
with open(new_path,'w',encoding='utf-8') as f:
|
370 |
+
f.write(st.session_state.edit_new_content)
|
371 |
+
st.session_state.editing_file = None
|
|
|
|
|
|
|
372 |
st.experimental_rerun()
|
373 |
+
with c2:
|
374 |
+
if st.button("Cancel"):
|
375 |
+
st.session_state.editing_file = None
|
376 |
+
st.rerun()
|
377 |
|
378 |
def main():
|
379 |
st.sidebar.markdown("### π²BikeAIπ Multi-Agent Research AI")
|
|
|
467 |
st.header("π¬ Media Gallery - Images and Videos")
|
468 |
tabs = st.tabs(["πΌοΈ Images", "π₯ Video"])
|
469 |
with tabs[0]:
|
470 |
+
imgs = glob.glob("*.png")+glob.glob("*.jpg")
|
471 |
if imgs:
|
472 |
c = st.slider("Cols",1,5,3)
|
473 |
cols = st.columns(c)
|
474 |
for i,f in enumerate(imgs):
|
475 |
with cols[i%c]:
|
476 |
st.image(Image.open(f),use_container_width=True)
|
477 |
+
if st.button(f"π Analyze {os.path.basename(f)}"):
|
478 |
a = process_image(f,"Describe this image.")
|
479 |
st.markdown(a)
|
480 |
else:
|
481 |
st.write("No images found.")
|
482 |
with tabs[1]:
|
483 |
+
vids = glob.glob("*.mp4")
|
484 |
if vids:
|
485 |
for v in vids:
|
486 |
with st.expander(f"π₯ {os.path.basename(v)}"):
|
487 |
st.markdown(get_media_html(v,"video"),unsafe_allow_html=True)
|
488 |
+
if st.button(f"Analyze {os.path.basename(v)}"):
|
489 |
a = process_video_with_gpt(v,"Describe video.")
|
490 |
st.markdown(a)
|
491 |
else:
|
492 |
st.write("No videos found.")
|
493 |
|
494 |
elif tab_main == "π File Editor":
|
495 |
+
if getattr(st.session_state,'current_file',None):
|
496 |
+
st.subheader(f"Editing: {st.session_state.current_file}")
|
497 |
+
new_text = st.text_area("Content:", st.session_state.file_content, height=300)
|
498 |
+
if st.button("Save"):
|
499 |
+
with open(st.session_state.current_file,'w',encoding='utf-8') as f:
|
500 |
+
f.write(new_text)
|
501 |
+
st.success("Updated!")
|
502 |
+
else:
|
503 |
+
st.write("Select a file from the sidebar to edit.")
|
504 |
+
|
505 |
+
display_file_manager()
|
506 |
|
507 |
if __name__=="__main__":
|
508 |
main()
|