Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -208,9 +208,28 @@ def query_crm_data_with_context(prompt, top_k=3):
|
|
208 |
|
209 |
|
210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
212 |
-
faiss_index = faiss.IndexFlatL2(384)
|
213 |
|
|
|
|
|
|
|
|
|
|
|
214 |
def load_objection_responses(csv_file_path):
|
215 |
try:
|
216 |
df = pd.read_csv(csv_file_path)
|
@@ -220,43 +239,23 @@ def load_objection_responses(csv_file_path):
|
|
220 |
print(f"Error loading objections CSV: {e}")
|
221 |
return {}
|
222 |
|
|
|
223 |
objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
|
224 |
objections = list(objection_response_pairs.keys())
|
225 |
objection_embeddings = sentence_model.encode(objections)
|
226 |
objection_embeddings = np.array(objection_embeddings, dtype="float32")
|
227 |
-
print(f"Shape of objection_embeddings: {objection_embeddings.shape}")
|
228 |
|
229 |
-
#
|
230 |
-
|
231 |
-
if len(objection_embeddings.shape) == 1:
|
232 |
-
objection_embeddings = objection_embeddings.reshape(-1, 1)
|
233 |
|
234 |
# Check if the embeddings dimensionality matches the expected dimension
|
235 |
if objection_embeddings.shape[1] != expected_dim:
|
236 |
raise ValueError(f"Dimensionality of embeddings {objection_embeddings.shape[1]} does not match expected dimension {expected_dim}.")
|
237 |
|
238 |
-
#
|
239 |
-
if len(objection_embeddings.shape) == 1:
|
240 |
-
objection_embeddings = objection_embeddings.reshape(1, -1) # Reshape for a single embedding
|
241 |
-
elif len(objection_embeddings.shape) == 2:
|
242 |
-
pass # The shape is already in the expected form (num_samples, dim)
|
243 |
-
else:
|
244 |
-
raise ValueError(f"Unexpected shape for objection embeddings: {objection_embeddings.shape}")
|
245 |
-
|
246 |
-
# Create Faiss index with the correct dimensionality (make sure this matches the embedding size)
|
247 |
-
faiss_index = faiss.IndexFlatL2(expected_dim)
|
248 |
-
|
249 |
-
# Now add the embeddings to the Faiss index
|
250 |
faiss_index.add(objection_embeddings)
|
251 |
|
252 |
print(f"Successfully added {objection_embeddings.shape[0]} embeddings to the Faiss index.")
|
253 |
-
# if len(objection_embeddings.shape) == 1:
|
254 |
-
# objection_embeddings = objection_embeddings.reshape(1, -1) # Reshape for a single embedding
|
255 |
-
# elif len(objection_embeddings.shape) == 2:
|
256 |
-
# pass
|
257 |
-
# else:
|
258 |
-
# raise ValueError("Unexpected shape for objection embeddings:", objection_embeddings.shape)
|
259 |
-
# faiss_index.add(objection_embeddings)
|
260 |
|
261 |
def find_closest_objection(query):
|
262 |
query_embedding = sentence_model.encode([query])
|
|
|
208 |
|
209 |
|
210 |
|
211 |
+
|
212 |
+
storage_path = r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\chromadb_storage" # Update with your storage path
|
213 |
+
|
214 |
+
# Ensure the storage path exists
|
215 |
+
if not os.path.exists(storage_path):
|
216 |
+
print(f"Storage path {storage_path} does not exist. Creating it...")
|
217 |
+
os.makedirs(storage_path)
|
218 |
+
|
219 |
+
# Clear any corrupted data
|
220 |
+
if os.path.exists(storage_path):
|
221 |
+
print(f"Removing existing storage at {storage_path}...")
|
222 |
+
shutil.rmtree(storage_path)
|
223 |
+
os.makedirs(storage_path)
|
224 |
+
|
225 |
+
# Initialize the sentence transformer model
|
226 |
sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
|
|
227 |
|
228 |
+
# Create Faiss index with the correct dimensionality (make sure this matches the embedding size)
|
229 |
+
expected_dim = 384 # Example value for sentence embeddings
|
230 |
+
faiss_index = faiss.IndexFlatL2(expected_dim)
|
231 |
+
|
232 |
+
# Load objection responses
|
233 |
def load_objection_responses(csv_file_path):
|
234 |
try:
|
235 |
df = pd.read_csv(csv_file_path)
|
|
|
239 |
print(f"Error loading objections CSV: {e}")
|
240 |
return {}
|
241 |
|
242 |
+
# Load objections and encode them into embeddings
|
243 |
objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
|
244 |
objections = list(objection_response_pairs.keys())
|
245 |
objection_embeddings = sentence_model.encode(objections)
|
246 |
objection_embeddings = np.array(objection_embeddings, dtype="float32")
|
|
|
247 |
|
248 |
+
# Check the shape of the embeddings
|
249 |
+
print(f"Shape of objection_embeddings: {objection_embeddings.shape}")
|
|
|
|
|
250 |
|
251 |
# Check if the embeddings dimensionality matches the expected dimension
|
252 |
if objection_embeddings.shape[1] != expected_dim:
|
253 |
raise ValueError(f"Dimensionality of embeddings {objection_embeddings.shape[1]} does not match expected dimension {expected_dim}.")
|
254 |
|
255 |
+
# Add the embeddings to the Faiss index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
faiss_index.add(objection_embeddings)
|
257 |
|
258 |
print(f"Successfully added {objection_embeddings.shape[0]} embeddings to the Faiss index.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
def find_closest_objection(query):
|
261 |
query_embedding = sentence_model.encode([query])
|