siddhartharya commited on
Commit
e3f2905
·
verified ·
1 Parent(s): a4303b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -11,7 +11,7 @@ import numpy as np
11
  # Initialize models and variables
12
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
13
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
14
- index = None
15
  bookmarks = []
16
  fetch_cache = {}
17
 
@@ -78,12 +78,12 @@ def vectorize_and_index(bookmarks):
78
  summaries = [bookmark['summary'] for bookmark in bookmarks]
79
  embeddings = embedding_model.encode(summaries)
80
  dimension = embeddings.shape[1]
81
- faiss_index = faiss.IndexFlatL2(dimension)
82
- faiss_index.add(np.array(embeddings))
83
- return faiss_index, embeddings
84
 
85
  def process_uploaded_file(file):
86
- global bookmarks, index
87
  if file is None:
88
  return "Please upload a bookmarks HTML file."
89
 
@@ -94,16 +94,16 @@ def process_uploaded_file(file):
94
  fetch_url_info(bookmark)
95
  generate_summary(bookmark)
96
 
97
- index, embeddings = vectorize_and_index(bookmarks)
98
  return f"Successfully processed {len(bookmarks)} bookmarks."
99
 
100
  def chatbot_response(user_query):
101
- if index is None or not bookmarks:
102
  return "No bookmarks available. Please upload and process your bookmarks first."
103
 
104
  # Vectorize user query
105
  user_embedding = embedding_model.encode([user_query])
106
- D, I = index.search(np.array(user_embedding), k=5) # Retrieve top 5 matches
107
 
108
  # Generate response
109
  response = ""
@@ -119,30 +119,30 @@ def display_bookmarks():
119
  bookmark_list.append([i, bookmark['title'], bookmark['url'], status])
120
  return bookmark_list
121
 
122
- def edit_bookmark(index, new_title, new_url):
123
- global index # Reference the global index variable
124
  try:
125
- index = int(index)
126
- bookmarks[index]['title'] = new_title
127
- bookmarks[index]['url'] = new_url
128
- fetch_url_info(bookmarks[index])
129
- generate_summary(bookmarks[index])
130
  # Rebuild the FAISS index
131
- index, embeddings = vectorize_and_index(bookmarks)
132
  return "Bookmark updated successfully."
133
  except Exception as e:
134
  return f"Error: {str(e)}"
135
 
136
- def delete_bookmark(index):
137
- global index # Reference the global index variable
138
  try:
139
- index = int(index)
140
- bookmarks.pop(index)
141
  # Rebuild the FAISS index
142
  if bookmarks:
143
- index, embeddings = vectorize_and_index(bookmarks)
144
  else:
145
- index = None # No bookmarks left
146
  return "Bookmark deleted successfully."
147
  except Exception as e:
148
  return f"Error: {str(e)}"
 
11
  # Initialize models and variables
12
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
13
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
14
+ faiss_index = None # Renamed from 'index' to 'faiss_index'
15
  bookmarks = []
16
  fetch_cache = {}
17
 
 
78
  summaries = [bookmark['summary'] for bookmark in bookmarks]
79
  embeddings = embedding_model.encode(summaries)
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
 
 
94
  fetch_url_info(bookmark)
95
  generate_summary(bookmark)
96
 
97
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
98
  return f"Successfully processed {len(bookmarks)} bookmarks."
99
 
100
  def chatbot_response(user_query):
101
+ if faiss_index is None or not bookmarks:
102
  return "No bookmarks available. Please upload and process your bookmarks first."
103
 
104
  # Vectorize user query
105
  user_embedding = embedding_model.encode([user_query])
106
+ D, I = faiss_index.search(np.array(user_embedding), k=5) # Retrieve top 5 matches
107
 
108
  # Generate response
109
  response = ""
 
119
  bookmark_list.append([i, bookmark['title'], bookmark['url'], status])
120
  return bookmark_list
121
 
122
+ def edit_bookmark(bookmark_idx, new_title, new_url):
123
+ global faiss_index # Reference the global faiss_index variable
124
  try:
125
+ bookmark_idx = int(bookmark_idx)
126
+ bookmarks[bookmark_idx]['title'] = new_title
127
+ bookmarks[bookmark_idx]['url'] = new_url
128
+ fetch_url_info(bookmarks[bookmark_idx])
129
+ generate_summary(bookmarks[bookmark_idx])
130
  # Rebuild the FAISS index
131
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
132
  return "Bookmark updated successfully."
133
  except Exception as e:
134
  return f"Error: {str(e)}"
135
 
136
+ def delete_bookmark(bookmark_idx):
137
+ global faiss_index # Reference the global faiss_index variable
138
  try:
139
+ bookmark_idx = int(bookmark_idx)
140
+ bookmarks.pop(bookmark_idx)
141
  # Rebuild the FAISS index
142
  if bookmarks:
143
+ faiss_index, embeddings = vectorize_and_index(bookmarks)
144
  else:
145
+ faiss_index = None # No bookmarks left
146
  return "Bookmark deleted successfully."
147
  except Exception as e:
148
  return f"Error: {str(e)}"