Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
# =============================================================================
|
| 3 |
# βββββββββββββ IMPORTS βββββββββββββ
|
| 4 |
# =============================================================================
|
|
@@ -150,11 +149,8 @@ def preprocess_text(text):
|
|
| 150 |
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
| 151 |
return text.strip()
|
| 152 |
|
| 153 |
-
# NEW: Sanitize JSON text before saving (remove problematic control characters)
|
| 154 |
def sanitize_json_text(text):
|
| 155 |
-
# Remove control characters except newline, carriage return, and tab
|
| 156 |
text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F]', '', text)
|
| 157 |
-
# Escape newline, tab, and carriage return
|
| 158 |
text = text.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")
|
| 159 |
return text
|
| 160 |
|
|
@@ -195,52 +191,15 @@ def delete_record(container, record):
|
|
| 195 |
if "id" not in record:
|
| 196 |
return False, "Record must contain an 'id' field. π"
|
| 197 |
doc_id = record["id"]
|
| 198 |
-
if "delete_log" not in st.session_state:
|
| 199 |
-
st.session_state.delete_log = []
|
| 200 |
-
st.session_state.delete_log.append(f"Attempting to delete document: {json.dumps(record, indent=2)}")
|
| 201 |
partition_key_value = record.get("pk", doc_id)
|
| 202 |
-
st.session_state.delete_log.append(f"Using ID and Partition Key: {partition_key_value}")
|
| 203 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 204 |
-
|
| 205 |
-
st.session_state.delete_log.append(success_msg)
|
| 206 |
-
return True, success_msg
|
| 207 |
except exceptions.CosmosResourceNotFoundError:
|
| 208 |
-
|
| 209 |
-
st.session_state.delete_log.append(success_msg)
|
| 210 |
-
return True, success_msg
|
| 211 |
except exceptions.CosmosHttpResponseError as e:
|
| 212 |
-
|
| 213 |
-
st.session_state.delete_log.append(error_msg)
|
| 214 |
-
return False, error_msg
|
| 215 |
except Exception as e:
|
| 216 |
-
|
| 217 |
-
st.session_state.delete_log.append(error_msg)
|
| 218 |
-
return False, error_msg
|
| 219 |
-
|
| 220 |
-
def save_to_cosmos_db(container, query, response1, response2):
|
| 221 |
-
try:
|
| 222 |
-
if container:
|
| 223 |
-
timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
|
| 224 |
-
unique_uuid = str(uuid.uuid4())
|
| 225 |
-
new_id = f"{timestamp}-{unique_uuid}"
|
| 226 |
-
record = {
|
| 227 |
-
"id": new_id,
|
| 228 |
-
"pk": new_id,
|
| 229 |
-
"name": new_id,
|
| 230 |
-
"query": query,
|
| 231 |
-
"response1": response1,
|
| 232 |
-
"response2": response2,
|
| 233 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 234 |
-
"type": "ai_response",
|
| 235 |
-
"version": "1.0"
|
| 236 |
-
}
|
| 237 |
-
container.create_item(body=record)
|
| 238 |
-
st.success(f"Saved: {record['id']}")
|
| 239 |
-
st.session_state.documents = get_documents(container)
|
| 240 |
-
else:
|
| 241 |
-
st.error("Cosmos container not initialized.")
|
| 242 |
-
except Exception as e:
|
| 243 |
-
st.error(f"Save error: {str(e)}")
|
| 244 |
|
| 245 |
def archive_current_container(database_name, container_name, client):
|
| 246 |
try:
|
|
@@ -264,881 +223,305 @@ def archive_current_container(database_name, container_name, client):
|
|
| 264 |
return f"Archive error: {str(e)} π’"
|
| 265 |
|
| 266 |
# =============================================================================
|
| 267 |
-
# βββββββββββββ ADVANCED COSMOS FUNCTIONS βββββββββββββ
|
| 268 |
# =============================================================================
|
| 269 |
-
|
| 270 |
-
analytical_storage_ttl=None, indexing_policy=None, vector_embedding_policy=None):
|
| 271 |
-
try:
|
| 272 |
-
if analytical_storage_ttl is not None:
|
| 273 |
-
container = database.create_container(
|
| 274 |
-
id=container_id,
|
| 275 |
-
partition_key=PartitionKey(path=partition_key_path),
|
| 276 |
-
analytical_storage_ttl=analytical_storage_ttl,
|
| 277 |
-
indexing_policy=indexing_policy,
|
| 278 |
-
vector_embedding_policy=vector_embedding_policy
|
| 279 |
-
)
|
| 280 |
-
else:
|
| 281 |
-
container = database.create_container(
|
| 282 |
-
id=container_id,
|
| 283 |
-
partition_key=PartitionKey(path=partition_key_path),
|
| 284 |
-
indexing_policy=indexing_policy,
|
| 285 |
-
vector_embedding_policy=vector_embedding_policy
|
| 286 |
-
)
|
| 287 |
-
except exceptions.CosmosHttpResponseError as e:
|
| 288 |
-
if analytical_storage_ttl is not None and "analyticalStorageTtl" in str(e):
|
| 289 |
-
try:
|
| 290 |
-
container = database.create_container(
|
| 291 |
-
id=container_id,
|
| 292 |
-
partition_key=PartitionKey(path=partition_key_path),
|
| 293 |
-
indexing_policy=indexing_policy,
|
| 294 |
-
vector_embedding_policy=vector_embedding_policy
|
| 295 |
-
)
|
| 296 |
-
except Exception as e2:
|
| 297 |
-
st.error(f"Error creating container without analytical_storage_ttl: {str(e2)}")
|
| 298 |
-
return None
|
| 299 |
-
elif isinstance(e, exceptions.CosmosResourceExistsError):
|
| 300 |
-
container = database.get_container_client(container_id)
|
| 301 |
-
else:
|
| 302 |
-
st.error(f"Error creating container: {str(e)}")
|
| 303 |
-
return None
|
| 304 |
-
return container
|
| 305 |
-
|
| 306 |
-
def advanced_insert_item(container, item):
|
| 307 |
-
try:
|
| 308 |
-
container.upsert_item(item)
|
| 309 |
-
return True, f"Item {item.get('id', '')} inserted. β"
|
| 310 |
-
except Exception as e:
|
| 311 |
-
return False, str(e)
|
| 312 |
-
|
| 313 |
-
def advanced_update_item(container, item):
|
| 314 |
-
try:
|
| 315 |
-
container.upsert_item(item)
|
| 316 |
-
return True, f"Item {item.get('id', '')} updated. βοΈ"
|
| 317 |
-
except Exception as e:
|
| 318 |
-
return False, str(e)
|
| 319 |
-
|
| 320 |
-
def advanced_delete_item(container, item_id, partition_key_value):
|
| 321 |
-
try:
|
| 322 |
-
container.delete_item(item=item_id, partition_key=partition_key_value)
|
| 323 |
-
return True, f"Item {item_id} deleted. ποΈ"
|
| 324 |
-
except Exception as e:
|
| 325 |
-
return False, str(e)
|
| 326 |
-
|
| 327 |
-
def vector_search(container, query_vector, vector_field, top=10, exact_search=False):
|
| 328 |
-
query_vector_str = json.dumps(query_vector)
|
| 329 |
-
query = f"""SELECT TOP {top} c.id, VectorDistance(c.{vector_field}, {query_vector_str}, {str(exact_search).lower()},
|
| 330 |
-
{{'dataType':'float32','distanceFunction':'cosine'}}) AS SimilarityScore
|
| 331 |
-
FROM c ORDER BY SimilarityScore"""
|
| 332 |
-
results = list(container.query_items(query=query, enable_cross_partition_query=True))
|
| 333 |
-
return results
|
| 334 |
|
| 335 |
# =============================================================================
|
| 336 |
-
# βββββββββββββ
|
|
|
|
|
|
|
| 337 |
# =============================================================================
|
| 338 |
-
def download_github_repo(url, local_path):
|
| 339 |
-
if os.path.exists(local_path):
|
| 340 |
-
shutil.rmtree(local_path)
|
| 341 |
-
Repo.clone_from(url, local_path)
|
| 342 |
-
|
| 343 |
-
def create_zip_file(source_dir, output_filename):
|
| 344 |
-
shutil.make_archive(output_filename, 'zip', source_dir)
|
| 345 |
|
| 346 |
-
def
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
)
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
content = load_file(file_path)
|
| 405 |
-
if content is not None:
|
| 406 |
-
st.session_state.file_content[file_path] = content
|
| 407 |
-
else:
|
| 408 |
return
|
| 409 |
-
|
| 410 |
-
st.markdown(f"**Editing:** {file_path}")
|
| 411 |
-
md_tab, code_tab = st.tabs(["Markdown", "Code"])
|
| 412 |
-
with md_tab:
|
| 413 |
-
st.markdown(st.session_state.file_content[file_path])
|
| 414 |
-
with code_tab:
|
| 415 |
-
new_content = st.text_area("Edit:", value=st.session_state.file_content[file_path], height=400, key=f"editor_{hash(file_path)}", on_change=lambda: auto_save_edit())
|
| 416 |
-
col1, col2 = st.columns([1, 5])
|
| 417 |
-
with col1:
|
| 418 |
-
if st.button("πΎ Save"):
|
| 419 |
-
sanitized = sanitize_json_text(new_content)
|
| 420 |
-
try:
|
| 421 |
-
json.loads(sanitized)
|
| 422 |
-
except Exception as e:
|
| 423 |
-
st.error(f"Sanitization failed: {str(e)}")
|
| 424 |
-
return
|
| 425 |
-
if save_file_content(file_path, sanitized):
|
| 426 |
-
st.session_state.file_content[file_path] = sanitized
|
| 427 |
-
st.success("Saved! π")
|
| 428 |
-
time.sleep(1)
|
| 429 |
-
st.rerun()
|
| 430 |
-
with col2:
|
| 431 |
-
st.download_button("β¬οΈ", data=new_content, file_name=os.path.basename(file_path), mime="text/markdown")
|
| 432 |
-
|
| 433 |
-
def save_file_content(file_path, content):
|
| 434 |
-
try:
|
| 435 |
-
with open(file_path, 'w', encoding='utf-8') as file:
|
| 436 |
-
file.write(content)
|
| 437 |
-
return True
|
| 438 |
-
except Exception as e:
|
| 439 |
-
st.error(f"Save error: {str(e)}")
|
| 440 |
-
return False
|
| 441 |
-
|
| 442 |
-
def update_file_management_section():
|
| 443 |
-
if 'file_view_mode' not in st.session_state:
|
| 444 |
-
st.session_state.file_view_mode = None
|
| 445 |
-
if 'current_file' not in st.session_state:
|
| 446 |
-
st.session_state.current_file = None
|
| 447 |
-
if 'file_content' not in st.session_state:
|
| 448 |
-
st.session_state.file_content = {}
|
| 449 |
-
all_files = sorted(glob.glob("*.md"), reverse=True)
|
| 450 |
-
st.sidebar.title("π Files")
|
| 451 |
-
if st.sidebar.button("π Delete All"):
|
| 452 |
-
for file in all_files:
|
| 453 |
-
os.remove(file)
|
| 454 |
-
st.session_state.file_content = {}
|
| 455 |
-
st.session_state.current_file = None
|
| 456 |
-
st.session_state.file_view_mode = None
|
| 457 |
-
st.rerun()
|
| 458 |
-
if st.sidebar.button("β¬οΈ Download All"):
|
| 459 |
-
zip_file = create_zip_of_files(all_files)
|
| 460 |
-
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
| 461 |
-
for file in all_files:
|
| 462 |
-
col1, col2, col3, col4 = st.sidebar.columns([1, 3, 1, 1])
|
| 463 |
-
with col1:
|
| 464 |
-
if st.button("π", key=f"view_{file}"):
|
| 465 |
-
st.session_state.current_file = file
|
| 466 |
-
st.session_state.file_view_mode = 'view'
|
| 467 |
-
if file not in st.session_state.file_content:
|
| 468 |
-
content = load_file(file)
|
| 469 |
-
if content is not None:
|
| 470 |
-
st.session_state.file_content[file] = content
|
| 471 |
-
st.rerun()
|
| 472 |
-
with col2:
|
| 473 |
-
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
| 474 |
-
with col3:
|
| 475 |
-
if st.button("π", key=f"edit_{file}"):
|
| 476 |
-
st.session_state.current_file = file
|
| 477 |
-
st.session_state.file_view_mode = 'edit'
|
| 478 |
-
if file not in st.session_state.file_content:
|
| 479 |
-
content = load_file(file)
|
| 480 |
-
if content is not None:
|
| 481 |
-
st.session_state.file_content[file] = content
|
| 482 |
-
st.rerun()
|
| 483 |
-
with col4:
|
| 484 |
-
if st.button("π", key=f"delete_{file}"):
|
| 485 |
-
os.remove(file)
|
| 486 |
-
if file in st.session_state.file_content:
|
| 487 |
-
del st.session_state.file_content[file]
|
| 488 |
-
if st.session_state.current_file == file:
|
| 489 |
-
st.session_state.current_file = None
|
| 490 |
-
st.session_state.file_view_mode = None
|
| 491 |
-
st.rerun()
|
| 492 |
-
st.sidebar.markdown("---")
|
| 493 |
-
st.sidebar.title("External Help Links")
|
| 494 |
-
for link in external_links:
|
| 495 |
-
st.sidebar.markdown(f"{link['emoji']} [{link['title']}]({link['url']})", unsafe_allow_html=True)
|
| 496 |
-
if st.session_state.current_file:
|
| 497 |
-
if st.session_state.file_view_mode == 'view':
|
| 498 |
-
display_file_viewer(st.session_state.current_file)
|
| 499 |
-
elif st.session_state.file_view_mode == 'edit':
|
| 500 |
-
display_file_editor(st.session_state.current_file)
|
| 501 |
-
|
| 502 |
-
# =============================================================================
|
| 503 |
-
# βββββββββββββ SIDEBAR DATA GRID (Records with formatted timestamps) βββββββββββββ
|
| 504 |
-
# =============================================================================
|
| 505 |
-
def show_sidebar_data_grid():
|
| 506 |
-
if st.session_state.get("current_container"):
|
| 507 |
try:
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
})
|
| 522 |
-
|
| 523 |
-
st.sidebar.markdown("### π Data Grid")
|
| 524 |
-
st.sidebar.dataframe(df[["Name", "Timestamp"]])
|
| 525 |
except Exception as e:
|
| 526 |
-
st.
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
edited = st.text_area("Edit Document", value=json.dumps(res, indent=2), key=f"search_{doc_id}")
|
| 541 |
-
if st.button(f"πΎ Save changes for {doc_id}", key=f"save_search_{doc_id}"):
|
| 542 |
-
try:
|
| 543 |
-
updated_doc = json.loads(edited)
|
| 544 |
-
container.upsert_item(body=updated_doc)
|
| 545 |
-
st.success(f"Updated {doc_id}!")
|
| 546 |
-
st.experimental_rerun()
|
| 547 |
-
except Exception as e:
|
| 548 |
-
st.error(f"Error saving {doc_id}: {str(e)}")
|
| 549 |
-
|
| 550 |
-
# =============================================================================
|
| 551 |
-
# βββββββββββββ DOCUMENTS LIST VIEW (Editable List with Sorting)
|
| 552 |
-
# =============================================================================
|
| 553 |
-
def edit_documents_list(container):
|
| 554 |
-
records = get_documents(container)
|
| 555 |
-
sort_option = st.selectbox("Sort by", ["Timestamp", "Name"], key="sort_option")
|
| 556 |
-
if sort_option == "Name":
|
| 557 |
-
records.sort(key=lambda r: r.get("name", "").lower())
|
| 558 |
-
else:
|
| 559 |
-
records.sort(key=lambda r: r.get("timestamp", ""), reverse=True)
|
| 560 |
-
data = []
|
| 561 |
-
for rec in records:
|
| 562 |
-
ts = rec.get("timestamp", "")
|
| 563 |
try:
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
st.experimental_rerun()
|
| 591 |
-
|
| 592 |
-
# =============================================================================
|
| 593 |
-
# βββββββββββββ VIDEO & AUDIO UI FUNCTIONS βββββββββββββ
|
| 594 |
-
# =============================================================================
|
| 595 |
-
def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
|
| 596 |
-
try:
|
| 597 |
-
st.write("Preprocessing image...")
|
| 598 |
-
if isinstance(file_data, bytes):
|
| 599 |
-
img = Image.open(io.BytesIO(file_data))
|
| 600 |
-
elif hasattr(file_data, 'read'):
|
| 601 |
-
if hasattr(file_data, 'seek'):
|
| 602 |
-
file_data.seek(0)
|
| 603 |
-
img = Image.open(file_data)
|
| 604 |
-
elif isinstance(file_data, Image.Image):
|
| 605 |
-
img = file_data
|
| 606 |
-
else:
|
| 607 |
-
raise ValueError(f"Unsupported input: {type(file_data)}")
|
| 608 |
-
if img.mode != 'RGB':
|
| 609 |
-
img = img.convert('RGB')
|
| 610 |
-
aspect_ratio = img.size[0] / img.size[1]
|
| 611 |
-
if aspect_ratio > target_size[0] / target_size[1]:
|
| 612 |
-
new_width = target_size[0]
|
| 613 |
-
new_height = int(new_width / aspect_ratio)
|
| 614 |
-
else:
|
| 615 |
-
new_height = target_size[1]
|
| 616 |
-
new_width = int(new_height * aspect_ratio)
|
| 617 |
-
new_width = (new_width // 2) * 2
|
| 618 |
-
new_height = (new_height // 2) * 2
|
| 619 |
-
resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 620 |
-
final_img = Image.new('RGB', target_size, (255, 255, 255))
|
| 621 |
-
paste_x = (target_size[0] - new_width) // 2
|
| 622 |
-
paste_y = (target_size[1] - new_height) // 2
|
| 623 |
-
final_img.paste(resized_img, (paste_x, paste_y))
|
| 624 |
-
return final_img
|
| 625 |
-
except Exception as e:
|
| 626 |
-
st.error(f"Image error: {str(e)}")
|
| 627 |
-
return None
|
| 628 |
-
|
| 629 |
-
def add_video_generation_ui(container):
|
| 630 |
-
st.markdown("### π₯ Video Gen")
|
| 631 |
-
col1, col2 = st.columns([2, 1])
|
| 632 |
-
with col1:
|
| 633 |
-
uploaded_file = st.file_uploader("Upload Image πΌοΈ", type=['png', 'jpg', 'jpeg'])
|
| 634 |
-
with col2:
|
| 635 |
-
st.markdown("#### Params")
|
| 636 |
-
motion = st.slider("π Motion", 1, 255, 127)
|
| 637 |
-
fps = st.slider("π¬ FPS", 1, 30, 6)
|
| 638 |
-
with st.expander("Advanced"):
|
| 639 |
-
use_custom = st.checkbox("Custom Seed")
|
| 640 |
-
seed = st.number_input("Seed", value=int(time.time() * 1000)) if use_custom else None
|
| 641 |
-
if uploaded_file is not None:
|
| 642 |
try:
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
st.write("Processed")
|
| 652 |
-
st.image(proc_img, use_column_width=True)
|
| 653 |
-
else:
|
| 654 |
-
st.error("Preprocess failed")
|
| 655 |
-
return
|
| 656 |
-
if st.button("π₯ Generate"):
|
| 657 |
-
with st.spinner("Generating video..."):
|
| 658 |
-
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
| 659 |
-
proc_img.save(temp_file.name, format='PNG')
|
| 660 |
-
try:
|
| 661 |
-
client = Client("awacke1/stable-video-diffusion", hf_token=os.environ.get("HUGGINGFACE_TOKEN"))
|
| 662 |
-
result = client.predict(
|
| 663 |
-
image=temp_file.name,
|
| 664 |
-
seed=seed if seed is not None else int(time.time() * 1000),
|
| 665 |
-
randomize_seed=seed is None,
|
| 666 |
-
motion_bucket_id=motion,
|
| 667 |
-
fps_id=fps,
|
| 668 |
-
api_name="/video"
|
| 669 |
-
)
|
| 670 |
-
if result and isinstance(result, tuple) and len(result) >= 1:
|
| 671 |
-
video_path = result[0].get('video') if isinstance(result[0], dict) else None
|
| 672 |
-
if video_path and os.path.exists(video_path):
|
| 673 |
-
video_filename = f"generated_video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
| 674 |
-
shutil.copy(video_path, video_filename)
|
| 675 |
-
st.success(f"Video generated! π")
|
| 676 |
-
st.video(video_filename)
|
| 677 |
-
if container:
|
| 678 |
-
video_record = {
|
| 679 |
-
"id": generate_unique_id(),
|
| 680 |
-
"pk": generate_unique_id(),
|
| 681 |
-
"type": "generated_video",
|
| 682 |
-
"filename": video_filename,
|
| 683 |
-
"seed": seed if seed is not None else "random",
|
| 684 |
-
"motion": motion,
|
| 685 |
-
"fps": fps,
|
| 686 |
-
"timestamp": datetime.now().isoformat()
|
| 687 |
-
}
|
| 688 |
-
success, message = insert_record(container, video_record)
|
| 689 |
-
if success:
|
| 690 |
-
st.success("DB record saved!")
|
| 691 |
-
else:
|
| 692 |
-
st.error(f"DB error: {message}")
|
| 693 |
-
else:
|
| 694 |
-
st.error("Invalid result format")
|
| 695 |
-
else:
|
| 696 |
-
st.error("No result returned")
|
| 697 |
-
except Exception as e:
|
| 698 |
-
st.error(f"Video gen error: {str(e)}")
|
| 699 |
-
finally:
|
| 700 |
-
try:
|
| 701 |
-
os.unlink(temp_file.name)
|
| 702 |
-
st.write("Temp file removed")
|
| 703 |
-
except Exception as e:
|
| 704 |
-
st.warning(f"Cleanup error: {str(e)}")
|
| 705 |
except Exception as e:
|
| 706 |
-
st.error(f"
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
}
|
| 721 |
-
success, message = insert_record(container, default_doc)
|
| 722 |
-
if success:
|
| 723 |
-
st.success("New sample document created! β¨")
|
| 724 |
-
return default_doc
|
| 725 |
-
else:
|
| 726 |
-
st.error("Error creating new item: " + message)
|
| 727 |
-
return None
|
| 728 |
-
|
| 729 |
-
def auto_save_edit():
|
| 730 |
-
try:
|
| 731 |
-
edited_str = st.session_state.doc_editor
|
| 732 |
-
try:
|
| 733 |
-
json.loads(edited_str)
|
| 734 |
-
except Exception:
|
| 735 |
-
edited_str = sanitize_json_text(edited_str)
|
| 736 |
-
edited_doc = json.loads(edited_str)
|
| 737 |
-
container = st.session_state.current_container
|
| 738 |
-
container.upsert_item(edited_doc)
|
| 739 |
-
st.success("Auto-saved! πΎ")
|
| 740 |
-
except Exception as e:
|
| 741 |
-
st.error(f"Auto-save error: {str(e)}")
|
| 742 |
-
|
| 743 |
-
def add_field_to_doc():
|
| 744 |
-
key = st.session_state.new_field_key
|
| 745 |
-
value = st.session_state.new_field_value
|
| 746 |
-
try:
|
| 747 |
-
doc = json.loads(st.session_state.doc_editor)
|
| 748 |
-
doc[key] = value
|
| 749 |
-
st.session_state.doc_editor = json.dumps(doc, indent=2)
|
| 750 |
-
auto_save_edit()
|
| 751 |
-
st.success(f"Added field {key} π")
|
| 752 |
-
except Exception as e:
|
| 753 |
-
st.error(f"Error adding field: {str(e)}")
|
| 754 |
-
|
| 755 |
-
# =============================================================================
|
| 756 |
-
# βββββββββββββ VECTOR SEARCH INTERFACE (Simple keyword search)
|
| 757 |
-
# =============================================================================
|
| 758 |
-
def vector_keyword_search(keyword, container):
|
| 759 |
-
try:
|
| 760 |
-
query = f"SELECT * FROM c WHERE CONTAINS(c.content, '{keyword}')"
|
| 761 |
-
results = list(container.query_items(query=query, enable_cross_partition_query=True))
|
| 762 |
-
return results
|
| 763 |
-
except Exception as e:
|
| 764 |
-
st.error(f"Vector search error: {str(e)}")
|
| 765 |
-
return []
|
| 766 |
-
|
| 767 |
-
# =============================================================================
|
| 768 |
-
# βββββββββββββ NEW AI MODALITY RECORD TEMPLATES
|
| 769 |
-
# =============================================================================
|
| 770 |
-
def new_ai_record(container):
|
| 771 |
-
new_id = generate_unique_id()
|
| 772 |
-
default_doc = {
|
| 773 |
-
"id": new_id,
|
| 774 |
-
"pk": new_id,
|
| 775 |
-
"name": "AI Modality Record",
|
| 776 |
-
"function_url": "https://example.com/function",
|
| 777 |
-
"input_text": "### Input (markdown)\n\nType your input here.",
|
| 778 |
-
"output_text": "### Output (markdown)\n\nResult will appear here.",
|
| 779 |
-
"timestamp": datetime.now().isoformat(),
|
| 780 |
-
"type": "ai_modality"
|
| 781 |
-
}
|
| 782 |
-
success, message = insert_record(container, default_doc)
|
| 783 |
-
if success:
|
| 784 |
-
st.success("New AI modality record created! π‘")
|
| 785 |
-
return default_doc
|
| 786 |
-
else:
|
| 787 |
-
st.error("Error creating AI record: " + message)
|
| 788 |
-
return None
|
| 789 |
-
|
| 790 |
-
def new_links_record(container):
|
| 791 |
-
new_id = generate_unique_id()
|
| 792 |
-
links_md = "\n".join([f"- {link['emoji']} [{link['title']}]({link['url']})" for link in external_links])
|
| 793 |
-
default_doc = {
|
| 794 |
-
"id": new_id,
|
| 795 |
-
"pk": new_id,
|
| 796 |
-
"name": "Portal Links Record",
|
| 797 |
-
"function_url": "",
|
| 798 |
-
"input_text": links_md,
|
| 799 |
-
"output_text": "",
|
| 800 |
-
"timestamp": datetime.now().isoformat(),
|
| 801 |
-
"type": "ai_modality"
|
| 802 |
-
}
|
| 803 |
-
success, message = insert_record(container, default_doc)
|
| 804 |
-
if success:
|
| 805 |
-
st.success("New Portal Links record created! π")
|
| 806 |
-
return default_doc
|
| 807 |
-
else:
|
| 808 |
-
st.error("Error creating links record: " + message)
|
| 809 |
-
return None
|
| 810 |
-
|
| 811 |
-
# =============================================================================
|
| 812 |
-
# βββββββββββββ SIDEBAR DATA GRID (Editable Names Grid)
|
| 813 |
-
# =============================================================================
|
| 814 |
-
def edit_names_grid(container):
|
| 815 |
-
records = get_documents(container)
|
| 816 |
-
data = []
|
| 817 |
-
for rec in records:
|
| 818 |
-
ts = rec.get("timestamp", "")
|
| 819 |
try:
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
else:
|
| 843 |
-
st.sidebar.error(f"Update error for {doc_id}: {message}")
|
| 844 |
-
st.experimental_rerun()
|
| 845 |
-
|
| 846 |
-
# =============================================================================
|
| 847 |
-
# βββββββββββββ SEARCH RESULTS DISPLAY (Editable Code Editors)
|
| 848 |
-
# =============================================================================
|
| 849 |
-
def display_search_results(keyword, container):
|
| 850 |
-
results = vector_keyword_search(keyword, container)
|
| 851 |
-
st.markdown("### π Search Results")
|
| 852 |
-
for res in results:
|
| 853 |
-
doc_id = res.get("id", "")
|
| 854 |
-
exp = st.expander(f"Result {doc_id}")
|
| 855 |
-
with exp:
|
| 856 |
-
edited = st.text_area("Edit Document", value=json.dumps(res, indent=2), key=f"search_{doc_id}")
|
| 857 |
-
if st.button(f"πΎ Save changes for {doc_id}", key=f"save_search_{doc_id}"):
|
| 858 |
-
try:
|
| 859 |
-
updated_doc = json.loads(edited)
|
| 860 |
-
container.upsert_item(body=updated_doc)
|
| 861 |
-
st.success(f"Updated {doc_id}!")
|
| 862 |
-
st.experimental_rerun()
|
| 863 |
-
except Exception as e:
|
| 864 |
-
st.error(f"Error saving {doc_id}: {str(e)}")
|
| 865 |
-
|
| 866 |
-
# =============================================================================
|
| 867 |
-
# βββββββββββββ DOCUMENTS LIST VIEW (Editable List with Sorting)
|
| 868 |
-
# =============================================================================
|
| 869 |
-
def edit_documents_list(container):
|
| 870 |
-
records = get_documents(container)
|
| 871 |
-
sort_option = st.selectbox("Sort by", ["Timestamp", "Name"], key="sort_option")
|
| 872 |
-
if sort_option == "Name":
|
| 873 |
-
records.sort(key=lambda r: r.get("name", "").lower())
|
| 874 |
-
else:
|
| 875 |
-
records.sort(key=lambda r: r.get("timestamp", ""), reverse=True)
|
| 876 |
-
data = []
|
| 877 |
-
for rec in records:
|
| 878 |
-
ts = rec.get("timestamp", "")
|
| 879 |
try:
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
doc = next((r for r in records if r.get("id") == doc_id), None)
|
| 898 |
-
if doc:
|
| 899 |
-
doc["name"] = row["Name"]
|
| 900 |
-
doc["content"] = row["Content"]
|
| 901 |
-
success, message = update_record(container, doc)
|
| 902 |
-
if success:
|
| 903 |
-
st.success(f"Updated {doc_id} π")
|
| 904 |
-
else:
|
| 905 |
-
st.error(f"Error updating {doc_id}: {message}")
|
| 906 |
-
st.experimental_rerun()
|
| 907 |
-
|
| 908 |
-
# =============================================================================
|
| 909 |
-
# βββββββββββββ VIDEO & AUDIO UI FUNCTIONS βββββββββββββ
|
| 910 |
-
# =============================================================================
|
| 911 |
-
def validate_and_preprocess_image(file_data, target_size=(576, 1024)):
|
| 912 |
-
try:
|
| 913 |
-
st.write("Preprocessing image...")
|
| 914 |
-
if isinstance(file_data, bytes):
|
| 915 |
-
img = Image.open(io.BytesIO(file_data))
|
| 916 |
-
elif hasattr(file_data, 'read'):
|
| 917 |
-
if hasattr(file_data, 'seek'):
|
| 918 |
-
file_data.seek(0)
|
| 919 |
-
img = Image.open(file_data)
|
| 920 |
-
elif isinstance(file_data, Image.Image):
|
| 921 |
-
img = file_data
|
| 922 |
-
else:
|
| 923 |
-
raise ValueError(f"Unsupported input: {type(file_data)}")
|
| 924 |
-
if img.mode != 'RGB':
|
| 925 |
-
img = img.convert('RGB')
|
| 926 |
-
aspect_ratio = img.size[0] / img.size[1]
|
| 927 |
-
if aspect_ratio > target_size[0] / target_size[1]:
|
| 928 |
-
new_width = target_size[0]
|
| 929 |
-
new_height = int(new_width / aspect_ratio)
|
| 930 |
-
else:
|
| 931 |
-
new_height = target_size[1]
|
| 932 |
-
new_width = int(new_height * aspect_ratio)
|
| 933 |
-
new_width = (new_width // 2) * 2
|
| 934 |
-
new_height = (new_height // 2) * 2
|
| 935 |
-
resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 936 |
-
final_img = Image.new('RGB', target_size, (255, 255, 255))
|
| 937 |
-
paste_x = (target_size[0] - new_width) // 2
|
| 938 |
-
paste_y = (target_size[1] - new_height) // 2
|
| 939 |
-
final_img.paste(resized_img, (paste_x, paste_y))
|
| 940 |
-
return final_img
|
| 941 |
-
except Exception as e:
|
| 942 |
-
st.error(f"Image error: {str(e)}")
|
| 943 |
-
return None
|
| 944 |
-
|
| 945 |
-
def add_video_generation_ui(container):
|
| 946 |
-
st.markdown("### π₯ Video Gen")
|
| 947 |
-
col1, col2 = st.columns([2, 1])
|
| 948 |
-
with col1:
|
| 949 |
-
uploaded_file = st.file_uploader("Upload Image πΌοΈ", type=['png', 'jpg', 'jpeg'])
|
| 950 |
-
with col2:
|
| 951 |
-
st.markdown("#### Params")
|
| 952 |
-
motion = st.slider("π Motion", 1, 255, 127)
|
| 953 |
-
fps = st.slider("π¬ FPS", 1, 30, 6)
|
| 954 |
-
with st.expander("Advanced"):
|
| 955 |
-
use_custom = st.checkbox("Custom Seed")
|
| 956 |
-
seed = st.number_input("Seed", value=int(time.time() * 1000)) if use_custom else None
|
| 957 |
-
if uploaded_file is not None:
|
| 958 |
try:
|
| 959 |
-
|
| 960 |
-
|
| 961 |
-
with
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
st.image(proc_img, use_column_width=True)
|
| 969 |
-
else:
|
| 970 |
-
st.error("Preprocess failed")
|
| 971 |
-
return
|
| 972 |
-
if st.button("π₯ Generate"):
|
| 973 |
-
with st.spinner("Generating video..."):
|
| 974 |
-
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
| 975 |
-
proc_img.save(temp_file.name, format='PNG')
|
| 976 |
-
try:
|
| 977 |
-
client = Client("awacke1/stable-video-diffusion", hf_token=os.environ.get("HUGGINGFACE_TOKEN"))
|
| 978 |
-
result = client.predict(
|
| 979 |
-
image=temp_file.name,
|
| 980 |
-
seed=seed if seed is not None else int(time.time() * 1000),
|
| 981 |
-
randomize_seed=seed is None,
|
| 982 |
-
motion_bucket_id=motion,
|
| 983 |
-
fps_id=fps,
|
| 984 |
-
api_name="/video"
|
| 985 |
-
)
|
| 986 |
-
if result and isinstance(result, tuple) and len(result) >= 1:
|
| 987 |
-
video_path = result[0].get('video') if isinstance(result[0], dict) else None
|
| 988 |
-
if video_path and os.path.exists(video_path):
|
| 989 |
-
video_filename = f"generated_video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
| 990 |
-
shutil.copy(video_path, video_filename)
|
| 991 |
-
st.success(f"Video generated! π")
|
| 992 |
-
st.video(video_filename)
|
| 993 |
-
if container:
|
| 994 |
-
video_record = {
|
| 995 |
-
"id": generate_unique_id(),
|
| 996 |
-
"pk": generate_unique_id(),
|
| 997 |
-
"type": "generated_video",
|
| 998 |
-
"filename": video_filename,
|
| 999 |
-
"seed": seed if seed is not None else "random",
|
| 1000 |
-
"motion": motion,
|
| 1001 |
-
"fps": fps,
|
| 1002 |
-
"timestamp": datetime.now().isoformat()
|
| 1003 |
-
}
|
| 1004 |
-
success, message = insert_record(container, video_record)
|
| 1005 |
-
if success:
|
| 1006 |
-
st.success("DB record saved!")
|
| 1007 |
-
else:
|
| 1008 |
-
st.error(f"DB error: {message}")
|
| 1009 |
-
else:
|
| 1010 |
-
st.error("Invalid result format")
|
| 1011 |
-
else:
|
| 1012 |
-
st.error("No result returned")
|
| 1013 |
-
except Exception as e:
|
| 1014 |
-
st.error(f"Video gen error: {str(e)}")
|
| 1015 |
-
finally:
|
| 1016 |
-
try:
|
| 1017 |
-
os.unlink(temp_file.name)
|
| 1018 |
-
st.write("Temp file removed")
|
| 1019 |
-
except Exception as e:
|
| 1020 |
-
st.warning(f"Cleanup error: {str(e)}")
|
| 1021 |
except Exception as e:
|
| 1022 |
-
st.error(f"
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
}
|
| 1037 |
-
success, message = insert_record(container, default_doc)
|
| 1038 |
-
if success:
|
| 1039 |
-
st.success("New sample document created! β¨")
|
| 1040 |
-
return default_doc
|
| 1041 |
-
else:
|
| 1042 |
-
st.error("Error creating new item: " + message)
|
| 1043 |
-
return None
|
| 1044 |
-
|
| 1045 |
-
def auto_save_edit():
|
| 1046 |
-
try:
|
| 1047 |
-
edited_str = st.session_state.doc_editor
|
| 1048 |
-
try:
|
| 1049 |
-
json.loads(edited_str)
|
| 1050 |
-
except Exception:
|
| 1051 |
-
edited_str = sanitize_json_text(edited_str)
|
| 1052 |
-
edited_doc = json.loads(edited_str)
|
| 1053 |
-
container = st.session_state.current_container
|
| 1054 |
-
container.upsert_item(edited_doc)
|
| 1055 |
-
st.success("Auto-saved! πΎ")
|
| 1056 |
-
except Exception as e:
|
| 1057 |
-
st.error(f"Auto-save error: {str(e)}")
|
| 1058 |
-
|
| 1059 |
-
def add_field_to_doc():
|
| 1060 |
-
key = st.session_state.new_field_key
|
| 1061 |
-
value = st.session_state.new_field_value
|
| 1062 |
-
try:
|
| 1063 |
-
doc = json.loads(st.session_state.doc_editor)
|
| 1064 |
-
doc[key] = value
|
| 1065 |
-
st.session_state.doc_editor = json.dumps(doc, indent=2)
|
| 1066 |
-
auto_save_edit()
|
| 1067 |
-
st.success(f"Added field {key} π")
|
| 1068 |
-
except Exception as e:
|
| 1069 |
-
st.error(f"Error adding field: {str(e)}")
|
| 1070 |
-
|
| 1071 |
-
# =============================================================================
|
| 1072 |
-
# βββββββββββββ SEARCH RESULTS DISPLAY (Editable Code Editors)
|
| 1073 |
-
# =============================================================================
|
| 1074 |
-
def display_search_results(keyword, container):
|
| 1075 |
-
results = vector_keyword_search(keyword, container)
|
| 1076 |
-
st.markdown("### π Search Results")
|
| 1077 |
-
for res in results:
|
| 1078 |
-
doc_id = res.get("id", "")
|
| 1079 |
-
exp = st.expander(f"Result {doc_id}")
|
| 1080 |
-
with exp:
|
| 1081 |
-
edited = st.text_area("Edit Document", value=json.dumps(res, indent=2), key=f"search_{doc_id}")
|
| 1082 |
-
if st.button(f"πΎ Save changes for {doc_id}", key=f"save_search_{doc_id}"):
|
| 1083 |
-
try:
|
| 1084 |
-
updated_doc = json.loads(edited)
|
| 1085 |
-
container.upsert_item(body=updated_doc)
|
| 1086 |
-
st.success(f"Updated {doc_id}!")
|
| 1087 |
-
st.experimental_rerun()
|
| 1088 |
-
except Exception as e:
|
| 1089 |
-
st.error(f"Error saving {doc_id}: {str(e)}")
|
| 1090 |
-
|
| 1091 |
-
# =============================================================================
|
| 1092 |
-
# βββββββββββββ DOCUMENTS LIST VIEW (Editable List with Sorting)
|
| 1093 |
-
# =============================================================================
|
| 1094 |
-
def edit_documents_list(container):
|
| 1095 |
-
records = get_documents(container)
|
| 1096 |
-
sort_option = st.selectbox("Sort by", ["Timestamp", "Name"], key="sort_option")
|
| 1097 |
-
if sort_option == "Name":
|
| 1098 |
-
records.sort(key=lambda r: r.get("name", "").lower())
|
| 1099 |
-
else:
|
| 1100 |
-
records.sort(key=lambda r: r.get("timestamp", ""), reverse=True)
|
| 1101 |
-
data = []
|
| 1102 |
-
for rec in records:
|
| 1103 |
-
ts = rec.get("timestamp", "")
|
| 1104 |
try:
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1132 |
|
| 1133 |
# =============================================================================
|
| 1134 |
-
# βββββββββββββ
|
| 1135 |
# =============================================================================
|
| 1136 |
-
|
| 1137 |
-
with st.sidebar.form("search_form"):
|
| 1138 |
-
keyword = st.text_input("Search Keyword", key="search_keyword")
|
| 1139 |
-
submitted = st.form_submit_button("π Search")
|
| 1140 |
-
if submitted and keyword:
|
| 1141 |
-
display_search_results(keyword, container)
|
| 1142 |
|
| 1143 |
# =============================================================================
|
| 1144 |
# βββββββββββββ MAIN FUNCTION βββββββββββββ
|
|
@@ -1155,6 +538,42 @@ def main():
|
|
| 1155 |
else:
|
| 1156 |
st.error("Missing Cosmos Key πβ")
|
| 1157 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1158 |
st.sidebar.markdown("## π οΈ Item Management")
|
| 1159 |
if st.sidebar.button("New Item"):
|
| 1160 |
if st.session_state.get("current_container"):
|
|
@@ -1184,243 +603,8 @@ def main():
|
|
| 1184 |
search_documents_ui(st.session_state.get("current_container"))
|
| 1185 |
show_sidebar_data_grid()
|
| 1186 |
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
if st.session_state.get("current_container"):
|
| 1190 |
-
edit_names_grid(st.session_state.current_container)
|
| 1191 |
-
try:
|
| 1192 |
-
if st.session_state.get("client") is None:
|
| 1193 |
-
st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
| 1194 |
-
st.sidebar.title("π Navigator")
|
| 1195 |
-
databases = get_databases(st.session_state.client)
|
| 1196 |
-
selected_db = st.sidebar.selectbox("ποΈ DB", databases)
|
| 1197 |
-
if selected_db != st.session_state.get("selected_database"):
|
| 1198 |
-
st.session_state.selected_database = selected_db
|
| 1199 |
-
st.session_state.selected_container = None
|
| 1200 |
-
st.session_state.selected_document_id = None
|
| 1201 |
-
st.session_state.current_index = 0
|
| 1202 |
-
st.rerun()
|
| 1203 |
-
if st.session_state.selected_database:
|
| 1204 |
-
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
| 1205 |
-
if "show_new_container_form" not in st.session_state:
|
| 1206 |
-
st.session_state.show_new_container_form = False
|
| 1207 |
-
if st.sidebar.button("π New Container"):
|
| 1208 |
-
st.session_state.show_new_container_form = True
|
| 1209 |
-
if st.session_state.show_new_container_form:
|
| 1210 |
-
with st.sidebar.form("new_container_form"):
|
| 1211 |
-
new_container_id = st.text_input("Container ID", value="aiml-container")
|
| 1212 |
-
new_partition_key = st.text_input("Partition Key", value="/pk")
|
| 1213 |
-
new_analytical = st.checkbox("Enable Analytical Store", value=True)
|
| 1214 |
-
submitted = st.form_submit_button("Create Container")
|
| 1215 |
-
if submitted:
|
| 1216 |
-
analytical_ttl = -1 if new_analytical else None
|
| 1217 |
-
new_container = create_new_container(database, new_container_id, new_partition_key, analytical_storage_ttl=analytical_ttl)
|
| 1218 |
-
if new_container:
|
| 1219 |
-
st.success(f"Container '{new_container_id}' created.")
|
| 1220 |
-
default_id = generate_unique_id()
|
| 1221 |
-
default_item = {
|
| 1222 |
-
"id": default_id,
|
| 1223 |
-
"pk": default_id,
|
| 1224 |
-
"name": "Default Image Prompt",
|
| 1225 |
-
"prompt": "Enter your image prompt here",
|
| 1226 |
-
"timestamp": datetime.now().isoformat(),
|
| 1227 |
-
"type": "image_prompt"
|
| 1228 |
-
}
|
| 1229 |
-
insert_success, insert_message = insert_record(new_container, default_item)
|
| 1230 |
-
if insert_success:
|
| 1231 |
-
st.info("Default templated item created in new container.")
|
| 1232 |
-
else:
|
| 1233 |
-
st.error(f"Default item insertion error: {insert_message}")
|
| 1234 |
-
st.session_state.show_new_container_form = False
|
| 1235 |
-
st.session_state.new_container_created = new_container_id
|
| 1236 |
-
st.rerun()
|
| 1237 |
-
containers = get_containers(database)
|
| 1238 |
-
if "new_container_created" in st.session_state and st.session_state.new_container_created not in containers:
|
| 1239 |
-
containers.append(st.session_state.new_container_created)
|
| 1240 |
-
selected_container = st.sidebar.selectbox("π Container", containers)
|
| 1241 |
-
if selected_container != st.session_state.get("selected_container"):
|
| 1242 |
-
st.session_state.selected_container = selected_container
|
| 1243 |
-
st.session_state.selected_document_id = None
|
| 1244 |
-
st.session_state.current_index = 0
|
| 1245 |
-
st.rerun()
|
| 1246 |
-
if st.session_state.selected_container:
|
| 1247 |
-
container = database.get_container_client(st.session_state.selected_container)
|
| 1248 |
-
st.session_state.current_container = container
|
| 1249 |
-
if st.sidebar.button("π¦ Export"):
|
| 1250 |
-
download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
|
| 1251 |
-
if download_link.startswith('<a'):
|
| 1252 |
-
st.markdown(download_link, unsafe_allow_html=True)
|
| 1253 |
-
else:
|
| 1254 |
-
st.error(download_link)
|
| 1255 |
-
documents = get_documents(container)
|
| 1256 |
-
total_docs = len(documents)
|
| 1257 |
-
num_docs = st.slider("Docs", 1, 20, 1)
|
| 1258 |
-
documents_to_display = documents[:num_docs] if total_docs > num_docs else documents
|
| 1259 |
-
st.sidebar.info(f"Showing {len(documents_to_display)} docs")
|
| 1260 |
-
view_options = ['List', 'Markdown', 'Code', 'Run AI', 'Clone', 'New']
|
| 1261 |
-
selected_view = st.sidebar.selectbox("View", view_options, index=1)
|
| 1262 |
-
if selected_view == 'List':
|
| 1263 |
-
edit_documents_list(container)
|
| 1264 |
-
elif selected_view == 'Markdown':
|
| 1265 |
-
st.markdown("#### π Markdown")
|
| 1266 |
-
if documents:
|
| 1267 |
-
doc = documents[st.session_state.current_index]
|
| 1268 |
-
content = json.dumps(doc, indent=2)
|
| 1269 |
-
st.markdown(f"```json\n{content}\n```")
|
| 1270 |
-
col_prev, col_next = st.columns(2)
|
| 1271 |
-
with col_prev:
|
| 1272 |
-
if st.button("β¬
οΈ") and st.session_state.current_index > 0:
|
| 1273 |
-
st.session_state.current_index -= 1
|
| 1274 |
-
st.rerun()
|
| 1275 |
-
with col_next:
|
| 1276 |
-
if st.button("β‘οΈ") and st.session_state.current_index < total_docs - 1:
|
| 1277 |
-
st.session_state.current_index += 1
|
| 1278 |
-
st.rerun()
|
| 1279 |
-
elif selected_view == 'Code':
|
| 1280 |
-
st.markdown("#### π» Code Editor")
|
| 1281 |
-
if documents:
|
| 1282 |
-
doc = documents[st.session_state.current_index]
|
| 1283 |
-
if "doc_editor" not in st.session_state:
|
| 1284 |
-
st.session_state.doc_editor = json.dumps(doc, indent=2)
|
| 1285 |
-
edited = st.text_area("Edit JSON", value=st.session_state.doc_editor, height=300, key="doc_editor", on_change=lambda: auto_save_edit())
|
| 1286 |
-
col_prev, col_next = st.columns(2)
|
| 1287 |
-
with col_prev:
|
| 1288 |
-
if st.button("β¬
οΈ") and st.session_state.current_index > 0:
|
| 1289 |
-
st.session_state.current_index -= 1
|
| 1290 |
-
st.rerun()
|
| 1291 |
-
with col_next:
|
| 1292 |
-
if st.button("β‘οΈ") and st.session_state.current_index < total_docs - 1:
|
| 1293 |
-
st.session_state.current_index += 1
|
| 1294 |
-
st.rerun()
|
| 1295 |
-
col_save, col_delete = st.columns(2)
|
| 1296 |
-
with col_save:
|
| 1297 |
-
if st.button("πΎ Save", key=f'save_{st.session_state.current_index}'):
|
| 1298 |
-
try:
|
| 1299 |
-
updated_doc = json.loads(edited)
|
| 1300 |
-
container.upsert_item(body=updated_doc)
|
| 1301 |
-
st.success(f"Saved {updated_doc['id']}")
|
| 1302 |
-
st.rerun()
|
| 1303 |
-
except Exception as e:
|
| 1304 |
-
st.error(f"Save err: {str(e)}")
|
| 1305 |
-
with col_delete:
|
| 1306 |
-
if st.button("ποΈ Delete", key=f'delete_{st.session_state.current_index}'):
|
| 1307 |
-
try:
|
| 1308 |
-
current_doc = json.loads(edited)
|
| 1309 |
-
success, message = delete_record(container, current_doc)
|
| 1310 |
-
if success:
|
| 1311 |
-
st.success(message)
|
| 1312 |
-
st.rerun()
|
| 1313 |
-
else:
|
| 1314 |
-
st.error(message)
|
| 1315 |
-
except Exception as e:
|
| 1316 |
-
st.error(f"Delete err: {str(e)}")
|
| 1317 |
-
if "delete_log" in st.session_state and st.session_state.delete_log:
|
| 1318 |
-
st.subheader("Delete Log")
|
| 1319 |
-
for log_entry in st.session_state.delete_log[-5:]:
|
| 1320 |
-
st.write(log_entry)
|
| 1321 |
-
elif selected_view == 'Run AI':
|
| 1322 |
-
st.markdown("#### π€ Run AI")
|
| 1323 |
-
ai_query = st.text_area("Enter your query for ArXiv search:", key="arxiv_query", height=100)
|
| 1324 |
-
if st.button("Send"):
|
| 1325 |
-
st.session_state.last_query = ai_query
|
| 1326 |
-
perform_ai_lookup(ai_query, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=True, useArxiv=True, useArxivAudio=False)
|
| 1327 |
-
elif selected_view == 'Clone':
|
| 1328 |
-
st.markdown("#### π Clone")
|
| 1329 |
-
if documents:
|
| 1330 |
-
doc = documents[st.session_state.current_index]
|
| 1331 |
-
st.markdown(f"Original ID: {doc.get('id', '')}")
|
| 1332 |
-
new_id = st.text_input("New ID", value=generate_unique_id(), key='new_clone_id')
|
| 1333 |
-
new_name = st.text_input("New Name", value=f"Clone_{new_id[:8]}", key='new_clone_name')
|
| 1334 |
-
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']}}
|
| 1335 |
-
doc_str = st.text_area("Edit JSON", value=json.dumps(new_doc, indent=2), height=300, key='clone_preview')
|
| 1336 |
-
col1, col2 = st.columns(2)
|
| 1337 |
-
with col1:
|
| 1338 |
-
if st.button("π Regenerate"):
|
| 1339 |
-
new_id = generate_unique_id()
|
| 1340 |
-
st.session_state.new_clone_id = new_id
|
| 1341 |
-
st.rerun()
|
| 1342 |
-
with col2:
|
| 1343 |
-
if st.button("πΎ Save Clone"):
|
| 1344 |
-
try:
|
| 1345 |
-
final_doc = json.loads(doc_str)
|
| 1346 |
-
for field in ['_rid', '_self', '_etag', '_attachments', '_ts']:
|
| 1347 |
-
final_doc.pop(field, None)
|
| 1348 |
-
container.create_item(body=final_doc)
|
| 1349 |
-
st.success(f"Cloned {final_doc['id']}")
|
| 1350 |
-
st.rerun()
|
| 1351 |
-
except Exception as e:
|
| 1352 |
-
st.error(f"Clone err: {str(e)}")
|
| 1353 |
-
col_prev, col_next = st.columns(2)
|
| 1354 |
-
with col_prev:
|
| 1355 |
-
if st.button("β¬
οΈ") and st.session_state.current_index > 0:
|
| 1356 |
-
st.session_state.current_index -= 1
|
| 1357 |
-
st.rerun()
|
| 1358 |
-
with col_next:
|
| 1359 |
-
if st.button("β‘οΈ") and st.session_state.current_index < total_docs - 1:
|
| 1360 |
-
st.session_state.current_index += 1
|
| 1361 |
-
st.rerun()
|
| 1362 |
-
elif selected_view == 'New':
|
| 1363 |
-
st.markdown("#### β New Doc")
|
| 1364 |
-
if st.button("π€ Auto-Gen"):
|
| 1365 |
-
auto_doc = {
|
| 1366 |
-
"id": generate_unique_id(),
|
| 1367 |
-
"pk": generate_unique_id(),
|
| 1368 |
-
"name": f"Auto {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
| 1369 |
-
"content": "Auto-generated record.",
|
| 1370 |
-
"timestamp": datetime.now().isoformat()
|
| 1371 |
-
}
|
| 1372 |
-
success, message = insert_record(container, auto_doc)
|
| 1373 |
-
if success:
|
| 1374 |
-
st.success(message)
|
| 1375 |
-
st.rerun()
|
| 1376 |
-
else:
|
| 1377 |
-
st.error(message)
|
| 1378 |
-
else:
|
| 1379 |
-
new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
|
| 1380 |
-
default_doc = {
|
| 1381 |
-
"id": new_id,
|
| 1382 |
-
"pk": new_id,
|
| 1383 |
-
"name": "New Doc",
|
| 1384 |
-
"content": "",
|
| 1385 |
-
"timestamp": datetime.now().isoformat()
|
| 1386 |
-
}
|
| 1387 |
-
new_doc_str = st.text_area("JSON", value=json.dumps(default_doc, indent=2), height=300)
|
| 1388 |
-
if st.button("β Create"):
|
| 1389 |
-
try:
|
| 1390 |
-
cleaned = preprocess_text(new_doc_str)
|
| 1391 |
-
new_doc = json.loads(cleaned)
|
| 1392 |
-
new_doc['id'] = new_id
|
| 1393 |
-
new_doc['pk'] = new_id
|
| 1394 |
-
success, message = insert_record(container, new_doc)
|
| 1395 |
-
if success:
|
| 1396 |
-
st.success(f"Created {new_doc['id']}")
|
| 1397 |
-
st.rerun()
|
| 1398 |
-
else:
|
| 1399 |
-
st.error(message)
|
| 1400 |
-
except Exception as e:
|
| 1401 |
-
st.error(f"Create err: {str(e)}")
|
| 1402 |
-
st.subheader(f"π {st.session_state.selected_container}")
|
| 1403 |
-
if documents_to_display:
|
| 1404 |
-
df = pd.DataFrame(documents_to_display)
|
| 1405 |
-
st.dataframe(df)
|
| 1406 |
-
else:
|
| 1407 |
-
st.info("No docs.")
|
| 1408 |
-
update_file_management_section()
|
| 1409 |
-
except exceptions.CosmosHttpResponseError as e:
|
| 1410 |
-
st.error(f"Cosmos error: {str(e)} π¨")
|
| 1411 |
-
except Exception as e:
|
| 1412 |
-
st.error(f"Error: {str(e)} π±")
|
| 1413 |
-
if st.session_state.logged_in and st.sidebar.button("πͺ Logout"):
|
| 1414 |
-
st.markdown("#### πͺ Logout")
|
| 1415 |
-
st.session_state.logged_in = False
|
| 1416 |
-
st.session_state.selected_records = []
|
| 1417 |
-
st.session_state.client = None
|
| 1418 |
-
st.session_state.selected_database = None
|
| 1419 |
-
st.session_state.selected_container = None
|
| 1420 |
-
st.session_state.selected_document_id = None
|
| 1421 |
-
st.session_state.current_index = 0
|
| 1422 |
-
st.rerun()
|
| 1423 |
-
show_sidebar_data_grid()
|
| 1424 |
|
| 1425 |
if __name__ == "__main__":
|
| 1426 |
main()
|
|
|
|
|
|
|
| 1 |
# =============================================================================
|
| 2 |
# βββββββββββββ IMPORTS βββββββββββββ
|
| 3 |
# =============================================================================
|
|
|
|
| 149 |
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
| 150 |
return text.strip()
|
| 151 |
|
|
|
|
| 152 |
def sanitize_json_text(text):
|
|
|
|
| 153 |
text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F]', '', text)
|
|
|
|
| 154 |
text = text.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")
|
| 155 |
return text
|
| 156 |
|
|
|
|
| 191 |
if "id" not in record:
|
| 192 |
return False, "Record must contain an 'id' field. π"
|
| 193 |
doc_id = record["id"]
|
|
|
|
|
|
|
|
|
|
| 194 |
partition_key_value = record.get("pk", doc_id)
|
|
|
|
| 195 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 196 |
+
return True, f"Record {doc_id} successfully deleted from Cosmos DB. ποΈ"
|
|
|
|
|
|
|
| 197 |
except exceptions.CosmosResourceNotFoundError:
|
| 198 |
+
return True, f"Record {doc_id} not found in Cosmos DB (already deleted or never existed). ποΈ"
|
|
|
|
|
|
|
| 199 |
except exceptions.CosmosHttpResponseError as e:
|
| 200 |
+
return False, f"HTTP error deleting {doc_id}: {str(e)}. π¨"
|
|
|
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
+
return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. π±"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
def archive_current_container(database_name, container_name, client):
|
| 205 |
try:
|
|
|
|
| 223 |
return f"Archive error: {str(e)} π’"
|
| 224 |
|
| 225 |
# =============================================================================
|
| 226 |
+
# βββββββββββββ ADVANCED COSMOS FUNCTIONS (omitted for brevity) βββββββββββββ
|
| 227 |
# =============================================================================
|
| 228 |
+
# β¦ (your existing advanced cosmos functions remain here) β¦
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
# =============================================================================
|
| 231 |
+
# βββββββββββββ NEW COSMOSDB DEMO FUNCTIONS βββββββββββββ
|
| 232 |
+
# Each function below corresponds to one of your provided code snippets.
|
| 233 |
+
# They expose a simple UI (with text inputs and buttons) to run the demo code.
|
| 234 |
# =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
def demo_create_database():
|
| 237 |
+
st.markdown("### Demo: Create Database")
|
| 238 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_name")
|
| 239 |
+
if st.button("Create Database", key="btn_create_db"):
|
| 240 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 241 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 242 |
+
if not url or not key_env:
|
| 243 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 244 |
+
return
|
| 245 |
+
client_demo = CosmosClient(url, credential=key_env)
|
| 246 |
+
try:
|
| 247 |
+
database = client_demo.create_database(database_name)
|
| 248 |
+
st.success(f"Database '{database_name}' created.")
|
| 249 |
+
st.write(database)
|
| 250 |
+
except exceptions.CosmosResourceExistsError:
|
| 251 |
+
database = client_demo.get_database_client(database_name)
|
| 252 |
+
st.info(f"Database '{database_name}' already exists.")
|
| 253 |
+
st.write(database)
|
| 254 |
+
except Exception as e:
|
| 255 |
+
st.error(f"Error: {str(e)}")
|
| 256 |
+
|
| 257 |
+
def demo_create_container():
|
| 258 |
+
st.markdown("### Demo: Create Container")
|
| 259 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_for_container")
|
| 260 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_name")
|
| 261 |
+
partition_key = st.text_input("Enter Partition Key Path", value="/productName", key="demo_partition_key")
|
| 262 |
+
if st.button("Create Container", key="btn_create_container"):
|
| 263 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 264 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 265 |
+
if not url or not key_env:
|
| 266 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 267 |
+
return
|
| 268 |
+
client_demo = CosmosClient(url, credential=key_env)
|
| 269 |
+
try:
|
| 270 |
+
database = client_demo.get_database_client(database_name)
|
| 271 |
+
container = database.create_container(id=container_name, partition_key=PartitionKey(path=partition_key))
|
| 272 |
+
st.success(f"Container '{container_name}' created.")
|
| 273 |
+
st.write(container)
|
| 274 |
+
except exceptions.CosmosResourceExistsError:
|
| 275 |
+
container = database.get_container_client(container_name)
|
| 276 |
+
st.info(f"Container '{container_name}' already exists.")
|
| 277 |
+
st.write(container)
|
| 278 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 279 |
+
st.error(f"HTTP error: {str(e)}")
|
| 280 |
+
except Exception as e:
|
| 281 |
+
st.error(f"Error: {str(e)}")
|
| 282 |
+
|
| 283 |
+
def demo_create_analytical_container():
|
| 284 |
+
st.markdown("### Demo: Create Analytical Store Enabled Container")
|
| 285 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_for_analytical")
|
| 286 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_analytical")
|
| 287 |
+
partition_key = st.text_input("Enter Partition Key Path", value="/productName", key="demo_partition_key_analytical")
|
| 288 |
+
if st.button("Create Analytical Container", key="btn_create_analytical"):
|
| 289 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 290 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 291 |
+
if not url or not key_env:
|
| 292 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 293 |
+
return
|
| 294 |
+
client_demo = CosmosClient(url, credential=key_env)
|
| 295 |
+
try:
|
| 296 |
+
database = client_demo.get_database_client(database_name)
|
| 297 |
+
container = database.create_container(
|
| 298 |
+
id=container_name,
|
| 299 |
+
partition_key=PartitionKey(path=partition_key),
|
| 300 |
+
analytical_storage_ttl=-1
|
| 301 |
)
|
| 302 |
+
st.success(f"Analytical container '{container_name}' created.")
|
| 303 |
+
st.write(container)
|
| 304 |
+
except exceptions.CosmosResourceExistsError:
|
| 305 |
+
container = database.get_container_client(container_name)
|
| 306 |
+
st.info(f"Container '{container_name}' already exists.")
|
| 307 |
+
st.write(container)
|
| 308 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 309 |
+
st.error(f"HTTP error: {str(e)}")
|
| 310 |
+
except Exception as e:
|
| 311 |
+
st.error(f"Error: {str(e)}")
|
| 312 |
+
|
| 313 |
+
def demo_get_existing_container():
|
| 314 |
+
st.markdown("### Demo: Get Existing Container")
|
| 315 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_get")
|
| 316 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_get")
|
| 317 |
+
if st.button("Get Container", key="btn_get_container"):
|
| 318 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 319 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 320 |
+
if not url or not key_env:
|
| 321 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
return
|
| 323 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
try:
|
| 325 |
+
database = client_demo.get_database_client(database_name)
|
| 326 |
+
container = database.get_container_client(container_name)
|
| 327 |
+
st.success(f"Retrieved container '{container_name}'.")
|
| 328 |
+
st.write(container)
|
| 329 |
+
except Exception as e:
|
| 330 |
+
st.error(f"Error: {str(e)}")
|
| 331 |
+
|
| 332 |
+
def demo_insert_data():
|
| 333 |
+
st.markdown("### Demo: Insert Data")
|
| 334 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_insert")
|
| 335 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_insert")
|
| 336 |
+
num_items = st.number_input("Number of items to insert", min_value=1, max_value=20, value=9, key="demo_num_items")
|
| 337 |
+
if st.button("Insert Data", key="btn_insert_data"):
|
| 338 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 339 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 340 |
+
if not url or not key_env:
|
| 341 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 342 |
+
return
|
| 343 |
+
client_demo = CosmosClient(url, credential=key_env)
|
| 344 |
+
try:
|
| 345 |
+
database = client_demo.get_database_client(database_name)
|
| 346 |
+
container = database.get_container_client(container_name)
|
| 347 |
+
for i in range(1, int(num_items) + 1):
|
| 348 |
+
container.upsert_item({
|
| 349 |
+
'id': f'item{i}',
|
| 350 |
+
'productName': 'Widget',
|
| 351 |
+
'productModel': f'Model {i}'
|
| 352 |
})
|
| 353 |
+
st.success(f"Inserted {num_items} items.")
|
|
|
|
|
|
|
| 354 |
except Exception as e:
|
| 355 |
+
st.error(f"Error: {str(e)}")
|
| 356 |
+
|
| 357 |
+
def demo_delete_data():
|
| 358 |
+
st.markdown("### Demo: Delete Data")
|
| 359 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_delete")
|
| 360 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_delete")
|
| 361 |
+
query_model = st.text_input("Product Model to delete", value="Model 2", key="demo_query_model")
|
| 362 |
+
if st.button("Delete Data", key="btn_delete_data"):
|
| 363 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 364 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 365 |
+
if not url or not key_env:
|
| 366 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 367 |
+
return
|
| 368 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
try:
|
| 370 |
+
database = client_demo.get_database_client(database_name)
|
| 371 |
+
container = database.get_container_client(container_name)
|
| 372 |
+
items = list(container.query_items(
|
| 373 |
+
query=f'SELECT * FROM products p WHERE p.productModel = "{query_model}"',
|
| 374 |
+
enable_cross_partition_query=True
|
| 375 |
+
))
|
| 376 |
+
count = 0
|
| 377 |
+
for item in items:
|
| 378 |
+
container.delete_item(item, partition_key=item.get("productName", "Widget"))
|
| 379 |
+
count += 1
|
| 380 |
+
st.success(f"Deleted {count} items with productModel = '{query_model}'.")
|
| 381 |
+
except Exception as e:
|
| 382 |
+
st.error(f"Error: {str(e)}")
|
| 383 |
+
|
| 384 |
+
def demo_query_database():
|
| 385 |
+
st.markdown("### Demo: Query Database")
|
| 386 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_query")
|
| 387 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_query")
|
| 388 |
+
query_str = st.text_area("Enter SQL Query", value='SELECT * FROM mycontainer r WHERE r.id="item3"', key="demo_query_str")
|
| 389 |
+
if st.button("Run Query", key="btn_query_database"):
|
| 390 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 391 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 392 |
+
if not url or not key_env:
|
| 393 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 394 |
+
return
|
| 395 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
try:
|
| 397 |
+
database = client_demo.get_database_client(database_name)
|
| 398 |
+
container = database.get_container_client(container_name)
|
| 399 |
+
results = list(container.query_items(query=query_str, enable_cross_partition_query=True))
|
| 400 |
+
if results:
|
| 401 |
+
for item in results:
|
| 402 |
+
st.json(item)
|
| 403 |
+
else:
|
| 404 |
+
st.info("No results found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
except Exception as e:
|
| 406 |
+
st.error(f"Error: {str(e)}")
|
| 407 |
+
|
| 408 |
+
def demo_parameterized_query():
|
| 409 |
+
st.markdown("### Demo: Parameterized Query")
|
| 410 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_param")
|
| 411 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_param")
|
| 412 |
+
model_value = st.text_input("Enter productModel value", value="Model 7", key="demo_model_value")
|
| 413 |
+
if st.button("Run Parameterized Query", key="btn_param_query"):
|
| 414 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 415 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 416 |
+
if not url or not key_env:
|
| 417 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 418 |
+
return
|
| 419 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
try:
|
| 421 |
+
database = client_demo.get_database_client(database_name)
|
| 422 |
+
container = database.get_container_client(container_name)
|
| 423 |
+
discontinued_items = container.query_items(
|
| 424 |
+
query='SELECT * FROM products p WHERE p.productModel = @model',
|
| 425 |
+
parameters=[{"name": "@model", "value": model_value}],
|
| 426 |
+
enable_cross_partition_query=True
|
| 427 |
+
)
|
| 428 |
+
for item in discontinued_items:
|
| 429 |
+
st.json(item)
|
| 430 |
+
except Exception as e:
|
| 431 |
+
st.error(f"Error: {str(e)}")
|
| 432 |
+
|
| 433 |
+
def demo_get_db_properties():
|
| 434 |
+
st.markdown("### Demo: Get Database Properties")
|
| 435 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_props")
|
| 436 |
+
if st.button("Get Properties", key="btn_db_props"):
|
| 437 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 438 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 439 |
+
if not url or not key_env:
|
| 440 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 441 |
+
return
|
| 442 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
try:
|
| 444 |
+
database = client_demo.get_database_client(database_name)
|
| 445 |
+
props = database.read()
|
| 446 |
+
st.json(props)
|
| 447 |
+
except Exception as e:
|
| 448 |
+
st.error(f"Error: {str(e)}")
|
| 449 |
+
|
| 450 |
+
def demo_get_throughput():
|
| 451 |
+
st.markdown("### Demo: Get Throughput (Database & Container)")
|
| 452 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_throughput")
|
| 453 |
+
container_name = st.text_input("Enter Container Name (for container throughput)", value="testContainer", key="demo_container_throughput")
|
| 454 |
+
if st.button("Get Throughput", key="btn_get_throughput"):
|
| 455 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 456 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 457 |
+
if not url or not key_env:
|
| 458 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 459 |
+
return
|
| 460 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
try:
|
| 462 |
+
database = client_demo.get_database_client(database_name)
|
| 463 |
+
db_offer = database.get_throughput()
|
| 464 |
+
st.write(f"Database Offer: {db_offer.properties['id']} with throughput {db_offer.properties['content']['offerThroughput']}")
|
| 465 |
+
try:
|
| 466 |
+
container = database.get_container_client(container_name)
|
| 467 |
+
container_offer = container.get_throughput()
|
| 468 |
+
st.write(f"Container Offer: {container_offer.properties['id']} with throughput {container_offer.properties['content']['offerThroughput']}")
|
| 469 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 470 |
+
st.error(f"Container throughput error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
except Exception as e:
|
| 472 |
+
st.error(f"Error: {str(e)}")
|
| 473 |
+
|
| 474 |
+
def demo_modify_container_properties():
|
| 475 |
+
st.markdown("### Demo: Modify Container Properties (Set default TTL)")
|
| 476 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_modify")
|
| 477 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_modify")
|
| 478 |
+
new_ttl = st.number_input("Enter new default TTL (seconds)", min_value=0, value=10, key="demo_new_ttl")
|
| 479 |
+
if st.button("Modify Container", key="btn_modify_container"):
|
| 480 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 481 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 482 |
+
if not url or not key_env:
|
| 483 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 484 |
+
return
|
| 485 |
+
client_demo = CosmosClient(url, credential=key_env)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
try:
|
| 487 |
+
database = client_demo.get_database_client(database_name)
|
| 488 |
+
container = database.get_container_client(container_name)
|
| 489 |
+
# Note: replace_container is used here as per your snippet.
|
| 490 |
+
database.replace_container(
|
| 491 |
+
container,
|
| 492 |
+
partition_key=PartitionKey(path="/productName"),
|
| 493 |
+
default_ttl=new_ttl,
|
| 494 |
+
)
|
| 495 |
+
container_props = container.read()
|
| 496 |
+
st.write("New default TTL:", container_props.get("defaultTtl"))
|
| 497 |
+
except Exception as e:
|
| 498 |
+
st.error(f"Error: {str(e)}")
|
| 499 |
+
|
| 500 |
+
def demo_item_response_headers():
|
| 501 |
+
st.markdown("### Demo: Using Item Point Operation Response Headers")
|
| 502 |
+
database_name = st.text_input("Enter Database Name", value="testDatabase", key="demo_db_headers")
|
| 503 |
+
container_name = st.text_input("Enter Container Name", value="products", key="demo_container_headers")
|
| 504 |
+
if st.button("Create Item & Show Headers", key="btn_item_headers"):
|
| 505 |
+
url = os.environ.get("ACCOUNT_URI")
|
| 506 |
+
key_env = os.environ.get("ACCOUNT_KEY")
|
| 507 |
+
if not url or not key_env:
|
| 508 |
+
st.error("Missing ACCOUNT_URI or ACCOUNT_KEY environment variables.")
|
| 509 |
+
return
|
| 510 |
+
client_demo = CosmosClient(url, credential=key_env)
|
| 511 |
+
try:
|
| 512 |
+
database = client_demo.get_database_client(database_name)
|
| 513 |
+
container = database.get_container_client(container_name)
|
| 514 |
+
operation_response = container.create_item({"id": "test_item", "productName": "test_item"})
|
| 515 |
+
headers = operation_response.get_response_headers()
|
| 516 |
+
st.write("ETag:", headers.get("etag"))
|
| 517 |
+
st.write("Request Charge:", headers.get("x-ms-request-charge"))
|
| 518 |
+
except Exception as e:
|
| 519 |
+
st.error(f"Error: {str(e)}")
|
| 520 |
|
| 521 |
# =============================================================================
|
| 522 |
+
# βββββββββββββ EXISTING FUNCTIONS (GitHub, File Management, etc.) βββββββββββββ
|
| 523 |
# =============================================================================
|
| 524 |
+
# β¦ (your other functions remain unchanged) β¦
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
# =============================================================================
|
| 527 |
# βββββββββββββ MAIN FUNCTION βββββββββββββ
|
|
|
|
| 538 |
else:
|
| 539 |
st.error("Missing Cosmos Key πβ")
|
| 540 |
return
|
| 541 |
+
|
| 542 |
+
# ββ New: CosmosDB Demo Features Section in Sidebar ββ
|
| 543 |
+
st.sidebar.markdown("## CosmosDB Demo Features")
|
| 544 |
+
demo_feature = st.sidebar.selectbox("Select a Demo",
|
| 545 |
+
["Select", "Create Database", "Create Container", "Create Analytical Container",
|
| 546 |
+
"Get Existing Container", "Insert Data", "Delete Data", "Query Database",
|
| 547 |
+
"Parameterized Query", "Get Database Properties", "Get Throughput",
|
| 548 |
+
"Modify Container Properties", "Item Response Headers"],
|
| 549 |
+
key="demo_select")
|
| 550 |
+
if demo_feature != "Select":
|
| 551 |
+
if demo_feature == "Create Database":
|
| 552 |
+
demo_create_database()
|
| 553 |
+
elif demo_feature == "Create Container":
|
| 554 |
+
demo_create_container()
|
| 555 |
+
elif demo_feature == "Create Analytical Container":
|
| 556 |
+
demo_create_analytical_container()
|
| 557 |
+
elif demo_feature == "Get Existing Container":
|
| 558 |
+
demo_get_existing_container()
|
| 559 |
+
elif demo_feature == "Insert Data":
|
| 560 |
+
demo_insert_data()
|
| 561 |
+
elif demo_feature == "Delete Data":
|
| 562 |
+
demo_delete_data()
|
| 563 |
+
elif demo_feature == "Query Database":
|
| 564 |
+
demo_query_database()
|
| 565 |
+
elif demo_feature == "Parameterized Query":
|
| 566 |
+
demo_parameterized_query()
|
| 567 |
+
elif demo_feature == "Get Database Properties":
|
| 568 |
+
demo_get_db_properties()
|
| 569 |
+
elif demo_feature == "Get Throughput":
|
| 570 |
+
demo_get_throughput()
|
| 571 |
+
elif demo_feature == "Modify Container Properties":
|
| 572 |
+
demo_modify_container_properties()
|
| 573 |
+
elif demo_feature == "Item Response Headers":
|
| 574 |
+
demo_item_response_headers()
|
| 575 |
+
|
| 576 |
+
# ββ Existing Sidebar Items (Item Management, File Management, etc.)
|
| 577 |
st.sidebar.markdown("## π οΈ Item Management")
|
| 578 |
if st.sidebar.button("New Item"):
|
| 579 |
if st.session_state.get("current_container"):
|
|
|
|
| 603 |
search_documents_ui(st.session_state.get("current_container"))
|
| 604 |
show_sidebar_data_grid()
|
| 605 |
|
| 606 |
+
# β¦ (rest of your main() function continues unchanged) β¦
|
| 607 |
+
# For brevity, your remaining navigation, file management, and document editing UI remain as-is.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
|
| 609 |
if __name__ == "__main__":
|
| 610 |
main()
|