siddhartharya commited on
Commit
ab5c457
Β·
verified Β·
1 Parent(s): eec2f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -448,7 +448,7 @@ def display_bookmarks():
448
  logger.info("HTML display generated")
449
  return cards
450
 
451
- def process_uploaded_file(file, state_bookmarks, bookmark_selector):
452
  """
453
  Process the uploaded bookmarks file.
454
  """
@@ -457,23 +457,23 @@ def process_uploaded_file(file, state_bookmarks, bookmark_selector):
457
 
458
  if file is None:
459
  logger.warning("No file uploaded")
460
- return "Please upload a bookmarks HTML file.", '', state_bookmarks, bookmark_selector.update(choices=[])
461
 
462
  try:
463
  file_content = file.decode('utf-8')
464
  except UnicodeDecodeError as e:
465
  logger.error(f"Error decoding the file: {e}", exc_info=True)
466
- return "Error decoding the file. Please ensure it's a valid HTML file.", '', state_bookmarks, bookmark_selector.update(choices=[])
467
 
468
  try:
469
  bookmarks = parse_bookmarks(file_content)
470
  except Exception as e:
471
  logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
472
- return "Error parsing the bookmarks HTML file.", '', state_bookmarks, bookmark_selector.update(choices=[])
473
 
474
  if not bookmarks:
475
  logger.warning("No bookmarks found in the uploaded file")
476
- return "No bookmarks found in the uploaded file.", '', state_bookmarks, bookmark_selector.update(choices=[])
477
 
478
  # Assign unique IDs to bookmarks
479
  for idx, bookmark in enumerate(bookmarks):
@@ -493,7 +493,7 @@ def process_uploaded_file(file, state_bookmarks, bookmark_selector):
493
  faiss_index = vectorize_and_index(bookmarks)
494
  except Exception as e:
495
  logger.error(f"Error building FAISS index: {e}", exc_info=True)
496
- return "Error building search index.", '', state_bookmarks, bookmark_selector.update(choices=[])
497
 
498
  message = f"βœ… Successfully processed {len(bookmarks)} bookmarks."
499
  logger.info(message)
@@ -506,15 +506,15 @@ def process_uploaded_file(file, state_bookmarks, bookmark_selector):
506
  # Update state
507
  state_bookmarks = bookmarks.copy()
508
 
509
- return message, bookmark_html, state_bookmarks, bookmark_selector.update(choices=choices)
510
 
511
- def delete_selected_bookmarks(selected_indices, state_bookmarks, bookmark_selector, bookmark_display_manage):
512
  """
513
  Delete selected bookmarks and remove their vectors from the FAISS index.
514
  """
515
  global bookmarks, faiss_index
516
  if not selected_indices:
517
- return "⚠️ No bookmarks selected.", bookmark_selector.update(choices=[]), display_bookmarks()
518
 
519
  ids_to_delete = []
520
  indices_to_delete = []
@@ -545,16 +545,16 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks, bookmark_select
545
  # Update state
546
  state_bookmarks = bookmarks.copy()
547
 
548
- return message, bookmark_selector.update(choices=choices), display_bookmarks()
549
 
550
- def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks, bookmark_selector, bookmark_display_manage):
551
  """
552
  Edit category of selected bookmarks.
553
  """
554
  if not selected_indices:
555
- return "⚠️ No bookmarks selected.", bookmark_selector.update(choices=[]), display_bookmarks(), state_bookmarks
556
  if not new_category:
557
- return "⚠️ No new category selected.", bookmark_selector.update(choices=[]), display_bookmarks(), state_bookmarks
558
 
559
  indices = []
560
  for s in selected_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, bookmark_selector.update(choices=choices), display_bookmarks(), state_bookmarks
585
 
586
  def export_bookmarks():
587
  """
@@ -698,6 +698,12 @@ def build_app():
698
  # Initialize state
699
  state_bookmarks = gr.State([])
700
 
 
 
 
 
 
 
701
  # General Overview
702
  gr.Markdown("""
703
  # πŸ“š SmartMarks - AI Browser Bookmarks Manager
@@ -742,8 +748,8 @@ Navigate through the tabs to explore each feature in detail.
742
 
743
  process_button.click(
744
  process_uploaded_file,
745
- inputs=[upload, state_bookmarks, None], # 'bookmark_selector' is defined within "Manage Bookmarks" tab
746
- outputs=[output_text, bookmark_display, state_bookmarks, gr.State(None)]
747
  )
748
 
749
  with gr.Tab("Chat with Bookmarks"):
@@ -813,11 +819,9 @@ Navigate through the tabs to explore each feature in detail.
813
  bookmark_display_manage = gr.HTML(label="πŸ“„ Bookmarks")
814
 
815
  with gr.Row():
816
- # Define 'bookmark_selector' within the tab
817
- bookmark_selector = gr.CheckboxGroup(
818
- label="βœ… Select Bookmarks",
819
- choices=[]
820
- )
821
 
822
  with gr.Row():
823
  delete_button = gr.Button("πŸ—‘οΈ Delete Selected")
 
448
  logger.info("HTML display generated")
449
  return cards
450
 
451
+ def process_uploaded_file(file, state_bookmarks):
452
  """
453
  Process the uploaded bookmarks file.
454
  """
 
457
 
458
  if file is None:
459
  logger.warning("No file uploaded")
460
+ return "Please upload a bookmarks HTML file.", '', state_bookmarks, gr.update(choices=[])
461
 
462
  try:
463
  file_content = file.decode('utf-8')
464
  except UnicodeDecodeError as e:
465
  logger.error(f"Error decoding the file: {e}", exc_info=True)
466
+ return "Error decoding the file. Please ensure it's a valid HTML file.", '', state_bookmarks, gr.update(choices=[])
467
 
468
  try:
469
  bookmarks = parse_bookmarks(file_content)
470
  except Exception as e:
471
  logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
472
+ return "Error parsing the bookmarks HTML file.", '', state_bookmarks, gr.update(choices=[])
473
 
474
  if not bookmarks:
475
  logger.warning("No bookmarks found in the uploaded file")
476
+ return "No bookmarks found in the uploaded file.", '', state_bookmarks, gr.update(choices=[])
477
 
478
  # Assign unique IDs to bookmarks
479
  for idx, bookmark in enumerate(bookmarks):
 
493
  faiss_index = vectorize_and_index(bookmarks)
494
  except Exception as e:
495
  logger.error(f"Error building FAISS index: {e}", exc_info=True)
496
+ return "Error building search index.", '', state_bookmarks, gr.update(choices=[])
497
 
498
  message = f"βœ… Successfully processed {len(bookmarks)} bookmarks."
499
  logger.info(message)
 
506
  # Update state
507
  state_bookmarks = bookmarks.copy()
508
 
509
+ return message, bookmark_html, state_bookmarks, gr.update(choices=choices)
510
 
511
+ def delete_selected_bookmarks(selected_indices, state_bookmarks):
512
  """
513
  Delete selected bookmarks and remove their vectors from the FAISS index.
514
  """
515
  global bookmarks, faiss_index
516
  if not selected_indices:
517
+ return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
518
 
519
  ids_to_delete = []
520
  indices_to_delete = []
 
545
  # Update state
546
  state_bookmarks = bookmarks.copy()
547
 
548
+ return message, gr.update(choices=choices), display_bookmarks()
549
 
550
+ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
551
  """
552
  Edit category of selected bookmarks.
553
  """
554
  if not selected_indices:
555
+ return "⚠️ No bookmarks selected.", gr.update(choices=[]), display_bookmarks(), state_bookmarks
556
  if not new_category:
557
+ return "⚠️ No new category selected.", gr.update(choices=[]), display_bookmarks(), state_bookmarks
558
 
559
  indices = []
560
  for s in selected_indices:
 
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
  """
 
698
  # Initialize state
699
  state_bookmarks = gr.State([])
700
 
701
+ # Define 'bookmark_selector' globally
702
+ bookmark_selector = gr.CheckboxGroup(
703
+ label="βœ… Select Bookmarks",
704
+ choices=[]
705
+ )
706
+
707
  # General Overview
708
  gr.Markdown("""
709
  # πŸ“š SmartMarks - AI Browser Bookmarks Manager
 
748
 
749
  process_button.click(
750
  process_uploaded_file,
751
+ inputs=[upload, state_bookmarks],
752
+ outputs=[output_text, bookmark_display, state_bookmarks, bookmark_selector]
753
  )
754
 
755
  with gr.Tab("Chat with Bookmarks"):
 
819
  bookmark_display_manage = gr.HTML(label="πŸ“„ Bookmarks")
820
 
821
  with gr.Row():
822
+ # Include 'bookmark_selector' within the tab
823
+ # It is defined globally and will be displayed only in this tab via CSS
824
+ bookmark_selector
 
 
825
 
826
  with gr.Row():
827
  delete_button = gr.Button("πŸ—‘οΈ Delete Selected")