Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import numpy as np
|
|
| 9 |
import asyncio
|
| 10 |
import aiohttp
|
| 11 |
import re
|
|
|
|
| 12 |
|
| 13 |
# Initialize models and variables
|
| 14 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
@@ -115,6 +116,10 @@ def generate_summary(bookmark):
|
|
| 115 |
return bookmark
|
| 116 |
|
| 117 |
def assign_category(bookmark):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
summary = bookmark.get('summary', '').lower()
|
| 119 |
assigned_category = 'Uncategorized'
|
| 120 |
|
|
@@ -183,6 +188,7 @@ def display_bookmarks():
|
|
| 183 |
card_html = f'''
|
| 184 |
<div class="card" style="{card_style}">
|
| 185 |
<div class="card-content">
|
|
|
|
| 186 |
<h3 style="{text_style}">{index}. {title}</h3>
|
| 187 |
<p style="{text_style}"><strong>Category:</strong> {category}</p>
|
| 188 |
<p style="{text_style}"><strong>URL:</strong> <a href="{url}" target="_blank" style="{text_style}">{url}</a></p>
|
|
@@ -239,7 +245,7 @@ def chatbot_response(user_query):
|
|
| 239 |
response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nCategory: {bookmark.get('category', 'Uncategorized')}\nSummary: {bookmark['summary']}\n\n"
|
| 240 |
return response.strip()
|
| 241 |
|
| 242 |
-
def edit_bookmark(bookmark_idx, new_title, new_url):
|
| 243 |
global faiss_index
|
| 244 |
try:
|
| 245 |
bookmark_idx = int(bookmark_idx) - 1 # Adjust index to match list (starting at 0)
|
|
@@ -247,10 +253,10 @@ def edit_bookmark(bookmark_idx, new_title, new_url):
|
|
| 247 |
return "Invalid bookmark index.", display_bookmarks()
|
| 248 |
bookmarks[bookmark_idx]['title'] = new_title
|
| 249 |
bookmarks[bookmark_idx]['url'] = new_url
|
|
|
|
| 250 |
# Re-fetch bookmark info
|
| 251 |
asyncio.run(process_bookmarks_async([bookmarks[bookmark_idx]]))
|
| 252 |
generate_summary(bookmarks[bookmark_idx])
|
| 253 |
-
assign_category(bookmarks[bookmark_idx])
|
| 254 |
# Rebuild the FAISS index
|
| 255 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 256 |
message = "Bookmark updated successfully."
|
|
@@ -259,24 +265,43 @@ def edit_bookmark(bookmark_idx, new_title, new_url):
|
|
| 259 |
except Exception as e:
|
| 260 |
return f"Error: {str(e)}", display_bookmarks()
|
| 261 |
|
| 262 |
-
def
|
| 263 |
global faiss_index
|
| 264 |
try:
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
# Rebuild the FAISS index
|
| 270 |
if bookmarks:
|
| 271 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 272 |
else:
|
| 273 |
faiss_index = None
|
| 274 |
-
message = "
|
| 275 |
updated_html = display_bookmarks()
|
| 276 |
return message, updated_html
|
| 277 |
except Exception as e:
|
| 278 |
return f"Error: {str(e)}", display_bookmarks()
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
def build_app():
|
| 281 |
with gr.Blocks(css="app.css") as demo:
|
| 282 |
gr.Markdown("<h1>Bookmark Manager App</h1>")
|
|
@@ -313,13 +338,19 @@ def build_app():
|
|
| 313 |
bookmark_display_manage = gr.HTML(label="Bookmarks")
|
| 314 |
refresh_button = gr.Button("Refresh Bookmark List")
|
| 315 |
|
|
|
|
|
|
|
|
|
|
| 316 |
with gr.Row():
|
| 317 |
index_input = gr.Number(label="Bookmark Index (Starting from 1)", precision=0)
|
| 318 |
new_title_input = gr.Textbox(label="New Title")
|
| 319 |
new_url_input = gr.Textbox(label="New URL")
|
|
|
|
| 320 |
|
| 321 |
edit_button = gr.Button("Edit Bookmark")
|
| 322 |
-
delete_button = gr.Button("Delete
|
|
|
|
|
|
|
| 323 |
|
| 324 |
def update_manage_display():
|
| 325 |
html_content = display_bookmarks()
|
|
@@ -333,19 +364,63 @@ def build_app():
|
|
| 333 |
|
| 334 |
edit_button.click(
|
| 335 |
edit_bookmark,
|
| 336 |
-
inputs=[index_input, new_title_input, new_url_input],
|
| 337 |
outputs=[manage_output, bookmark_display_manage]
|
| 338 |
)
|
| 339 |
|
| 340 |
delete_button.click(
|
| 341 |
-
|
| 342 |
-
inputs=
|
| 343 |
outputs=[manage_output, bookmark_display_manage]
|
| 344 |
)
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
# Initial load of the bookmarks display
|
| 347 |
bookmark_display_manage.value = update_manage_display()
|
| 348 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
demo.launch()
|
| 350 |
|
| 351 |
if __name__ == "__main__":
|
|
|
|
| 9 |
import asyncio
|
| 10 |
import aiohttp
|
| 11 |
import re
|
| 12 |
+
import base64
|
| 13 |
|
| 14 |
# Initialize models and variables
|
| 15 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 116 |
return bookmark
|
| 117 |
|
| 118 |
def assign_category(bookmark):
|
| 119 |
+
if bookmark.get('dead_link'):
|
| 120 |
+
bookmark['category'] = 'Dead Link'
|
| 121 |
+
return bookmark
|
| 122 |
+
|
| 123 |
summary = bookmark.get('summary', '').lower()
|
| 124 |
assigned_category = 'Uncategorized'
|
| 125 |
|
|
|
|
| 188 |
card_html = f'''
|
| 189 |
<div class="card" style="{card_style}">
|
| 190 |
<div class="card-content">
|
| 191 |
+
<input type="checkbox" class="bookmark-checkbox" data-index="{index-1}">
|
| 192 |
<h3 style="{text_style}">{index}. {title}</h3>
|
| 193 |
<p style="{text_style}"><strong>Category:</strong> {category}</p>
|
| 194 |
<p style="{text_style}"><strong>URL:</strong> <a href="{url}" target="_blank" style="{text_style}">{url}</a></p>
|
|
|
|
| 245 |
response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nCategory: {bookmark.get('category', 'Uncategorized')}\nSummary: {bookmark['summary']}\n\n"
|
| 246 |
return response.strip()
|
| 247 |
|
| 248 |
+
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
| 249 |
global faiss_index
|
| 250 |
try:
|
| 251 |
bookmark_idx = int(bookmark_idx) - 1 # Adjust index to match list (starting at 0)
|
|
|
|
| 253 |
return "Invalid bookmark index.", display_bookmarks()
|
| 254 |
bookmarks[bookmark_idx]['title'] = new_title
|
| 255 |
bookmarks[bookmark_idx]['url'] = new_url
|
| 256 |
+
bookmarks[bookmark_idx]['category'] = new_category
|
| 257 |
# Re-fetch bookmark info
|
| 258 |
asyncio.run(process_bookmarks_async([bookmarks[bookmark_idx]]))
|
| 259 |
generate_summary(bookmarks[bookmark_idx])
|
|
|
|
| 260 |
# Rebuild the FAISS index
|
| 261 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 262 |
message = "Bookmark updated successfully."
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
return f"Error: {str(e)}", display_bookmarks()
|
| 267 |
|
| 268 |
+
def delete_bookmarks(indices):
|
| 269 |
global faiss_index
|
| 270 |
try:
|
| 271 |
+
indices = sorted([int(idx) for idx in indices], reverse=True)
|
| 272 |
+
for idx in indices:
|
| 273 |
+
if 0 <= idx < len(bookmarks):
|
| 274 |
+
bookmarks.pop(idx)
|
| 275 |
# Rebuild the FAISS index
|
| 276 |
if bookmarks:
|
| 277 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 278 |
else:
|
| 279 |
faiss_index = None
|
| 280 |
+
message = "Selected bookmarks deleted successfully."
|
| 281 |
updated_html = display_bookmarks()
|
| 282 |
return message, updated_html
|
| 283 |
except Exception as e:
|
| 284 |
return f"Error: {str(e)}", display_bookmarks()
|
| 285 |
|
| 286 |
+
def export_bookmarks():
|
| 287 |
+
if not bookmarks:
|
| 288 |
+
return None
|
| 289 |
+
# Create an HTML content similar to the imported bookmarks file
|
| 290 |
+
soup = BeautifulSoup("<!DOCTYPE NETSCAPE-Bookmark-file-1><Title>Bookmarks</Title><H1>Bookmarks</H1>", 'html.parser')
|
| 291 |
+
dl = soup.new_tag('DL')
|
| 292 |
+
for bookmark in bookmarks:
|
| 293 |
+
dt = soup.new_tag('DT')
|
| 294 |
+
a = soup.new_tag('A', href=bookmark['url'])
|
| 295 |
+
a.string = bookmark['title']
|
| 296 |
+
dt.append(a)
|
| 297 |
+
dl.append(dt)
|
| 298 |
+
soup.append(dl)
|
| 299 |
+
html_content = str(soup)
|
| 300 |
+
# Encode the HTML content to base64 for download
|
| 301 |
+
b64 = base64.b64encode(html_content.encode()).decode()
|
| 302 |
+
href = f'data:text/html;base64,{b64}'
|
| 303 |
+
return href
|
| 304 |
+
|
| 305 |
def build_app():
|
| 306 |
with gr.Blocks(css="app.css") as demo:
|
| 307 |
gr.Markdown("<h1>Bookmark Manager App</h1>")
|
|
|
|
| 338 |
bookmark_display_manage = gr.HTML(label="Bookmarks")
|
| 339 |
refresh_button = gr.Button("Refresh Bookmark List")
|
| 340 |
|
| 341 |
+
select_all_checkbox = gr.Checkbox(label="Select All")
|
| 342 |
+
selected_indices = gr.Textbox(label="Selected Indices (comma-separated)", visible=False)
|
| 343 |
+
|
| 344 |
with gr.Row():
|
| 345 |
index_input = gr.Number(label="Bookmark Index (Starting from 1)", precision=0)
|
| 346 |
new_title_input = gr.Textbox(label="New Title")
|
| 347 |
new_url_input = gr.Textbox(label="New URL")
|
| 348 |
+
new_category_input = gr.Dropdown(label="New Category", choices=CATEGORIES)
|
| 349 |
|
| 350 |
edit_button = gr.Button("Edit Bookmark")
|
| 351 |
+
delete_button = gr.Button("Delete Selected Bookmarks")
|
| 352 |
+
export_button = gr.Button("Export Bookmarks")
|
| 353 |
+
download_link = gr.HTML(label="Download Exported Bookmarks")
|
| 354 |
|
| 355 |
def update_manage_display():
|
| 356 |
html_content = display_bookmarks()
|
|
|
|
| 364 |
|
| 365 |
edit_button.click(
|
| 366 |
edit_bookmark,
|
| 367 |
+
inputs=[index_input, new_title_input, new_url_input, new_category_input],
|
| 368 |
outputs=[manage_output, bookmark_display_manage]
|
| 369 |
)
|
| 370 |
|
| 371 |
delete_button.click(
|
| 372 |
+
delete_bookmarks,
|
| 373 |
+
inputs=selected_indices,
|
| 374 |
outputs=[manage_output, bookmark_display_manage]
|
| 375 |
)
|
| 376 |
|
| 377 |
+
def provide_download_link():
|
| 378 |
+
href = export_bookmarks()
|
| 379 |
+
if href:
|
| 380 |
+
return f'<a href="{href}" download="bookmarks.html">Download Exported Bookmarks</a>'
|
| 381 |
+
else:
|
| 382 |
+
return "No bookmarks to export."
|
| 383 |
+
|
| 384 |
+
export_button.click(
|
| 385 |
+
provide_download_link,
|
| 386 |
+
inputs=None,
|
| 387 |
+
outputs=download_link
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
# Initial load of the bookmarks display
|
| 391 |
bookmark_display_manage.value = update_manage_display()
|
| 392 |
|
| 393 |
+
# Include JavaScript to handle checkbox selection and Select All functionality
|
| 394 |
+
demo.load(None, None, None, _js="""
|
| 395 |
+
function() {
|
| 396 |
+
// Handle Select All checkbox
|
| 397 |
+
document.querySelector('input[type="checkbox"][label="Select All"]').addEventListener('change', function() {
|
| 398 |
+
var checkboxes = document.querySelectorAll('.bookmark-checkbox');
|
| 399 |
+
for (var i = 0; i < checkboxes.length; i++) {
|
| 400 |
+
checkboxes[i].checked = this.checked;
|
| 401 |
+
}
|
| 402 |
+
});
|
| 403 |
+
|
| 404 |
+
// Update selected indices
|
| 405 |
+
function updateSelectedIndices() {
|
| 406 |
+
var checkboxes = document.querySelectorAll('.bookmark-checkbox');
|
| 407 |
+
var indices = [];
|
| 408 |
+
for (var i = 0; i < checkboxes.length; i++) {
|
| 409 |
+
if (checkboxes[i].checked) {
|
| 410 |
+
indices.push(checkboxes[i].getAttribute('data-index'));
|
| 411 |
+
}
|
| 412 |
+
}
|
| 413 |
+
document.querySelector('textarea[label="Selected Indices (comma-separated)"]').value = indices.join(',');
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
document.addEventListener('change', function(e) {
|
| 417 |
+
if (e.target && e.target.classList.contains('bookmark-checkbox')) {
|
| 418 |
+
updateSelectedIndices();
|
| 419 |
+
}
|
| 420 |
+
});
|
| 421 |
+
}
|
| 422 |
+
""")
|
| 423 |
+
|
| 424 |
demo.launch()
|
| 425 |
|
| 426 |
if __name__ == "__main__":
|