siddhartharya commited on
Commit
0eb712b
·
verified ·
1 Parent(s): cd9d0c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -70
app.py CHANGED
@@ -9,7 +9,7 @@ import numpy as np
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')
@@ -168,35 +168,53 @@ def vectorize_and_index(bookmarks):
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,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
- 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:
@@ -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(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,10 +280,10 @@ def delete_bookmarks(selected_indices):
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,7 +299,10 @@ def export_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,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
- 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,50 +337,59 @@ def build_app():
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
 
 
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