Update app.py
Browse files
app.py
CHANGED
|
@@ -469,23 +469,23 @@ def process_uploaded_file(file, state_bookmarks):
|
|
| 469 |
|
| 470 |
if file is None:
|
| 471 |
logger.warning("No file uploaded")
|
| 472 |
-
return "Please upload a bookmarks HTML file.", '', state_bookmarks, gr.update(choices=[])
|
| 473 |
|
| 474 |
try:
|
| 475 |
file_content = file.decode('utf-8')
|
| 476 |
except UnicodeDecodeError as e:
|
| 477 |
logger.error(f"Error decoding the file: {e}", exc_info=True)
|
| 478 |
-
return "Error decoding the file. Please ensure it's a valid HTML file.", '', state_bookmarks, gr.update(choices=[])
|
| 479 |
|
| 480 |
try:
|
| 481 |
bookmarks = parse_bookmarks(file_content)
|
| 482 |
except Exception as e:
|
| 483 |
logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
|
| 484 |
-
return "Error parsing the bookmarks HTML file.", '', state_bookmarks, gr.update(choices=[])
|
| 485 |
|
| 486 |
if not bookmarks:
|
| 487 |
logger.warning("No bookmarks found in the uploaded file")
|
| 488 |
-
return "No bookmarks found in the uploaded file.", '', state_bookmarks, gr.update(choices=[])
|
| 489 |
|
| 490 |
# Assign unique IDs to bookmarks
|
| 491 |
for idx, bookmark in enumerate(bookmarks):
|
|
@@ -505,7 +505,7 @@ def process_uploaded_file(file, state_bookmarks):
|
|
| 505 |
faiss_index = vectorize_and_index(bookmarks)
|
| 506 |
except Exception as e:
|
| 507 |
logger.error(f"Error building FAISS index: {e}", exc_info=True)
|
| 508 |
-
return "Error building search index.", '', state_bookmarks, gr.update(choices=[])
|
| 509 |
|
| 510 |
message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
|
| 511 |
logger.info(message)
|
|
@@ -518,7 +518,7 @@ def process_uploaded_file(file, state_bookmarks):
|
|
| 518 |
# Update state
|
| 519 |
state_bookmarks = bookmarks.copy()
|
| 520 |
|
| 521 |
-
return message, bookmark_html, state_bookmarks, gr.update(choices=choices)
|
| 522 |
|
| 523 |
def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
| 524 |
"""
|
|
@@ -526,7 +526,7 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
|
| 526 |
"""
|
| 527 |
global bookmarks, faiss_index
|
| 528 |
if not selected_indices:
|
| 529 |
-
return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
| 530 |
|
| 531 |
ids_to_delete = []
|
| 532 |
indices_to_delete = []
|
|
@@ -554,16 +554,16 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
|
| 554 |
# Update state
|
| 555 |
state_bookmarks = bookmarks.copy()
|
| 556 |
|
| 557 |
-
return message, gr.update(choices=choices), display_bookmarks()
|
| 558 |
|
| 559 |
def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
|
| 560 |
"""
|
| 561 |
Edit category of selected bookmarks.
|
| 562 |
"""
|
| 563 |
if not selected_indices:
|
| 564 |
-
return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks(), state_bookmarks
|
| 565 |
if not new_category:
|
| 566 |
-
return "⚠️ No new category selected.", gr.update(choices=[]), display_bookmarks(), state_bookmarks
|
| 567 |
|
| 568 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
| 569 |
for idx in indices:
|
|
@@ -581,7 +581,7 @@ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookm
|
|
| 581 |
# Update state
|
| 582 |
state_bookmarks = bookmarks.copy()
|
| 583 |
|
| 584 |
-
return message, gr.update(choices=choices), display_bookmarks(), state_bookmarks
|
| 585 |
|
| 586 |
def export_bookmarks():
|
| 587 |
"""
|
|
@@ -722,7 +722,7 @@ def build_app():
|
|
| 722 |
# Initialize state
|
| 723 |
state_bookmarks = gr.State([])
|
| 724 |
|
| 725 |
-
# Define 'bookmark_selector'
|
| 726 |
bookmark_selector = gr.CheckboxGroup(
|
| 727 |
label="✅ Select Bookmarks",
|
| 728 |
choices=[]
|
|
@@ -773,7 +773,7 @@ Navigate through the tabs to explore each feature in detail.
|
|
| 773 |
process_button.click(
|
| 774 |
process_uploaded_file,
|
| 775 |
inputs=[upload, state_bookmarks],
|
| 776 |
-
outputs=[output_text, bookmark_display, state_bookmarks,
|
| 777 |
)
|
| 778 |
|
| 779 |
with gr.Tab("Chat with Bookmarks"):
|
|
@@ -843,8 +843,8 @@ Navigate through the tabs to explore each feature in detail.
|
|
| 843 |
bookmark_display_manage = gr.HTML(label="📄 Bookmarks")
|
| 844 |
|
| 845 |
with gr.Row():
|
| 846 |
-
#
|
| 847 |
-
bookmark_selector
|
| 848 |
|
| 849 |
with gr.Row():
|
| 850 |
delete_button = gr.Button("🗑️ Delete Selected")
|
|
@@ -885,6 +885,9 @@ Navigate through the tabs to explore each feature in detail.
|
|
| 885 |
|
| 886 |
logger.info("Launching Gradio app")
|
| 887 |
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
| 888 |
except Exception as e:
|
| 889 |
logger.error(f"Error building the app: {e}", exc_info=True)
|
| 890 |
print(f"Error building the app: {e}")
|
|
|
|
| 469 |
|
| 470 |
if file is None:
|
| 471 |
logger.warning("No file uploaded")
|
| 472 |
+
return "Please upload a bookmarks HTML file.", '', state_bookmarks, gr.CheckboxGroup.update(choices=[])
|
| 473 |
|
| 474 |
try:
|
| 475 |
file_content = file.decode('utf-8')
|
| 476 |
except UnicodeDecodeError as e:
|
| 477 |
logger.error(f"Error decoding the file: {e}", exc_info=True)
|
| 478 |
+
return "Error decoding the file. Please ensure it's a valid HTML file.", '', state_bookmarks, gr.CheckboxGroup.update(choices=[])
|
| 479 |
|
| 480 |
try:
|
| 481 |
bookmarks = parse_bookmarks(file_content)
|
| 482 |
except Exception as e:
|
| 483 |
logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
|
| 484 |
+
return "Error parsing the bookmarks HTML file.", '', state_bookmarks, gr.CheckboxGroup.update(choices=[])
|
| 485 |
|
| 486 |
if not bookmarks:
|
| 487 |
logger.warning("No bookmarks found in the uploaded file")
|
| 488 |
+
return "No bookmarks found in the uploaded file.", '', state_bookmarks, gr.CheckboxGroup.update(choices=[])
|
| 489 |
|
| 490 |
# Assign unique IDs to bookmarks
|
| 491 |
for idx, bookmark in enumerate(bookmarks):
|
|
|
|
| 505 |
faiss_index = vectorize_and_index(bookmarks)
|
| 506 |
except Exception as e:
|
| 507 |
logger.error(f"Error building FAISS index: {e}", exc_info=True)
|
| 508 |
+
return "Error building search index.", '', state_bookmarks, gr.CheckboxGroup.update(choices=[])
|
| 509 |
|
| 510 |
message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
|
| 511 |
logger.info(message)
|
|
|
|
| 518 |
# Update state
|
| 519 |
state_bookmarks = bookmarks.copy()
|
| 520 |
|
| 521 |
+
return message, bookmark_html, state_bookmarks, gr.CheckboxGroup.update(choices=choices)
|
| 522 |
|
| 523 |
def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
| 524 |
"""
|
|
|
|
| 526 |
"""
|
| 527 |
global bookmarks, faiss_index
|
| 528 |
if not selected_indices:
|
| 529 |
+
return "⚠️ No bookmarks selected.", gr.CheckboxGroup.update(choices=[]), display_bookmarks()
|
| 530 |
|
| 531 |
ids_to_delete = []
|
| 532 |
indices_to_delete = []
|
|
|
|
| 554 |
# Update state
|
| 555 |
state_bookmarks = bookmarks.copy()
|
| 556 |
|
| 557 |
+
return message, gr.CheckboxGroup.update(choices=choices), display_bookmarks()
|
| 558 |
|
| 559 |
def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
|
| 560 |
"""
|
| 561 |
Edit category of selected bookmarks.
|
| 562 |
"""
|
| 563 |
if not selected_indices:
|
| 564 |
+
return "⚠️ No bookmarks selected.", gr.CheckboxGroup.update(choices=[]), display_bookmarks(), state_bookmarks
|
| 565 |
if not new_category:
|
| 566 |
+
return "⚠️ No new category selected.", gr.CheckboxGroup.update(choices=[]), display_bookmarks(), state_bookmarks
|
| 567 |
|
| 568 |
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
| 569 |
for idx in indices:
|
|
|
|
| 581 |
# Update state
|
| 582 |
state_bookmarks = bookmarks.copy()
|
| 583 |
|
| 584 |
+
return message, gr.CheckboxGroup.update(choices=choices), display_bookmarks(), state_bookmarks
|
| 585 |
|
| 586 |
def export_bookmarks():
|
| 587 |
"""
|
|
|
|
| 722 |
# Initialize state
|
| 723 |
state_bookmarks = gr.State([])
|
| 724 |
|
| 725 |
+
# Define 'bookmark_selector' once
|
| 726 |
bookmark_selector = gr.CheckboxGroup(
|
| 727 |
label="✅ Select Bookmarks",
|
| 728 |
choices=[]
|
|
|
|
| 773 |
process_button.click(
|
| 774 |
process_uploaded_file,
|
| 775 |
inputs=[upload, state_bookmarks],
|
| 776 |
+
outputs=[output_text, bookmark_display, state_bookmarks, bookmark_selector]
|
| 777 |
)
|
| 778 |
|
| 779 |
with gr.Tab("Chat with Bookmarks"):
|
|
|
|
| 843 |
bookmark_display_manage = gr.HTML(label="📄 Bookmarks")
|
| 844 |
|
| 845 |
with gr.Row():
|
| 846 |
+
# Include 'bookmark_selector' directly in the layout
|
| 847 |
+
bookmark_selector
|
| 848 |
|
| 849 |
with gr.Row():
|
| 850 |
delete_button = gr.Button("🗑️ Delete Selected")
|
|
|
|
| 885 |
|
| 886 |
logger.info("Launching Gradio app")
|
| 887 |
demo.launch(debug=True)
|
| 888 |
+
except gr.Error as e:
|
| 889 |
+
logger.error(f"Gradio Error: {e}", exc_info=True)
|
| 890 |
+
print(f"Gradio Error: {e}")
|
| 891 |
except Exception as e:
|
| 892 |
logger.error(f"Error building the app: {e}", exc_info=True)
|
| 893 |
print(f"Error building the app: {e}")
|