-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstandalone.py
More file actions
65 lines (49 loc) · 1.72 KB
/
Copy pathstandalone.py
File metadata and controls
65 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from functools import lru_cache
import math
from colbert import Searcher
from flask import Flask, request
INDEX_NAME = "wiki17.nbits.local"
INDEX_ROOT = "<path/to/root/of/INDEX_NAME>"
# INDEX_ROOT = "/Users/niels.van.Galen.last/code/hola-dspy/experiments/notebook/indexes"
COLLECTION_PATH = "/path/to/wiki.abstracts.2017/collection.tsv"
# COLLECTION_PATH = (
# "/Users/niels.van.Galen.last/code/hola-dspy/data/wiki/wiki.abstracts.2017/collection.tsv"
# )
app = Flask(__name__)
searcher = Searcher(
index=INDEX_NAME,
checkpoint="colbert-ir/colbertv2.0",
collection=COLLECTION_PATH,
index_root=INDEX_ROOT,
)
counter = {"api": 0}
@lru_cache(maxsize=1000000)
def api_search_query(query, k: int = 10):
print(f"Query={query}")
if k <= 0:
k = 10
k = min(int(k), 100)
pids, ranks, scores = searcher.search(query, k=100)
pids, ranks, scores = pids[:k], ranks[:k], scores[:k]
probs = [math.exp(score) for score in scores]
probs = [prob / sum(probs) for prob in probs]
topk = []
for pid, rank, score, prob in zip(pids, ranks, scores, probs):
text = searcher.collection[pid]
d = {"text": text, "pid": pid, "rank": rank, "score": score, "prob": prob}
topk.append(d)
topk = list(sorted(topk, key=lambda p: (-1 * p["score"], p["pid"])))
return {"query": query, "topk": topk}
@app.route("/api/search", methods=["GET"])
def api_search():
if request.method != "GET":
return ("", 405)
counter["api"] += 1
print("API request count:", counter["api"])
try:
k = int(request.args.get("k", 10))
except ValueError:
k = 10
return api_search_query(request.args.get("query"), k)
if __name__ == "__main__":
app.run("0.0.0.0", 8893)