-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
38 lines (32 loc) · 1.38 KB
/
demo.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
import flask
from flask import Flask
from flask import render_template, request
import re
import os
import interactive_index
import sys
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
APP_ROOT = os.environ.get("APP_ROOT","")
app.config["APPLICATION_ROOT"] = APP_ROOT
sbert_tokenizer_name = os.environ.get("SBERT_TOKENIZER","TurkuNLP/bert-base-finnish-cased-v1")
sbert_model_name = os.environ.get("SBERT_MODEL","/scratch/project_2000539/pb_faiss/sbert-cased-finnish-paraphrase")
mmap_sentence_filename = os.environ.get("MMAP_SFILENAME","/scratch/project_2000539/pb_faiss/all_data_pos_uniq")
faiss_index_fname = os.environ.get("FAISS_IDX_FILENAME","/scratch/project_2000539/pb_faiss/faiss_index_filled_sbert.faiss")
nn_qry=interactive_index.IDemoSBert(sbert_tokenizer_name,sbert_model_name,faiss_index_fname,mmap_sentence_filename)
nn_qry.knn(["Minulla on koira"])
print("Done loading",file=sys.stderr,flush=True)
@app.route("/")
def index():
return render_template("index.html",app_root=APP_ROOT)
@app.route("/predict",methods=["POST"])
def predict():
global nn_qry
inpsentence=request.json["sentencein"].strip()
print("INP",inpsentence,file=sys.stderr,flush=True)
res=nn_qry.knn([inpsentence])
nearest=[]
for sent,hits in res:
for score,h in hits:
nearest.append(h)
return {"predictions_html":render_template("result.html",knns=nearest)}