siddhartharya commited on
Commit
99d9847
·
verified ·
1 Parent(s): 09bf047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -40
app.py CHANGED
@@ -3,7 +3,6 @@
3
  import gradio as gr
4
  from bs4 import BeautifulSoup
5
  import requests
6
- from transformers import pipeline
7
  from sentence_transformers import SentenceTransformer
8
  import faiss
9
  import numpy as np
@@ -100,37 +99,43 @@ def vectorize_and_index(bookmarks):
100
  return faiss_idx, embeddings
101
 
102
  def display_bookmarks():
103
- data = []
104
- classes = []
105
  for i, bookmark in enumerate(bookmarks):
 
106
  status = "Dead Link" if bookmark.get('dead_link') else "Active"
107
- row_class = "dead-link" if bookmark.get('dead_link') else ""
108
- data.append({
109
- 'Index': i,
110
- 'Title': bookmark['title'],
111
- 'URL': f"<a href='{bookmark['url']}' target='_blank'>{bookmark['url']}</a>",
112
- 'Status': status,
113
- 'ETag': bookmark.get('etag', 'N/A'),
114
- 'Summary': bookmark.get('summary', ''),
115
- })
116
- classes.append(row_class)
117
- df = pd.DataFrame(data)
118
- # Return HTML with styles
119
- return df.to_html(escape=False, index=False)
 
 
 
 
 
 
120
 
121
  def process_uploaded_file(file):
122
  global bookmarks, faiss_index
123
  if file is None:
124
- return "Please upload a bookmarks HTML file.", pd.DataFrame()
125
  try:
126
  file_content = file.decode('utf-8')
127
  except UnicodeDecodeError:
128
- return "Error decoding the file. Please ensure it's a valid HTML file.", pd.DataFrame()
129
 
130
  bookmarks = parse_bookmarks(file_content)
131
 
132
  if not bookmarks:
133
- return "No bookmarks found in the uploaded file.", pd.DataFrame()
134
 
135
  # Asynchronously fetch bookmark info
136
  asyncio.run(process_bookmarks_async(bookmarks))
@@ -157,13 +162,14 @@ def chatbot_response(user_query):
157
  for idx in I[0]:
158
  if idx < len(bookmarks):
159
  bookmark = bookmarks[idx]
160
- response += f"Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}\n\n"
 
161
  return response.strip()
162
 
163
  def edit_bookmark(bookmark_idx, new_title, new_url):
164
  global faiss_index
165
  try:
166
- bookmark_idx = int(bookmark_idx)
167
  if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
168
  return "Invalid bookmark index.", display_bookmarks()
169
  bookmarks[bookmark_idx]['title'] = new_title
@@ -182,7 +188,7 @@ def edit_bookmark(bookmark_idx, new_title, new_url):
182
  def delete_bookmark(bookmark_idx):
183
  global faiss_index
184
  try:
185
- bookmark_idx = int(bookmark_idx)
186
  if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
187
  return "Invalid bookmark index.", display_bookmarks()
188
  bookmarks.pop(bookmark_idx)
@@ -199,22 +205,22 @@ def delete_bookmark(bookmark_idx):
199
 
200
  def build_app():
201
  with gr.Blocks(css="app.css") as demo:
202
- gr.Markdown("<h1 style='text-align: center;'>Bookmark Manager App</h1>")
203
 
204
  with gr.Tab("Upload and Process Bookmarks"):
205
  upload = gr.File(label="Upload Bookmarks HTML File", type='binary')
206
  process_button = gr.Button("Process Bookmarks")
207
  output_text = gr.Textbox(label="Output")
208
- bookmark_table = gr.HTML(label="Bookmarks")
209
 
210
- def update_bookmark_table(file):
211
- message, html_table = process_uploaded_file(file)
212
- return message, html_table
213
 
214
  process_button.click(
215
- update_bookmark_table,
216
  inputs=upload,
217
- outputs=[output_text, bookmark_table]
218
  )
219
 
220
  with gr.Tab("Chat with Bookmarks"):
@@ -230,41 +236,41 @@ def build_app():
230
 
231
  with gr.Tab("Manage Bookmarks"):
232
  manage_output = gr.Textbox(label="Manage Output")
233
- bookmark_table_manage = gr.HTML(label="Bookmarks")
234
  refresh_button = gr.Button("Refresh Bookmark List")
235
 
236
  with gr.Row():
237
- index_input = gr.Number(label="Bookmark Index")
238
  new_title_input = gr.Textbox(label="New Title")
239
  new_url_input = gr.Textbox(label="New URL")
240
 
241
  edit_button = gr.Button("Edit Bookmark")
242
  delete_button = gr.Button("Delete Bookmark")
243
 
244
- def update_manage_table():
245
- html_table = display_bookmarks()
246
- return html_table
247
 
248
  refresh_button.click(
249
- update_manage_table,
250
  inputs=None,
251
- outputs=bookmark_table_manage
252
  )
253
 
254
  edit_button.click(
255
  edit_bookmark,
256
  inputs=[index_input, new_title_input, new_url_input],
257
- outputs=[manage_output, bookmark_table_manage]
258
  )
259
 
260
  delete_button.click(
261
  delete_bookmark,
262
  inputs=index_input,
263
- outputs=[manage_output, bookmark_table_manage]
264
  )
265
 
266
- # Initial load of the bookmarks table
267
- bookmark_table_manage.value = update_manage_table()
268
 
269
  demo.launch()
270
 
 
3
  import gradio as gr
4
  from bs4 import BeautifulSoup
5
  import requests
 
6
  from sentence_transformers import SentenceTransformer
7
  import faiss
8
  import numpy as np
 
99
  return faiss_idx, embeddings
100
 
101
  def display_bookmarks():
102
+ cards = ''
 
103
  for i, bookmark in enumerate(bookmarks):
104
+ index = i + 1 # Start index at 1
105
  status = "Dead Link" if bookmark.get('dead_link') else "Active"
106
+ card_class = "card dead-link" if bookmark.get('dead_link') else "card"
107
+ title = bookmark['title']
108
+ url = bookmark['url']
109
+ etag = bookmark.get('etag', 'N/A')
110
+ summary = bookmark.get('summary', '')
111
+
112
+ card_html = f'''
113
+ <div class="{card_class}">
114
+ <div class="card-content">
115
+ <h3>{index}. {title}</h3>
116
+ <p><strong>URL:</strong> <a href="{url}" target="_blank">{url}</a></p>
117
+ <p><strong>Status:</strong> {status}</p>
118
+ <p><strong>ETag:</strong> {etag}</p>
119
+ <p><strong>Summary:</strong> {summary}</p>
120
+ </div>
121
+ </div>
122
+ '''
123
+ cards += card_html
124
+ return cards
125
 
126
  def process_uploaded_file(file):
127
  global bookmarks, faiss_index
128
  if file is None:
129
+ return "Please upload a bookmarks HTML file.", ''
130
  try:
131
  file_content = file.decode('utf-8')
132
  except UnicodeDecodeError:
133
+ return "Error decoding the file. Please ensure it's a valid HTML file.", ''
134
 
135
  bookmarks = parse_bookmarks(file_content)
136
 
137
  if not bookmarks:
138
+ return "No bookmarks found in the uploaded file.", ''
139
 
140
  # Asynchronously fetch bookmark info
141
  asyncio.run(process_bookmarks_async(bookmarks))
 
162
  for idx in I[0]:
163
  if idx < len(bookmarks):
164
  bookmark = bookmarks[idx]
165
+ index = bookmarks.index(bookmark) + 1 # Start index at 1
166
+ response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}\n\n"
167
  return response.strip()
168
 
169
  def edit_bookmark(bookmark_idx, new_title, new_url):
170
  global faiss_index
171
  try:
172
+ bookmark_idx = int(bookmark_idx) - 1 # Adjust index to match list (starting at 0)
173
  if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
174
  return "Invalid bookmark index.", display_bookmarks()
175
  bookmarks[bookmark_idx]['title'] = new_title
 
188
  def delete_bookmark(bookmark_idx):
189
  global faiss_index
190
  try:
191
+ bookmark_idx = int(bookmark_idx) - 1 # Adjust index to match list (starting at 0)
192
  if bookmark_idx < 0 or bookmark_idx >= len(bookmarks):
193
  return "Invalid bookmark index.", display_bookmarks()
194
  bookmarks.pop(bookmark_idx)
 
205
 
206
  def build_app():
207
  with gr.Blocks(css="app.css") as demo:
208
+ gr.Markdown("<h1>Bookmark Manager App</h1>")
209
 
210
  with gr.Tab("Upload and Process Bookmarks"):
211
  upload = gr.File(label="Upload Bookmarks HTML File", type='binary')
212
  process_button = gr.Button("Process Bookmarks")
213
  output_text = gr.Textbox(label="Output")
214
+ bookmark_display = gr.HTML(label="Bookmarks")
215
 
216
+ def update_bookmark_display(file):
217
+ message, html_content = process_uploaded_file(file)
218
+ return message, html_content
219
 
220
  process_button.click(
221
+ update_bookmark_display,
222
  inputs=upload,
223
+ outputs=[output_text, bookmark_display]
224
  )
225
 
226
  with gr.Tab("Chat with Bookmarks"):
 
236
 
237
  with gr.Tab("Manage Bookmarks"):
238
  manage_output = gr.Textbox(label="Manage Output")
239
+ bookmark_display_manage = gr.HTML(label="Bookmarks")
240
  refresh_button = gr.Button("Refresh Bookmark List")
241
 
242
  with gr.Row():
243
+ index_input = gr.Number(label="Bookmark Index (Starting from 1)", precision=0)
244
  new_title_input = gr.Textbox(label="New Title")
245
  new_url_input = gr.Textbox(label="New URL")
246
 
247
  edit_button = gr.Button("Edit Bookmark")
248
  delete_button = gr.Button("Delete Bookmark")
249
 
250
+ def update_manage_display():
251
+ html_content = display_bookmarks()
252
+ return html_content
253
 
254
  refresh_button.click(
255
+ update_manage_display,
256
  inputs=None,
257
+ outputs=bookmark_display_manage
258
  )
259
 
260
  edit_button.click(
261
  edit_bookmark,
262
  inputs=[index_input, new_title_input, new_url_input],
263
+ outputs=[manage_output, bookmark_display_manage]
264
  )
265
 
266
  delete_button.click(
267
  delete_bookmark,
268
  inputs=index_input,
269
+ outputs=[manage_output, bookmark_display_manage]
270
  )
271
 
272
+ # Initial load of the bookmarks display
273
+ bookmark_display_manage.value = update_manage_display()
274
 
275
  demo.launch()
276