awacke1 commited on
Commit
88e04a0
โ€ข
1 Parent(s): 8d51b6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -1
app.py CHANGED
@@ -21,6 +21,8 @@ import zipfile
21
  import hashlib
22
  import time
23
 
 
 
24
  # ๐ŸŽญ App Configuration - Because every app needs a good costume!
25
  Site_Name = '๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent'
26
  title = "๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent"
@@ -308,7 +310,6 @@ def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=No
308
  st.error("Failed to save document after maximum retries.")
309
  return None
310
 
311
-
312
 
313
  # ๐Ÿ’พ Save or clone to Cosmos DB - Because every document deserves a twin
314
  def save_or_clone_to_cosmos_db2(container, query=None, response=None, clone_id=None):
@@ -452,6 +453,56 @@ def search_glossary(query):
452
 
453
  return result, result2, result3, response2
454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  # ๐ŸŽญ Main function - Where the magic happens (and occasionally breaks)
456
  def main():
457
  st.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent")
@@ -482,6 +533,28 @@ def main():
482
  # ๐Ÿ•ต๏ธโ€โ™‚๏ธ We have a query! Let's process it!
483
  result, result2, result3, response2 = search_glossary(query)
484
  # When saving results, pass the container
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485
  try:
486
  save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
487
  save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)
 
21
  import hashlib
22
  import time
23
 
24
+
25
+
26
  # ๐ŸŽญ App Configuration - Because every app needs a good costume!
27
  Site_Name = '๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent'
28
  title = "๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent"
 
310
  st.error("Failed to save document after maximum retries.")
311
  return None
312
 
 
313
 
314
  # ๐Ÿ’พ Save or clone to Cosmos DB - Because every document deserves a twin
315
  def save_or_clone_to_cosmos_db2(container, query=None, response=None, clone_id=None):
 
453
 
454
  return result, result2, result3, response2
455
 
456
+
457
+
458
+
459
+
460
+ # ๐Ÿ“ Generate a safe filename from the first few lines of content
461
+ def generate_filename_from_content(content, file_type="md"):
462
+ # Extract the first few lines or sentences
463
+ first_sentence = content.split('\n', 1)[0][:90] # Limit the length to 90 characters
464
+ # Remove special characters to make it a valid filename
465
+ safe_name = re.sub(r'[^\w\s-]', '', first_sentence)
466
+ # Limit length to be compatible with Windows and Linux
467
+ safe_name = safe_name[:50].strip() # Adjust length limit
468
+ return f"{safe_name}.{file_type}"
469
+
470
+ # ๐Ÿ’พ Create and save a file
471
+ def create_file_from_content(content, should_save=True):
472
+ if not should_save:
473
+ return
474
+ filename = generate_filename_from_content(content)
475
+ with open(filename, 'w', encoding='utf-8') as file:
476
+ file.write(content)
477
+ return filename
478
+
479
+ # ๐Ÿ“‚ Display list of saved .md files in the sidebar
480
+ def display_saved_files_in_sidebar():
481
+ all_files = glob.glob("*.md")
482
+ all_files.sort(reverse=True)
483
+ all_files = [file for file in all_files if not file.lower().startswith('readme')] # Exclude README.md
484
+
485
+ st.sidebar.markdown("## ๐Ÿ“ Saved Markdown Files")
486
+ for file in all_files:
487
+ col1, col2, col3 = st.sidebar.columns([6, 2, 1])
488
+ with col1:
489
+ st.markdown(f"๐Ÿ“„ {file}")
490
+ with col2:
491
+ st.sidebar.download_button(
492
+ label="โฌ‡๏ธ Download",
493
+ data=open(file, 'rb').read(),
494
+ file_name=file
495
+ )
496
+ with col3:
497
+ if st.sidebar.button("๐Ÿ—‘", key=f"delete_{file}"):
498
+ os.remove(file)
499
+ st.rerun()
500
+
501
+
502
+
503
+
504
+
505
+
506
  # ๐ŸŽญ Main function - Where the magic happens (and occasionally breaks)
507
  def main():
508
  st.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent")
 
533
  # ๐Ÿ•ต๏ธโ€โ™‚๏ธ We have a query! Let's process it!
534
  result, result2, result3, response2 = search_glossary(query)
535
  # When saving results, pass the container
536
+ try:
537
+ # ๐Ÿ“ Automatically save the AI-generated content
538
+ if st.button("Save AI Output"):
539
+
540
+ # ๐Ÿ•ต๏ธโ€โ™‚๏ธ Save our nice research summaries for later sleuthing
541
+ filename = create_file_from_content(result)
542
+ st.success(f"File saved: {filename}")
543
+ filename = create_file_from_content(result2)
544
+ st.success(f"File saved: {filename}")
545
+ filename = create_file_from_content(result3)
546
+ st.success(f"File saved: {filename}")
547
+ filename = create_file_from_content(response2)
548
+ st.success(f"File saved: {filename}")
549
+
550
+ #st.rerun() # Rerun the app to update the sidebar
551
+
552
+ # ๐Ÿ“ Display saved markdown files in the sidebar
553
+ display_saved_files_in_sidebar()
554
+ except Exception as e:
555
+ st.error(f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ")
556
+
557
+
558
  try:
559
  save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
560
  save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)