Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,7 +116,6 @@ def preprocess_text(text):
|
|
| 116 |
return text.strip()
|
| 117 |
|
| 118 |
def sanitize_json_text(text):
|
| 119 |
-
# Remove control characters but keep newlines intact
|
| 120 |
text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F]', '', text)
|
| 121 |
return text.strip()
|
| 122 |
|
|
@@ -156,7 +155,7 @@ def delete_record(container, record):
|
|
| 156 |
try:
|
| 157 |
doc_id = record["id"]
|
| 158 |
partition_key_value = record.get("pk", doc_id)
|
| 159 |
-
st.write(f"Deleting {doc_id} with partition key {partition_key_value}")
|
| 160 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 161 |
return True, f"Record {doc_id} deleted. ποΈ"
|
| 162 |
except exceptions.CosmosResourceNotFoundError:
|
|
@@ -347,14 +346,16 @@ def update_file_management_section():
|
|
| 347 |
# =============================================================================
|
| 348 |
# βββββββββββββ UI FUNCTIONS βββββββββββββ
|
| 349 |
# =============================================================================
|
| 350 |
-
def edit_all_documents(container):
|
| 351 |
-
st.markdown("### π All Documents")
|
| 352 |
documents = get_documents(container)
|
|
|
|
|
|
|
|
|
|
| 353 |
if not documents:
|
| 354 |
-
st.info("No documents in this container.")
|
| 355 |
return
|
| 356 |
|
| 357 |
-
# Initialize saved_docs if not present
|
| 358 |
if 'saved_docs' not in st.session_state:
|
| 359 |
st.session_state.saved_docs = {}
|
| 360 |
|
|
@@ -365,7 +366,6 @@ def edit_all_documents(container):
|
|
| 365 |
header = f"{doc.get('name', 'Unnamed')} - {formatted_ts}"
|
| 366 |
with st.expander(header):
|
| 367 |
doc_key = f"editor_{doc['id']}"
|
| 368 |
-
# Use saved_docs for initial value if available, else original doc
|
| 369 |
initial_value = st.session_state.saved_docs.get(doc['id'], json.dumps(doc, indent=2))
|
| 370 |
edited_content = st.text_area("Edit JSON", value=initial_value, height=300, key=doc_key)
|
| 371 |
col_save, col_delete = st.columns(2)
|
|
@@ -382,7 +382,7 @@ def edit_all_documents(container):
|
|
| 382 |
if success:
|
| 383 |
st.success(f"Saved {doc['id']}")
|
| 384 |
st.session_state.saved_docs[doc['id']] = json.dumps(updated_doc, indent=2)
|
| 385 |
-
st.rerun()
|
| 386 |
else:
|
| 387 |
st.error(message)
|
| 388 |
except json.JSONDecodeError as e:
|
|
@@ -472,39 +472,29 @@ def new_links_record(container):
|
|
| 472 |
else:
|
| 473 |
st.error(f"Error creating links record: {message}")
|
| 474 |
|
| 475 |
-
def vector_keyword_search(keyword,
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
return []
|
| 483 |
-
|
| 484 |
-
def display_search_results(keyword, container):
|
| 485 |
-
results = vector_keyword_search(keyword, container)
|
| 486 |
-
st.markdown("### π Search Results")
|
| 487 |
-
for res in results:
|
| 488 |
-
doc_id = res.get("id", "")
|
| 489 |
-
with st.expander(f"Result {doc_id}"):
|
| 490 |
-
edited = st.text_area("Edit Document", value=json.dumps(res, indent=2), key=f"search_{doc_id}")
|
| 491 |
-
if st.button(f"πΎ Save changes", key=f"save_search_{doc_id}"):
|
| 492 |
-
try:
|
| 493 |
-
updated_doc = json.loads(edited)
|
| 494 |
-
success, message = update_record(container, updated_doc)
|
| 495 |
-
if success:
|
| 496 |
-
st.success(f"Updated {doc_id}!")
|
| 497 |
-
st.rerun()
|
| 498 |
-
else:
|
| 499 |
-
st.error(message)
|
| 500 |
-
except Exception as e:
|
| 501 |
-
st.error(f"Error saving: {str(e)}")
|
| 502 |
|
| 503 |
def search_documents_ui(container):
|
| 504 |
with st.sidebar.form("search_form"):
|
| 505 |
keyword = st.text_input("Search Keyword", key="search_keyword")
|
| 506 |
-
|
| 507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
|
| 510 |
try:
|
|
@@ -633,6 +623,8 @@ def main():
|
|
| 633 |
st.session_state.selected_database = selected_db
|
| 634 |
st.session_state.selected_container = None
|
| 635 |
st.session_state.current_container = None
|
|
|
|
|
|
|
| 636 |
st.rerun()
|
| 637 |
|
| 638 |
# Containers Section
|
|
@@ -653,6 +645,8 @@ def main():
|
|
| 653 |
if selected_container != st.session_state.get("selected_container"):
|
| 654 |
st.session_state.selected_container = selected_container
|
| 655 |
st.session_state.current_container = database.get_container_client(selected_container)
|
|
|
|
|
|
|
| 656 |
st.rerun()
|
| 657 |
|
| 658 |
# Actions Section
|
|
@@ -677,9 +671,10 @@ def main():
|
|
| 677 |
new_links_record(st.session_state.current_container)
|
| 678 |
search_documents_ui(st.session_state.current_container)
|
| 679 |
|
| 680 |
-
# Central Area: Editable Documents
|
| 681 |
if st.session_state.current_container:
|
| 682 |
-
|
|
|
|
| 683 |
else:
|
| 684 |
st.info("Select a database and container to view and edit documents.")
|
| 685 |
|
|
@@ -694,6 +689,8 @@ def main():
|
|
| 694 |
st.session_state.selected_database = None
|
| 695 |
st.session_state.selected_container = None
|
| 696 |
st.session_state.current_container = None
|
|
|
|
|
|
|
| 697 |
st.rerun()
|
| 698 |
|
| 699 |
if __name__ == "__main__":
|
|
|
|
| 116 |
return text.strip()
|
| 117 |
|
| 118 |
def sanitize_json_text(text):
|
|
|
|
| 119 |
text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F]', '', text)
|
| 120 |
return text.strip()
|
| 121 |
|
|
|
|
| 155 |
try:
|
| 156 |
doc_id = record["id"]
|
| 157 |
partition_key_value = record.get("pk", doc_id)
|
| 158 |
+
st.write(f"Deleting {doc_id} with partition key {partition_key_value}")
|
| 159 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 160 |
return True, f"Record {doc_id} deleted. ποΈ"
|
| 161 |
except exceptions.CosmosResourceNotFoundError:
|
|
|
|
| 346 |
# =============================================================================
|
| 347 |
# βββββββββββββ UI FUNCTIONS βββββββββββββ
|
| 348 |
# =============================================================================
|
| 349 |
+
def edit_all_documents(container, search_keyword=None):
|
| 350 |
+
st.markdown("### π All Documents" + (f" (Filtered: '{search_keyword}')" if search_keyword else ""))
|
| 351 |
documents = get_documents(container)
|
| 352 |
+
if search_keyword:
|
| 353 |
+
# Filter documents by search keyword
|
| 354 |
+
documents = [doc for doc in documents if vector_keyword_search(search_keyword, doc)]
|
| 355 |
if not documents:
|
| 356 |
+
st.info("No documents match the current filter." if search_keyword else "No documents in this container.")
|
| 357 |
return
|
| 358 |
|
|
|
|
| 359 |
if 'saved_docs' not in st.session_state:
|
| 360 |
st.session_state.saved_docs = {}
|
| 361 |
|
|
|
|
| 366 |
header = f"{doc.get('name', 'Unnamed')} - {formatted_ts}"
|
| 367 |
with st.expander(header):
|
| 368 |
doc_key = f"editor_{doc['id']}"
|
|
|
|
| 369 |
initial_value = st.session_state.saved_docs.get(doc['id'], json.dumps(doc, indent=2))
|
| 370 |
edited_content = st.text_area("Edit JSON", value=initial_value, height=300, key=doc_key)
|
| 371 |
col_save, col_delete = st.columns(2)
|
|
|
|
| 382 |
if success:
|
| 383 |
st.success(f"Saved {doc['id']}")
|
| 384 |
st.session_state.saved_docs[doc['id']] = json.dumps(updated_doc, indent=2)
|
| 385 |
+
st.rerun()
|
| 386 |
else:
|
| 387 |
st.error(message)
|
| 388 |
except json.JSONDecodeError as e:
|
|
|
|
| 472 |
else:
|
| 473 |
st.error(f"Error creating links record: {message}")
|
| 474 |
|
| 475 |
+
def vector_keyword_search(keyword, doc):
|
| 476 |
+
# Check if keyword exists in any string field of the document
|
| 477 |
+
keyword = keyword.lower()
|
| 478 |
+
for key, value in doc.items():
|
| 479 |
+
if isinstance(value, str) and keyword in value.lower():
|
| 480 |
+
return True
|
| 481 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
def search_documents_ui(container):
|
| 484 |
with st.sidebar.form("search_form"):
|
| 485 |
keyword = st.text_input("Search Keyword", key="search_keyword")
|
| 486 |
+
col1, col2 = st.columns(2)
|
| 487 |
+
with col1:
|
| 488 |
+
search_submitted = st.form_submit_button("π Search")
|
| 489 |
+
with col2:
|
| 490 |
+
clear_submitted = st.form_submit_button("ποΈ Clear")
|
| 491 |
+
if search_submitted and keyword:
|
| 492 |
+
st.session_state.search_keyword = keyword
|
| 493 |
+
st.rerun()
|
| 494 |
+
if clear_submitted:
|
| 495 |
+
if 'search_keyword' in st.session_state:
|
| 496 |
+
del st.session_state.search_keyword
|
| 497 |
+
st.rerun()
|
| 498 |
|
| 499 |
def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
|
| 500 |
try:
|
|
|
|
| 623 |
st.session_state.selected_database = selected_db
|
| 624 |
st.session_state.selected_container = None
|
| 625 |
st.session_state.current_container = None
|
| 626 |
+
if 'search_keyword' in st.session_state:
|
| 627 |
+
del st.session_state.search_keyword
|
| 628 |
st.rerun()
|
| 629 |
|
| 630 |
# Containers Section
|
|
|
|
| 645 |
if selected_container != st.session_state.get("selected_container"):
|
| 646 |
st.session_state.selected_container = selected_container
|
| 647 |
st.session_state.current_container = database.get_container_client(selected_container)
|
| 648 |
+
if 'search_keyword' in st.session_state:
|
| 649 |
+
del st.session_state.search_keyword
|
| 650 |
st.rerun()
|
| 651 |
|
| 652 |
# Actions Section
|
|
|
|
| 671 |
new_links_record(st.session_state.current_container)
|
| 672 |
search_documents_ui(st.session_state.current_container)
|
| 673 |
|
| 674 |
+
# Central Area: Editable Documents with Search Filter
|
| 675 |
if st.session_state.current_container:
|
| 676 |
+
search_keyword = st.session_state.get('search_keyword', None)
|
| 677 |
+
edit_all_documents(st.session_state.current_container, search_keyword)
|
| 678 |
else:
|
| 679 |
st.info("Select a database and container to view and edit documents.")
|
| 680 |
|
|
|
|
| 689 |
st.session_state.selected_database = None
|
| 690 |
st.session_state.selected_container = None
|
| 691 |
st.session_state.current_container = None
|
| 692 |
+
if 'search_keyword' in st.session_state:
|
| 693 |
+
del st.session_state.search_keyword
|
| 694 |
st.rerun()
|
| 695 |
|
| 696 |
if __name__ == "__main__":
|