Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
-
#
|
|
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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 |
-
#
|
|
|
|
|
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,7 +602,63 @@ def add_video_generation_ui(container):
|
|
583 |
except Exception as e:
|
584 |
st.error(f"Upload error: {str(e)}")
|
585 |
|
586 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
def main():
|
588 |
st.markdown("### π GitCosmos - Cosmos & Git Hub")
|
589 |
if "chat_history" not in st.session_state:
|
@@ -619,9 +694,9 @@ def main():
|
|
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="
|
623 |
-
new_partition_key = st.text_input("Partition Key", value="/
|
624 |
-
new_analytical = st.checkbox("Enable Analytical Store", value=
|
625 |
submitted = st.form_submit_button("Create Container")
|
626 |
if submitted:
|
627 |
analytical_ttl = -1 if new_analytical else None
|
@@ -633,6 +708,21 @@ def main():
|
|
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()
|
@@ -728,7 +818,7 @@ def main():
|
|
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:
|
@@ -761,6 +851,7 @@ def main():
|
|
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()
|
@@ -775,6 +866,7 @@ def main():
|
|
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()
|
@@ -785,6 +877,7 @@ def main():
|
|
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']}")
|
@@ -842,6 +935,8 @@ def main():
|
|
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)} π¨")
|
|
|
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 (Processed as a Python List) βββββββββββββ
|
607 |
+
# =============================================================================
|
608 |
+
def display_ai_samples():
|
609 |
+
# Define a list of sample queries
|
610 |
+
ai_samples = [
|
611 |
+
{
|
612 |
+
"name": "FullTextContains",
|
613 |
+
"description": "Query using FullTextContains",
|
614 |
+
"query": 'SELECT TOP 10 * FROM c WHERE FullTextContains(c.text, "bicycle")'
|
615 |
+
},
|
616 |
+
{
|
617 |
+
"name": "FullTextContainsAll",
|
618 |
+
"description": "Query using FullTextContainsAll",
|
619 |
+
"query": 'SELECT TOP 10 * FROM c WHERE FullTextContainsAll(c.text, "red", "bicycle")'
|
620 |
+
},
|
621 |
+
{
|
622 |
+
"name": "FullTextContainsAny",
|
623 |
+
"description": "Query using FullTextContainsAny",
|
624 |
+
"query": 'SELECT TOP 10 * FROM c WHERE FullTextContains(c.text, "red") AND FullTextContainsAny(c.text, "bicycle", "skateboard")'
|
625 |
+
},
|
626 |
+
{
|
627 |
+
"name": "FullTextScore",
|
628 |
+
"description": "Query using FullTextScore (order by relevance)",
|
629 |
+
"query": 'SELECT TOP 10 * FROM c ORDER BY RANK FullTextScore(c.text, ["bicycle", "mountain"])'
|
630 |
+
},
|
631 |
+
{
|
632 |
+
"name": "Vector Search with Score",
|
633 |
+
"description": "Example vector search snippet",
|
634 |
+
"query": 'results = vector_search.similarity_search_with_score(query="Your query", k=5)\nfor result, score in results:\n print(result.json(), score)'
|
635 |
+
},
|
636 |
+
{
|
637 |
+
"name": "Vector Search with Filtering",
|
638 |
+
"description": "Example vector search with a filter",
|
639 |
+
"query": 'pre_filter = {"conditions": [{"property": "metadata.page", "operator": "$eq", "value": 0}]}\nresults = vector_search.similarity_search_with_score(query="Your query", k=5, pre_filter=pre_filter)'
|
640 |
+
},
|
641 |
+
{
|
642 |
+
"name": "Hybrid Search",
|
643 |
+
"description": "Example hybrid search snippet",
|
644 |
+
"query": 'results = vector_search.similarity_search_with_score(query="Your query", k=5, query_type=CosmosDBQueryType.HYBRID)'
|
645 |
+
}
|
646 |
+
]
|
647 |
+
st.sidebar.markdown("### π€ AI Samples")
|
648 |
+
st.sidebar.info("π Get started with our AI samples! Time free access to get started today.")
|
649 |
+
# Provide a dropdown to select one sample
|
650 |
+
sample_names = [sample["name"] for sample in ai_samples]
|
651 |
+
selected_sample_name = st.sidebar.selectbox("Select an AI Sample", sample_names)
|
652 |
+
selected_sample = next((s for s in ai_samples if s["name"] == selected_sample_name), None)
|
653 |
+
if selected_sample:
|
654 |
+
st.sidebar.markdown(f"**{selected_sample['name']}**: {selected_sample['description']}")
|
655 |
+
# Use language 'sql' for queries containing FullText, else python
|
656 |
+
lang = "sql" if "FullText" in selected_sample["name"] else "python"
|
657 |
+
st.sidebar.code(selected_sample["query"], language=lang)
|
658 |
+
|
659 |
+
# =============================================================================
|
660 |
+
# βββββββββββββ MAIN FUNCTION βββββββββββββ
|
661 |
+
# =============================================================================
|
662 |
def main():
|
663 |
st.markdown("### π GitCosmos - Cosmos & Git Hub")
|
664 |
if "chat_history" not in st.session_state:
|
|
|
694 |
st.session_state.show_new_container_form = True
|
695 |
if st.session_state.show_new_container_form:
|
696 |
with st.sidebar.form("new_container_form"):
|
697 |
+
new_container_id = st.text_input("Container ID", value="aiml-container")
|
698 |
+
new_partition_key = st.text_input("Partition Key", value="/pk")
|
699 |
+
new_analytical = st.checkbox("Enable Analytical Store", value=True)
|
700 |
submitted = st.form_submit_button("Create Container")
|
701 |
if submitted:
|
702 |
analytical_ttl = -1 if new_analytical else None
|
|
|
708 |
)
|
709 |
if new_container:
|
710 |
st.success(f"Container '{new_container_id}' created.")
|
711 |
+
# Insert a default templated item into the new container
|
712 |
+
default_id = generate_unique_id()
|
713 |
+
default_item = {
|
714 |
+
"id": default_id,
|
715 |
+
"pk": default_id,
|
716 |
+
"name": "Default Image Prompt",
|
717 |
+
"prompt": "Enter your image prompt here",
|
718 |
+
"timestamp": datetime.now().isoformat(),
|
719 |
+
"type": "image_prompt"
|
720 |
+
}
|
721 |
+
insert_success, insert_message = insert_record(new_container, default_item)
|
722 |
+
if insert_success:
|
723 |
+
st.info("Default templated item created in new container.")
|
724 |
+
else:
|
725 |
+
st.error(f"Default item insertion error: {insert_message}")
|
726 |
st.session_state.show_new_container_form = False
|
727 |
st.session_state.new_container_created = new_container_id
|
728 |
st.rerun()
|
|
|
818 |
st.markdown(f"Original ID: {doc.get('id', '')}")
|
819 |
new_id = st.text_input("New ID", value=generate_unique_id(), key='new_clone_id')
|
820 |
new_name = st.text_input("New Name", value=f"Clone_{new_id[:8]}", key='new_clone_name')
|
821 |
+
new_doc = {'id': new_id, 'pk': new_id, 'name': new_name, **{k: v for k, v in doc.items() if k not in ['id', 'name', 'pk', '_rid', '_self', '_etag', '_attachments', '_ts']}}
|
822 |
doc_str = st.text_area("Edit JSON", value=json.dumps(new_doc, indent=2), height=300, key='clone_preview')
|
823 |
col1, col2 = st.columns(2)
|
824 |
with col1:
|
|
|
851 |
if st.button("π€ Auto-Gen"):
|
852 |
auto_doc = {
|
853 |
"id": generate_unique_id(),
|
854 |
+
"pk": generate_unique_id(),
|
855 |
"name": f"Auto {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
856 |
"content": "Auto-generated record.",
|
857 |
"timestamp": datetime.now().isoformat()
|
|
|
866 |
new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
|
867 |
default_doc = {
|
868 |
"id": new_id,
|
869 |
+
"pk": new_id,
|
870 |
"name": "New Doc",
|
871 |
"content": "",
|
872 |
"timestamp": datetime.now().isoformat()
|
|
|
877 |
cleaned = preprocess_text(new_doc_str)
|
878 |
new_doc = json.loads(cleaned)
|
879 |
new_doc['id'] = new_id
|
880 |
+
new_doc['pk'] = new_id
|
881 |
success, message = insert_record(container, new_doc)
|
882 |
if success:
|
883 |
st.success(f"Created {new_doc['id']}")
|
|
|
935 |
shutil.rmtree(local_path)
|
936 |
else:
|
937 |
st.error("Missing token or URL πβ")
|
938 |
+
# Display AI Samples sidebar UI (processed from Python list)
|
939 |
+
display_ai_samples()
|
940 |
update_file_management_section()
|
941 |
except exceptions.CosmosHttpResponseError as e:
|
942 |
st.error(f"Cosmos error: {str(e)} π¨")
|