Spaces:
Running
Running
siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,7 @@
|
|
1 |
# app.py
|
|
|
2 |
import gradio as gr
|
3 |
from bs4 import BeautifulSoup
|
4 |
-
import requests
|
5 |
-
from sentence_transformers import SentenceTransformer
|
6 |
-
import faiss
|
7 |
-
import numpy as np
|
8 |
import asyncio
|
9 |
import aiohttp
|
10 |
import re
|
@@ -12,23 +9,18 @@ import base64
|
|
12 |
import logging
|
13 |
import os
|
14 |
import sys
|
15 |
-
|
16 |
-
|
|
|
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
|
@@ -289,30 +281,30 @@ def process_uploaded_file(file, state_bookmarks):
|
|
289 |
logger.info("Processing uploaded file")
|
290 |
if file is None:
|
291 |
logger.warning("No file uploaded")
|
292 |
-
return "⚠️ Please upload a bookmarks HTML file.", "", [], ""
|
293 |
|
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,27 +315,25 @@ 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[0] = bookmarks
|
334 |
-
|
335 |
# Prepare Manage Bookmarks tab outputs
|
336 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
|
337 |
bookmarks_html_manage = display_bookmarks(bookmarks)
|
338 |
|
339 |
-
|
|
|
340 |
|
341 |
# Delete selected bookmarks
|
342 |
def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
343 |
if not selected_indices:
|
344 |
return "⚠️ No bookmarks selected.", gr.update(choices=[]), ""
|
345 |
|
346 |
-
bookmarks = state_bookmarks
|
347 |
indices = []
|
348 |
for s in selected_indices:
|
349 |
try:
|
@@ -354,31 +344,40 @@ def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
|
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:
|
364 |
faiss_index = None
|
|
|
365 |
message = "🗑️ Selected bookmarks deleted successfully."
|
366 |
logger.info(message)
|
|
|
367 |
# Regenerate HTML display
|
368 |
bookmarks_html = display_bookmarks(bookmarks)
|
|
|
369 |
# Update the shared state
|
370 |
-
state_bookmarks
|
|
|
371 |
# Update choices for selection
|
372 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
|
|
|
373 |
return message, gr.update(choices=choices), bookmarks_html
|
374 |
|
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
|
381 |
-
|
|
|
382 |
indices = []
|
383 |
for s in selected_indices:
|
384 |
try:
|
@@ -389,22 +388,28 @@ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookm
|
|
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
|
398 |
bookmarks_html = display_bookmarks(bookmarks)
|
|
|
399 |
# Update the shared state
|
400 |
-
state_bookmarks
|
|
|
401 |
# Update choices for selection
|
402 |
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)]
|
|
|
403 |
return message, gr.update(choices=choices), bookmarks_html
|
404 |
|
405 |
# Export bookmarks to HTML
|
406 |
def export_bookmarks(state_bookmarks):
|
407 |
-
bookmarks = state_bookmarks
|
408 |
if not bookmarks:
|
409 |
logger.warning("No bookmarks to export")
|
410 |
return "⚠️ No bookmarks to export."
|
@@ -436,7 +441,7 @@ def chatbot_response(user_query, state_bookmarks):
|
|
436 |
logger.warning("GROQ_API_KEY not set.")
|
437 |
return "⚠️ API key not set. Please set the GROQ_API_KEY environment variable in the Hugging Face Space settings."
|
438 |
|
439 |
-
bookmarks = state_bookmarks
|
440 |
if not bookmarks:
|
441 |
logger.warning("No bookmarks available for chatbot")
|
442 |
return "⚠️ No bookmarks available. Please upload and process your bookmarks first."
|
@@ -545,7 +550,7 @@ Navigate through the tabs to explore each feature in detail.
|
|
545 |
process_button.click(
|
546 |
process_uploaded_file,
|
547 |
inputs=[upload, state_bookmarks],
|
548 |
-
outputs=[output_text, bookmark_display, bookmark_selector, bookmark_display_manage]
|
549 |
)
|
550 |
|
551 |
# Chat with Bookmarks Tab
|
|
|
1 |
# app.py
|
2 |
+
|
3 |
import gradio as gr
|
4 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
5 |
import asyncio
|
6 |
import aiohttp
|
7 |
import re
|
|
|
9 |
import logging
|
10 |
import os
|
11 |
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
|
|
|
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 |
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):
|
333 |
if not selected_indices:
|
334 |
return "⚠️ No bookmarks selected.", gr.update(choices=[]), ""
|
335 |
|
336 |
+
bookmarks = state_bookmarks.copy()
|
337 |
indices = []
|
338 |
for s in selected_indices:
|
339 |
try:
|
|
|
344 |
logger.warning(f"Index out of range: {idx + 1}")
|
345 |
except ValueError:
|
346 |
logger.error(f"Invalid selection format: {s}")
|
347 |
+
|
348 |
indices = sorted(indices, reverse=True)
|
349 |
for idx in indices:
|
350 |
logger.info(f"Deleting bookmark at index {idx + 1}")
|
351 |
bookmarks.pop(idx)
|
352 |
+
|
353 |
if bookmarks:
|
354 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
355 |
else:
|
356 |
faiss_index = None
|
357 |
+
|
358 |
message = "🗑️ Selected bookmarks deleted successfully."
|
359 |
logger.info(message)
|
360 |
+
|
361 |
# Regenerate HTML display
|
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)]
|
369 |
+
|
370 |
return message, gr.update(choices=choices), bookmarks_html
|
371 |
|
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 = []
|
382 |
for s in selected_indices:
|
383 |
try:
|
|
|
388 |
logger.warning(f"Index out of range: {idx + 1}")
|
389 |
except ValueError:
|
390 |
logger.error(f"Invalid selection format: {s}")
|
391 |
+
|
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 |
+
|
396 |
message = "✏️ Category updated for selected bookmarks."
|
397 |
logger.info(message)
|
398 |
+
|
399 |
# Regenerate HTML display
|
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)]
|
407 |
+
|
408 |
return message, gr.update(choices=choices), bookmarks_html
|
409 |
|
410 |
# Export bookmarks to HTML
|
411 |
def export_bookmarks(state_bookmarks):
|
412 |
+
bookmarks = state_bookmarks
|
413 |
if not bookmarks:
|
414 |
logger.warning("No bookmarks to export")
|
415 |
return "⚠️ No bookmarks to export."
|
|
|
441 |
logger.warning("GROQ_API_KEY not set.")
|
442 |
return "⚠️ API key not set. Please set the GROQ_API_KEY environment variable in the Hugging Face Space settings."
|
443 |
|
444 |
+
bookmarks = state_bookmarks
|
445 |
if not bookmarks:
|
446 |
logger.warning("No bookmarks available for chatbot")
|
447 |
return "⚠️ No bookmarks available. Please upload and process your bookmarks first."
|
|
|
550 |
process_button.click(
|
551 |
process_uploaded_file,
|
552 |
inputs=[upload, state_bookmarks],
|
553 |
+
outputs=[output_text, bookmark_display, bookmark_selector, bookmark_display_manage, state_bookmarks]
|
554 |
)
|
555 |
|
556 |
# Chat with Bookmarks Tab
|