Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,10 @@ import threading
|
|
| 19 |
# Import OpenAI library
|
| 20 |
import openai
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Set up logging to output to the console
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
logger.setLevel(logging.INFO)
|
|
@@ -34,8 +38,8 @@ console_handler.setFormatter(formatter)
|
|
| 34 |
# Add the handler to the logger
|
| 35 |
logger.addHandler(console_handler)
|
| 36 |
|
| 37 |
-
# Initialize
|
| 38 |
-
logger.info("Initializing
|
| 39 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 40 |
faiss_index = None
|
| 41 |
bookmarks = []
|
|
@@ -77,7 +81,10 @@ if not GROQ_API_KEY:
|
|
| 77 |
logger.error("GROQ_API_KEY environment variable not set.")
|
| 78 |
|
| 79 |
openai.api_key = GROQ_API_KEY
|
| 80 |
-
openai.api_base = "https://api.groq.com/openai/v1"
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def extract_main_content(soup):
|
| 83 |
"""
|
|
@@ -235,15 +242,22 @@ Category: [One category]
|
|
| 235 |
required_delay = total_tokens / tokens_per_second
|
| 236 |
sleep_time = max(required_delay, 1)
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
content = response['choices'][0]['message']['content'].strip()
|
| 248 |
if not content:
|
| 249 |
raise ValueError("Empty response received from the model.")
|
|
@@ -281,7 +295,7 @@ Category: [One category]
|
|
| 281 |
except openai.error.RateLimitError as e:
|
| 282 |
retry_count += 1
|
| 283 |
wait_time = int(e.headers.get("Retry-After", 5))
|
| 284 |
-
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying...")
|
| 285 |
time.sleep(wait_time)
|
| 286 |
except Exception as e:
|
| 287 |
logger.error(f"Error generating summary and assigning category: {e}", exc_info=True)
|
|
@@ -289,339 +303,341 @@ Category: [One category]
|
|
| 289 |
bookmark['category'] = 'Uncategorized'
|
| 290 |
break # Exit the retry loop on other exceptions
|
| 291 |
|
| 292 |
-
def parse_bookmarks(file_content):
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
def fetch_url_info(bookmark):
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
bookmark['description'] = ''
|
| 342 |
bookmark['html_content'] = ''
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
bookmark['
|
|
|
|
|
|
|
| 347 |
bookmark['description'] = ''
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
url = escape(url)
|
| 426 |
-
summary = escape(summary)
|
| 427 |
-
category = escape(category)
|
| 428 |
-
|
| 429 |
-
card_html = f'''
|
| 430 |
-
<div class="card" style="{card_style} padding: 10px; margin: 10px; border-radius: 5px; background-color: #1e1e1e;">
|
| 431 |
-
<div class="card-content">
|
| 432 |
-
<h3 style="{text_style}">{index}. {title} {status}</h3>
|
| 433 |
-
<p style="{text_style}"><strong>Category:</strong> {category}</p>
|
| 434 |
-
<p style="{text_style}"><strong>URL:</strong> <a href="{url}" target="_blank" style="{text_style}">{url}</a></p>
|
| 435 |
-
<p style="{text_style}"><strong>ETag:</strong> {etag}</p>
|
| 436 |
-
<p style="{text_style}"><strong>Summary:</strong> {summary}</p>
|
| 437 |
</div>
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
logger.info("Processing uploaded file")
|
| 450 |
-
|
| 451 |
-
if file is None:
|
| 452 |
-
logger.warning("No file uploaded")
|
| 453 |
-
return "Please upload a bookmarks HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
| 454 |
-
|
| 455 |
-
try:
|
| 456 |
-
file_content = file.decode('utf-8')
|
| 457 |
-
except UnicodeDecodeError as e:
|
| 458 |
-
logger.error(f"Error decoding the file: {e}", exc_info=True)
|
| 459 |
-
return "Error decoding the file. Please ensure it's a valid HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
| 460 |
-
|
| 461 |
-
try:
|
| 462 |
-
bookmarks = parse_bookmarks(file_content)
|
| 463 |
-
except Exception as e:
|
| 464 |
-
logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
|
| 465 |
-
return "Error parsing the bookmarks HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
| 466 |
-
|
| 467 |
-
if not bookmarks:
|
| 468 |
-
logger.warning("No bookmarks found in the uploaded file")
|
| 469 |
-
return "No bookmarks found in the uploaded file.", '', gr.update(choices=[]), display_bookmarks()
|
| 470 |
-
|
| 471 |
-
# Assign unique IDs to bookmarks
|
| 472 |
-
for idx, bookmark in enumerate(bookmarks):
|
| 473 |
-
bookmark['id'] = idx
|
| 474 |
-
|
| 475 |
-
# Fetch bookmark info concurrently
|
| 476 |
-
logger.info("Fetching URL info concurrently")
|
| 477 |
-
with ThreadPoolExecutor(max_workers=20) as executor:
|
| 478 |
-
executor.map(fetch_url_info, bookmarks)
|
| 479 |
-
|
| 480 |
-
# Process bookmarks concurrently with LLM calls
|
| 481 |
-
logger.info("Processing bookmarks with LLM concurrently")
|
| 482 |
-
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 483 |
-
executor.map(generate_summary_and_assign_category, bookmarks)
|
| 484 |
-
|
| 485 |
-
try:
|
| 486 |
-
faiss_index = vectorize_and_index(bookmarks)
|
| 487 |
-
except Exception as e:
|
| 488 |
-
logger.error(f"Error building FAISS index: {e}", exc_info=True)
|
| 489 |
-
return "Error building search index.", '', gr.update(choices=[]), display_bookmarks()
|
| 490 |
-
|
| 491 |
-
message = f"β
Successfully processed {len(bookmarks)} bookmarks."
|
| 492 |
-
logger.info(message)
|
| 493 |
-
|
| 494 |
-
# Generate displays and updates
|
| 495 |
-
bookmark_html = display_bookmarks()
|
| 496 |
-
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 497 |
-
for i, bookmark in enumerate(bookmarks)]
|
| 498 |
-
|
| 499 |
-
return message, bookmark_html, gr.update(choices=choices), bookmark_html
|
| 500 |
-
|
| 501 |
-
def delete_selected_bookmarks(selected_indices):
|
| 502 |
-
"""
|
| 503 |
-
Delete selected bookmarks and remove their vectors from the FAISS index.
|
| 504 |
-
"""
|
| 505 |
-
global bookmarks, faiss_index
|
| 506 |
-
if not selected_indices:
|
| 507 |
-
return "β οΈ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
| 508 |
-
|
| 509 |
-
ids_to_delete = []
|
| 510 |
-
indices_to_delete = []
|
| 511 |
-
for s in selected_indices:
|
| 512 |
-
idx = int(s.split('.')[0]) - 1
|
| 513 |
-
if 0 <= idx < len(bookmarks):
|
| 514 |
-
bookmark_id = bookmarks[idx]['id']
|
| 515 |
-
ids_to_delete.append(bookmark_id)
|
| 516 |
-
indices_to_delete.append(idx)
|
| 517 |
-
logger.info(f"Deleting bookmark at index {idx + 1}")
|
| 518 |
-
|
| 519 |
-
# Remove vectors from FAISS index
|
| 520 |
-
if faiss_index is not None and ids_to_delete:
|
| 521 |
-
faiss_index.remove_ids(np.array(ids_to_delete, dtype=np.int64))
|
| 522 |
-
|
| 523 |
-
# Remove bookmarks from the list (reverse order to avoid index shifting)
|
| 524 |
-
for idx in sorted(indices_to_delete, reverse=True):
|
| 525 |
-
bookmarks.pop(idx)
|
| 526 |
-
|
| 527 |
-
message = "ποΈ Selected bookmarks deleted successfully."
|
| 528 |
-
logger.info(message)
|
| 529 |
-
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 530 |
-
for i, bookmark in enumerate(bookmarks)]
|
| 531 |
-
|
| 532 |
-
return message, gr.update(choices=choices), display_bookmarks()
|
| 533 |
-
|
| 534 |
-
def edit_selected_bookmarks_category(selected_indices, new_category):
|
| 535 |
-
"""
|
| 536 |
-
Edit category of selected bookmarks.
|
| 537 |
-
"""
|
| 538 |
-
if not selected_indices:
|
| 539 |
-
return "β οΈ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
| 540 |
-
if not new_category:
|
| 541 |
-
return "β οΈ No new category selected.", gr.update(choices=[]), display_bookmarks()
|
| 542 |
-
|
| 543 |
-
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
| 544 |
-
for idx in indices:
|
| 545 |
-
if 0 <= idx < len(bookmarks):
|
| 546 |
-
bookmarks[idx]['category'] = new_category
|
| 547 |
-
logger.info(f"Updated category for bookmark {idx + 1} to {new_category}")
|
| 548 |
-
|
| 549 |
-
message = "βοΈ Category updated for selected bookmarks."
|
| 550 |
-
logger.info(message)
|
| 551 |
-
|
| 552 |
-
# Update choices and display
|
| 553 |
-
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 554 |
-
for i, bookmark in enumerate(bookmarks)]
|
| 555 |
|
| 556 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 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")
|
| 568 |
-
soup = BeautifulSoup("<!DOCTYPE NETSCAPE-Bookmark-file-1><Title>Bookmarks</Title><H1>Bookmarks</H1>", 'html.parser')
|
| 569 |
-
dl = soup.new_tag('DL')
|
| 570 |
-
for bookmark in bookmarks:
|
| 571 |
-
dt = soup.new_tag('DT')
|
| 572 |
-
a = soup.new_tag('A', href=bookmark['url'])
|
| 573 |
-
a.string = bookmark['title']
|
| 574 |
-
dt.append(a)
|
| 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 |
-
|
|
|
|
|
|
|
| 598 |
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
| 602 |
|
| 603 |
-
#
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
return chat_history
|
| 616 |
|
| 617 |
-
|
| 618 |
-
bookmarks_info = "\n".join([
|
| 619 |
-
f"Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}"
|
| 620 |
-
for bookmark in matching_bookmarks
|
| 621 |
-
])
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
A user asked: "{user_query}"
|
| 626 |
Based on the bookmarks below, provide a helpful answer to the user's query, referencing the relevant bookmarks.
|
| 627 |
Bookmarks:
|
|
@@ -629,156 +645,227 @@ Bookmarks:
|
|
| 629 |
Provide a concise and helpful response.
|
| 630 |
"""
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 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 |
-
"""
|
| 675 |
-
Build and launch the Gradio app.
|
| 676 |
-
"""
|
| 677 |
-
try:
|
| 678 |
-
logger.info("Building Gradio app")
|
| 679 |
-
with gr.Blocks(css="app.css") as demo:
|
| 680 |
-
# General Overview
|
| 681 |
-
gr.Markdown("""
|
| 682 |
-
# π SmartMarks - AI Browser Bookmarks Manager
|
| 683 |
-
Welcome to **SmartMarks**, your intelligent assistant for managing browser bookmarks. SmartMarks leverages AI to help you organize, search, and interact with your bookmarks seamlessly.
|
| 684 |
-
---
|
| 685 |
-
## π **How to Use SmartMarks**
|
| 686 |
-
SmartMarks is divided into three main sections:
|
| 687 |
-
1. **π Upload and Process Bookmarks:** Import your existing bookmarks and let SmartMarks analyze and categorize them for you.
|
| 688 |
-
2. **π¬ Chat with Bookmarks:** Interact with your bookmarks using natural language queries to find relevant links effortlessly.
|
| 689 |
-
3. **π οΈ Manage Bookmarks:** View, edit, delete, and export your bookmarks with ease.
|
| 690 |
-
""")
|
| 691 |
-
|
| 692 |
-
# Upload and Process Bookmarks Tab
|
| 693 |
-
with gr.Tab("Upload and Process Bookmarks"):
|
| 694 |
-
gr.Markdown("""
|
| 695 |
-
## π **Upload and Process Bookmarks**
|
| 696 |
-
### π **Steps:**
|
| 697 |
-
1. Click on the "Upload Bookmarks HTML File" button
|
| 698 |
-
2. Select your bookmarks file
|
| 699 |
-
3. Click "Process Bookmarks" to analyze and organize your bookmarks
|
| 700 |
-
""")
|
| 701 |
-
|
| 702 |
-
upload = gr.File(label="π Upload Bookmarks HTML File", type='binary')
|
| 703 |
-
process_button = gr.Button("βοΈ Process Bookmarks")
|
| 704 |
-
output_text = gr.Textbox(label="β
Output", interactive=False)
|
| 705 |
-
bookmark_display = gr.HTML(label="π Processed Bookmarks")
|
| 706 |
-
|
| 707 |
-
# Chat with Bookmarks Tab
|
| 708 |
-
with gr.Tab("Chat with Bookmarks"):
|
| 709 |
-
gr.Markdown("""
|
| 710 |
-
## π¬ **Chat with Bookmarks**
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 720 |
|
| 721 |
-
#
|
| 722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
gr.Markdown("""
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Import OpenAI library
|
| 20 |
import openai
|
| 21 |
|
| 22 |
+
# Suppress only the single warning from urllib3 needed.
|
| 23 |
+
import urllib3
|
| 24 |
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 25 |
+
|
| 26 |
# Set up logging to output to the console
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
logger.setLevel(logging.INFO)
|
|
|
|
| 38 |
# Add the handler to the logger
|
| 39 |
logger.addHandler(console_handler)
|
| 40 |
|
| 41 |
+
# Initialize variables and models
|
| 42 |
+
logger.info("Initializing variables and models")
|
| 43 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 44 |
faiss_index = None
|
| 45 |
bookmarks = []
|
|
|
|
| 81 |
logger.error("GROQ_API_KEY environment variable not set.")
|
| 82 |
|
| 83 |
openai.api_key = GROQ_API_KEY
|
| 84 |
+
openai.api_base = "https://api.groq.com/openai/v1" # Ensure this is the correct base URL
|
| 85 |
+
|
| 86 |
+
# Initialize semaphore for rate limiting (allowing 1 concurrent API call)
|
| 87 |
+
api_semaphore = threading.Semaphore(1)
|
| 88 |
|
| 89 |
def extract_main_content(soup):
|
| 90 |
"""
|
|
|
|
| 242 |
required_delay = total_tokens / tokens_per_second
|
| 243 |
sleep_time = max(required_delay, 1)
|
| 244 |
|
| 245 |
+
# Acquire semaphore before making API call
|
| 246 |
+
api_semaphore.acquire()
|
| 247 |
+
try:
|
| 248 |
+
# Call the LLM via Groq Cloud API
|
| 249 |
+
response = openai.ChatCompletion.create(
|
| 250 |
+
model='llama-3.1-70b-versatile', # Using the specified model
|
| 251 |
+
messages=[
|
| 252 |
+
{"role": "user", "content": prompt}
|
| 253 |
+
],
|
| 254 |
+
max_tokens=int(max_tokens),
|
| 255 |
+
temperature=0.5,
|
| 256 |
+
)
|
| 257 |
+
finally:
|
| 258 |
+
# Release semaphore after API call
|
| 259 |
+
api_semaphore.release()
|
| 260 |
+
|
| 261 |
content = response['choices'][0]['message']['content'].strip()
|
| 262 |
if not content:
|
| 263 |
raise ValueError("Empty response received from the model.")
|
|
|
|
| 295 |
except openai.error.RateLimitError as e:
|
| 296 |
retry_count += 1
|
| 297 |
wait_time = int(e.headers.get("Retry-After", 5))
|
| 298 |
+
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying... (Attempt {retry_count}/{max_retries})")
|
| 299 |
time.sleep(wait_time)
|
| 300 |
except Exception as e:
|
| 301 |
logger.error(f"Error generating summary and assigning category: {e}", exc_info=True)
|
|
|
|
| 303 |
bookmark['category'] = 'Uncategorized'
|
| 304 |
break # Exit the retry loop on other exceptions
|
| 305 |
|
| 306 |
+
def parse_bookmarks(file_content):
|
| 307 |
+
"""
|
| 308 |
+
Parse bookmarks from HTML file.
|
| 309 |
+
"""
|
| 310 |
+
logger.info("Parsing bookmarks")
|
| 311 |
+
try:
|
| 312 |
+
soup = BeautifulSoup(file_content, 'html.parser')
|
| 313 |
+
extracted_bookmarks = []
|
| 314 |
+
for link in soup.find_all('a'):
|
| 315 |
+
url = link.get('href')
|
| 316 |
+
title = link.text.strip()
|
| 317 |
+
if url and title:
|
| 318 |
+
if url.startswith('http://') or url.startswith('https://'):
|
| 319 |
+
extracted_bookmarks.append({'url': url, 'title': title})
|
| 320 |
+
else:
|
| 321 |
+
logger.info(f"Skipping non-http/https URL: {url}")
|
| 322 |
+
logger.info(f"Extracted {len(extracted_bookmarks)} bookmarks")
|
| 323 |
+
return extracted_bookmarks
|
| 324 |
+
except Exception as e:
|
| 325 |
+
logger.error("Error parsing bookmarks: %s", e, exc_info=True)
|
| 326 |
+
raise
|
| 327 |
+
|
| 328 |
+
def fetch_url_info(bookmark):
|
| 329 |
+
"""
|
| 330 |
+
Fetch information about a URL.
|
| 331 |
+
"""
|
| 332 |
+
url = bookmark['url']
|
| 333 |
+
if url in fetch_cache:
|
| 334 |
+
with lock:
|
| 335 |
+
bookmark.update(fetch_cache[url])
|
| 336 |
+
return
|
| 337 |
+
|
| 338 |
+
try:
|
| 339 |
+
logger.info(f"Fetching URL info for: {url}")
|
| 340 |
+
headers = {
|
| 341 |
+
'User-Agent': 'Mozilla/5.0',
|
| 342 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 343 |
+
}
|
| 344 |
+
response = requests.get(url, headers=headers, timeout=5, verify=False, allow_redirects=True)
|
| 345 |
+
bookmark['etag'] = response.headers.get('ETag', 'N/A')
|
| 346 |
+
bookmark['status_code'] = response.status_code
|
| 347 |
+
|
| 348 |
+
content = response.text
|
| 349 |
+
logger.info(f"Fetched content length for {url}: {len(content)} characters")
|
| 350 |
+
|
| 351 |
+
# Handle status codes
|
| 352 |
+
if response.status_code >= 500:
|
| 353 |
+
# Server error, consider as dead link
|
| 354 |
+
bookmark['dead_link'] = True
|
| 355 |
+
bookmark['description'] = ''
|
| 356 |
+
bookmark['html_content'] = ''
|
| 357 |
+
logger.warning(f"Dead link detected: {url} with status {response.status_code}")
|
| 358 |
+
else:
|
| 359 |
+
bookmark['dead_link'] = False
|
| 360 |
+
bookmark['html_content'] = content
|
| 361 |
+
bookmark['description'] = ''
|
| 362 |
+
logger.info(f"Fetched information for {url}")
|
| 363 |
+
|
| 364 |
+
except requests.exceptions.Timeout:
|
| 365 |
+
bookmark['dead_link'] = False # Mark as 'Unknown' instead of 'Dead'
|
| 366 |
+
bookmark['etag'] = 'N/A'
|
| 367 |
+
bookmark['status_code'] = 'Timeout'
|
| 368 |
bookmark['description'] = ''
|
| 369 |
bookmark['html_content'] = ''
|
| 370 |
+
bookmark['slow_link'] = True # Custom flag to indicate slow response
|
| 371 |
+
logger.warning(f"Timeout while fetching {url}. Marking as 'Slow'.")
|
| 372 |
+
except Exception as e:
|
| 373 |
+
bookmark['dead_link'] = True
|
| 374 |
+
bookmark['etag'] = 'N/A'
|
| 375 |
+
bookmark['status_code'] = 'Error'
|
| 376 |
bookmark['description'] = ''
|
| 377 |
+
bookmark['html_content'] = ''
|
| 378 |
+
logger.error(f"Error fetching URL info for {url}: {e}", exc_info=True)
|
| 379 |
+
finally:
|
| 380 |
+
with lock:
|
| 381 |
+
fetch_cache[url] = {
|
| 382 |
+
'etag': bookmark.get('etag'),
|
| 383 |
+
'status_code': bookmark.get('status_code'),
|
| 384 |
+
'dead_link': bookmark.get('dead_link'),
|
| 385 |
+
'description': bookmark.get('description'),
|
| 386 |
+
'html_content': bookmark.get('html_content', ''),
|
| 387 |
+
'slow_link': bookmark.get('slow_link', False),
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
def vectorize_and_index(bookmarks_list):
|
| 391 |
+
"""
|
| 392 |
+
Create vector embeddings for bookmarks and build FAISS index with ID mapping.
|
| 393 |
+
"""
|
| 394 |
+
global faiss_index
|
| 395 |
+
logger.info("Vectorizing summaries and building FAISS index")
|
| 396 |
+
try:
|
| 397 |
+
summaries = [bookmark['summary'] for bookmark in bookmarks_list]
|
| 398 |
+
embeddings = embedding_model.encode(summaries)
|
| 399 |
+
dimension = embeddings.shape[1]
|
| 400 |
+
index = faiss.IndexIDMap(faiss.IndexFlatL2(dimension))
|
| 401 |
+
# Assign unique IDs to each bookmark
|
| 402 |
+
ids = np.array([bookmark['id'] for bookmark in bookmarks_list], dtype=np.int64)
|
| 403 |
+
index.add_with_ids(np.array(embeddings).astype('float32'), ids)
|
| 404 |
+
faiss_index = index
|
| 405 |
+
logger.info("FAISS index built successfully with IDs")
|
| 406 |
+
return index
|
| 407 |
+
except Exception as e:
|
| 408 |
+
logger.error(f"Error in vectorizing and indexing: {e}", exc_info=True)
|
| 409 |
+
raise
|
| 410 |
+
|
| 411 |
+
def display_bookmarks():
|
| 412 |
+
"""
|
| 413 |
+
Generate HTML display for bookmarks.
|
| 414 |
+
"""
|
| 415 |
+
logger.info("Generating HTML display for bookmarks")
|
| 416 |
+
cards = ''
|
| 417 |
+
for i, bookmark in enumerate(bookmarks):
|
| 418 |
+
index = i + 1
|
| 419 |
+
if bookmark.get('dead_link'):
|
| 420 |
+
status = "β Dead Link"
|
| 421 |
+
card_style = "border: 2px solid red;"
|
| 422 |
+
text_style = "color: white;" # Set font color to white
|
| 423 |
+
elif bookmark.get('slow_link'):
|
| 424 |
+
status = "β³ Slow Response"
|
| 425 |
+
card_style = "border: 2px solid orange;"
|
| 426 |
+
text_style = "color: white;" # Set font color to white
|
| 427 |
+
else:
|
| 428 |
+
status = "β
Active"
|
| 429 |
+
card_style = "border: 2px solid green;"
|
| 430 |
+
text_style = "color: white;" # Set font color to white
|
| 431 |
+
|
| 432 |
+
title = bookmark['title']
|
| 433 |
+
url = bookmark['url']
|
| 434 |
+
etag = bookmark.get('etag', 'N/A')
|
| 435 |
+
summary = bookmark.get('summary', '')
|
| 436 |
+
category = bookmark.get('category', 'Uncategorized')
|
| 437 |
+
|
| 438 |
+
# Escape HTML content to prevent XSS attacks
|
| 439 |
+
from html import escape
|
| 440 |
+
title = escape(title)
|
| 441 |
+
url = escape(url)
|
| 442 |
+
summary = escape(summary)
|
| 443 |
+
category = escape(category)
|
| 444 |
+
|
| 445 |
+
card_html = f'''
|
| 446 |
+
<div class="card" style="{card_style} padding: 10px; margin: 10px; border-radius: 5px; background-color: #1e1e1e;">
|
| 447 |
+
<div class="card-content">
|
| 448 |
+
<h3 style="{text_style}">{index}. {title} {status}</h3>
|
| 449 |
+
<p style="{text_style}"><strong>Category:</strong> {category}</p>
|
| 450 |
+
<p style="{text_style}"><strong>URL:</strong> <a href="{url}" target="_blank" style="{text_style}">{url}</a></p>
|
| 451 |
+
<p style="{text_style}"><strong>ETag:</strong> {etag}</p>
|
| 452 |
+
<p style="{text_style}"><strong>Summary:</strong> {summary}</p>
|
| 453 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
</div>
|
| 455 |
+
'''
|
| 456 |
+
cards += card_html
|
| 457 |
+
logger.info("HTML display generated")
|
| 458 |
+
return cards
|
| 459 |
+
|
| 460 |
+
def process_uploaded_file(file):
|
| 461 |
+
"""
|
| 462 |
+
Process the uploaded bookmarks file.
|
| 463 |
+
"""
|
| 464 |
+
global bookmarks, faiss_index
|
| 465 |
+
logger.info("Processing uploaded file")
|
| 466 |
+
|
| 467 |
+
if file is None:
|
| 468 |
+
logger.warning("No file uploaded")
|
| 469 |
+
return "Please upload a bookmarks HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
| 470 |
|
| 471 |
+
try:
|
| 472 |
+
file_content = file.decode('utf-8')
|
| 473 |
+
except UnicodeDecodeError as e:
|
| 474 |
+
logger.error(f"Error decoding the file: {e}", exc_info=True)
|
| 475 |
+
return "Error decoding the file. Please ensure it's a valid HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
|
| 477 |
+
try:
|
| 478 |
+
bookmarks = parse_bookmarks(file_content)
|
| 479 |
+
except Exception as e:
|
| 480 |
+
logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
|
| 481 |
+
return "Error parsing the bookmarks HTML file.", '', gr.update(choices=[]), display_bookmarks()
|
| 482 |
|
| 483 |
+
if not bookmarks:
|
| 484 |
+
logger.warning("No bookmarks found in the uploaded file")
|
| 485 |
+
return "No bookmarks found in the uploaded file.", '', gr.update(choices=[]), display_bookmarks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
+
# Assign unique IDs to bookmarks
|
| 488 |
+
for idx, bookmark in enumerate(bookmarks):
|
| 489 |
+
bookmark['id'] = idx
|
| 490 |
|
| 491 |
+
# Fetch bookmark info concurrently
|
| 492 |
+
logger.info("Fetching URL info concurrently")
|
| 493 |
+
with ThreadPoolExecutor(max_workers=3) as executor: # Adjusted max_workers to 3
|
| 494 |
+
executor.map(fetch_url_info, bookmarks)
|
| 495 |
|
| 496 |
+
# Process bookmarks concurrently with LLM calls
|
| 497 |
+
logger.info("Processing bookmarks with LLM concurrently")
|
| 498 |
+
with ThreadPoolExecutor(max_workers=1) as executor: # Adjusted max_workers to 1
|
| 499 |
+
executor.map(generate_summary_and_assign_category, bookmarks)
|
| 500 |
|
| 501 |
+
try:
|
| 502 |
+
faiss_index = vectorize_and_index(bookmarks)
|
| 503 |
+
except Exception as e:
|
| 504 |
+
logger.error(f"Error building FAISS index: {e}", exc_info=True)
|
| 505 |
+
return "Error building search index.", '', gr.update(choices=[]), display_bookmarks()
|
| 506 |
+
|
| 507 |
+
message = f"β
Successfully processed {len(bookmarks)} bookmarks."
|
| 508 |
+
logger.info(message)
|
| 509 |
+
|
| 510 |
+
# Generate displays and updates
|
| 511 |
+
bookmark_html = display_bookmarks()
|
| 512 |
+
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 513 |
+
for i, bookmark in enumerate(bookmarks)]
|
| 514 |
+
|
| 515 |
+
return message, bookmark_html, gr.update(choices=choices), bookmark_html
|
| 516 |
+
|
| 517 |
+
def delete_selected_bookmarks(selected_indices):
|
| 518 |
+
"""
|
| 519 |
+
Delete selected bookmarks and remove their vectors from the FAISS index.
|
| 520 |
+
"""
|
| 521 |
+
global bookmarks, faiss_index
|
| 522 |
+
if not selected_indices:
|
| 523 |
+
return "β οΈ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
| 524 |
+
|
| 525 |
+
ids_to_delete = []
|
| 526 |
+
indices_to_delete = []
|
| 527 |
+
for s in selected_indices:
|
| 528 |
+
idx = int(s.split('.')[0]) - 1
|
| 529 |
+
if 0 <= idx < len(bookmarks):
|
| 530 |
+
bookmark_id = bookmarks[idx]['id']
|
| 531 |
+
ids_to_delete.append(bookmark_id)
|
| 532 |
+
indices_to_delete.append(idx)
|
| 533 |
+
logger.info(f"Deleting bookmark at index {idx + 1}")
|
| 534 |
+
|
| 535 |
+
# Remove vectors from FAISS index
|
| 536 |
+
if faiss_index is not None and ids_to_delete:
|
| 537 |
+
faiss_index.remove_ids(np.array(ids_to_delete, dtype=np.int64))
|
| 538 |
+
|
| 539 |
+
# Remove bookmarks from the list (reverse order to avoid index shifting)
|
| 540 |
+
for idx in sorted(indices_to_delete, reverse=True):
|
| 541 |
+
bookmarks.pop(idx)
|
| 542 |
+
|
| 543 |
+
message = "ποΈ Selected bookmarks deleted successfully."
|
| 544 |
+
logger.info(message)
|
| 545 |
+
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 546 |
+
for i, bookmark in enumerate(bookmarks)]
|
| 547 |
+
|
| 548 |
+
return message, gr.update(choices=choices), display_bookmarks()
|
| 549 |
+
|
| 550 |
+
def edit_selected_bookmarks_category(selected_indices, new_category):
|
| 551 |
+
"""
|
| 552 |
+
Edit category of selected bookmarks.
|
| 553 |
+
"""
|
| 554 |
+
if not selected_indices:
|
| 555 |
+
return "β οΈ No bookmarks selected.", gr.update(choices=[]), display_bookmarks()
|
| 556 |
+
if not new_category:
|
| 557 |
+
return "β οΈ No new category selected.", gr.update(choices=[]), display_bookmarks()
|
| 558 |
+
|
| 559 |
+
indices = [int(s.split('.')[0])-1 for s in selected_indices]
|
| 560 |
+
for idx in indices:
|
| 561 |
+
if 0 <= idx < len(bookmarks):
|
| 562 |
+
bookmarks[idx]['category'] = new_category
|
| 563 |
+
logger.info(f"Updated category for bookmark {idx + 1} to {new_category}")
|
| 564 |
+
|
| 565 |
+
message = "βοΈ Category updated for selected bookmarks."
|
| 566 |
+
logger.info(message)
|
| 567 |
+
|
| 568 |
+
# Update choices and display
|
| 569 |
+
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 570 |
+
for i, bookmark in enumerate(bookmarks)]
|
| 571 |
+
|
| 572 |
+
return message, gr.update(choices=choices), display_bookmarks()
|
| 573 |
+
|
| 574 |
+
def export_bookmarks():
|
| 575 |
+
"""
|
| 576 |
+
Export bookmarks to an HTML file.
|
| 577 |
+
"""
|
| 578 |
+
if not bookmarks:
|
| 579 |
+
logger.warning("No bookmarks to export")
|
| 580 |
+
return None # Return None instead of a message
|
| 581 |
|
| 582 |
+
try:
|
| 583 |
+
logger.info("Exporting bookmarks to HTML")
|
| 584 |
+
soup = BeautifulSoup("<!DOCTYPE NETSCAPE-Bookmark-file-1><Title>Bookmarks</Title><H1>Bookmarks</H1>", 'html.parser')
|
| 585 |
+
dl = soup.new_tag('DL')
|
| 586 |
+
for bookmark in bookmarks:
|
| 587 |
+
dt = soup.new_tag('DT')
|
| 588 |
+
a = soup.new_tag('A', href=bookmark['url'])
|
| 589 |
+
a.string = bookmark['title']
|
| 590 |
+
dt.append(a)
|
| 591 |
+
dl.append(dt)
|
| 592 |
+
soup.append(dl)
|
| 593 |
+
html_content = str(soup)
|
| 594 |
+
# Save to a temporary file
|
| 595 |
+
output_file = "exported_bookmarks.html"
|
| 596 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 597 |
+
f.write(html_content)
|
| 598 |
+
logger.info("Bookmarks exported successfully")
|
| 599 |
+
return output_file # Return the file path
|
| 600 |
+
except Exception as e:
|
| 601 |
+
logger.error(f"Error exporting bookmarks: {e}", exc_info=True)
|
| 602 |
+
return None # Return None in case of error
|
| 603 |
+
|
| 604 |
+
def chatbot_response(user_query, chat_history):
|
| 605 |
+
"""
|
| 606 |
+
Generate chatbot response using the FAISS index and embeddings, maintaining chat history.
|
| 607 |
+
"""
|
| 608 |
+
if not bookmarks or faiss_index is None:
|
| 609 |
+
logger.warning("No bookmarks available for chatbot")
|
| 610 |
+
chat_history.append({"role": "assistant", "content": "β οΈ No bookmarks available. Please upload and process your bookmarks first."})
|
| 611 |
return chat_history
|
| 612 |
|
| 613 |
+
logger.info(f"Chatbot received query: {user_query}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
+
try:
|
| 616 |
+
# Encode the user query
|
| 617 |
+
query_vector = embedding_model.encode([user_query]).astype('float32')
|
| 618 |
+
|
| 619 |
+
# Search the FAISS index
|
| 620 |
+
k = 5 # Number of results to return
|
| 621 |
+
distances, ids = faiss_index.search(query_vector, k)
|
| 622 |
+
ids = ids.flatten()
|
| 623 |
+
|
| 624 |
+
# Retrieve the bookmarks
|
| 625 |
+
id_to_bookmark = {bookmark['id']: bookmark for bookmark in bookmarks}
|
| 626 |
+
matching_bookmarks = [id_to_bookmark.get(id) for id in ids if id in id_to_bookmark]
|
| 627 |
+
|
| 628 |
+
if not matching_bookmarks:
|
| 629 |
+
answer = "No relevant bookmarks found for your query."
|
| 630 |
+
chat_history.append({"role": "assistant", "content": answer})
|
| 631 |
+
return chat_history
|
| 632 |
+
|
| 633 |
+
# Format the response
|
| 634 |
+
bookmarks_info = "\n".join([
|
| 635 |
+
f"Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}"
|
| 636 |
+
for bookmark in matching_bookmarks
|
| 637 |
+
])
|
| 638 |
+
|
| 639 |
+
# Use the LLM via Groq Cloud API to generate a response
|
| 640 |
+
prompt = f"""
|
| 641 |
A user asked: "{user_query}"
|
| 642 |
Based on the bookmarks below, provide a helpful answer to the user's query, referencing the relevant bookmarks.
|
| 643 |
Bookmarks:
|
|
|
|
| 645 |
Provide a concise and helpful response.
|
| 646 |
"""
|
| 647 |
|
| 648 |
+
# Estimate tokens
|
| 649 |
+
def estimate_tokens(text):
|
| 650 |
+
return len(text) / 4 # Approximate token estimation
|
| 651 |
+
|
| 652 |
+
prompt_tokens = estimate_tokens(prompt)
|
| 653 |
+
max_tokens = 300 # Adjust as needed
|
| 654 |
+
total_tokens = prompt_tokens + max_tokens
|
| 655 |
+
|
| 656 |
+
# Calculate required delay
|
| 657 |
+
tokens_per_minute = 60000 # Adjust based on your rate limit
|
| 658 |
+
tokens_per_second = tokens_per_minute / 60
|
| 659 |
+
required_delay = total_tokens / tokens_per_second
|
| 660 |
+
sleep_time = max(required_delay, 1)
|
| 661 |
+
|
| 662 |
+
# Acquire semaphore before making API call
|
| 663 |
+
api_semaphore.acquire()
|
| 664 |
+
try:
|
| 665 |
+
# Call the LLM via Groq Cloud API
|
| 666 |
+
response = openai.ChatCompletion.create(
|
| 667 |
+
model='llama-3.1-70b-versatile', # Using the specified model
|
| 668 |
+
messages=[
|
| 669 |
+
{"role": "user", "content": prompt}
|
| 670 |
+
],
|
| 671 |
+
max_tokens=int(max_tokens),
|
| 672 |
+
temperature=0.7,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
)
|
| 674 |
+
finally:
|
| 675 |
+
# Release semaphore after API call
|
| 676 |
+
api_semaphore.release()
|
| 677 |
+
|
| 678 |
+
answer = response['choices'][0]['message']['content'].strip()
|
| 679 |
+
logger.info("Chatbot response generated")
|
| 680 |
+
time.sleep(sleep_time)
|
| 681 |
|
| 682 |
+
# Append the interaction to chat history
|
| 683 |
+
chat_history.append({"role": "assistant", "content": answer})
|
| 684 |
+
return chat_history
|
| 685 |
+
|
| 686 |
+
except openai.error.RateLimitError as e:
|
| 687 |
+
wait_time = int(e.headers.get("Retry-After", 5))
|
| 688 |
+
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying...")
|
| 689 |
+
time.sleep(wait_time)
|
| 690 |
+
return chatbot_response(user_query, chat_history) # Retry after waiting
|
| 691 |
+
except Exception as e:
|
| 692 |
+
error_message = f"β οΈ Error processing your query: {str(e)}"
|
| 693 |
+
logger.error(error_message, exc_info=True)
|
| 694 |
+
chat_history.append({"role": "assistant", "content": error_message})
|
| 695 |
+
return chat_history
|
| 696 |
+
|
| 697 |
+
def build_app():
|
| 698 |
+
"""
|
| 699 |
+
Build and launch the Gradio app.
|
| 700 |
+
"""
|
| 701 |
+
try:
|
| 702 |
+
logger.info("Building Gradio app")
|
| 703 |
+
with gr.Blocks(css="app.css") as demo:
|
| 704 |
+
# General Overview
|
| 705 |
gr.Markdown("""
|
| 706 |
+
# π SmartMarks - AI Browser Bookmarks Manager
|
| 707 |
+
|
| 708 |
+
Welcome to **SmartMarks**, your intelligent assistant for managing browser bookmarks. SmartMarks leverages AI to help you organize, search, and interact with your bookmarks seamlessly.
|
| 709 |
+
|
| 710 |
+
---
|
| 711 |
+
|
| 712 |
+
## π **How to Use SmartMarks**
|
| 713 |
+
|
| 714 |
+
SmartMarks is divided into three main sections:
|
| 715 |
+
|
| 716 |
+
1. **π Upload and Process Bookmarks:** Import your existing bookmarks and let SmartMarks analyze and categorize them for you.
|
| 717 |
+
2. **π¬ Chat with Bookmarks:** Interact with your bookmarks using natural language queries to find relevant links effortlessly.
|
| 718 |
+
3. **π οΈ Manage Bookmarks:** View, edit, delete, and export your bookmarks with ease.
|
| 719 |
+
|
| 720 |
+
Navigate through the tabs to explore each feature in detail.
|
| 721 |
+
""")
|
| 722 |
+
|
| 723 |
+
# Upload and Process Bookmarks Tab
|
| 724 |
+
with gr.Tab("Upload and Process Bookmarks"):
|
| 725 |
+
gr.Markdown("""
|
| 726 |
+
## π **Upload and Process Bookmarks**
|
| 727 |
+
|
| 728 |
+
### π **Steps to Upload and Process:**
|
| 729 |
+
|
| 730 |
+
1. **Upload Bookmarks File:**
|
| 731 |
+
- Click on the **"π Upload Bookmarks HTML File"** button.
|
| 732 |
+
- Select your browser's exported bookmarks HTML file from your device.
|
| 733 |
+
|
| 734 |
+
2. **Process Bookmarks:**
|
| 735 |
+
- After uploading, click on the **"βοΈ Process Bookmarks"** button.
|
| 736 |
+
- SmartMarks will parse your bookmarks, fetch additional information, generate summaries, and categorize each link based on predefined categories.
|
| 737 |
+
|
| 738 |
+
3. **View Processed Bookmarks:**
|
| 739 |
+
- Once processing is complete, your bookmarks will be displayed in an organized and visually appealing format below.
|
| 740 |
+
""")
|
| 741 |
+
|
| 742 |
+
upload = gr.File(label="π Upload Bookmarks HTML File", type='binary')
|
| 743 |
+
process_button = gr.Button("βοΈ Process Bookmarks")
|
| 744 |
+
output_text = gr.Textbox(label="β
Output", interactive=False)
|
| 745 |
+
bookmark_display = gr.HTML(label="π Processed Bookmarks")
|
| 746 |
+
|
| 747 |
+
process_button.click(
|
| 748 |
+
process_uploaded_file,
|
| 749 |
+
inputs=upload,
|
| 750 |
+
outputs=[output_text, bookmark_display, gr.State(), gr.State()]
|
| 751 |
+
)
|
| 752 |
+
|
| 753 |
+
# Chat with Bookmarks Tab
|
| 754 |
+
with gr.Tab("Chat with Bookmarks"):
|
| 755 |
+
gr.Markdown("""
|
| 756 |
+
## π¬ **Chat with Bookmarks**
|
| 757 |
+
|
| 758 |
+
### π€ **How to Interact:**
|
| 759 |
+
|
| 760 |
+
1. **Enter Your Query:**
|
| 761 |
+
- In the **"βοΈ Ask about your bookmarks"** textbox, type your question or keyword related to your bookmarks. For example, "Do I have any bookmarks about GenerativeAI?"
|
| 762 |
+
|
| 763 |
+
2. **Submit Your Query:**
|
| 764 |
+
- Click the **"π¨ Send"** button to submit your query.
|
| 765 |
+
|
| 766 |
+
3. **Receive AI-Driven Responses:**
|
| 767 |
+
- SmartMarks will analyze your query and provide relevant bookmarks that match your request, making it easier to find specific links without manual searching.
|
| 768 |
+
|
| 769 |
+
4. **View Chat History:**
|
| 770 |
+
- All your queries and the corresponding AI responses are displayed in the chat history for your reference.
|
| 771 |
+
""")
|
| 772 |
+
|
| 773 |
+
chatbot = gr.Chatbot(label="π¬ Chat with SmartMarks", type='messages')
|
| 774 |
+
user_input = gr.Textbox(
|
| 775 |
+
label="βοΈ Ask about your bookmarks",
|
| 776 |
+
placeholder="e.g., Do I have any bookmarks about AI?"
|
| 777 |
+
)
|
| 778 |
+
chat_button = gr.Button("π¨ Send")
|
| 779 |
+
|
| 780 |
+
chat_button.click(
|
| 781 |
+
chatbot_response,
|
| 782 |
+
inputs=[user_input, chatbot],
|
| 783 |
+
outputs=chatbot
|
| 784 |
+
)
|
| 785 |
+
|
| 786 |
+
# Manage Bookmarks Tab
|
| 787 |
+
with gr.Tab("Manage Bookmarks"):
|
| 788 |
+
gr.Markdown("""
|
| 789 |
+
## π οΈ **Manage Bookmarks**
|
| 790 |
+
|
| 791 |
+
### ποΈ **Features:**
|
| 792 |
+
|
| 793 |
+
1. **View Bookmarks:**
|
| 794 |
+
- All your processed bookmarks are displayed here with their respective categories and summaries.
|
| 795 |
+
|
| 796 |
+
2. **Select Bookmarks:**
|
| 797 |
+
- Use the checkboxes next to each bookmark to select one, multiple, or all bookmarks you wish to manage.
|
| 798 |
+
|
| 799 |
+
3. **Delete Selected Bookmarks:**
|
| 800 |
+
- After selecting the desired bookmarks, click the **"ποΈ Delete Selected"** button to remove them from your list.
|
| 801 |
+
|
| 802 |
+
4. **Edit Categories:**
|
| 803 |
+
- Select the bookmarks you want to re-categorize.
|
| 804 |
+
- Choose a new category from the dropdown menu labeled **"π New Category"**.
|
| 805 |
+
- Click the **"βοΈ Edit Category"** button to update their categories.
|
| 806 |
+
|
| 807 |
+
5. **Export Bookmarks:**
|
| 808 |
+
- Click the **"πΎ Export"** button to download your updated bookmarks as an HTML file.
|
| 809 |
+
- This file can be uploaded back to your browser to reflect the changes made within SmartMarks.
|
| 810 |
+
|
| 811 |
+
6. **Refresh Bookmarks:**
|
| 812 |
+
- Click the **"π Refresh Bookmarks"** button to ensure the latest state is reflected in the display.
|
| 813 |
+
""")
|
| 814 |
+
|
| 815 |
+
manage_output = gr.Textbox(label="π Status", interactive=False)
|
| 816 |
+
bookmark_selector = gr.CheckboxGroup(
|
| 817 |
+
label="β
Select Bookmarks",
|
| 818 |
+
choices=[]
|
| 819 |
+
)
|
| 820 |
+
new_category = gr.Dropdown(
|
| 821 |
+
label="π New Category",
|
| 822 |
+
choices=CATEGORIES,
|
| 823 |
+
value="Uncategorized"
|
| 824 |
+
)
|
| 825 |
+
bookmark_display_manage = gr.HTML(label="π Bookmarks")
|
| 826 |
+
|
| 827 |
+
with gr.Row():
|
| 828 |
+
delete_button = gr.Button("ποΈ Delete Selected")
|
| 829 |
+
edit_category_button = gr.Button("βοΈ Edit Category")
|
| 830 |
+
export_button = gr.Button("πΎ Export")
|
| 831 |
+
refresh_button = gr.Button("π Refresh Bookmarks")
|
| 832 |
+
|
| 833 |
+
download_link = gr.File(label="π₯ Download Exported Bookmarks")
|
| 834 |
+
|
| 835 |
+
# Define button actions
|
| 836 |
+
delete_button.click(
|
| 837 |
+
delete_selected_bookmarks,
|
| 838 |
+
inputs=bookmark_selector,
|
| 839 |
+
outputs=[manage_output, bookmark_selector, bookmark_display_manage]
|
| 840 |
+
)
|
| 841 |
+
|
| 842 |
+
edit_category_button.click(
|
| 843 |
+
edit_selected_bookmarks_category,
|
| 844 |
+
inputs=[bookmark_selector, new_category],
|
| 845 |
+
outputs=[manage_output, bookmark_selector, bookmark_display_manage]
|
| 846 |
+
)
|
| 847 |
+
|
| 848 |
+
export_button.click(
|
| 849 |
+
export_bookmarks,
|
| 850 |
+
outputs=download_link
|
| 851 |
+
)
|
| 852 |
+
|
| 853 |
+
refresh_button.click(
|
| 854 |
+
lambda bookmarks: (
|
| 855 |
+
[
|
| 856 |
+
f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})" for i, bookmark in enumerate(bookmarks)
|
| 857 |
+
],
|
| 858 |
+
display_bookmarks()
|
| 859 |
+
),
|
| 860 |
+
inputs=gr.State(),
|
| 861 |
+
outputs=[bookmark_selector, bookmark_display_manage]
|
| 862 |
+
)
|
| 863 |
+
|
| 864 |
+
logger.info("Launching Gradio app")
|
| 865 |
+
demo.launch(debug=True)
|
| 866 |
+
except Exception as e:
|
| 867 |
+
logger.error(f"Error building the app: {e}", exc_info=True)
|
| 868 |
+
print(f"Error building the app: {e}")
|
| 869 |
+
|
| 870 |
+
if __name__ == "__main__":
|
| 871 |
+
build_app()
|