bhagyabonam commited on
Commit
8030591
·
verified ·
1 Parent(s): 7fb2c11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -71
app.py CHANGED
@@ -202,96 +202,38 @@ def query_crm_data_with_context(prompt, top_k=3):
202
  return ["Error in querying recommendations."]
203
 
204
 
205
- def load_objection_responses(file_path):
206
- df = pd.read_csv(file_path)
207
- objection_response_dict = dict(zip(df["Objection"], df["Response"]))
208
- return objection_response_dict
 
 
 
 
 
 
 
209
 
210
- # Load objections from CSV file
211
  objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
212
  objections = list(objection_response_pairs.keys())
213
-
214
- # Load SentenceTransformer model
215
- sentence_model = SentenceTransformer("all-MiniLM-L6-v2") # Change model if needed
216
-
217
- # Convert objections into embeddings
218
  objection_embeddings = sentence_model.encode(objections)
 
219
 
220
- # Ensure embeddings are 2D
221
- objection_embeddings = np.array(objection_embeddings, dtype="float32")
222
- if len(objection_embeddings.shape) == 1:
223
- objection_embeddings = objection_embeddings.reshape(-1, 1)
224
-
225
- # Debug: Print shape of embeddings
226
- print("Objection Embeddings Shape:", objection_embeddings.shape)
227
-
228
- # Initialize FAISS index
229
- d = objection_embeddings.shape[1] # Get embedding dimension
230
- faiss_index = faiss.IndexFlatL2(d) # L2 similarity index
231
-
232
- # Add embeddings to FAISS index
233
- faiss_index.add(objection_embeddings)
234
-
235
- # Function to find closest objection
236
  def find_closest_objection(query):
237
  query_embedding = sentence_model.encode([query])
238
-
239
- # Ensure query_embedding is 2D
240
- query_embedding = np.array(query_embedding, dtype="float32")
241
- if len(query_embedding.shape) == 1:
242
- query_embedding = query_embedding.reshape(-1, 1)
243
-
244
- distances, indices = faiss_index.search(query_embedding, 1)
245
-
246
  closest_index = indices[0][0]
247
  closest_objection = objections[closest_index]
248
  response = objection_response_pairs[closest_objection]
249
-
250
  if distances[0][0] > 0.6:
251
  return "No objection found", "No Response"
252
-
253
  return closest_objection, response
254
 
255
- # Function to handle objections and recommend responses
256
  def handle_objection_and_recommendation(prompt):
257
  closest_objection, objection_response = find_closest_objection(prompt)
258
-
259
- # Placeholder function: Replace with actual CRM data retrieval
260
  recommendations = query_crm_data_with_context(prompt)
261
 
262
  return closest_objection, objection_response, recommendations
263
- # sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
264
- # faiss_index = faiss.IndexFlatL2(384)
265
-
266
- # def load_objection_responses(csv_file_path):
267
- # try:
268
- # df = pd.read_csv(csv_file_path)
269
- # objection_response_pairs = dict(zip(df['Objection'], df['Response']))
270
- # return objection_response_pairs
271
- # except Exception as e:
272
- # print(f"Error loading objections CSV: {e}")
273
- # return {}
274
-
275
- # objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
276
- # objections = list(objection_response_pairs.keys())
277
- # objection_embeddings = sentence_model.encode(objections)
278
- # faiss_index.add(np.array(objection_embeddings, dtype="float32"))
279
-
280
- # def find_closest_objection(query):
281
- # query_embedding = sentence_model.encode([query])
282
- # distances, indices = faiss_index.search(np.array(query_embedding, dtype="float32"), 1)
283
- # closest_index = indices[0][0]
284
- # closest_objection = objections[closest_index]
285
- # response = objection_response_pairs[closest_objection]
286
- # if distances[0][0] > 0.6:
287
- # return "No objection found", "No Response"
288
- # return closest_objection, response
289
-
290
- # def handle_objection_and_recommendation(prompt):
291
- # closest_objection, objection_response = find_closest_objection(prompt)
292
- # recommendations = query_crm_data_with_context(prompt)
293
-
294
- # return closest_objection, objection_response, recommendations
295
 
296
 
297
  if "is_listening" not in st.session_state:
 
202
  return ["Error in querying recommendations."]
203
 
204
 
205
+ sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
206
+ faiss_index = faiss.IndexFlatL2(384)
207
+
208
+ def load_objection_responses(csv_file_path):
209
+ try:
210
+ df = pd.read_csv(csv_file_path)
211
+ objection_response_pairs = dict(zip(df['Objection'], df['Response']))
212
+ return objection_response_pairs
213
+ except Exception as e:
214
+ print(f"Error loading objections CSV: {e}")
215
+ return {}
216
 
 
217
  objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
218
  objections = list(objection_response_pairs.keys())
 
 
 
 
 
219
  objection_embeddings = sentence_model.encode(objections)
220
+ faiss_index.add(np.array(objection_embeddings, dtype="float32"))
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  def find_closest_objection(query):
223
  query_embedding = sentence_model.encode([query])
224
+ distances, indices = faiss_index.search(np.array(query_embedding, dtype="float32"), 1)
 
 
 
 
 
 
 
225
  closest_index = indices[0][0]
226
  closest_objection = objections[closest_index]
227
  response = objection_response_pairs[closest_objection]
 
228
  if distances[0][0] > 0.6:
229
  return "No objection found", "No Response"
 
230
  return closest_objection, response
231
 
 
232
  def handle_objection_and_recommendation(prompt):
233
  closest_objection, objection_response = find_closest_objection(prompt)
 
 
234
  recommendations = query_crm_data_with_context(prompt)
235
 
236
  return closest_objection, objection_response, recommendations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
 
239
  if "is_listening" not in st.session_state: