siddhartharya commited on
Commit
15dbed5
·
verified ·
1 Parent(s): 7468d44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -15
app.py CHANGED
@@ -281,30 +281,60 @@ def process_uploaded_file(file, state_bookmarks):
281
  logger.info("Processing uploaded file")
282
  if file is None:
283
  logger.warning("No file uploaded")
284
- return "⚠️ Please upload a bookmarks HTML file.", "", [], "", []
 
 
 
 
 
 
285
 
286
  try:
287
  file_content = file.decode('utf-8')
288
  except UnicodeDecodeError as e:
289
  logger.error(f"Error decoding the file: {e}")
290
- return "⚠️ Error decoding the file. Please ensure it's a valid HTML file.", "", [], "", state_bookmarks
 
 
 
 
 
 
291
 
292
  try:
293
  bookmarks = parse_bookmarks(file_content)
294
  except Exception as e:
295
  logger.error(f"Error parsing bookmarks: {e}")
296
- return "⚠️ Error parsing the bookmarks HTML file.", "", [], "", state_bookmarks
 
 
 
 
 
 
297
 
298
  if not bookmarks:
299
  logger.warning("No bookmarks found in the uploaded file")
300
- return "⚠️ No bookmarks found in the uploaded file.", "", [], "", state_bookmarks
 
 
 
 
 
 
301
 
302
  # Asynchronously fetch bookmark info
303
  try:
304
  asyncio.run(process_bookmarks_async(bookmarks))
305
  except Exception as e:
306
  logger.error(f"Error processing bookmarks asynchronously: {e}")
307
- return "⚠️ Error processing bookmarks.", "", [], "", state_bookmarks
 
 
 
 
 
 
308
 
309
  # Generate summaries and assign categories
310
  for bookmark in bookmarks:
@@ -315,18 +345,32 @@ def process_uploaded_file(file, state_bookmarks):
315
  faiss_index, embeddings = vectorize_and_index(bookmarks)
316
  except Exception as e:
317
  logger.error(f"Error building FAISS index: {e}")
318
- return "⚠️ Error building search index.", "", [], "", state_bookmarks
 
 
 
 
 
 
319
 
320
  message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
321
  logger.info(message)
322
  bookmark_html = display_bookmarks(bookmarks)
323
 
 
 
 
324
  # Prepare Manage Bookmarks tab outputs
325
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
326
  bookmarks_html_manage = display_bookmarks(bookmarks)
327
 
328
- # Return the updated state as the last output
329
- return message, bookmark_html, choices, bookmarks_html_manage, bookmarks
 
 
 
 
 
330
 
331
  # Delete selected bookmarks
332
  def delete_selected_bookmarks(selected_indices, state_bookmarks):
@@ -362,7 +406,7 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
362
  bookmarks_html = display_bookmarks(bookmarks)
363
 
364
  # Update the shared state
365
- state_bookmarks = bookmarks.copy()
366
 
367
  # Update choices for selection
368
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
@@ -372,10 +416,18 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
372
  # Edit category of selected bookmarks
373
  def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
374
  if not selected_indices:
375
- return "⚠️ No bookmarks selected.", gr.update(choices=[f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(state_bookmarks)]), display_bookmarks(state_bookmarks)
 
 
 
 
376
 
377
  if not new_category:
378
- return "⚠️ No new category selected.", gr.update(choices=[f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(state_bookmarks)]), display_bookmarks(state_bookmarks)
 
 
 
 
379
 
380
  bookmarks = state_bookmarks.copy()
381
  indices = []
@@ -400,7 +452,7 @@ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookm
400
  bookmarks_html = display_bookmarks(bookmarks)
401
 
402
  # Update the shared state
403
- state_bookmarks = bookmarks.copy()
404
 
405
  # Update choices for selection
406
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
@@ -653,9 +705,12 @@ Navigate through the tabs to explore each feature in detail.
653
  )
654
 
655
  refresh_button.click(
656
- lambda bookmarks: ([
657
- f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)
658
- ], display_bookmarks(bookmarks)),
 
 
 
659
  inputs=[state_bookmarks],
660
  outputs=[bookmark_selector, bookmark_display_manage]
661
  )
 
281
  logger.info("Processing uploaded file")
282
  if file is None:
283
  logger.warning("No file uploaded")
284
+ return (
285
+ "⚠️ Please upload a bookmarks HTML file.",
286
+ "",
287
+ [],
288
+ "",
289
+ state_bookmarks # Return the unchanged state
290
+ )
291
 
292
  try:
293
  file_content = file.decode('utf-8')
294
  except UnicodeDecodeError as e:
295
  logger.error(f"Error decoding the file: {e}")
296
+ return (
297
+ "⚠️ Error decoding the file. Please ensure it's a valid HTML file.",
298
+ "",
299
+ [],
300
+ "",
301
+ state_bookmarks # Return the unchanged state
302
+ )
303
 
304
  try:
305
  bookmarks = parse_bookmarks(file_content)
306
  except Exception as e:
307
  logger.error(f"Error parsing bookmarks: {e}")
308
+ return (
309
+ "⚠️ Error parsing the bookmarks HTML file.",
310
+ "",
311
+ [],
312
+ "",
313
+ state_bookmarks # Return the unchanged state
314
+ )
315
 
316
  if not bookmarks:
317
  logger.warning("No bookmarks found in the uploaded file")
318
+ return (
319
+ "⚠️ No bookmarks found in the uploaded file.",
320
+ "",
321
+ [],
322
+ "",
323
+ state_bookmarks # Return the unchanged state
324
+ )
325
 
326
  # Asynchronously fetch bookmark info
327
  try:
328
  asyncio.run(process_bookmarks_async(bookmarks))
329
  except Exception as e:
330
  logger.error(f"Error processing bookmarks asynchronously: {e}")
331
+ return (
332
+ "⚠️ Error processing bookmarks.",
333
+ "",
334
+ [],
335
+ "",
336
+ state_bookmarks # Return the unchanged state
337
+ )
338
 
339
  # Generate summaries and assign categories
340
  for bookmark in bookmarks:
 
345
  faiss_index, embeddings = vectorize_and_index(bookmarks)
346
  except Exception as e:
347
  logger.error(f"Error building FAISS index: {e}")
348
+ return (
349
+ "⚠️ Error building search index.",
350
+ "",
351
+ [],
352
+ "",
353
+ state_bookmarks # Return the unchanged state
354
+ )
355
 
356
  message = f"✅ Successfully processed {len(bookmarks)} bookmarks."
357
  logger.info(message)
358
  bookmark_html = display_bookmarks(bookmarks)
359
 
360
+ # Update the shared state
361
+ updated_state = bookmarks.copy()
362
+
363
  # Prepare Manage Bookmarks tab outputs
364
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
365
  bookmarks_html_manage = display_bookmarks(bookmarks)
366
 
367
+ return (
368
+ message,
369
+ bookmark_html,
370
+ choices,
371
+ bookmarks_html_manage,
372
+ updated_state # Return the updated state
373
+ )
374
 
375
  # Delete selected bookmarks
376
  def delete_selected_bookmarks(selected_indices, state_bookmarks):
 
406
  bookmarks_html = display_bookmarks(bookmarks)
407
 
408
  # Update the shared state
409
+ updated_state = bookmarks.copy()
410
 
411
  # Update choices for selection
412
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
 
416
  # Edit category of selected bookmarks
417
  def edit_selected_bookmarks_category(selected_indices, new_category, state_bookmarks):
418
  if not selected_indices:
419
+ return (
420
+ "⚠️ No bookmarks selected.",
421
+ gr.update(choices=[f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(state_bookmarks)]),
422
+ display_bookmarks(state_bookmarks)
423
+ )
424
 
425
  if not new_category:
426
+ return (
427
+ "⚠️ No new category selected.",
428
+ gr.update(choices=[f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(state_bookmarks)]),
429
+ display_bookmarks(state_bookmarks)
430
+ )
431
 
432
  bookmarks = state_bookmarks.copy()
433
  indices = []
 
452
  bookmarks_html = display_bookmarks(bookmarks)
453
 
454
  # Update the shared state
455
+ updated_state = bookmarks.copy()
456
 
457
  # Update choices for selection
458
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
 
705
  )
706
 
707
  refresh_button.click(
708
+ lambda bookmarks: (
709
+ [
710
+ f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)
711
+ ],
712
+ display_bookmarks(bookmarks)
713
+ ),
714
  inputs=[state_bookmarks],
715
  outputs=[bookmark_selector, bookmark_display_manage]
716
  )