Update app.py
Browse files
app.py
CHANGED
@@ -213,14 +213,38 @@ objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\De
|
|
213 |
objections = list(objection_response_pairs.keys())
|
214 |
objection_embeddings = sentence_model.encode(objections)
|
215 |
objection_embeddings = np.array(objection_embeddings, dtype="float32")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
if len(objection_embeddings.shape) == 1:
|
217 |
objection_embeddings = objection_embeddings.reshape(1, -1) # Reshape for a single embedding
|
218 |
elif len(objection_embeddings.shape) == 2:
|
219 |
-
pass
|
220 |
else:
|
221 |
-
raise ValueError("Unexpected shape for objection embeddings:
|
|
|
|
|
|
|
|
|
|
|
222 |
faiss_index.add(objection_embeddings)
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
def find_closest_objection(query):
|
225 |
query_embedding = sentence_model.encode([query])
|
226 |
distances, indices = faiss_index.search(np.array(query_embedding, dtype="float32"), 1)
|
|
|
213 |
objections = list(objection_response_pairs.keys())
|
214 |
objection_embeddings = sentence_model.encode(objections)
|
215 |
objection_embeddings = np.array(objection_embeddings, dtype="float32")
|
216 |
+
print(f"Shape of objection_embeddings: {objection_embeddings.shape}")
|
217 |
+
|
218 |
+
# Assuming you know the expected dimension of the embeddings (e.g., 768)
|
219 |
+
expected_dim = 768 # Example value for sentence embeddings (replace with the actual dimension)
|
220 |
+
|
221 |
+
# Check if the embeddings dimensionality matches the expected dimension
|
222 |
+
if objection_embeddings.shape[1] != expected_dim:
|
223 |
+
raise ValueError(f"Dimensionality of embeddings {objection_embeddings.shape[1]} does not match expected dimension {expected_dim}.")
|
224 |
+
|
225 |
+
# If the shape is (1, d) (e.g., a single embedding), reshape it to (1, d)
|
226 |
if len(objection_embeddings.shape) == 1:
|
227 |
objection_embeddings = objection_embeddings.reshape(1, -1) # Reshape for a single embedding
|
228 |
elif len(objection_embeddings.shape) == 2:
|
229 |
+
pass # The shape is already in the expected form (num_samples, dim)
|
230 |
else:
|
231 |
+
raise ValueError(f"Unexpected shape for objection embeddings: {objection_embeddings.shape}")
|
232 |
+
|
233 |
+
# Create Faiss index with the correct dimensionality (make sure this matches the embedding size)
|
234 |
+
faiss_index = faiss.IndexFlatL2(expected_dim)
|
235 |
+
|
236 |
+
# Now add the embeddings to the Faiss index
|
237 |
faiss_index.add(objection_embeddings)
|
238 |
|
239 |
+
print(f"Successfully added {objection_embeddings.shape[0]} embeddings to the Faiss index.")
|
240 |
+
# if len(objection_embeddings.shape) == 1:
|
241 |
+
# objection_embeddings = objection_embeddings.reshape(1, -1) # Reshape for a single embedding
|
242 |
+
# elif len(objection_embeddings.shape) == 2:
|
243 |
+
# pass
|
244 |
+
# else:
|
245 |
+
# raise ValueError("Unexpected shape for objection embeddings:", objection_embeddings.shape)
|
246 |
+
# faiss_index.add(objection_embeddings)
|
247 |
+
|
248 |
def find_closest_objection(query):
|
249 |
query_embedding = sentence_model.encode([query])
|
250 |
distances, indices = faiss_index.search(np.array(query_embedding, dtype="float32"), 1)
|