-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
55 lines (36 loc) · 1.61 KB
/
server.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
import torch
from flask import Flask, render_template, request
from Configuration import Configuration
from Parsing.parser_utils import parse_args
from Prediction.Predictor import Predictor
from Training.NERCRFClassifier import NERCRFClassifier
app = Flask(__name__)
args, _ = parse_args()
conf = Configuration(args)
conf.show_parameters(["bert"])
models = ["saved_models/model.a.pt", "saved_models/model.b.pt"]
id2lab_group_a = {0: 'B-ACTI', 1: 'B-DISO', 2: 'B-DRUG', 3: 'B-SIGN', 4: 'I-ACTI', 5: 'I-DISO', 6: 'I-DRUG',
7: 'I-SIGN', 8: 'O'}
id2lab_group_b = {0: 'B-BODY', 1: 'B-TREA', 2: 'I-BODY', 3: 'I-TREA', 4: 'O'}
modelA = NERCRFClassifier(conf.bert, id2lab_group_a)
modelA.load_state_dict(torch.load(models[0], map_location=torch.device('cpu')))
modelB = NERCRFClassifier(conf.bert, id2lab_group_b)
modelB.load_state_dict(torch.load(models[1], map_location=torch.device('cpu')))
if conf.cuda:
modelA = modelA.to(conf.gpu)
modelB = modelB.to(conf.gpu)
predictor = Predictor(conf)
predictor.add_model("a", modelA, id2lab_group_a)
predictor.add_model("b", modelB, id2lab_group_b)
list_of_result = []
@app.route('/', methods=('GET', 'POST'))
def create():
if request.method == 'POST':
sentence = request.form['Sentence']
if "predict" in request.form and sentence != "":
tag_pred, mask = predictor.predict(sentence)
result_ = [*zip(sentence.split(), tag_pred, mask)]
list_of_result.append(result_)
elif "clear" in request.form:
list_of_result.clear()
return render_template('main.html', list_of_result=list_of_result)