avacaondata commited on
Commit
fa00011
1 Parent(s): 05aebdd

añadida mejora en el ranker

Browse files
Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -97,7 +97,7 @@ def query_index(question: str):
97
  context for context in contexts if len(context.split()) > min_snippet_length
98
  ]
99
 
100
-
101
  def sort_on_similarity(question, contexts, include_rank: int = 5):
102
  # TODO: METER AQUÍ EL CROSSENCODER nuestro
103
  question_encoded = similarity_model.encode([question])[0]
@@ -107,6 +107,19 @@ def sort_on_similarity(question, contexts, include_rank: int = 5):
107
  ]
108
  similarity_ranking_idx = np.flip(np.argsort(similarity_scores))
109
  return [contexts[idx] for idx in similarity_ranking_idx][:include_rank]
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
 
112
  def create_context(contexts: List):
 
97
  context for context in contexts if len(context.split()) > min_snippet_length
98
  ]
99
 
100
+ """
101
  def sort_on_similarity(question, contexts, include_rank: int = 5):
102
  # TODO: METER AQUÍ EL CROSSENCODER nuestro
103
  question_encoded = similarity_model.encode([question])[0]
 
107
  ]
108
  similarity_ranking_idx = np.flip(np.argsort(similarity_scores))
109
  return [contexts[idx] for idx in similarity_ranking_idx][:include_rank]
110
+ """
111
+
112
+ def sort_on_similarity(question, contexts, include_rank: int = 5):
113
+ question_encoded = similarity_model.encode([question])[0]
114
+ ctxs_encoded = similarity_model.encode(contexts)
115
+ sim_scores_ss = [
116
+ util.cos_sim(question_encoded, ctx_encoded) for ctx_encoded in ctxs_encoded
117
+ ]
118
+ text_pairs = [[question, ctx] for ctx in contexts]
119
+ similarity_scores = crossencoder.predict(text_pairs)
120
+ similarity_scores = sim_scores_ss * similarity_scores
121
+ similarity_ranking_idx = np.flip(np.argsort(similarity_scores))
122
+ return [contexts[idx] for idx in similarity_ranking_idx][:include_rank]
123
 
124
 
125
  def create_context(contexts: List):