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')
|
|
@@ -39,6 +39,8 @@ CATEGORIES = [
|
|
| 39 |
"Music and Audio",
|
| 40 |
"Videos and Movies",
|
| 41 |
"Reference and Knowledge Bases",
|
|
|
|
|
|
|
| 42 |
]
|
| 43 |
|
| 44 |
def parse_bookmarks(file_content):
|
|
@@ -166,54 +168,35 @@ def vectorize_and_index(bookmarks):
|
|
| 166 |
faiss_idx.add(np.array(embeddings))
|
| 167 |
return faiss_idx, embeddings
|
| 168 |
|
| 169 |
-
def
|
| 170 |
-
|
| 171 |
for i, bookmark in enumerate(bookmarks):
|
| 172 |
-
index = i + 1
|
| 173 |
status = "Dead Link" if bookmark.get('dead_link') else "Active"
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
else:
|
| 185 |
-
card_style = ""
|
| 186 |
-
text_style = ""
|
| 187 |
-
|
| 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>
|
| 195 |
-
<p style="{text_style}"><strong>Status:</strong> {status}</p>
|
| 196 |
-
<p style="{text_style}"><strong>ETag:</strong> {etag}</p>
|
| 197 |
-
<p style="{text_style}"><strong>Summary:</strong> {summary}</p>
|
| 198 |
-
</div>
|
| 199 |
-
</div>
|
| 200 |
-
'''
|
| 201 |
-
cards += card_html
|
| 202 |
-
return cards
|
| 203 |
|
| 204 |
def process_uploaded_file(file):
|
| 205 |
global bookmarks, faiss_index
|
| 206 |
if file is None:
|
| 207 |
-
return "Please upload a bookmarks HTML file.",
|
| 208 |
try:
|
| 209 |
file_content = file.decode('utf-8')
|
| 210 |
except UnicodeDecodeError:
|
| 211 |
-
return "Error decoding the file. Please ensure it's a valid HTML file.",
|
| 212 |
|
| 213 |
bookmarks = parse_bookmarks(file_content)
|
| 214 |
|
| 215 |
if not bookmarks:
|
| 216 |
-
return "No bookmarks found in the uploaded file.",
|
| 217 |
|
| 218 |
# Asynchronously fetch bookmark info
|
| 219 |
asyncio.run(process_bookmarks_async(bookmarks))
|
|
@@ -225,8 +208,8 @@ def process_uploaded_file(file):
|
|
| 225 |
|
| 226 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 227 |
message = f"Successfully processed {len(bookmarks)} bookmarks."
|
| 228 |
-
|
| 229 |
-
return message,
|
| 230 |
|
| 231 |
def chatbot_response(user_query):
|
| 232 |
if faiss_index is None or not bookmarks:
|
|
@@ -241,34 +224,34 @@ def chatbot_response(user_query):
|
|
| 241 |
for idx in I[0]:
|
| 242 |
if idx < len(bookmarks):
|
| 243 |
bookmark = bookmarks[idx]
|
| 244 |
-
index =
|
| 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(
|
| 249 |
global faiss_index
|
| 250 |
try:
|
| 251 |
-
bookmark_idx = int(
|
| 252 |
if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
|
| 253 |
-
return "Invalid bookmark index.",
|
| 254 |
-
bookmarks[bookmark_idx]['title'] =
|
| 255 |
-
bookmarks[bookmark_idx]['url'] =
|
| 256 |
-
bookmarks[bookmark_idx]['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."
|
| 263 |
-
|
| 264 |
-
return message,
|
| 265 |
except Exception as e:
|
| 266 |
-
return f"Error: {str(e)}",
|
| 267 |
|
| 268 |
-
def delete_bookmarks(
|
| 269 |
global faiss_index
|
| 270 |
try:
|
| 271 |
-
indices = sorted([int(idx) for idx in
|
| 272 |
for idx in indices:
|
| 273 |
if 0 <= idx < len(bookmarks):
|
| 274 |
bookmarks.pop(idx)
|
|
@@ -278,10 +261,10 @@ def delete_bookmarks(indices):
|
|
| 278 |
else:
|
| 279 |
faiss_index = None
|
| 280 |
message = "Selected bookmarks deleted successfully."
|
| 281 |
-
|
| 282 |
-
return message,
|
| 283 |
except Exception as e:
|
| 284 |
-
return f"Error: {str(e)}",
|
| 285 |
|
| 286 |
def export_bookmarks():
|
| 287 |
if not bookmarks:
|
|
@@ -297,10 +280,7 @@ def export_bookmarks():
|
|
| 297 |
dl.append(dt)
|
| 298 |
soup.append(dl)
|
| 299 |
html_content = str(soup)
|
| 300 |
-
|
| 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:
|
|
@@ -310,16 +290,16 @@ def build_app():
|
|
| 310 |
upload = gr.File(label="Upload Bookmarks HTML File", type='binary')
|
| 311 |
process_button = gr.Button("Process Bookmarks")
|
| 312 |
output_text = gr.Textbox(label="Output")
|
| 313 |
-
|
| 314 |
|
| 315 |
-
def
|
| 316 |
-
message,
|
| 317 |
-
return message,
|
| 318 |
|
| 319 |
process_button.click(
|
| 320 |
-
|
| 321 |
inputs=upload,
|
| 322 |
-
outputs=[output_text,
|
| 323 |
)
|
| 324 |
|
| 325 |
with gr.Tab("Chat with Bookmarks"):
|
|
@@ -335,91 +315,50 @@ def build_app():
|
|
| 335 |
|
| 336 |
with gr.Tab("Manage Bookmarks"):
|
| 337 |
manage_output = gr.Textbox(label="Manage Output")
|
| 338 |
-
|
| 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.
|
| 354 |
-
|
| 355 |
-
def
|
| 356 |
-
|
| 357 |
-
return
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
-
|
| 366 |
edit_bookmark,
|
| 367 |
-
inputs=
|
| 368 |
-
outputs=[manage_output,
|
| 369 |
)
|
| 370 |
|
| 371 |
delete_button.click(
|
| 372 |
-
|
| 373 |
-
inputs=
|
| 374 |
-
outputs=[manage_output,
|
| 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 |
-
|
| 386 |
inputs=None,
|
| 387 |
outputs=download_link
|
| 388 |
)
|
| 389 |
|
| 390 |
-
# Initial load of the bookmarks
|
| 391 |
-
|
| 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 |
|
|
|
|
| 9 |
import asyncio
|
| 10 |
import aiohttp
|
| 11 |
import re
|
| 12 |
+
import pandas as pd
|
| 13 |
|
| 14 |
# Initialize models and variables
|
| 15 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 39 |
"Music and Audio",
|
| 40 |
"Videos and Movies",
|
| 41 |
"Reference and Knowledge Bases",
|
| 42 |
+
"Dead Link",
|
| 43 |
+
"Uncategorized",
|
| 44 |
]
|
| 45 |
|
| 46 |
def parse_bookmarks(file_content):
|
|
|
|
| 168 |
faiss_idx.add(np.array(embeddings))
|
| 169 |
return faiss_idx, embeddings
|
| 170 |
|
| 171 |
+
def bookmarks_to_dataframe():
|
| 172 |
+
data = []
|
| 173 |
for i, bookmark in enumerate(bookmarks):
|
| 174 |
+
index = i + 1
|
| 175 |
status = "Dead Link" if bookmark.get('dead_link') else "Active"
|
| 176 |
+
data.append({
|
| 177 |
+
'Index': index,
|
| 178 |
+
'Title': bookmark['title'],
|
| 179 |
+
'URL': bookmark['url'],
|
| 180 |
+
'Category': bookmark.get('category', 'Uncategorized'),
|
| 181 |
+
'Status': status,
|
| 182 |
+
'Summary': bookmark.get('summary', ''),
|
| 183 |
+
})
|
| 184 |
+
df = pd.DataFrame(data)
|
| 185 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.", pd.DataFrame()
|
| 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.", pd.DataFrame()
|
| 195 |
|
| 196 |
bookmarks = parse_bookmarks(file_content)
|
| 197 |
|
| 198 |
if not bookmarks:
|
| 199 |
+
return "No bookmarks found in the uploaded file.", pd.DataFrame()
|
| 200 |
|
| 201 |
# Asynchronously fetch bookmark info
|
| 202 |
asyncio.run(process_bookmarks_async(bookmarks))
|
|
|
|
| 208 |
|
| 209 |
faiss_index, embeddings = vectorize_and_index(bookmarks)
|
| 210 |
message = f"Successfully processed {len(bookmarks)} bookmarks."
|
| 211 |
+
bookmark_df = bookmarks_to_dataframe()
|
| 212 |
+
return message, bookmark_df
|
| 213 |
|
| 214 |
def chatbot_response(user_query):
|
| 215 |
if faiss_index is None or not bookmarks:
|
|
|
|
| 224 |
for idx in I[0]:
|
| 225 |
if idx < len(bookmarks):
|
| 226 |
bookmark = bookmarks[idx]
|
| 227 |
+
index = idx + 1 # Start index at 1
|
| 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(row):
|
| 232 |
global faiss_index
|
| 233 |
try:
|
| 234 |
+
bookmark_idx = int(row['Index']) - 1 # Adjust index to match list (starting at 0)
|
| 235 |
if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
|
| 236 |
+
return "Invalid bookmark index.", bookmarks_to_dataframe()
|
| 237 |
+
bookmarks[bookmark_idx]['title'] = row['Title']
|
| 238 |
+
bookmarks[bookmark_idx]['url'] = row['URL']
|
| 239 |
+
bookmarks[bookmark_idx]['category'] = row['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 |
+
updated_df = bookmarks_to_dataframe()
|
| 247 |
+
return message, updated_df
|
| 248 |
except Exception as e:
|
| 249 |
+
return f"Error: {str(e)}", bookmarks_to_dataframe()
|
| 250 |
|
| 251 |
+
def delete_bookmarks(selected_indices):
|
| 252 |
global faiss_index
|
| 253 |
try:
|
| 254 |
+
indices = sorted([int(idx) - 1 for idx in selected_indices], reverse=True)
|
| 255 |
for idx in indices:
|
| 256 |
if 0 <= idx < len(bookmarks):
|
| 257 |
bookmarks.pop(idx)
|
|
|
|
| 261 |
else:
|
| 262 |
faiss_index = None
|
| 263 |
message = "Selected bookmarks deleted successfully."
|
| 264 |
+
updated_df = bookmarks_to_dataframe()
|
| 265 |
+
return message, updated_df
|
| 266 |
except Exception as e:
|
| 267 |
+
return f"Error: {str(e)}", bookmarks_to_dataframe()
|
| 268 |
|
| 269 |
def export_bookmarks():
|
| 270 |
if not bookmarks:
|
|
|
|
| 280 |
dl.append(dt)
|
| 281 |
soup.append(dl)
|
| 282 |
html_content = str(soup)
|
| 283 |
+
return html_content
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
def build_app():
|
| 286 |
with gr.Blocks(css="app.css") as demo:
|
|
|
|
| 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 |
+
bookmark_table = gr.Dataframe(label="Bookmarks", interactive=False)
|
| 294 |
|
| 295 |
+
def update_bookmark_table(file):
|
| 296 |
+
message, df = process_uploaded_file(file)
|
| 297 |
+
return message, df
|
| 298 |
|
| 299 |
process_button.click(
|
| 300 |
+
update_bookmark_table,
|
| 301 |
inputs=upload,
|
| 302 |
+
outputs=[output_text, bookmark_table]
|
| 303 |
)
|
| 304 |
|
| 305 |
with gr.Tab("Chat with Bookmarks"):
|
|
|
|
| 315 |
|
| 316 |
with gr.Tab("Manage Bookmarks"):
|
| 317 |
manage_output = gr.Textbox(label="Manage Output")
|
| 318 |
+
bookmark_table_manage = gr.Dataframe(label="Bookmarks", interactive=True)
|
|
|
|
|
|
|
|
|
|
| 319 |
selected_indices = gr.Textbox(label="Selected Indices (comma-separated)", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
delete_button = gr.Button("Delete Selected Bookmarks")
|
| 321 |
export_button = gr.Button("Export Bookmarks")
|
| 322 |
+
download_link = gr.File(label="Download Exported Bookmarks", interactive=False)
|
| 323 |
+
|
| 324 |
+
def update_manage_table():
|
| 325 |
+
df = bookmarks_to_dataframe()
|
| 326 |
+
return df
|
| 327 |
+
|
| 328 |
+
def delete_selected_bookmarks(dataframe):
|
| 329 |
+
selected_indices = dataframe['Index'].tolist()
|
| 330 |
+
message, updated_df = delete_bookmarks(selected_indices)
|
| 331 |
+
return message, updated_df
|
| 332 |
+
|
| 333 |
+
def export_bookmarks_file():
|
| 334 |
+
content = export_bookmarks()
|
| 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 |
+
bookmark_table_manage.change(
|
| 343 |
edit_bookmark,
|
| 344 |
+
inputs=bookmark_table_manage,
|
| 345 |
+
outputs=[manage_output, bookmark_table_manage]
|
| 346 |
)
|
| 347 |
|
| 348 |
delete_button.click(
|
| 349 |
+
delete_selected_bookmarks,
|
| 350 |
+
inputs=bookmark_table_manage,
|
| 351 |
+
outputs=[manage_output, bookmark_table_manage]
|
| 352 |
)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
export_button.click(
|
| 355 |
+
export_bookmarks_file,
|
| 356 |
inputs=None,
|
| 357 |
outputs=download_link
|
| 358 |
)
|
| 359 |
|
| 360 |
+
# Initial load of the bookmarks table
|
| 361 |
+
bookmark_table_manage.value = update_manage_table()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
demo.launch()
|
| 364 |
|