-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (67 loc) · 2.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask
from flask import request, jsonify
from flask import render_template
import pickle
import json
import requests
import sys
sys.path.append("./src")
import utils
import config
import numpy as np
app = Flask(__name__)
@app.route('/rater1', methods=['POST'])
def rater1():
data = request.get_json()
SERVER_ENDPOINT = 'http://localhost:8501/v1/models/rater1:predict'
essay = data['essay']
with open('input/tokenizer_essays.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
preprocessed_text = utils.preprocess(["dummy", essay],
tokenizer,
config.MAX_LEN)[1]
preprocessed_text = np.reshape(preprocessed_text, (1, 300))
payload = {
"instances": preprocessed_text.tolist()
}
r = requests.post(
SERVER_ENDPOINT,
json=payload)
return jsonify({'result': json.loads(r.content)}), 201
@app.route('/rater2', methods=['POST'])
def rater2():
data = request.get_json()
SERVER_ENDPOINT = 'http://localhost:8501/v1/models/rater2:predict'
essay = data['essay']
with open('input/tokenizer_essays.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
preprocessed_text = utils.preprocess(["dummy", essay],
tokenizer,
config.MAX_LEN)[1]
preprocessed_text = np.reshape(preprocessed_text, (1, 300))
payload = {
"instances": preprocessed_text.tolist()
}
r = requests.post(
SERVER_ENDPOINT,
json=payload)
return jsonify({'result': json.loads(r.content)}), 201
@app.route('/rater', methods=['POST'])
def rater():
data = request.form['essay']
SERVER_ENDPOINT1 = 'http://0.0.0.0:8080/rater1'
r1 = requests.post(
SERVER_ENDPOINT1,
json={'essay': data})
score1 = json.loads(r1.text)['result']['predictions'][0][0]
SERVER_ENDPOINT2 = 'http://0.0.0.0:8080/rater2'
r2 = requests.post(
SERVER_ENDPOINT2,
json={'essay': data})
score2 = json.loads(r2.text)['result']['predictions'][0][0]
return render_template('index.html', rater_score=f'Rater 1 scored {round(score1*10, 1)} and Rater 2 scored {round(score2*10, 1)} ')
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)