Spaces:
Sleeping
Sleeping
siddhartharya
commited on
Commit
•
85c9bd6
1
Parent(s):
880f9ee
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,10 @@ import aiohttp
|
|
11 |
import re
|
12 |
import base64
|
13 |
import logging
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Set up logging
|
16 |
logging.basicConfig(filename='app.log', level=logging.INFO,
|
@@ -50,6 +54,12 @@ CATEGORIES = [
|
|
50 |
"Uncategorized",
|
51 |
]
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def parse_bookmarks(file_content):
|
54 |
logger.info("Parsing bookmarks")
|
55 |
try:
|
@@ -66,6 +76,7 @@ def parse_bookmarks(file_content):
|
|
66 |
logger.error("Error parsing bookmarks: %s", e)
|
67 |
raise
|
68 |
|
|
|
69 |
async def fetch_url_info(session, bookmark):
|
70 |
url = bookmark['url']
|
71 |
if url in fetch_cache:
|
@@ -114,6 +125,7 @@ async def fetch_url_info(session, bookmark):
|
|
114 |
}
|
115 |
return bookmark
|
116 |
|
|
|
117 |
async def process_bookmarks_async(bookmarks):
|
118 |
logger.info("Processing bookmarks asynchronously")
|
119 |
try:
|
@@ -128,6 +140,7 @@ async def process_bookmarks_async(bookmarks):
|
|
128 |
logger.error(f"Error in asynchronous processing of bookmarks: {e}")
|
129 |
raise
|
130 |
|
|
|
131 |
def generate_summary(bookmark):
|
132 |
description = bookmark.get('description', '')
|
133 |
if description:
|
@@ -141,6 +154,7 @@ def generate_summary(bookmark):
|
|
141 |
logger.info(f"Generated summary for bookmark: {bookmark.get('url')}")
|
142 |
return bookmark
|
143 |
|
|
|
144 |
def assign_category(bookmark):
|
145 |
if bookmark.get('dead_link'):
|
146 |
bookmark['category'] = 'Dead Link'
|
@@ -188,6 +202,7 @@ def assign_category(bookmark):
|
|
188 |
logger.info(f"No matching category found for bookmark: {bookmark.get('url')}")
|
189 |
return bookmark
|
190 |
|
|
|
191 |
def vectorize_and_index(bookmarks):
|
192 |
logger.info("Vectorizing summaries and building FAISS index")
|
193 |
try:
|
@@ -202,6 +217,7 @@ def vectorize_and_index(bookmarks):
|
|
202 |
logger.error(f"Error in vectorizing and indexing: {e}")
|
203 |
raise
|
204 |
|
|
|
205 |
def display_bookmarks():
|
206 |
logger.info("Generating HTML display for bookmarks")
|
207 |
cards = ''
|
@@ -238,6 +254,7 @@ def display_bookmarks():
|
|
238 |
logger.info("HTML display generated")
|
239 |
return cards
|
240 |
|
|
|
241 |
def process_uploaded_file(file):
|
242 |
global bookmarks, faiss_index
|
243 |
logger.info("Processing uploaded file")
|
@@ -283,30 +300,38 @@ def process_uploaded_file(file):
|
|
283 |
bookmark_html = display_bookmarks()
|
284 |
return message, bookmark_html
|
285 |
|
|
|
286 |
def chatbot_response(user_query):
|
287 |
-
if
|
288 |
logger.warning("No bookmarks available for chatbot")
|
289 |
return "No bookmarks available. Please upload and process your bookmarks first."
|
290 |
|
291 |
logger.info(f"Chatbot received query: {user_query}")
|
292 |
-
|
|
|
293 |
try:
|
294 |
-
|
295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
except Exception as e:
|
297 |
-
logger.error(f"Error in chatbot
|
298 |
return "Error processing your query."
|
299 |
|
300 |
-
|
301 |
-
response = ""
|
302 |
-
for idx in I[0]:
|
303 |
-
if idx < len(bookmarks):
|
304 |
-
bookmark = bookmarks[idx]
|
305 |
-
index = idx + 1 # Start index at 1
|
306 |
-
response += f"{index}. Title: {bookmark['title']}\nURL: {bookmark['url']}\nCategory: {bookmark.get('category', 'Uncategorized')}\nSummary: {bookmark['summary']}\n\n"
|
307 |
-
logger.info("Chatbot response generated")
|
308 |
-
return response.strip()
|
309 |
-
|
310 |
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
311 |
global faiss_index
|
312 |
try:
|
@@ -331,6 +356,7 @@ def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
|
331 |
logger.error(f"Error editing bookmark: {e}")
|
332 |
return f"Error: {str(e)}", display_bookmarks()
|
333 |
|
|
|
334 |
def delete_bookmarks(indices_str):
|
335 |
global faiss_index
|
336 |
try:
|
@@ -354,6 +380,7 @@ def delete_bookmarks(indices_str):
|
|
354 |
logger.error(f"Error deleting bookmarks: {e}")
|
355 |
return f"Error: {str(e)}", display_bookmarks()
|
356 |
|
|
|
357 |
def export_bookmarks():
|
358 |
if not bookmarks:
|
359 |
logger.warning("No bookmarks to export")
|
@@ -380,6 +407,7 @@ def export_bookmarks():
|
|
380 |
logger.error(f"Error exporting bookmarks: {e}")
|
381 |
return None
|
382 |
|
|
|
383 |
def build_app():
|
384 |
logger.info("Building Gradio app")
|
385 |
with gr.Blocks(css="app.css") as demo:
|
|
|
11 |
import re
|
12 |
import base64
|
13 |
import logging
|
14 |
+
import os
|
15 |
+
|
16 |
+
# Import Hugging Face transformers
|
17 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
18 |
|
19 |
# Set up logging
|
20 |
logging.basicConfig(filename='app.log', level=logging.INFO,
|
|
|
54 |
"Uncategorized",
|
55 |
]
|
56 |
|
57 |
+
# Load FLAN-T5 model and tokenizer
|
58 |
+
logger.info("Loading FLAN-T5 model and tokenizer")
|
59 |
+
tokenizer = AutoTokenizer.from_pretrained('google/flan-t5-small')
|
60 |
+
model = AutoModelForSeq2SeqLM.from_pretrained('google/flan-t5-small')
|
61 |
+
|
62 |
+
# Function to parse bookmarks from HTML
|
63 |
def parse_bookmarks(file_content):
|
64 |
logger.info("Parsing bookmarks")
|
65 |
try:
|
|
|
76 |
logger.error("Error parsing bookmarks: %s", e)
|
77 |
raise
|
78 |
|
79 |
+
# Asynchronous function to fetch URL info
|
80 |
async def fetch_url_info(session, bookmark):
|
81 |
url = bookmark['url']
|
82 |
if url in fetch_cache:
|
|
|
125 |
}
|
126 |
return bookmark
|
127 |
|
128 |
+
# Asynchronous processing of bookmarks
|
129 |
async def process_bookmarks_async(bookmarks):
|
130 |
logger.info("Processing bookmarks asynchronously")
|
131 |
try:
|
|
|
140 |
logger.error(f"Error in asynchronous processing of bookmarks: {e}")
|
141 |
raise
|
142 |
|
143 |
+
# Generate summary for a bookmark
|
144 |
def generate_summary(bookmark):
|
145 |
description = bookmark.get('description', '')
|
146 |
if description:
|
|
|
154 |
logger.info(f"Generated summary for bookmark: {bookmark.get('url')}")
|
155 |
return bookmark
|
156 |
|
157 |
+
# Assign category to a bookmark
|
158 |
def assign_category(bookmark):
|
159 |
if bookmark.get('dead_link'):
|
160 |
bookmark['category'] = 'Dead Link'
|
|
|
202 |
logger.info(f"No matching category found for bookmark: {bookmark.get('url')}")
|
203 |
return bookmark
|
204 |
|
205 |
+
# Vectorize summaries and build FAISS index
|
206 |
def vectorize_and_index(bookmarks):
|
207 |
logger.info("Vectorizing summaries and building FAISS index")
|
208 |
try:
|
|
|
217 |
logger.error(f"Error in vectorizing and indexing: {e}")
|
218 |
raise
|
219 |
|
220 |
+
# Generate HTML display for bookmarks
|
221 |
def display_bookmarks():
|
222 |
logger.info("Generating HTML display for bookmarks")
|
223 |
cards = ''
|
|
|
254 |
logger.info("HTML display generated")
|
255 |
return cards
|
256 |
|
257 |
+
# Process the uploaded file
|
258 |
def process_uploaded_file(file):
|
259 |
global bookmarks, faiss_index
|
260 |
logger.info("Processing uploaded file")
|
|
|
300 |
bookmark_html = display_bookmarks()
|
301 |
return message, bookmark_html
|
302 |
|
303 |
+
# Chatbot response using Hugging Face model
|
304 |
def chatbot_response(user_query):
|
305 |
+
if not bookmarks:
|
306 |
logger.warning("No bookmarks available for chatbot")
|
307 |
return "No bookmarks available. Please upload and process your bookmarks first."
|
308 |
|
309 |
logger.info(f"Chatbot received query: {user_query}")
|
310 |
+
|
311 |
+
# Prepare the context
|
312 |
try:
|
313 |
+
# Combine bookmark summaries into context
|
314 |
+
max_bookmarks = 50 # Adjust as needed
|
315 |
+
bookmark_context = ""
|
316 |
+
for idx, bookmark in enumerate(bookmarks[:max_bookmarks]):
|
317 |
+
bookmark_context += f"{idx+1}. Title: {bookmark['title']}\nSummary: {bookmark['summary']}\n\n"
|
318 |
+
|
319 |
+
# Construct the prompt
|
320 |
+
prompt = f"Based on the following bookmarks, answer the user's query.\n\nUser query: {user_query}\n\nBookmarks:\n{bookmark_context}"
|
321 |
+
|
322 |
+
# Generate response
|
323 |
+
inputs = tokenizer(prompt, return_tensors='pt', max_length=512, truncation=True)
|
324 |
+
outputs = model.generate(**inputs, max_length=512)
|
325 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
326 |
+
|
327 |
+
logger.info("Chatbot response generated using FLAN-T5 model")
|
328 |
+
return generated_text.strip()
|
329 |
+
|
330 |
except Exception as e:
|
331 |
+
logger.error(f"Error in chatbot response generation: {e}")
|
332 |
return "Error processing your query."
|
333 |
|
334 |
+
# Edit a bookmark
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
def edit_bookmark(bookmark_idx, new_title, new_url, new_category):
|
336 |
global faiss_index
|
337 |
try:
|
|
|
356 |
logger.error(f"Error editing bookmark: {e}")
|
357 |
return f"Error: {str(e)}", display_bookmarks()
|
358 |
|
359 |
+
# Delete selected bookmarks
|
360 |
def delete_bookmarks(indices_str):
|
361 |
global faiss_index
|
362 |
try:
|
|
|
380 |
logger.error(f"Error deleting bookmarks: {e}")
|
381 |
return f"Error: {str(e)}", display_bookmarks()
|
382 |
|
383 |
+
# Export bookmarks to HTML
|
384 |
def export_bookmarks():
|
385 |
if not bookmarks:
|
386 |
logger.warning("No bookmarks to export")
|
|
|
407 |
logger.error(f"Error exporting bookmarks: {e}")
|
408 |
return None
|
409 |
|
410 |
+
# Build the Gradio app
|
411 |
def build_app():
|
412 |
logger.info("Building Gradio app")
|
413 |
with gr.Blocks(css="app.css") as demo:
|