siddhartharya commited on
Commit
211e5fb
·
verified ·
1 Parent(s): 02fc02d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
app.py CHANGED
@@ -290,29 +290,30 @@ def process_uploaded_file(file, state_bookmarks):
290
  logger.info("Processing uploaded file")
291
  if file is None:
292
  logger.warning("No file uploaded")
293
- return "⚠️ Please upload a bookmarks HTML file.", ""
 
294
  try:
295
  file_content = file.decode('utf-8')
296
  except UnicodeDecodeError as e:
297
  logger.error(f"Error decoding the file: {e}")
298
- return "⚠️ Error decoding the file. Please ensure it's a valid HTML file.", ""
299
 
300
  try:
301
  bookmarks = parse_bookmarks(file_content)
302
  except Exception as e:
303
  logger.error(f"Error parsing bookmarks: {e}")
304
- return "⚠️ Error parsing the bookmarks HTML file.", ""
305
 
306
  if not bookmarks:
307
  logger.warning("No bookmarks found in the uploaded file")
308
- return "⚠️ No bookmarks found in the uploaded file.", ""
309
 
310
  # Asynchronously fetch bookmark info
311
  try:
312
  asyncio.run(process_bookmarks_async(bookmarks))
313
  except Exception as e:
314
  logger.error(f"Error processing bookmarks asynchronously: {e}")
315
- return "⚠️ Error processing bookmarks.", ""
316
 
317
  # Generate summaries and assign categories
318
  for bookmark in bookmarks:
@@ -323,28 +324,40 @@ def process_uploaded_file(file, state_bookmarks):
323
  faiss_index, embeddings = vectorize_and_index(bookmarks)
324
  except Exception as e:
325
  logger.error(f"Error building FAISS index: {e}")
326
- return "⚠️ Error building search index.", ""
327
 
328
  message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
329
  logger.info(message)
330
  bookmark_html = display_bookmarks(bookmarks)
331
 
332
  # Update the shared state
333
- state_bookmarks.append(bookmarks)
 
 
 
 
334
 
335
- return message, bookmark_html
336
 
337
  # Delete selected bookmarks
338
  def delete_selected_bookmarks(selected_indices, state_bookmarks):
339
  if not selected_indices:
340
  return "⚠️ No bookmarks selected.", gr.update(choices=[]), ""
341
  bookmarks = state_bookmarks[0]
342
- indices = [int(s.split('.')[0])-1 for s in selected_indices]
 
 
 
 
 
 
 
 
 
343
  indices = sorted(indices, reverse=True)
344
  for idx in indices:
345
- if 0 <= idx < len(bookmarks):
346
- logger.info(f"Deleting bookmark at index {idx + 1}")
347
- bookmarks.pop(idx)
348
  if bookmarks:
349
  faiss_index, embeddings = vectorize_and_index(bookmarks)
350
  else:
@@ -362,15 +375,23 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
362
  # Edit category of selected bookmarks
363
  def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
364
  if not selected_indices:
365
- return "⚠️ No bookmarks selected.", "", ""
366
  if not new_category:
367
- return "⚠️ No new category selected.", "", ""
368
  bookmarks = state_bookmarks[0]
369
- indices = [int(s.split('.')[0])-1 for s in selected_indices]
 
 
 
 
 
 
 
 
 
370
  for idx in indices:
371
- if 0 <= idx < len(bookmarks):
372
- bookmarks[idx]['category'] = new_category
373
- logger.info(f"Updated category for bookmark {idx + 1} to {new_category}")
374
  message = "✏️ Category updated for selected bookmarks."
375
  logger.info(message)
376
  # Regenerate HTML display
@@ -520,7 +541,7 @@ Navigate through the tabs to explore each feature in detail.
520
  process_button.click(
521
  process_uploaded_file,
522
  inputs=[upload, state_bookmarks],
523
- outputs=[output_text, bookmark_display]
524
  )
525
 
526
  # Chat with Bookmarks Tab
@@ -558,14 +579,14 @@ Navigate through the tabs to explore each feature in detail.
558
  chat_input.submit(
559
  send_message,
560
  inputs=[chat_input, chat_history, state_bookmarks],
561
- outputs=[chat_history_display, chat_history_display]
562
  )
563
 
564
  # When user clicks Send button
565
  chat_button.click(
566
  send_message,
567
  inputs=[chat_input, chat_history, state_bookmarks],
568
- outputs=[chat_history_display, chat_history_display]
569
  )
570
 
571
  # Manage Bookmarks Tab
@@ -605,22 +626,6 @@ Navigate through the tabs to explore each feature in detail.
605
  bookmark_display_manage = gr.HTML(label="📄 Manage Bookmarks Display")
606
  refresh_button = gr.Button("🔄 Refresh Bookmarks")
607
 
608
- # Function to refresh Manage Bookmarks Tab
609
- def refresh_manage_tab(state_bookmarks):
610
- bookmarks = state_bookmarks[0]
611
- if bookmarks:
612
- choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
613
- bookmarks_html = display_bookmarks(bookmarks)
614
- return choices, bookmarks_html
615
- else:
616
- return [], ""
617
-
618
- refresh_button.click(
619
- refresh_manage_tab,
620
- inputs=[state_bookmarks],
621
- outputs=[bookmark_selector, bookmark_display_manage]
622
- )
623
-
624
  # Define button actions
625
  delete_button.click(
626
  delete_selected_bookmarks,
@@ -641,7 +646,9 @@ Navigate through the tabs to explore each feature in detail.
641
  )
642
 
643
  refresh_button.click(
644
- refresh_manage_tab,
 
 
645
  inputs=[state_bookmarks],
646
  outputs=[bookmark_selector, bookmark_display_manage]
647
  )
 
290
  logger.info("Processing uploaded file")
291
  if file is None:
292
  logger.warning("No file uploaded")
293
+ return "⚠️ Please upload a bookmarks HTML file.", "", [], ""
294
+
295
  try:
296
  file_content = file.decode('utf-8')
297
  except UnicodeDecodeError as e:
298
  logger.error(f"Error decoding the file: {e}")
299
+ return "⚠️ Error decoding the file. Please ensure it's a valid HTML file.", "", [], ""
300
 
301
  try:
302
  bookmarks = parse_bookmarks(file_content)
303
  except Exception as e:
304
  logger.error(f"Error parsing bookmarks: {e}")
305
+ return "⚠️ Error parsing the bookmarks HTML file.", "", [], ""
306
 
307
  if not bookmarks:
308
  logger.warning("No bookmarks found in the uploaded file")
309
+ return "⚠️ No bookmarks found in the uploaded file.", "", [], ""
310
 
311
  # Asynchronously fetch bookmark info
312
  try:
313
  asyncio.run(process_bookmarks_async(bookmarks))
314
  except Exception as e:
315
  logger.error(f"Error processing bookmarks asynchronously: {e}")
316
+ return "⚠️ Error processing bookmarks.", "", [], ""
317
 
318
  # Generate summaries and assign categories
319
  for bookmark in bookmarks:
 
324
  faiss_index, embeddings = vectorize_and_index(bookmarks)
325
  except Exception as e:
326
  logger.error(f"Error building FAISS index: {e}")
327
+ return "⚠️ Error building search index.", "", [], ""
328
 
329
  message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
330
  logger.info(message)
331
  bookmark_html = display_bookmarks(bookmarks)
332
 
333
  # Update the shared state
334
+ state_bookmarks[0] = bookmarks
335
+
336
+ # Prepare Manage Bookmarks tab outputs
337
+ choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
338
+ bookmarks_html_manage = display_bookmarks(bookmarks)
339
 
340
+ return message, bookmark_html, choices, bookmarks_html_manage
341
 
342
  # Delete selected bookmarks
343
  def delete_selected_bookmarks(selected_indices, state_bookmarks):
344
  if not selected_indices:
345
  return "⚠️ No bookmarks selected.", gr.update(choices=[]), ""
346
  bookmarks = state_bookmarks[0]
347
+ indices = []
348
+ for s in selected_indices:
349
+ try:
350
+ idx = int(s.split('.')[0]) - 1
351
+ if 0 <= idx < len(bookmarks):
352
+ indices.append(idx)
353
+ else:
354
+ logger.warning(f"Index out of range: {idx + 1}")
355
+ except ValueError:
356
+ logger.error(f"Invalid selection format: {s}")
357
  indices = sorted(indices, reverse=True)
358
  for idx in indices:
359
+ logger.info(f"Deleting bookmark at index {idx + 1}")
360
+ bookmarks.pop(idx)
 
361
  if bookmarks:
362
  faiss_index, embeddings = vectorize_and_index(bookmarks)
363
  else:
 
375
  # Edit category of selected bookmarks
376
  def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
377
  if not selected_indices:
378
+ return "⚠️ No bookmarks selected.", gr.update(choices=[]), ""
379
  if not new_category:
380
+ return "⚠️ No new category selected.", gr.update(choices=[f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(state_bookmarks[0])]), display_bookmarks(state_bookmarks[0])
381
  bookmarks = state_bookmarks[0]
382
+ indices = []
383
+ for s in selected_indices:
384
+ try:
385
+ idx = int(s.split('.')[0]) - 1
386
+ if 0 <= idx < len(bookmarks):
387
+ indices.append(idx)
388
+ else:
389
+ logger.warning(f"Index out of range: {idx + 1}")
390
+ except ValueError:
391
+ logger.error(f"Invalid selection format: {s}")
392
  for idx in indices:
393
+ bookmarks[idx]['category'] = new_category
394
+ logger.info(f"Updated category for bookmark {idx + 1} to {new_category}")
 
395
  message = "✏️ Category updated for selected bookmarks."
396
  logger.info(message)
397
  # Regenerate HTML display
 
541
  process_button.click(
542
  process_uploaded_file,
543
  inputs=[upload, state_bookmarks],
544
+ outputs=[output_text, bookmark_display, "bookmark_selector", "bookmark_display_manage"] # Added outputs for Manage Bookmarks
545
  )
546
 
547
  # Chat with Bookmarks Tab
 
579
  chat_input.submit(
580
  send_message,
581
  inputs=[chat_input, chat_history, state_bookmarks],
582
+ outputs=[chat_history_display, chat_history]
583
  )
584
 
585
  # When user clicks Send button
586
  chat_button.click(
587
  send_message,
588
  inputs=[chat_input, chat_history, state_bookmarks],
589
+ outputs=[chat_history_display, chat_history]
590
  )
591
 
592
  # Manage Bookmarks Tab
 
626
  bookmark_display_manage = gr.HTML(label="📄 Manage Bookmarks Display")
627
  refresh_button = gr.Button("🔄 Refresh Bookmarks")
628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
629
  # Define button actions
630
  delete_button.click(
631
  delete_selected_bookmarks,
 
646
  )
647
 
648
  refresh_button.click(
649
+ lambda bookmarks: ([
650
+ f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)
651
+ ], display_bookmarks(bookmarks)),
652
  inputs=[state_bookmarks],
653
  outputs=[bookmark_selector, bookmark_display_manage]
654
  )