awacke1 commited on
Commit
d92c1cb
Β·
verified Β·
1 Parent(s): c3ff99f

Update app15.py

Browse files
Files changed (1) hide show
  1. app15.py +42 -289
app15.py CHANGED
@@ -1,4 +1,7 @@
1
- # %% ───────────── IMPORTS ─────────────
 
 
 
2
  import base64
3
  import glob
4
  import hashlib
@@ -26,7 +29,9 @@ import requests
26
  import numpy as np
27
  from urllib.parse import quote
28
 
29
- # External help links for AI tools
 
 
30
  external_links = [
31
  {"title": "MergeKit Official GitHub", "url": "https://github.com/arcee-ai/MergeKit", "emoji": "πŸ’»"},
32
  {"title": "MergeKit arXiv Paper", "url": "https://arxiv.org/abs/xxxx.xxxxx", "emoji": "πŸ“˜"},
@@ -40,7 +45,9 @@ external_links = [
40
  {"title": "arcee.ai Official Website", "url": "https://arcee.ai", "emoji": "🌐"},
41
  ]
42
 
43
- # %% ───────────── APP CONFIGURATION ─────────────
 
 
44
  Site_Name = 'πŸ™ GitCosmos'
45
  title = "πŸ™ GitCosmos"
46
  helpURL = 'https://huggingface.co/awacke1'
@@ -66,7 +73,9 @@ Key = os.environ.get("Key")
66
  LOCAL_APP_URL = "https://huggingface.co/spaces/awacke1/AzureCosmosDBUI"
67
  CosmosDBUrl = 'https://portal.azure.com/#@AaronCWackergmail.onmicrosoft.com/resource/subscriptions/003fba60-5b3f-48f4-ab36-3ed11bc40816/resourceGroups/datasets/providers/Microsoft.DocumentDB/databaseAccounts/acae-afd/dataExplorer'
68
 
69
- # %% ───────────── HELPER FUNCTIONS ─────────────
 
 
70
  def get_download_link(file_path):
71
  with open(file_path, "rb") as file:
72
  contents = file.read()
@@ -140,7 +149,9 @@ def preprocess_text(text):
140
  text = re.sub(r'[^\x00-\x7F]+', '', text)
141
  return text.strip()
142
 
143
- # %% ───────────── COSMOS DB FUNCTIONS ─────────────
 
 
144
  def get_databases(client):
145
  return [db['id'] for db in client.list_databases()]
146
 
@@ -178,7 +189,7 @@ def delete_record(container, record):
178
  if "delete_log" not in st.session_state:
179
  st.session_state.delete_log = []
180
  st.session_state.delete_log.append(f"Attempting to delete document: {json.dumps(record, indent=2)}")
181
- partition_key_value = doc_id
182
  st.session_state.delete_log.append(f"Using ID and Partition Key: {partition_key_value}")
183
  container.delete_item(item=doc_id, partition_key=partition_key_value)
184
  success_msg = f"Record {doc_id} successfully deleted from Cosmos DB. πŸ—‘οΈ"
@@ -205,6 +216,7 @@ def save_to_cosmos_db(container, query, response1, response2):
205
  new_id = f"{timestamp}-{unique_uuid}"
206
  record = {
207
  "id": new_id,
 
208
  "name": new_id,
209
  "query": query,
210
  "response1": response1,
@@ -242,11 +254,12 @@ def archive_current_container(database_name, container_name, client):
242
  except Exception as e:
243
  return f"Archive error: {str(e)} 😒"
244
 
245
- # %% ───────────── ADVANCED COSMOS FUNCTIONS ─────────────
 
 
246
  def create_new_container(database, container_id, partition_key_path,
247
  analytical_storage_ttl=None, indexing_policy=None, vector_embedding_policy=None):
248
  try:
249
- # Try creating with analytical_storage_ttl if provided.
250
  if analytical_storage_ttl is not None:
251
  container = database.create_container(
252
  id=container_id,
@@ -263,7 +276,6 @@ def create_new_container(database, container_id, partition_key_path,
263
  vector_embedding_policy=vector_embedding_policy
264
  )
265
  except exceptions.CosmosHttpResponseError as e:
266
- # If the error is due to the analytical_storage_ttl property not being valid, try again without it.
267
  if analytical_storage_ttl is not None and "analyticalStorageTtl" in str(e):
268
  try:
269
  container = database.create_container(
@@ -311,7 +323,9 @@ def vector_search(container, query_vector, vector_field, top=10, exact_search=Fa
311
  results = list(container.query_items(query=query, enable_cross_partition_query=True))
312
  return results
313
 
314
- # %% ───────────── GITHUB FUNCTIONS ─────────────
 
 
315
  def download_github_repo(url, local_path):
316
  if os.path.exists(local_path):
317
  shutil.rmtree(local_path)
@@ -342,7 +356,9 @@ def push_to_github(local_path, repo, github_token):
342
  local_repo.git.commit('-m', 'Initial commit')
343
  origin.push(refspec=f'{current_branch}:{current_branch}')
344
 
345
- # %% ───────────── FILE & MEDIA MANAGEMENT FUNCTIONS ─────────────
 
 
346
  def display_saved_files_in_sidebar():
347
  all_files = sorted([f for f in glob.glob("*.md") if not f.lower().startswith('readme')], reverse=True)
348
  st.sidebar.markdown("## πŸ“ Files")
@@ -470,7 +486,9 @@ def update_file_management_section():
470
  elif st.session_state.file_view_mode == 'edit':
471
  display_file_editor(st.session_state.current_file)
472
 
473
- # %% ───────────── VIDEO & AUDIO UI FUNCTIONS ─────────────
 
 
474
  def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
475
  try:
476
  st.write("Preprocessing image...")
@@ -556,6 +574,7 @@ def add_video_generation_ui(container):
556
  if container:
557
  video_record = {
558
  "id": generate_unique_id(),
 
559
  "type": "generated_video",
560
  "filename": video_filename,
561
  "seed": seed if seed is not None else "random",
@@ -583,280 +602,14 @@ def add_video_generation_ui(container):
583
  except Exception as e:
584
  st.error(f"Upload error: {str(e)}")
585
 
586
- # %% ───────────── MAIN FUNCTION ─────────────
587
- def main():
588
- st.markdown("### πŸ™ GitCosmos - Cosmos & Git Hub")
589
- if "chat_history" not in st.session_state:
590
- st.session_state.chat_history = []
591
- # Auth & Cosmos client initialization
592
- if Key:
593
- st.session_state.primary_key = Key
594
- st.session_state.logged_in = True
595
- else:
596
- st.error("Missing Cosmos Key πŸ”‘βŒ")
597
- return
598
- if st.session_state.logged_in:
599
- try:
600
- if st.session_state.get("client") is None:
601
- st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
602
- st.sidebar.title("πŸ™ Navigator")
603
- databases = get_databases(st.session_state.client)
604
- selected_db = st.sidebar.selectbox("πŸ—ƒοΈ DB", databases)
605
- st.markdown(CosmosDBUrl)
606
- if selected_db != st.session_state.get("selected_database"):
607
- st.session_state.selected_database = selected_db
608
- st.session_state.selected_container = None
609
- st.session_state.selected_document_id = None
610
- st.session_state.current_index = 0
611
- st.rerun()
612
- if st.session_state.selected_database:
613
- database = st.session_state.client.get_database_client(st.session_state.selected_database)
614
-
615
- # New Container button under DB menu
616
- if "show_new_container_form" not in st.session_state:
617
- st.session_state.show_new_container_form = False
618
- if st.sidebar.button("πŸ†• New Container"):
619
- st.session_state.show_new_container_form = True
620
- if st.session_state.show_new_container_form:
621
- with st.sidebar.form("new_container_form"):
622
- new_container_id = st.text_input("Container ID", value="newContainer")
623
- new_partition_key = st.text_input("Partition Key", value="/id")
624
- new_analytical = st.checkbox("Enable Analytical Store", value=False)
625
- submitted = st.form_submit_button("Create Container")
626
- if submitted:
627
- analytical_ttl = -1 if new_analytical else None
628
- new_container = create_new_container(
629
- database,
630
- new_container_id,
631
- new_partition_key,
632
- analytical_storage_ttl=analytical_ttl
633
- )
634
- if new_container:
635
- st.success(f"Container '{new_container_id}' created.")
636
- st.session_state.show_new_container_form = False
637
- st.session_state.new_container_created = new_container_id
638
- st.rerun()
639
-
640
- # Update container list
641
- containers = get_containers(database)
642
- if "new_container_created" in st.session_state and st.session_state.new_container_created not in containers:
643
- containers.append(st.session_state.new_container_created)
644
- selected_container = st.sidebar.selectbox("πŸ“ Container", containers)
645
- if selected_container != st.session_state.get("selected_container"):
646
- st.session_state.selected_container = selected_container
647
- st.session_state.selected_document_id = None
648
- st.session_state.current_index = 0
649
- st.rerun()
650
- if st.session_state.selected_container:
651
- container = database.get_container_client(st.session_state.selected_container)
652
- if st.sidebar.button("πŸ“¦ Export"):
653
- download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
654
- if download_link.startswith('<a'):
655
- st.markdown(download_link, unsafe_allow_html=True)
656
- else:
657
- st.error(download_link)
658
- documents = get_documents(container)
659
- total_docs = len(documents)
660
- num_docs = st.slider("Docs", 1, 20, 1)
661
- documents_to_display = documents[:num_docs] if total_docs > num_docs else documents
662
- st.sidebar.info(f"Showing {len(documents_to_display)} docs")
663
- # Document Viewer / Editor
664
- view_options = ['Markdown', 'Code', 'Run AI', 'Clone', 'New']
665
- selected_view = st.sidebar.selectbox("View", view_options, index=1)
666
- if selected_view == 'Markdown':
667
- st.markdown("#### πŸ“„ Markdown")
668
- if documents:
669
- doc = documents[st.session_state.current_index]
670
- content = json.dumps(doc, indent=2)
671
- st.markdown(f"```json\n{content}\n```")
672
- col_prev, col_next = st.columns(2)
673
- with col_prev:
674
- if st.button("⬅️") and st.session_state.current_index > 0:
675
- st.session_state.current_index -= 1
676
- st.rerun()
677
- with col_next:
678
- if st.button("➑️") and st.session_state.current_index < total_docs - 1:
679
- st.session_state.current_index += 1
680
- st.rerun()
681
- elif selected_view == 'Code':
682
- st.markdown("#### πŸ’» Code Editor")
683
- if documents:
684
- doc = documents[st.session_state.current_index]
685
- doc_str = st.text_area("Edit JSON", value=json.dumps(doc, indent=2), height=300, key=f'code_{st.session_state.current_index}')
686
- col_prev, col_next = st.columns(2)
687
- with col_prev:
688
- if st.button("⬅️") and st.session_state.current_index > 0:
689
- st.session_state.current_index -= 1
690
- st.rerun()
691
- with col_next:
692
- if st.button("➑️") and st.session_state.current_index < total_docs - 1:
693
- st.session_state.current_index += 1
694
- st.rerun()
695
- col_save, col_delete = st.columns(2)
696
- with col_save:
697
- if st.button("πŸ’Ύ Save", key=f'save_{st.session_state.current_index}'):
698
- try:
699
- updated_doc = json.loads(doc_str)
700
- container.upsert_item(body=updated_doc)
701
- st.success(f"Saved {updated_doc['id']}")
702
- st.rerun()
703
- except Exception as e:
704
- st.error(f"Save err: {str(e)}")
705
- with col_delete:
706
- if st.button("πŸ—‘οΈ Delete", key=f'delete_{st.session_state.current_index}'):
707
- try:
708
- current_doc = json.loads(doc_str)
709
- success, message = delete_record(container, current_doc)
710
- if success:
711
- st.success(message)
712
- st.rerun()
713
- else:
714
- st.error(message)
715
- except Exception as e:
716
- st.error(f"Delete err: {str(e)}")
717
- if "delete_log" in st.session_state and st.session_state.delete_log:
718
- st.subheader("Delete Log")
719
- for log_entry in st.session_state.delete_log[-5:]:
720
- st.write(log_entry)
721
- elif selected_view == 'Run AI':
722
- st.markdown("#### πŸ€– Run AI (stub)")
723
- st.info("AI functionality not implemented.")
724
- elif selected_view == 'Clone':
725
- st.markdown("#### πŸ“„ Clone")
726
- if documents:
727
- doc = documents[st.session_state.current_index]
728
- st.markdown(f"Original ID: {doc.get('id', '')}")
729
- new_id = st.text_input("New ID", value=generate_unique_id(), key='new_clone_id')
730
- new_name = st.text_input("New Name", value=f"Clone_{new_id[:8]}", key='new_clone_name')
731
- new_doc = {'id': new_id, 'name': new_name, **{k: v for k, v in doc.items() if k not in ['id', 'name', '_rid', '_self', '_etag', '_attachments', '_ts']}}
732
- doc_str = st.text_area("Edit JSON", value=json.dumps(new_doc, indent=2), height=300, key='clone_preview')
733
- col1, col2 = st.columns(2)
734
- with col1:
735
- if st.button("πŸ”„ Regenerate"):
736
- new_id = generate_unique_id()
737
- st.session_state.new_clone_id = new_id
738
- st.rerun()
739
- with col2:
740
- if st.button("πŸ’Ύ Save Clone"):
741
- try:
742
- final_doc = json.loads(doc_str)
743
- for field in ['_rid', '_self', '_etag', '_attachments', '_ts']:
744
- final_doc.pop(field, None)
745
- container.create_item(body=final_doc)
746
- st.success(f"Cloned {final_doc['id']}")
747
- st.rerun()
748
- except Exception as e:
749
- st.error(f"Clone err: {str(e)}")
750
- col_prev, col_next = st.columns(2)
751
- with col_prev:
752
- if st.button("⬅️") and st.session_state.current_index > 0:
753
- st.session_state.current_index -= 1
754
- st.rerun()
755
- with col_next:
756
- if st.button("➑️") and st.session_state.current_index < total_docs - 1:
757
- st.session_state.current_index += 1
758
- st.rerun()
759
- elif selected_view == 'New':
760
- st.markdown("#### βž• New Doc")
761
- if st.button("πŸ€– Auto-Gen"):
762
- auto_doc = {
763
- "id": generate_unique_id(),
764
- "name": f"Auto {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
765
- "content": "Auto-generated record.",
766
- "timestamp": datetime.now().isoformat()
767
- }
768
- success, message = insert_record(container, auto_doc)
769
- if success:
770
- st.success(message)
771
- st.rerun()
772
- else:
773
- st.error(message)
774
- else:
775
- new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
776
- default_doc = {
777
- "id": new_id,
778
- "name": "New Doc",
779
- "content": "",
780
- "timestamp": datetime.now().isoformat()
781
- }
782
- new_doc_str = st.text_area("JSON", value=json.dumps(default_doc, indent=2), height=300)
783
- if st.button("βž• Create"):
784
- try:
785
- cleaned = preprocess_text(new_doc_str)
786
- new_doc = json.loads(cleaned)
787
- new_doc['id'] = new_id
788
- success, message = insert_record(container, new_doc)
789
- if success:
790
- st.success(f"Created {new_doc['id']}")
791
- st.rerun()
792
- else:
793
- st.error(message)
794
- except Exception as e:
795
- st.error(f"Create err: {str(e)}")
796
- st.subheader(f"πŸ“Š {st.session_state.selected_container}")
797
- if documents_to_display:
798
- df = pd.DataFrame(documents_to_display)
799
- st.dataframe(df)
800
- else:
801
- st.info("No docs.")
802
-
803
- # GitHub Ops section
804
- st.subheader("πŸ™ GitHub Ops")
805
- github_token = os.environ.get("GITHUB")
806
- source_repo = st.text_input("Source Repo URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
807
- new_repo_name = st.text_input("New Repo Name", value=f"Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
808
- col_g1, col_g2 = st.columns(2)
809
- with col_g1:
810
- if st.button("πŸ“₯ Clone Repo"):
811
- if github_token and source_repo:
812
- try:
813
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
814
- download_github_repo(source_repo, local_path)
815
- zip_filename = f"{new_repo_name}.zip"
816
- create_zip_file(local_path, zip_filename[:-4])
817
- st.markdown(get_download_link(zip_filename), unsafe_allow_html=True)
818
- st.success("Cloned! πŸŽ‰")
819
- except Exception as e:
820
- st.error(f"Clone err: {str(e)}")
821
- finally:
822
- if os.path.exists(local_path):
823
- shutil.rmtree(local_path)
824
- if os.path.exists(zip_filename):
825
- os.remove(zip_filename)
826
- else:
827
- st.error("Missing token or URL πŸ”‘β“")
828
- with col_g2:
829
- if st.button("πŸ“€ Push Repo"):
830
- if github_token and source_repo:
831
- try:
832
- g = Github(github_token)
833
- new_repo = create_repo(g, new_repo_name)
834
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
835
- download_github_repo(source_repo, local_path)
836
- push_to_github(local_path, new_repo, github_token)
837
- st.success(f"Pushed to {new_repo.html_url} πŸš€")
838
- except Exception as e:
839
- st.error(f"Push err: {str(e)}")
840
- finally:
841
- if os.path.exists(local_path):
842
- shutil.rmtree(local_path)
843
- else:
844
- st.error("Missing token or URL πŸ”‘β“")
845
- update_file_management_section()
846
- except exceptions.CosmosHttpResponseError as e:
847
- st.error(f"Cosmos error: {str(e)} 🚨")
848
- except Exception as e:
849
- st.error(f"Error: {str(e)} 😱")
850
- if st.session_state.logged_in and st.sidebar.button("πŸšͺ Logout"):
851
- st.markdown("#### πŸšͺ Logout")
852
- st.session_state.logged_in = False
853
- st.session_state.selected_records = []
854
- st.session_state.client = None
855
- st.session_state.selected_database = None
856
- st.session_state.selected_container = None
857
- st.session_state.selected_document_id = None
858
- st.session_state.current_index = 0
859
- st.rerun()
860
-
861
- if __name__ == "__main__":
862
- main()
 
1
+ # app.py
2
+ # =============================================================================
3
+ # ───────────── IMPORTS ─────────────
4
+ # =============================================================================
5
  import base64
6
  import glob
7
  import hashlib
 
29
  import numpy as np
30
  from urllib.parse import quote
31
 
32
+ # =============================================================================
33
+ # ───────────── EXTERNAL HELP LINKS (Always visible in sidebar) ─────────────
34
+ # =============================================================================
35
  external_links = [
36
  {"title": "MergeKit Official GitHub", "url": "https://github.com/arcee-ai/MergeKit", "emoji": "πŸ’»"},
37
  {"title": "MergeKit arXiv Paper", "url": "https://arxiv.org/abs/xxxx.xxxxx", "emoji": "πŸ“˜"},
 
45
  {"title": "arcee.ai Official Website", "url": "https://arcee.ai", "emoji": "🌐"},
46
  ]
47
 
48
+ # =============================================================================
49
+ # ───────────── APP CONFIGURATION ─────────────
50
+ # =============================================================================
51
  Site_Name = 'πŸ™ GitCosmos'
52
  title = "πŸ™ GitCosmos"
53
  helpURL = 'https://huggingface.co/awacke1'
 
73
  LOCAL_APP_URL = "https://huggingface.co/spaces/awacke1/AzureCosmosDBUI"
74
  CosmosDBUrl = 'https://portal.azure.com/#@AaronCWackergmail.onmicrosoft.com/resource/subscriptions/003fba60-5b3f-48f4-ab36-3ed11bc40816/resourceGroups/datasets/providers/Microsoft.DocumentDB/databaseAccounts/acae-afd/dataExplorer'
75
 
76
+ # =============================================================================
77
+ # ───────────── HELPER FUNCTIONS ─────────────
78
+ # =============================================================================
79
  def get_download_link(file_path):
80
  with open(file_path, "rb") as file:
81
  contents = file.read()
 
149
  text = re.sub(r'[^\x00-\x7F]+', '', text)
150
  return text.strip()
151
 
152
+ # =============================================================================
153
+ # ───────────── COSMOS DB FUNCTIONS ─────────────
154
+ # =============================================================================
155
  def get_databases(client):
156
  return [db['id'] for db in client.list_databases()]
157
 
 
189
  if "delete_log" not in st.session_state:
190
  st.session_state.delete_log = []
191
  st.session_state.delete_log.append(f"Attempting to delete document: {json.dumps(record, indent=2)}")
192
+ partition_key_value = record.get("pk", doc_id)
193
  st.session_state.delete_log.append(f"Using ID and Partition Key: {partition_key_value}")
194
  container.delete_item(item=doc_id, partition_key=partition_key_value)
195
  success_msg = f"Record {doc_id} successfully deleted from Cosmos DB. πŸ—‘οΈ"
 
216
  new_id = f"{timestamp}-{unique_uuid}"
217
  record = {
218
  "id": new_id,
219
+ "pk": new_id,
220
  "name": new_id,
221
  "query": query,
222
  "response1": response1,
 
254
  except Exception as e:
255
  return f"Archive error: {str(e)} 😒"
256
 
257
+ # =============================================================================
258
+ # ───────────── ADVANCED COSMOS FUNCTIONS ─────────────
259
+ # =============================================================================
260
  def create_new_container(database, container_id, partition_key_path,
261
  analytical_storage_ttl=None, indexing_policy=None, vector_embedding_policy=None):
262
  try:
 
263
  if analytical_storage_ttl is not None:
264
  container = database.create_container(
265
  id=container_id,
 
276
  vector_embedding_policy=vector_embedding_policy
277
  )
278
  except exceptions.CosmosHttpResponseError as e:
 
279
  if analytical_storage_ttl is not None and "analyticalStorageTtl" in str(e):
280
  try:
281
  container = database.create_container(
 
323
  results = list(container.query_items(query=query, enable_cross_partition_query=True))
324
  return results
325
 
326
+ # =============================================================================
327
+ # ───────────── GITHUB FUNCTIONS ─────────────
328
+ # =============================================================================
329
  def download_github_repo(url, local_path):
330
  if os.path.exists(local_path):
331
  shutil.rmtree(local_path)
 
356
  local_repo.git.commit('-m', 'Initial commit')
357
  origin.push(refspec=f'{current_branch}:{current_branch}')
358
 
359
+ # =============================================================================
360
+ # ───────────── FILE & MEDIA MANAGEMENT FUNCTIONS ─────────────
361
+ # =============================================================================
362
  def display_saved_files_in_sidebar():
363
  all_files = sorted([f for f in glob.glob("*.md") if not f.lower().startswith('readme')], reverse=True)
364
  st.sidebar.markdown("## πŸ“ Files")
 
486
  elif st.session_state.file_view_mode == 'edit':
487
  display_file_editor(st.session_state.current_file)
488
 
489
+ # =============================================================================
490
+ # ───────────── VIDEO & AUDIO UI FUNCTIONS ─────────────
491
+ # =============================================================================
492
  def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
493
  try:
494
  st.write("Preprocessing image...")
 
574
  if container:
575
  video_record = {
576
  "id": generate_unique_id(),
577
+ "pk": generate_unique_id(),
578
  "type": "generated_video",
579
  "filename": video_filename,
580
  "seed": seed if seed is not None else "random",
 
602
  except Exception as e:
603
  st.error(f"Upload error: {str(e)}")
604
 
605
+ # =============================================================================
606
+ # ───────────── AI SAMPLES SIDEBAR (Full Text & RAG/Vector Search Demos) ─────────────
607
+ # =============================================================================
608
+ def display_ai_samples():
609
+ st.sidebar.markdown("### πŸ€– AI Samples")
610
+ st.sidebar.info("πŸš€ Get started with our AI samples! Time free access to get started today.")
611
+ with st.sidebar.expander("πŸ” Full Text Search Examples"):
612
+ st.markdown("""
613
+ **FullTextContains Example**
614
+ ```sql
615
+ SELECT TOP 10 * FROM c WHERE FullTextContains(c.text, "bicycle")