Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -126,23 +126,37 @@ def predict(text):
|
|
| 126 |
return "No query provided"
|
| 127 |
|
| 128 |
query_embedding = model.encode(text, convert_to_tensor=True)
|
| 129 |
-
top_idx = int(util.pytorch_cos_sim(query_embedding, embeddings)[0].argmax())
|
| 130 |
-
top_idx2 = int(util.pytorch_cos_sim(query_embedding, embeddings2)[0].argmax())
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
return
|
| 141 |
-
|
| 142 |
-
"link1": df.iloc[top_idx]["link"],
|
| 143 |
-
"question2": df2.iloc[top_idx2]["question"],
|
| 144 |
-
"link2": df2.iloc[top_idx2]["link"]
|
| 145 |
-
}
|
| 146 |
|
| 147 |
# Match the EXACT structure of your working translation app
|
| 148 |
title = "Search CSV"
|
|
|
|
| 126 |
return "No query provided"
|
| 127 |
|
| 128 |
query_embedding = model.encode(text, convert_to_tensor=True)
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Compute similarity scores
|
| 131 |
+
sim_scores1 = util.pytorch_cos_sim(query_embedding, embeddings)[0]
|
| 132 |
+
sim_scores2 = util.pytorch_cos_sim(query_embedding, embeddings2)[0]
|
| 133 |
+
|
| 134 |
+
# Get top 3 indices
|
| 135 |
+
top3_idx1 = sim_scores1.topk(3).indices.cpu().numpy()
|
| 136 |
+
top3_idx2 = sim_scores2.topk(3).indices.cpu().numpy()
|
| 137 |
+
|
| 138 |
+
# Prepare results
|
| 139 |
+
results = {
|
| 140 |
+
"top1": [],
|
| 141 |
+
"top2": []
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
for idx in top3_idx1:
|
| 145 |
+
results["top1"].append({
|
| 146 |
+
"question": df.iloc[idx]["question"],
|
| 147 |
+
"link": df.iloc[idx]["link"],
|
| 148 |
+
"score": float(sim_scores1[idx])
|
| 149 |
+
})
|
| 150 |
|
| 151 |
+
for idx in top3_idx2:
|
| 152 |
+
results["top2"].append({
|
| 153 |
+
"question": df2.iloc[idx]["question"],
|
| 154 |
+
"link": df2.iloc[idx]["link"],
|
| 155 |
+
"score": float(sim_scores2[idx])
|
| 156 |
+
})
|
| 157 |
|
| 158 |
+
return results
|
| 159 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Match the EXACT structure of your working translation app
|
| 162 |
title = "Search CSV"
|