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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -12,15 +12,23 @@ import sys
12
  from sentence_transformers import SentenceTransformer
13
  import faiss
14
  import numpy as np
 
 
15
  import openai
16
 
17
  # Set up logging to output to the console
18
  logger = logging.getLogger(__name__)
19
  logger.setLevel(logging.INFO)
 
 
20
  console_handler = logging.StreamHandler(sys.stdout)
21
  console_handler.setLevel(logging.INFO)
 
 
22
  formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
23
  console_handler.setFormatter(formatter)
 
 
24
  logger.addHandler(console_handler)
25
 
26
  # Initialize models and variables
@@ -257,25 +265,6 @@ def display_bookmarks(bookmarks_list):
257
  logger.info("HTML display generated")
258
  return cards
259
 
260
- # Function to handle sending messages in chat
261
- def send_message(user_message, chat_history, state_bookmarks):
262
- if not user_message:
263
- return chat_history, chat_history
264
-
265
- # Append user message to chat history
266
- chat_history = chat_history + [(user_message, None)]
267
-
268
- # Generate chatbot response
269
- try:
270
- response = chatbot_response(user_message, state_bookmarks)
271
- except Exception as e:
272
- response = f"⚠️ Error: {str(e)}"
273
-
274
- # Append assistant response to chat history
275
- chat_history[-1] = (user_message, response)
276
-
277
- return chat_history, chat_history
278
-
279
  # Process the uploaded file
280
  def process_uploaded_file(file, state_bookmarks):
281
  logger.info("Processing uploaded file")
@@ -357,13 +346,13 @@ def process_uploaded_file(file, state_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,
@@ -405,12 +394,12 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
405
  # Regenerate HTML display
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)]
413
 
 
 
 
414
  return message, gr.update(choices=choices), bookmarks_html
415
 
416
  # Edit category of selected bookmarks
@@ -451,12 +440,12 @@ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookm
451
  # Regenerate HTML display
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)]
459
 
 
 
 
460
  return message, gr.update(choices=choices), bookmarks_html
461
 
462
  # Export bookmarks to HTML
@@ -638,16 +627,16 @@ Navigate through the tabs to explore each feature in detail.
638
 
639
  # When user presses Enter in chat_input
640
  chat_input.submit(
641
- send_message,
642
- inputs=[chat_input, chat_history, state_bookmarks],
643
- outputs=[chat_history_display, chat_history]
644
  )
645
 
646
  # When user clicks Send button
647
  chat_button.click(
648
- send_message,
649
- inputs=[chat_input, chat_history, state_bookmarks],
650
- outputs=[chat_history_display, chat_history]
651
  )
652
 
653
  # Manage Bookmarks Tab
 
12
  from sentence_transformers import SentenceTransformer
13
  import faiss
14
  import numpy as np
15
+
16
+ # Import OpenAI library
17
  import openai
18
 
19
  # Set up logging to output to the console
20
  logger = logging.getLogger(__name__)
21
  logger.setLevel(logging.INFO)
22
+
23
+ # Create a console handler
24
  console_handler = logging.StreamHandler(sys.stdout)
25
  console_handler.setLevel(logging.INFO)
26
+
27
+ # Create a formatter and set it for the handler
28
  formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
29
  console_handler.setFormatter(formatter)
30
+
31
+ # Add the handler to the logger
32
  logger.addHandler(console_handler)
33
 
34
  # Initialize models and variables
 
265
  logger.info("HTML display generated")
266
  return cards
267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  # Process the uploaded file
269
  def process_uploaded_file(file, state_bookmarks):
270
  logger.info("Processing uploaded file")
 
346
  logger.info(message)
347
  bookmark_html = display_bookmarks(bookmarks)
348
 
 
 
 
349
  # Prepare Manage Bookmarks tab outputs
350
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
351
  bookmarks_html_manage = display_bookmarks(bookmarks)
352
 
353
+ # Update the shared state
354
+ updated_state = bookmarks.copy()
355
+
356
  return (
357
  message,
358
  bookmark_html,
 
394
  # Regenerate HTML display
395
  bookmarks_html = display_bookmarks(bookmarks)
396
 
 
 
 
397
  # Update choices for selection
398
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
399
 
400
+ # Update the shared state
401
+ updated_state = bookmarks.copy()
402
+
403
  return message, gr.update(choices=choices), bookmarks_html
404
 
405
  # Edit category of selected bookmarks
 
440
  # Regenerate HTML display
441
  bookmarks_html = display_bookmarks(bookmarks)
442
 
 
 
 
443
  # Update choices for selection
444
  choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
445
 
446
+ # Update the shared state
447
+ updated_state = bookmarks.copy()
448
+
449
  return message, gr.update(choices=choices), bookmarks_html
450
 
451
  # Export bookmarks to HTML
 
627
 
628
  # When user presses Enter in chat_input
629
  chat_input.submit(
630
+ chatbot_response,
631
+ inputs=[chat_input, state_bookmarks],
632
+ outputs=chat_history_display
633
  )
634
 
635
  # When user clicks Send button
636
  chat_button.click(
637
+ chatbot_response,
638
+ inputs=[chat_input, state_bookmarks],
639
+ outputs=chat_history_display
640
  )
641
 
642
  # Manage Bookmarks Tab