siddhartharya commited on
Commit
f745765
1 Parent(s): db7b30b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -1
app.py CHANGED
@@ -80,4 +80,136 @@ def vectorize_and_index(bookmarks):
80
  dimension = embeddings.shape[1]
81
  faiss_idx = faiss.IndexFlatL2(dimension)
82
  faiss_idx.add(np.array(embeddings))
83
- return f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  dimension = embeddings.shape[1]
81
  faiss_idx = faiss.IndexFlatL2(dimension)
82
  faiss_idx.add(np.array(embeddings))
83
+ return faiss_idx, embeddings
84
+
85
+ def process_uploaded_file(file):
86
+ global bookmarks, faiss_index
87
+ if file is None:
88
+ return "Please upload a bookmarks HTML file."
89
+
90
+ # Since 'file' is now bytes, decode it directly
91
+ file_content = file.decode('utf-8')
92
+ bookmarks = parse_bookmarks(file_content)
93
+
94
+ for bookmark in bookmarks:
95
+ fetch_url_info(bookmark)
96
+ generate_summary(bookmark)
97
+
98
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
99
+ return f"Successfully processed {len(bookmarks)} bookmarks."
100
+
101
+ def chatbot_response(user_query):
102
+ if faiss_index is None or not bookmarks:
103
+ return "No bookmarks available. Please upload and process your bookmarks first."
104
+
105
+ # Vectorize user query
106
+ user_embedding = embedding_model.encode([user_query])
107
+ D, I = faiss_index.search(np.array(user_embedding), k=5) # Retrieve top 5 matches
108
+
109
+ # Generate response
110
+ response = ""
111
+ for idx in I[0]:
112
+ bookmark = bookmarks[idx]
113
+ response += f"Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}\n\n"
114
+ return response.strip()
115
+
116
+ def display_bookmarks():
117
+ bookmark_list = []
118
+ for i, bookmark in enumerate(bookmarks):
119
+ status = "Dead Link" if bookmark.get('dead_link') else "Active"
120
+ bookmark_list.append([i, bookmark['title'], bookmark['url'], status])
121
+ return bookmark_list
122
+
123
+ def edit_bookmark(bookmark_idx, new_title, new_url):
124
+ global faiss_index # Reference the global faiss_index variable
125
+ try:
126
+ bookmark_idx = int(bookmark_idx)
127
+ bookmarks[bookmark_idx]['title'] = new_title
128
+ bookmarks[bookmark_idx]['url'] = new_url
129
+ fetch_url_info(bookmarks[bookmark_idx])
130
+ generate_summary(bookmarks[bookmark_idx])
131
+ # Rebuild the FAISS index
132
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
133
+ return "Bookmark updated successfully."
134
+ except Exception as e:
135
+ return f"Error: {str(e)}"
136
+
137
+ def delete_bookmark(bookmark_idx):
138
+ global faiss_index # Reference the global faiss_index variable
139
+ try:
140
+ bookmark_idx = int(bookmark_idx)
141
+ bookmarks.pop(bookmark_idx)
142
+ # Rebuild the FAISS index
143
+ if bookmarks:
144
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
145
+ else:
146
+ faiss_index = None # No bookmarks left
147
+ return "Bookmark deleted successfully."
148
+ except Exception as e:
149
+ return f"Error: {str(e)}"
150
+
151
+ def build_app():
152
+ with gr.Blocks() as demo:
153
+ gr.Markdown("# Bookmark Manager App")
154
+
155
+ with gr.Tab("Upload and Process Bookmarks"):
156
+ upload = gr.File(label="Upload Bookmarks HTML File", type='bytes') # Updated here
157
+ process_button = gr.Button("Process Bookmarks")
158
+ output_text = gr.Textbox(label="Output")
159
+
160
+ process_button.click(
161
+ process_uploaded_file,
162
+ inputs=upload,
163
+ outputs=output_text
164
+ )
165
+
166
+ with gr.Tab("Chat with Bookmarks"):
167
+ user_input = gr.Textbox(label="Ask about your bookmarks")
168
+ chat_output = gr.Textbox(label="Chatbot Response")
169
+ chat_button = gr.Button("Send")
170
+
171
+ chat_button.click(
172
+ chatbot_response,
173
+ inputs=user_input,
174
+ outputs=chat_output
175
+ )
176
+
177
+ with gr.Tab("Manage Bookmarks"):
178
+ bookmark_table = gr.Dataframe(
179
+ headers=["Index", "Title", "URL", "Status"],
180
+ datatype=["number", "str", "str", "str"],
181
+ interactive=False
182
+ )
183
+ refresh_button = gr.Button("Refresh Bookmark List")
184
+
185
+ with gr.Row():
186
+ index_input = gr.Number(label="Bookmark Index")
187
+ new_title_input = gr.Textbox(label="New Title")
188
+ new_url_input = gr.Textbox(label="New URL")
189
+
190
+ edit_button = gr.Button("Edit Bookmark")
191
+ delete_button = gr.Button("Delete Bookmark")
192
+ manage_output = gr.Textbox(label="Manage Output")
193
+
194
+ refresh_button.click(
195
+ display_bookmarks,
196
+ inputs=None,
197
+ outputs=bookmark_table
198
+ )
199
+
200
+ edit_button.click(
201
+ edit_bookmark,
202
+ inputs=[index_input, new_title_input, new_url_input],
203
+ outputs=manage_output
204
+ )
205
+
206
+ delete_button.click(
207
+ delete_bookmark,
208
+ inputs=index_input,
209
+ outputs=manage_output
210
+ )
211
+
212
+ demo.launch()
213
+
214
+ if __name__ == "__main__":
215
+ build_app()