awacke1 commited on
Commit
5b15408
β€’
1 Parent(s): 3117ef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -37
app.py CHANGED
@@ -40,15 +40,16 @@ openai_client = OpenAI(api_key=openai.api_key, organization=os.getenv('OPENAI_OR
40
  HF_KEY = os.getenv('HF_KEY')
41
  API_URL = os.getenv('API_URL')
42
 
 
43
  st.session_state.setdefault('transcript_history', [])
44
  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
- # 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("""
@@ -76,7 +77,7 @@ def generate_filename(prompt, file_type="md"):
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:
@@ -103,7 +104,7 @@ async def edge_tts_generate_audio(text, voice="en-US-AriaNeural", rate=0, pitch=
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,7 +136,7 @@ def process_audio(audio_path):
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,20 +189,17 @@ def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary
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'):
@@ -261,7 +259,7 @@ def create_zip_of_files():
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,44 +269,46 @@ 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])
@@ -337,15 +337,14 @@ def display_file_manager():
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)"):
@@ -362,26 +361,25 @@ def display_file_manager():
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")
380
  tab_main = st.radio("Action:",["🎀 Voice Input","πŸ“Έ Media Gallery","πŸ” Search ArXiv","πŸ“ File Editor"],horizontal=True)
381
-
382
  model_choice = st.sidebar.radio("AI Model:", ["Arxiv","GPT-4o","Claude-3","GPT+Claude+Arxiv"], index=0)
383
 
384
- # Declare the component
385
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
386
  val = mycomponent(my_input_value="Hello")
387
  if val:
@@ -474,7 +472,7 @@ def main():
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:
@@ -485,13 +483,14 @@ def main():
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)
@@ -499,10 +498,18 @@ def main():
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()
 
40
  HF_KEY = os.getenv('HF_KEY')
41
  API_URL = os.getenv('API_URL')
42
 
43
+ # Session states
44
  st.session_state.setdefault('transcript_history', [])
45
  st.session_state.setdefault('chat_history', [])
46
  st.session_state.setdefault('openai_model', "gpt-4o-2024-05-13")
47
  st.session_state.setdefault('messages', [])
48
  st.session_state.setdefault('last_voice_input', "")
 
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
+ st.session_state.setdefault('should_rerun', False) # Flag to indicate we need to rerun after operations
53
 
54
  # 🎨 Minimal Custom CSS
55
  st.markdown("""
 
77
  def create_file(filename, prompt, response):
78
  with open(filename, 'w', encoding='utf-8') as f:
79
  f.write(prompt + "\n\n" + response)
80
+ st.session_state.should_rerun = True
81
 
82
  def get_download_link(file):
83
  with open(file, "rb") as f:
 
104
  communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
105
  out_fn = generate_filename(text,"mp3")
106
  await communicate.save(out_fn)
107
+ st.session_state.should_rerun = True
108
  return out_fn
109
 
110
  def speak_with_edge_tts(text, voice="en-US-AriaNeural", rate=0, pitch=0):
 
136
  with open(audio_path, "rb") as f:
137
  transcription = openai_client.audio.transcriptions.create(model="whisper-1", file=f)
138
  st.session_state.messages.append({"role": "user", "content": transcription.text})
139
+ st.session_state.should_rerun = True
140
  return transcription.text
141
 
142
  def process_video(video_path, seconds_per_frame=1):
 
189
 
190
  st.markdown(result)
191
 
 
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
  if extended_refs:
198
  summaries_text = "Here are the summaries from the references: " + refs.replace('"','')
199
  audio_file_refs = speak_with_edge_tts(summaries_text, voice="en-US-AriaNeural", rate=0, pitch=0)
200
  st.write("### πŸ“œ Extended References & Summaries")
201
  play_and_download_audio(audio_file_refs)
202
 
 
203
  if titles_summary:
204
  titles = []
205
  for line in refs.split('\n'):
 
259
  with zipfile.ZipFile(zip_name,'w') as z:
260
  for f in all_files:
261
  z.write(f)
262
+ st.session_state.should_rerun = True
263
  return zip_name
264
 
265
  def get_media_html(p,typ="video",w="100%"):
 
269
  else:
270
  return f'<audio controls style="width:{w};"><source src="data:audio/mpeg;base64,{d}" type="audio/mpeg"></audio>'
271
 
272
+ def load_files_for_sidebar():
 
 
 
273
  # Gather all md and mp3 files
274
  md_files = glob.glob("*.md")
275
  mp3_files = glob.glob("*.mp3")
276
 
 
277
  files_by_ext = defaultdict(list)
278
  for f in md_files:
279
+ files_by_ext['md'].append(f)
 
280
  for f in mp3_files:
281
+ files_by_ext['mp3'].append(f)
 
282
 
283
  # Sort each extension group by modification time descending
284
  for ext in files_by_ext:
285
  files_by_ext[ext].sort(key=lambda x: os.path.getmtime(x), reverse=True)
286
+ return files_by_ext
287
 
288
+ def display_file_manager_sidebar(files_by_ext):
289
+ st.sidebar.title("🎡 Audio & Document Manager")
290
+
291
+ md_files = files_by_ext.get('md', [])
292
+ mp3_files = files_by_ext.get('mp3', [])
293
 
294
  # Delete all buttons
295
+ col_del = st.sidebar.columns(2)
296
+ with col_del[0]:
297
  if st.button("πŸ—‘ Delete All MD"):
298
  for f in md_files:
299
  os.remove(f)
300
+ st.session_state.should_rerun = True
301
+ with col_del[1]:
302
  if st.button("πŸ—‘ Delete All MP3"):
303
  for f in mp3_files:
304
  os.remove(f)
305
+ st.session_state.should_rerun = True
306
 
307
+ # Sort extensions by number of files descending
308
+ ext_counts = {ext: len(files) for ext, files in files_by_ext.items()}
309
+ sorted_ext = sorted(files_by_ext.keys(), key=lambda x: ext_counts[x], reverse=True)
310
+
311
+ # Display groups
312
  for ext in sorted_ext:
313
  emoji = FILE_EMOJIS.get(ext, "πŸ“¦")
314
  count = len(files_by_ext[ext])
 
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.session_state.should_rerun = True
341
  else:
 
342
  pass
343
  with col4:
344
  # Delete button
345
  if st.button("πŸ—‘", key="del_"+f):
346
  os.remove(f)
347
+ st.session_state.should_rerun = True
348
 
349
  # Download all as zip
350
  if (len(md_files) > 0 or len(mp3_files) > 0) and st.sidebar.button("⬇️ Download All (.md and .mp3)"):
 
361
  if st.button("Save Changes"):
362
  old_path = st.session_state.editing_file
363
  new_path = st.session_state.edit_new_name + ".md"
 
364
  if new_path != os.path.basename(old_path):
365
  os.rename(old_path, new_path)
 
366
  with open(new_path,'w',encoding='utf-8') as f:
367
  f.write(st.session_state.edit_new_content)
368
  st.session_state.editing_file = None
369
+ st.session_state.should_rerun = True
370
  with c2:
371
  if st.button("Cancel"):
372
  st.session_state.editing_file = None
373
+ st.session_state.should_rerun = True
374
+
375
 
376
  def main():
377
  st.sidebar.markdown("### 🚲BikeAIπŸ† Multi-Agent Research AI")
378
  tab_main = st.radio("Action:",["🎀 Voice Input","πŸ“Έ Media Gallery","πŸ” Search ArXiv","πŸ“ File Editor"],horizontal=True)
379
+
380
  model_choice = st.sidebar.radio("AI Model:", ["Arxiv","GPT-4o","Claude-3","GPT+Claude+Arxiv"], index=0)
381
 
382
+ # Main Input Component
383
  mycomponent = components.declare_component("mycomponent", path="mycomponent")
384
  val = mycomponent(my_input_value="Hello")
385
  if val:
 
472
  for i,f in enumerate(imgs):
473
  with cols[i%c]:
474
  st.image(Image.open(f),use_container_width=True)
475
+ if st.button(f"πŸ‘€ Analyze {os.path.basename(f)}", key=f"analyze_{f}"):
476
  a = process_image(f,"Describe this image.")
477
  st.markdown(a)
478
  else:
 
483
  for v in vids:
484
  with st.expander(f"πŸŽ₯ {os.path.basename(v)}"):
485
  st.markdown(get_media_html(v,"video"),unsafe_allow_html=True)
486
+ if st.button(f"Analyze {os.path.basename(v)}", key=f"analyze_{v}"):
487
  a = process_video_with_gpt(v,"Describe video.")
488
  st.markdown(a)
489
  else:
490
  st.write("No videos found.")
491
 
492
  elif tab_main == "πŸ“ File Editor":
493
+ # Existing code for inline editing if needed
494
  if getattr(st.session_state,'current_file',None):
495
  st.subheader(f"Editing: {st.session_state.current_file}")
496
  new_text = st.text_area("Content:", st.session_state.file_content, height=300)
 
498
  with open(st.session_state.current_file,'w',encoding='utf-8') as f:
499
  f.write(new_text)
500
  st.success("Updated!")
501
+ st.session_state.should_rerun = True
502
  else:
503
  st.write("Select a file from the sidebar to edit.")
504
 
505
+ # After all main content is processed, load files and display in sidebar
506
+ files_by_ext = load_files_for_sidebar()
507
+ display_file_manager_sidebar(files_by_ext)
508
+
509
+ # If we performed an operation, rerun now at the end
510
+ if st.session_state.should_rerun:
511
+ st.session_state.should_rerun = False
512
+ st.rerun()
513
 
514
  if __name__=="__main__":
515
  main()