Update app.py
Browse files
app.py
CHANGED
|
@@ -85,19 +85,23 @@ def encode_passages(texts):
|
|
| 85 |
PASSAGE_EMBS=encode_passages(CORPUS)
|
| 86 |
|
| 87 |
|
| 88 |
-
def retrieve_top_k(query,k=3):
|
| 89 |
|
| 90 |
-
qv=encode_queries([query])[0]
|
| 91 |
-
sims=np.dot(PASSAGE_EMBS,qv)
|
| 92 |
|
| 93 |
-
idxs=np.argsort(-sims)[:k]
|
| 94 |
|
| 95 |
-
results=[]
|
| 96 |
-
for rank,i in enumerate(idxs):
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
return results
|
| 103 |
|
|
@@ -117,21 +121,33 @@ def answer_with_context(question,context):
|
|
| 117 |
return {"answer":out["answer"],"score":float(out["score"])}
|
| 118 |
|
| 119 |
|
| 120 |
-
def no_context_flow(question,top_k=3):
|
| 121 |
|
| 122 |
-
cands=retrieve_top_k(question,k=top_k)
|
| 123 |
|
| 124 |
-
best={"answer":"","score":-1,"used_context":""}
|
| 125 |
|
| 126 |
for c in cands:
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
|
|
@@ -209,7 +225,7 @@ def evaluate_answer(question):
|
|
| 209 |
|
| 210 |
gold=row.iloc[0]["answer_text"]
|
| 211 |
|
| 212 |
-
result=no_context_flow(question)
|
| 213 |
|
| 214 |
pred=result["answer"]
|
| 215 |
|
|
|
|
| 85 |
PASSAGE_EMBS=encode_passages(CORPUS)
|
| 86 |
|
| 87 |
|
| 88 |
+
def retrieve_top_k(query, k=3):
|
| 89 |
|
| 90 |
+
qv = encode_queries([query])[0]
|
| 91 |
+
sims = np.dot(PASSAGE_EMBS, qv)
|
| 92 |
|
| 93 |
+
idxs = np.argsort(-sims)[:k]
|
| 94 |
|
| 95 |
+
results = []
|
|
|
|
| 96 |
|
| 97 |
+
for rank, i in enumerate(idxs):
|
| 98 |
+
|
| 99 |
+
results.append({
|
| 100 |
+
"rank": rank + 1,
|
| 101 |
+
"similarity": float(sims[i]),
|
| 102 |
+
"context": CORPUS[i],
|
| 103 |
+
"language": df.iloc[i]["language"]
|
| 104 |
+
})
|
| 105 |
|
| 106 |
return results
|
| 107 |
|
|
|
|
| 121 |
return {"answer":out["answer"],"score":float(out["score"])}
|
| 122 |
|
| 123 |
|
| 124 |
+
def no_context_flow(question, lang_choice, top_k=3):
|
| 125 |
|
| 126 |
+
cands = retrieve_top_k(question, k=top_k)
|
| 127 |
|
| 128 |
+
best = {"answer": "", "score": -1, "used_context": ""}
|
| 129 |
|
| 130 |
for c in cands:
|
| 131 |
|
| 132 |
+
# skip passages of other languages
|
| 133 |
+
if c["language"] != lang_choice[:2].lower():
|
| 134 |
+
continue
|
| 135 |
|
| 136 |
+
out = answer_with_context(question, c["context"])
|
| 137 |
|
| 138 |
+
if out["score"] > best["score"]:
|
| 139 |
+
best = {
|
| 140 |
+
"answer": out["answer"],
|
| 141 |
+
"score": out["score"],
|
| 142 |
+
"used_context": c["context"]
|
| 143 |
+
}
|
| 144 |
|
| 145 |
+
return {
|
| 146 |
+
"answer": best["answer"],
|
| 147 |
+
"score": best["score"],
|
| 148 |
+
"used_context": best["used_context"],
|
| 149 |
+
"retrieved": cands
|
| 150 |
+
}
|
| 151 |
|
| 152 |
|
| 153 |
|
|
|
|
| 225 |
|
| 226 |
gold=row.iloc[0]["answer_text"]
|
| 227 |
|
| 228 |
+
result=no_context_flow(question,"kn",3)
|
| 229 |
|
| 230 |
pred=result["answer"]
|
| 231 |
|