-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (49 loc) · 1.93 KB
/
app.py
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
from src import Index, Retrieval, ReRanking
from flask import Flask, request, flash, render_template
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(32)
index = Index()
retriever = Retrieval(index)
reranker = ReRanking(retriever)
@app.get('/retrieve/<string:qexp>')
def retrieve(qexp) -> str:
"""retrieve search results using either rm3 or splade query expansion"""
num = request.args.get("num")
query = request.args.get("q")
if query is None:
return "invalid or empty query", 400
response: str = retriever.search(query, num, qexp).to_json(orient="index")
return response, 200
@app.get('/search/<string:qexp>')
def rerank(qexp) -> str:
"""retrieve (bm25 + rm3) and rerank (monoT5) search results"""
num = request.args.get("num")
query = request.args.get("q")
if query is None:
return "invalid or empty query", 400
response: str = reranker.search(query, num, qexp).to_json(orient="index")
return response, 200
@app.route('/', methods=("GET", "POST"))
def search():
"""search engine with a GUI"""
if request.method == "POST":
query = request.form["query"]
model = request.form["model"]
if not query or not model:
flash("Query and Model is required.")
else:
if model == "bm25_rm3":
results = retriever.search(query, 100, "rm3")
elif model == "bm25_splade":
results = retriever.search(query, 100, "splade")
elif model == "bm25_rm3_t5":
results = reranker.search(query, 100, "rm3")
elif model == "bm25_splade_t5":
results = reranker.search(query, 100, "splade")
else:
flash('invalid model')
return render_template("search.html", results=results.to_dict(orient="records"))
return render_template("search.html", results=None)
if __name__ == "__main__":
app.run(port=8000)