Spaces:
Sleeping
Sleeping
siddhartharya
commited on
Commit
•
6842452
1
Parent(s):
0e041b2
Update app.py
Browse files
app.py
CHANGED
@@ -557,11 +557,11 @@ def edit_selected_bookmarks_category(selected_indices, new_category):
|
|
557 |
|
558 |
def export_bookmarks():
|
559 |
"""
|
560 |
-
Export bookmarks to HTML file.
|
561 |
"""
|
562 |
if not bookmarks:
|
563 |
logger.warning("No bookmarks to export")
|
564 |
-
return
|
565 |
|
566 |
try:
|
567 |
logger.info("Exporting bookmarks to HTML")
|
@@ -575,21 +575,24 @@ def export_bookmarks():
|
|
575 |
dl.append(dt)
|
576 |
soup.append(dl)
|
577 |
html_content = str(soup)
|
578 |
-
|
579 |
-
|
|
|
|
|
580 |
logger.info("Bookmarks exported successfully")
|
581 |
-
return
|
582 |
except Exception as e:
|
583 |
logger.error(f"Error exporting bookmarks: {e}", exc_info=True)
|
584 |
-
return
|
585 |
|
586 |
-
def chatbot_response(user_query):
|
587 |
"""
|
588 |
-
Generate chatbot response using the FAISS index and embeddings.
|
589 |
"""
|
590 |
if not bookmarks or faiss_index is None:
|
591 |
logger.warning("No bookmarks available for chatbot")
|
592 |
-
|
|
|
593 |
|
594 |
logger.info(f"Chatbot received query: {user_query}")
|
595 |
|
@@ -607,7 +610,9 @@ def chatbot_response(user_query):
|
|
607 |
matching_bookmarks = [id_to_bookmark.get(id) for id in ids if id in id_to_bookmark]
|
608 |
|
609 |
if not matching_bookmarks:
|
610 |
-
|
|
|
|
|
611 |
|
612 |
# Format the response
|
613 |
bookmarks_info = "\n".join([
|
@@ -649,17 +654,21 @@ Provide a concise and helpful response.
|
|
649 |
answer = response['choices'][0]['message']['content'].strip()
|
650 |
logger.info("Chatbot response generated")
|
651 |
time.sleep(sleep_time)
|
652 |
-
|
|
|
|
|
|
|
653 |
|
654 |
except openai.error.RateLimitError as e:
|
655 |
wait_time = int(e.headers.get("Retry-After", 5))
|
656 |
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying...")
|
657 |
time.sleep(wait_time)
|
658 |
-
return chatbot_response(user_query) # Retry after waiting
|
659 |
except Exception as e:
|
660 |
error_message = f"⚠️ Error processing your query: {str(e)}"
|
661 |
logger.error(error_message, exc_info=True)
|
662 |
-
|
|
|
663 |
|
664 |
def build_app():
|
665 |
"""
|
@@ -702,12 +711,12 @@ def build_app():
|
|
702 |
Ask questions about your bookmarks and get relevant results.
|
703 |
""")
|
704 |
|
|
|
705 |
user_input = gr.Textbox(
|
706 |
label="✍️ Ask about your bookmarks",
|
707 |
placeholder="e.g., Do I have any bookmarks about AI?"
|
708 |
)
|
709 |
chat_button = gr.Button("📨 Send")
|
710 |
-
chat_output = gr.Textbox(label="💬 Response", interactive=False)
|
711 |
|
712 |
# Manage Bookmarks Tab
|
713 |
with gr.Tab("Manage Bookmarks"):
|
@@ -733,7 +742,7 @@ def build_app():
|
|
733 |
edit_category_button = gr.Button("✏️ Edit Category")
|
734 |
export_button = gr.Button("💾 Export")
|
735 |
|
736 |
-
download_link = gr.
|
737 |
|
738 |
# Set up event handlers
|
739 |
process_button.click(
|
@@ -744,8 +753,8 @@ def build_app():
|
|
744 |
|
745 |
chat_button.click(
|
746 |
chatbot_response,
|
747 |
-
inputs=user_input,
|
748 |
-
outputs=
|
749 |
)
|
750 |
|
751 |
delete_button.click(
|
|
|
557 |
|
558 |
def export_bookmarks():
|
559 |
"""
|
560 |
+
Export bookmarks to an HTML file.
|
561 |
"""
|
562 |
if not bookmarks:
|
563 |
logger.warning("No bookmarks to export")
|
564 |
+
return None # Return None instead of a message
|
565 |
|
566 |
try:
|
567 |
logger.info("Exporting bookmarks to HTML")
|
|
|
575 |
dl.append(dt)
|
576 |
soup.append(dl)
|
577 |
html_content = str(soup)
|
578 |
+
# Save to a temporary file
|
579 |
+
output_file = "exported_bookmarks.html"
|
580 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
581 |
+
f.write(html_content)
|
582 |
logger.info("Bookmarks exported successfully")
|
583 |
+
return output_file # Return the file path
|
584 |
except Exception as e:
|
585 |
logger.error(f"Error exporting bookmarks: {e}", exc_info=True)
|
586 |
+
return None # Return None in case of error
|
587 |
|
588 |
+
def chatbot_response(user_query, chat_history):
|
589 |
"""
|
590 |
+
Generate chatbot response using the FAISS index and embeddings, maintaining chat history.
|
591 |
"""
|
592 |
if not bookmarks or faiss_index is None:
|
593 |
logger.warning("No bookmarks available for chatbot")
|
594 |
+
chat_history.append((user_query, "⚠️ No bookmarks available. Please upload and process your bookmarks first."))
|
595 |
+
return chat_history
|
596 |
|
597 |
logger.info(f"Chatbot received query: {user_query}")
|
598 |
|
|
|
610 |
matching_bookmarks = [id_to_bookmark.get(id) for id in ids if id in id_to_bookmark]
|
611 |
|
612 |
if not matching_bookmarks:
|
613 |
+
answer = "No relevant bookmarks found for your query."
|
614 |
+
chat_history.append((user_query, answer))
|
615 |
+
return chat_history
|
616 |
|
617 |
# Format the response
|
618 |
bookmarks_info = "\n".join([
|
|
|
654 |
answer = response['choices'][0]['message']['content'].strip()
|
655 |
logger.info("Chatbot response generated")
|
656 |
time.sleep(sleep_time)
|
657 |
+
|
658 |
+
# Append the interaction to chat history
|
659 |
+
chat_history.append((user_query, answer))
|
660 |
+
return chat_history
|
661 |
|
662 |
except openai.error.RateLimitError as e:
|
663 |
wait_time = int(e.headers.get("Retry-After", 5))
|
664 |
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying...")
|
665 |
time.sleep(wait_time)
|
666 |
+
return chatbot_response(user_query, chat_history) # Retry after waiting
|
667 |
except Exception as e:
|
668 |
error_message = f"⚠️ Error processing your query: {str(e)}"
|
669 |
logger.error(error_message, exc_info=True)
|
670 |
+
chat_history.append((user_query, error_message))
|
671 |
+
return chat_history
|
672 |
|
673 |
def build_app():
|
674 |
"""
|
|
|
711 |
Ask questions about your bookmarks and get relevant results.
|
712 |
""")
|
713 |
|
714 |
+
chatbot = gr.Chatbot(label="💬 Chat with SmartMarks")
|
715 |
user_input = gr.Textbox(
|
716 |
label="✍️ Ask about your bookmarks",
|
717 |
placeholder="e.g., Do I have any bookmarks about AI?"
|
718 |
)
|
719 |
chat_button = gr.Button("📨 Send")
|
|
|
720 |
|
721 |
# Manage Bookmarks Tab
|
722 |
with gr.Tab("Manage Bookmarks"):
|
|
|
742 |
edit_category_button = gr.Button("✏️ Edit Category")
|
743 |
export_button = gr.Button("💾 Export")
|
744 |
|
745 |
+
download_link = gr.File(label="📥 Download Exported Bookmarks")
|
746 |
|
747 |
# Set up event handlers
|
748 |
process_button.click(
|
|
|
753 |
|
754 |
chat_button.click(
|
755 |
chatbot_response,
|
756 |
+
inputs=[user_input, chatbot],
|
757 |
+
outputs=chatbot
|
758 |
)
|
759 |
|
760 |
delete_button.click(
|