Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import numpy as np
|
|
| 9 |
import asyncio
|
| 10 |
import aiohttp
|
| 11 |
import re
|
| 12 |
-
import
|
| 13 |
|
| 14 |
# Initialize models and variables
|
| 15 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
@@ -168,35 +168,53 @@ def vectorize_and_index(bookmarks):
|
|
| 168 |
faiss_idx.add(np.array(embeddings))
|
| 169 |
return faiss_idx, embeddings
|
| 170 |
|
| 171 |
-
def
|
| 172 |
-
|
| 173 |
for i, bookmark in enumerate(bookmarks):
|
| 174 |
-
index = i + 1
|
| 175 |
status = "Dead Link" if bookmark.get('dead_link') else "Active"
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def process_uploaded_file(file):
|
| 188 |
global bookmarks, faiss_index
|
| 189 |
if file is None:
|
| 190 |
-
return "Please upload a bookmarks HTML file.",
|
| 191 |
try:
|
| 192 |
file_content = file.decode('utf-8')
|
| 193 |
except UnicodeDecodeError:
|
| 194 |
-
return "Error decoding the file. Please ensure it's a valid HTML file.",
|
| 195 |
|
| 196 |
bookmarks = parse_bookmarks(file_content)
|
| 197 |
|
| 198 |
if not bookmarks:
|
| 199 |
-
return "No bookmarks found in the uploaded file.",
|
| 200 |
|
| 201 |
# Asynchronously fetch bookmark info
|
| 202 |
asyncio.run(process_bookmarks_async(bookmarks))
|
|
@@ -208,8 +226,8 @@ def process_uploaded_file(file):
|
|
| 208 |
|
| 209 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 210 |
message = f"Successfully processed {len(bookmarks)} bookmarks."
|
| 211 |
-
|
| 212 |
-
return message,
|
| 213 |
|
| 214 |
def chatbot_response(user_query):
|
| 215 |
if faiss_index is None or not bookmarks:
|
|
@@ -228,30 +246,31 @@ def chatbot_response(user_query):
|
|
| 228 |
response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nCategory: {bookmark.get('category', 'Uncategorized')}\nSummary: {bookmark['summary']}\n\n"
|
| 229 |
return response.strip()
|
| 230 |
|
| 231 |
-
def edit_bookmark(
|
| 232 |
global faiss_index
|
| 233 |
try:
|
| 234 |
-
bookmark_idx = int(
|
| 235 |
if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
|
| 236 |
-
return "Invalid bookmark index.",
|
| 237 |
-
bookmarks[bookmark_idx]['title'] =
|
| 238 |
-
bookmarks[bookmark_idx]['url'] =
|
| 239 |
-
bookmarks[bookmark_idx]['category'] =
|
| 240 |
# Re-fetch bookmark info
|
| 241 |
asyncio.run(process_bookmarks_async([bookmarks[bookmark_idx]]))
|
| 242 |
generate_summary(bookmarks[bookmark_idx])
|
| 243 |
# Rebuild the FAISS index
|
| 244 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 245 |
message = "Bookmark updated successfully."
|
| 246 |
-
|
| 247 |
-
return message,
|
| 248 |
except Exception as e:
|
| 249 |
-
return f"Error: {str(e)}",
|
| 250 |
|
| 251 |
-
def delete_bookmarks(
|
| 252 |
global faiss_index
|
| 253 |
try:
|
| 254 |
-
indices =
|
|
|
|
| 255 |
for idx in indices:
|
| 256 |
if 0 <= idx < len(bookmarks):
|
| 257 |
bookmarks.pop(idx)
|
|
@@ -261,10 +280,10 @@ def delete_bookmarks(selected_indices):
|
|
| 261 |
else:
|
| 262 |
faiss_index = None
|
| 263 |
message = "Selected bookmarks deleted successfully."
|
| 264 |
-
|
| 265 |
-
return message,
|
| 266 |
except Exception as e:
|
| 267 |
-
return f"Error: {str(e)}",
|
| 268 |
|
| 269 |
def export_bookmarks():
|
| 270 |
if not bookmarks:
|
|
@@ -280,7 +299,10 @@ def export_bookmarks():
|
|
| 280 |
dl.append(dt)
|
| 281 |
soup.append(dl)
|
| 282 |
html_content = str(soup)
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
def build_app():
|
| 286 |
with gr.Blocks(css="app.css") as demo:
|
|
@@ -290,16 +312,16 @@ def build_app():
|
|
| 290 |
upload = gr.File(label="Upload Bookmarks HTML File", type='binary')
|
| 291 |
process_button = gr.Button("Process Bookmarks")
|
| 292 |
output_text = gr.Textbox(label="Output")
|
| 293 |
-
|
| 294 |
|
| 295 |
-
def
|
| 296 |
-
message,
|
| 297 |
-
return message,
|
| 298 |
|
| 299 |
process_button.click(
|
| 300 |
-
|
| 301 |
inputs=upload,
|
| 302 |
-
outputs=[output_text,
|
| 303 |
)
|
| 304 |
|
| 305 |
with gr.Tab("Chat with Bookmarks"):
|
|
@@ -315,50 +337,59 @@ def build_app():
|
|
| 315 |
|
| 316 |
with gr.Tab("Manage Bookmarks"):
|
| 317 |
manage_output = gr.Textbox(label="Manage Output")
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
| 320 |
delete_button = gr.Button("Delete Selected Bookmarks")
|
| 321 |
export_button = gr.Button("Export Bookmarks")
|
| 322 |
-
download_link = gr.
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
if content:
|
| 336 |
-
with open('bookmarks.html', 'w', encoding='utf-8') as f:
|
| 337 |
-
f.write(content)
|
| 338 |
-
return 'bookmarks.html'
|
| 339 |
-
else:
|
| 340 |
-
return None
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
edit_bookmark,
|
| 344 |
-
inputs=
|
| 345 |
-
outputs=[manage_output,
|
| 346 |
)
|
| 347 |
|
| 348 |
delete_button.click(
|
| 349 |
-
|
| 350 |
-
inputs=
|
| 351 |
-
outputs=[manage_output,
|
| 352 |
)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
export_button.click(
|
| 355 |
-
|
| 356 |
inputs=None,
|
| 357 |
outputs=download_link
|
| 358 |
)
|
| 359 |
|
| 360 |
-
# Initial load of the bookmarks
|
| 361 |
-
|
| 362 |
|
| 363 |
demo.launch()
|
| 364 |
|
|
|
|
| 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')
|
|
|
|
| 168 |
faiss_idx.add(np.array(embeddings))
|
| 169 |
return faiss_idx, embeddings
|
| 170 |
|
| 171 |
+
def display_bookmarks():
|
| 172 |
+
cards = ''
|
| 173 |
for i, bookmark in enumerate(bookmarks):
|
| 174 |
+
index = i + 1 # Start index at 1
|
| 175 |
status = "Dead Link" if bookmark.get('dead_link') else "Active"
|
| 176 |
+
title = bookmark['title']
|
| 177 |
+
url = bookmark['url']
|
| 178 |
+
etag = bookmark.get('etag', 'N/A')
|
| 179 |
+
summary = bookmark.get('summary', '')
|
| 180 |
+
category = bookmark.get('category', 'Uncategorized')
|
| 181 |
+
|
| 182 |
+
# Apply inline styles for dead links
|
| 183 |
+
if bookmark.get('dead_link'):
|
| 184 |
+
card_style = "border: 2px solid #D32F2F;"
|
| 185 |
+
text_style = "color: #D32F2F;"
|
| 186 |
+
else:
|
| 187 |
+
card_style = ""
|
| 188 |
+
text_style = ""
|
| 189 |
+
|
| 190 |
+
card_html = f'''
|
| 191 |
+
<div class="card" style="{card_style}">
|
| 192 |
+
<div class="card-content">
|
| 193 |
+
<h3 style="{text_style}">{index}. {title}</h3>
|
| 194 |
+
<p style="{text_style}"><strong>Category:</strong> {category}</p>
|
| 195 |
+
<p style="{text_style}"><strong>URL:</strong> <a href="{url}" target="_blank" style="{text_style}">{url}</a></p>
|
| 196 |
+
<p style="{text_style}"><strong>Status:</strong> {status}</p>
|
| 197 |
+
<p style="{text_style}"><strong>ETag:</strong> {etag}</p>
|
| 198 |
+
<p style="{text_style}"><strong>Summary:</strong> {summary}</p>
|
| 199 |
+
</div>
|
| 200 |
+
</div>
|
| 201 |
+
'''
|
| 202 |
+
cards += card_html
|
| 203 |
+
return cards
|
| 204 |
|
| 205 |
def process_uploaded_file(file):
|
| 206 |
global bookmarks, faiss_index
|
| 207 |
if file is None:
|
| 208 |
+
return "Please upload a bookmarks HTML file.", ''
|
| 209 |
try:
|
| 210 |
file_content = file.decode('utf-8')
|
| 211 |
except UnicodeDecodeError:
|
| 212 |
+
return "Error decoding the file. Please ensure it's a valid HTML file.", ''
|
| 213 |
|
| 214 |
bookmarks = parse_bookmarks(file_content)
|
| 215 |
|
| 216 |
if not bookmarks:
|
| 217 |
+
return "No bookmarks found in the uploaded file.", ''
|
| 218 |
|
| 219 |
# Asynchronously fetch bookmark info
|
| 220 |
asyncio.run(process_bookmarks_async(bookmarks))
|
|
|
|
| 226 |
|
| 227 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 228 |
message = f"Successfully processed {len(bookmarks)} bookmarks."
|
| 229 |
+
bookmark_html = display_bookmarks()
|
| 230 |
+
return message, bookmark_html
|
| 231 |
|
| 232 |
def chatbot_response(user_query):
|
| 233 |
if faiss_index is None or not bookmarks:
|
|
|
|
| 246 |
response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nCategory: {bookmark.get('category', 'Uncategorized')}\nSummary: {bookmark['summary']}\n\n"
|
| 247 |
return response.strip()
|
| 248 |
|
| 249 |
+
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
| 250 |
global faiss_index
|
| 251 |
try:
|
| 252 |
+
bookmark_idx = int(bookmark_idx) - 1 # Adjust index to match list (starting at 0)
|
| 253 |
if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
|
| 254 |
+
return "Invalid bookmark index.", display_bookmarks()
|
| 255 |
+
bookmarks[bookmark_idx]['title'] = new_title
|
| 256 |
+
bookmarks[bookmark_idx]['url'] = new_url
|
| 257 |
+
bookmarks[bookmark_idx]['category'] = new_category
|
| 258 |
# Re-fetch bookmark info
|
| 259 |
asyncio.run(process_bookmarks_async([bookmarks[bookmark_idx]]))
|
| 260 |
generate_summary(bookmarks[bookmark_idx])
|
| 261 |
# Rebuild the FAISS index
|
| 262 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 263 |
message = "Bookmark updated successfully."
|
| 264 |
+
updated_html = display_bookmarks()
|
| 265 |
+
return message, updated_html
|
| 266 |
except Exception as e:
|
| 267 |
+
return f"Error: {str(e)}", display_bookmarks()
|
| 268 |
|
| 269 |
+
def delete_bookmarks(indices_str):
|
| 270 |
global faiss_index
|
| 271 |
try:
|
| 272 |
+
indices = [int(idx.strip()) - 1 for idx in indices_str.split(',') if idx.strip().isdigit()]
|
| 273 |
+
indices = sorted(indices, reverse=True)
|
| 274 |
for idx in indices:
|
| 275 |
if 0 <= idx < len(bookmarks):
|
| 276 |
bookmarks.pop(idx)
|
|
|
|
| 280 |
else:
|
| 281 |
faiss_index = None
|
| 282 |
message = "Selected bookmarks deleted successfully."
|
| 283 |
+
updated_html = display_bookmarks()
|
| 284 |
+
return message, updated_html
|
| 285 |
except Exception as e:
|
| 286 |
+
return f"Error: {str(e)}", display_bookmarks()
|
| 287 |
|
| 288 |
def export_bookmarks():
|
| 289 |
if not bookmarks:
|
|
|
|
| 299 |
dl.append(dt)
|
| 300 |
soup.append(dl)
|
| 301 |
html_content = str(soup)
|
| 302 |
+
# Encode the HTML content to base64 for download
|
| 303 |
+
b64 = base64.b64encode(html_content.encode()).decode()
|
| 304 |
+
href = f'data:text/html;base64,{b64}'
|
| 305 |
+
return href
|
| 306 |
|
| 307 |
def build_app():
|
| 308 |
with gr.Blocks(css="app.css") as demo:
|
|
|
|
| 312 |
upload = gr.File(label="Upload Bookmarks HTML File", type='binary')
|
| 313 |
process_button = gr.Button("Process Bookmarks")
|
| 314 |
output_text = gr.Textbox(label="Output")
|
| 315 |
+
bookmark_display = gr.HTML(label="Bookmarks")
|
| 316 |
|
| 317 |
+
def update_bookmark_display(file):
|
| 318 |
+
message, html_content = process_uploaded_file(file)
|
| 319 |
+
return message, html_content
|
| 320 |
|
| 321 |
process_button.click(
|
| 322 |
+
update_bookmark_display,
|
| 323 |
inputs=upload,
|
| 324 |
+
outputs=[output_text, bookmark_display]
|
| 325 |
)
|
| 326 |
|
| 327 |
with gr.Tab("Chat with Bookmarks"):
|
|
|
|
| 337 |
|
| 338 |
with gr.Tab("Manage Bookmarks"):
|
| 339 |
manage_output = gr.Textbox(label="Manage Output")
|
| 340 |
+
bookmark_display_manage = gr.HTML(label="Bookmarks")
|
| 341 |
+
refresh_button = gr.Button("Refresh Bookmark List")
|
| 342 |
+
|
| 343 |
+
indices_input = gr.Textbox(label="Bookmark Indices to Delete (comma-separated)")
|
| 344 |
delete_button = gr.Button("Delete Selected Bookmarks")
|
| 345 |
export_button = gr.Button("Export Bookmarks")
|
| 346 |
+
download_link = gr.HTML(label="Download Exported Bookmarks")
|
| 347 |
+
|
| 348 |
+
with gr.Row():
|
| 349 |
+
index_input = gr.Number(label="Bookmark Index (Starting from 1)", precision=0)
|
| 350 |
+
new_title_input = gr.Textbox(label="New Title")
|
| 351 |
+
new_url_input = gr.Textbox(label="New URL")
|
| 352 |
+
new_category_input = gr.Dropdown(label="New Category", choices=CATEGORIES)
|
| 353 |
+
|
| 354 |
+
edit_button = gr.Button("Edit Bookmark")
|
| 355 |
+
|
| 356 |
+
def update_manage_display():
|
| 357 |
+
html_content = display_bookmarks()
|
| 358 |
+
return html_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
+
refresh_button.click(
|
| 361 |
+
update_manage_display,
|
| 362 |
+
inputs=None,
|
| 363 |
+
outputs=bookmark_display_manage
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
edit_button.click(
|
| 367 |
edit_bookmark,
|
| 368 |
+
inputs=[index_input, new_title_input, new_url_input, new_category_input],
|
| 369 |
+
outputs=[manage_output, bookmark_display_manage]
|
| 370 |
)
|
| 371 |
|
| 372 |
delete_button.click(
|
| 373 |
+
delete_bookmarks,
|
| 374 |
+
inputs=indices_input,
|
| 375 |
+
outputs=[manage_output, bookmark_display_manage]
|
| 376 |
)
|
| 377 |
|
| 378 |
+
def provide_download_link():
|
| 379 |
+
href = export_bookmarks()
|
| 380 |
+
if href:
|
| 381 |
+
return f'<a href="{href}" download="bookmarks.html">Download Exported Bookmarks</a>'
|
| 382 |
+
else:
|
| 383 |
+
return "No bookmarks to export."
|
| 384 |
+
|
| 385 |
export_button.click(
|
| 386 |
+
provide_download_link,
|
| 387 |
inputs=None,
|
| 388 |
outputs=download_link
|
| 389 |
)
|
| 390 |
|
| 391 |
+
# Initial load of the bookmarks display
|
| 392 |
+
bookmark_display_manage.value = update_manage_display()
|
| 393 |
|
| 394 |
demo.launch()
|
| 395 |
|