YALCINKAYA commited on
Commit
f8f92cb
·
verified ·
1 Parent(s): 3fdeaa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -32
app.py CHANGED
@@ -101,33 +101,6 @@ save_faiss_index(index)
101
 
102
  # Load document store and populate FAISS index
103
  knowledgebase_file = os.path.join(UPLOAD_DIR, "knowledge_text.txt") # Ensure this path is correct
104
-
105
- def add_document_store_to_index():
106
- """Loads knowledgebase.txt into a dictionary where FAISS IDs map to text and embeddings"""
107
- global document_store
108
- document_store = {} # Reset document store
109
- all_texts = []
110
-
111
- if os.path.exists(knowledgebase_file):
112
- with open(knowledgebase_file, "r", encoding="utf-8") as f:
113
- lines = f.readlines()
114
-
115
- for i, line in enumerate(lines):
116
- text = line.strip()
117
- if text:
118
- document_store[i] = {"text": text} # Store text mapped to FAISS ID
119
- all_texts.append(text) # Collect all texts for embedding
120
-
121
- print(f"Loaded {len(document_store)} documents into document_store.")
122
- else:
123
- print("Error: knowledgebase.txt not found!")
124
-
125
- # Generate embeddings for all documents
126
- embeddings = bertmodel.encode(all_texts, batch_size=32, convert_to_numpy=True).astype("float32")
127
-
128
- # Add embeddings to FAISS index
129
- index.add_with_ids(embeddings, np.array(list(document_store.keys()), dtype=np.int64))
130
- print(f"Added {len(all_texts)} document embeddings to FAISS index.")
131
 
132
  def load_document_store():
133
  """Loads knowledgebase.txt into a dictionary where FAISS IDs map to text and embeddings"""
@@ -169,7 +142,7 @@ def upload_document(file_path, embed_model):
169
  text = f.read()
170
  except Exception as e:
171
  print(f"Error reading file {file_location}: {e}")
172
- return {"error": f"Error reading file: {e}"}, 502 # Error while reading file
173
 
174
  # Embed the text and add it to the FAISS index
175
  try:
@@ -192,11 +165,11 @@ def upload_document(file_path, embed_model):
192
  print(f"Document uploaded with doc_id: {doc_id}")
193
  except Exception as e:
194
  print(f"Error saving FAISS index: {e}")
195
- return {"error": f"Error saving FAISS index: {e}"}, 503 # Error while saving FAISS index
196
 
197
  except Exception as e:
198
  print(f"Error during document upload: {e}")
199
- return {"error": f"Error during document upload: {e}"}, 504 # Error during embedding or FAISS processing
200
 
201
  except Exception as e:
202
  print(f"Unexpected error: {e}")
@@ -243,8 +216,7 @@ def handle_upload():
243
  file.save(file_path)
244
 
245
  # Now that the document is uploaded, call load_document_store()
246
- print(f"File uploaded successfully. Calling load_document_store()...")
247
- add_document_store_to_index()
248
  except Exception as e:
249
  return jsonify({"error": f"Error saving file: {e}"}), 502
250
 
 
101
 
102
  # Load document store and populate FAISS index
103
  knowledgebase_file = os.path.join(UPLOAD_DIR, "knowledge_text.txt") # Ensure this path is correct
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  def load_document_store():
106
  """Loads knowledgebase.txt into a dictionary where FAISS IDs map to text and embeddings"""
 
142
  text = f.read()
143
  except Exception as e:
144
  print(f"Error reading file {file_location}: {e}")
145
+ return {"error": f"Error reading file: {e}"}, 507 # Error while reading file
146
 
147
  # Embed the text and add it to the FAISS index
148
  try:
 
165
  print(f"Document uploaded with doc_id: {doc_id}")
166
  except Exception as e:
167
  print(f"Error saving FAISS index: {e}")
168
+ return {"error": f"Error saving FAISS index: {e}"}, 508 # Error while saving FAISS index
169
 
170
  except Exception as e:
171
  print(f"Error during document upload: {e}")
172
+ return {"error": f"Error during document upload: {e}"}, 509 # Error during embedding or FAISS processing
173
 
174
  except Exception as e:
175
  print(f"Unexpected error: {e}")
 
216
  file.save(file_path)
217
 
218
  # Now that the document is uploaded, call load_document_store()
219
+ print(f"File uploaded successfully. Calling load_document_store()...")
 
220
  except Exception as e:
221
  return jsonify({"error": f"Error saving file: {e}"}), 502
222