capradeepgujaran commited on
Commit
da49ff7
1 Parent(s): 0d8d4ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -0
app.py CHANGED
@@ -137,6 +137,19 @@ def process_upload(api_key, files, lang):
137
  else:
138
  return f"No valid documents were indexed. Errors: {'; '.join(error_messages)}", None
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # This is the missing query_app function that needs to be defined
141
  def query_app(query, model_name, use_similarity_check, openai_api_key):
142
  global vector_index, query_log
 
137
  else:
138
  return f"No valid documents were indexed. Errors: {'; '.join(error_messages)}", None
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):
155
  global vector_index, query_log