capradeepgujaran commited on
Commit
a7d59e2
1 Parent(s): da49ff7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -139,16 +139,26 @@ def process_upload(api_key, files, lang):
139
 
140
  # Define the calculate_similarity function
141
  def calculate_similarity(response, ground_truth):
 
142
  response_embedding = sentence_model.encode(response, convert_to_tensor=True)
143
  truth_embedding = sentence_model.encode(ground_truth, convert_to_tensor=True)
144
-
145
- # Normalize the embeddings
146
- response_embedding = response_embedding / response_embedding.norm(p=2)
147
- truth_embedding = truth_embedding / truth_embedding.norm(p=2)
148
-
149
- # Calculate cosine similarity using sklearn's cosine_similarity function
150
- similarity = cosine_similarity(response_embedding.reshape(1, -1), truth_embedding.reshape(1, -1))[0][0]
151
- return similarity * 100 # Convert to percentage
 
 
 
 
 
 
 
 
 
152
 
153
  # This is the missing query_app function that needs to be defined
154
  def query_app(query, model_name, use_similarity_check, openai_api_key):
 
139
 
140
  # Define the calculate_similarity function
141
  def calculate_similarity(response, ground_truth):
142
+ # Encode the response and ground truth
143
  response_embedding = sentence_model.encode(response, convert_to_tensor=True)
144
  truth_embedding = sentence_model.encode(ground_truth, convert_to_tensor=True)
145
+
146
+ # Convert embeddings to numpy arrays for easier manipulation
147
+ response_embedding = response_embedding.cpu().numpy()
148
+ truth_embedding = truth_embedding.cpu().numpy()
149
+
150
+ # Normalize the embeddings to unit vectors (magnitude of 1)
151
+ response_embedding = response_embedding / np.linalg.norm(response_embedding)
152
+ truth_embedding = truth_embedding / np.linalg.norm(truth_embedding)
153
+
154
+ # Calculate cosine similarity using numpy's dot product
155
+ similarity = np.dot(response_embedding, truth_embedding)
156
+
157
+ # Return similarity as a percentage (between 0 and 100)
158
+ similarity_percentage = (similarity + 1) / 2 * 100 # Normalize from [-1, 1] to [0, 100]
159
+
160
+ return similarity_percentage
161
+
162
 
163
  # This is the missing query_app function that needs to be defined
164
  def query_app(query, model_name, use_similarity_check, openai_api_key):