bhagyabonam commited on
Commit
9a2d810
·
verified ·
1 Parent(s): b7a2630

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -81
app.py CHANGED
@@ -16,7 +16,6 @@ import matplotlib.pyplot as plt
16
  from huggingface_hub import login
17
  import os
18
  from dotenv import load_dotenv
19
- import shutil
20
 
21
  SPREADSHEET_ID = "1CsBub3Jlwyo7WHMQty6SDnBShIZMjl5XTVSoOKrxZhc"
22
  RANGE_NAME = 'Sheet1!A1:E'
@@ -26,11 +25,6 @@ SERVICE_ACCOUNT_FILE = r"C:\Users\bhagy\AI\credentials.json"
26
  csv_file_path = r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\900_products_dataset.csv"
27
 
28
 
29
- # storage_path = "chromadb_storage"
30
- # if not os.path.exists(storage_path):
31
- # os.makedirs(storage_path)
32
-
33
- # Define Custom Embedding Function
34
  class CustomEmbeddingFunction:
35
  def __init__(self, model_name="sentence-transformers/all-MiniLM-L6-v2"):
36
  self.tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -43,37 +37,15 @@ class CustomEmbeddingFunction:
43
  embeddings = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
44
  return embeddings
45
 
46
- # Initialize Sentiment Analysis Pipeline
47
  sentiment_pipeline = pipeline("sentiment-analysis")
48
-
49
- # Ensure no corrupted ChromaDB storage
50
- storage_path = r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\chromadb_storage" # Replace with the correct path
51
-
52
- try:
53
- # Initialize ChromaDB Client
54
- chroma_client = Client(Settings(persist_directory=storage_path))
55
- except ValueError as e:
56
- print(f"Error: {str(e)}")
57
- print("ChromaDB storage may be corrupted or the tenant might be missing. Resetting storage...")
58
-
59
- # Remove the existing storage path and recreate it
60
- shutil.rmtree(storage_path, ignore_errors=True)
61
- os.makedirs(storage_path)
62
-
63
- # Try reconnecting to ChromaDB after reset
64
- chroma_client = Client(Settings(persist_directory=storage_path))
65
-
66
- # Initialize Embedding Function
67
  embedding_fn = CustomEmbeddingFunction()
68
-
69
- # Define collection name
70
  collection_name = "crm_data"
71
 
72
- # Try fetching collection; create if missing
73
  try:
74
  collection = chroma_client.get_collection(collection_name)
75
- except Exception as e:
76
- print(f"Error fetching collection: {str(e)}. Creating new collection...")
77
  collection = chroma_client.create_collection(collection_name)
78
 
79
  def get_google_sheets_service():
@@ -111,11 +83,8 @@ def update_google_sheet(transcribed_text, sentiment,objection, recommendations,o
111
  st.error(f"Failed to update Google Sheets: {e}")
112
 
113
  load_dotenv()
114
- hf_token= os.getenv("HUGGINGFACE_TOKEN")
115
- login(token=hf_token)
116
- if not hf_token:
117
- raise ValueError("Hugging Face API key not found! Please set the HUGGINGFACE_TOKEN variable.")
118
- print(f"API Key Loaded: {hf_token[:5]}****")
119
 
120
  model_name = "tabularisai/multilingual-sentiment-analysis"
121
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
@@ -134,7 +103,7 @@ def analyze_sentiment(text):
134
 
135
  print(f"Sentiment Analysis Result: {result}")
136
 
137
-
138
  sentiment_map = {
139
  'Very Negative': "NEGATIVE",
140
  'Negative': "NEGATIVE",
@@ -208,28 +177,9 @@ def query_crm_data_with_context(prompt, top_k=3):
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,31 +189,10 @@ def load_objection_responses(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 the shape is (1,), reshape it to (1, expected_dim)
253
- if len(objection_embeddings.shape) == 1:
254
- # Reshape for a single embedding, turn it into a 2D array
255
- objection_embeddings = objection_embeddings.reshape(1, -1)
256
- elif len(objection_embeddings.shape) == 2:
257
- # Check if second dimension matches expected_dim
258
- if objection_embeddings.shape[1] != expected_dim:
259
- raise ValueError(f"Dimensionality of embeddings {objection_embeddings.shape[1]} does not match expected dimension {expected_dim}.")
260
- else:
261
- raise ValueError(f"Unexpected shape for objection embeddings: {objection_embeddings.shape}")
262
-
263
- # Add the embeddings to the Faiss index
264
- faiss_index.add(objection_embeddings)
265
-
266
- print(f"Successfully added {objection_embeddings.shape[0]} embeddings to the Faiss index.")
267
 
268
  def find_closest_objection(query):
269
  query_embedding = sentence_model.encode([query])
@@ -609,8 +538,6 @@ def generate_post_call_summary(sentiment_history, recommendations=[]):
609
 
610
  # Main
611
  def main():
612
-
613
- st.set_page_config(page_title="RealTime AI-Powered Sales Assistant", layout="wide")
614
  st.title("🤖 RealTime AI-Powered Sales Assistant For Enhanced Conversation")
615
  st.markdown(
616
  "An intelligent assistant to analyze speech, handle objections, and recommend products in real-time."
 
16
  from huggingface_hub import login
17
  import os
18
  from dotenv import load_dotenv
 
19
 
20
  SPREADSHEET_ID = "1CsBub3Jlwyo7WHMQty6SDnBShIZMjl5XTVSoOKrxZhc"
21
  RANGE_NAME = 'Sheet1!A1:E'
 
25
  csv_file_path = r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\900_products_dataset.csv"
26
 
27
 
 
 
 
 
 
28
  class CustomEmbeddingFunction:
29
  def __init__(self, model_name="sentence-transformers/all-MiniLM-L6-v2"):
30
  self.tokenizer = AutoTokenizer.from_pretrained(model_name)
 
37
  embeddings = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
38
  return embeddings
39
 
40
+ # Initialize components
41
  sentiment_pipeline = pipeline("sentiment-analysis")
42
+ chroma_client = Client(Settings(persist_directory="chromadb_storage"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  embedding_fn = CustomEmbeddingFunction()
 
 
44
  collection_name = "crm_data"
45
 
 
46
  try:
47
  collection = chroma_client.get_collection(collection_name)
48
+ except Exception:
 
49
  collection = chroma_client.create_collection(collection_name)
50
 
51
  def get_google_sheets_service():
 
83
  st.error(f"Failed to update Google Sheets: {e}")
84
 
85
  load_dotenv()
86
+ huggingface_api_key= os.getenv("HUGGINGFACE_TOKEN")
87
+ login(token=huggingface_api_key)
 
 
 
88
 
89
  model_name = "tabularisai/multilingual-sentiment-analysis"
90
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
103
 
104
  print(f"Sentiment Analysis Result: {result}")
105
 
106
+ # Map raw labels to sentiments
107
  sentiment_map = {
108
  'Very Negative': "NEGATIVE",
109
  'Negative': "NEGATIVE",
 
177
 
178
 
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
181
+ faiss_index = faiss.IndexFlatL2(384)
182
 
 
 
 
 
 
183
  def load_objection_responses(csv_file_path):
184
  try:
185
  df = pd.read_csv(csv_file_path)
 
189
  print(f"Error loading objections CSV: {e}")
190
  return {}
191
 
 
192
  objection_response_pairs = load_objection_responses(r"C:\Users\bhagy\OneDrive\Desktop\INFOSYS PROJECT\objections_responses.csv")
193
  objections = list(objection_response_pairs.keys())
194
  objection_embeddings = sentence_model.encode(objections)
195
+ faiss_index.add(np.array(objection_embeddings, dtype="float32"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
  def find_closest_objection(query):
198
  query_embedding = sentence_model.encode([query])
 
538
 
539
  # Main
540
  def main():
 
 
541
  st.title("🤖 RealTime AI-Powered Sales Assistant For Enhanced Conversation")
542
  st.markdown(
543
  "An intelligent assistant to analyze speech, handle objections, and recommend products in real-time."