-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
84 lines (70 loc) · 3.06 KB
/
api.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
from flask import Flask
from flask import request
from flask import jsonify
import datetime
import hashlib
import numpy as np
import pandas as pd
app = Flask(__name__)
####### PUT YOUR INFORMATION HERE #######
CAPTAIN_EMAIL = 'my@gmail.com' #
SALT = 'my_salt' #
#########################################
def generate_server_uuid(input_string):
""" Create your own server_uuid
@param input_string (str): information to be encoded as server_uuid
@returns server_uuid (str): your unique server_uuid
"""
s = hashlib.sha256()
data = (input_string+SALT).encode("utf-8")
s.update(data)
server_uuid = s.hexdigest()
return server_uuid
def predict(article):
""" Predict your model result
@param article (str): a news article
@returns prediction (list): a list of name
"""
####### PUT YOUR MODEL INFERENCING CODE HERE #######
prediction = ['aha','danny','jack']
####################################################
prediction = _check_datatype_to_list(prediction)
return prediction
def _check_datatype_to_list(prediction):
""" Check if your prediction is in list type or not.
And then convert your prediction to list type or raise error.
@param prediction (list / numpy array / pandas DataFrame): your prediction
@returns prediction (list): your prediction in list type
"""
if isinstance(prediction, np.ndarray):
_check_datatype_to_list(prediction.tolist())
elif isinstance(prediction, pd.core.frame.DataFrame):
_check_datatype_to_list(prediction.values)
elif isinstance(prediction, list):
return prediction
raise ValueError('Prediction is not in list type.')
@app.route('/healthcheck', methods=['POST'])
def healthcheck():
""" API for health check """
data = request.get_json(force=True)
t = datetime.datetime.now()
ts = str(int(t.utcnow().timestamp()))
server_uuid = generate_server_uuid(CAPTAIN_EMAIL+ts)
server_timestamp = t.strftime("%Y-%m-%d %H:%M:%S")
return jsonify({'esun_uuid': data['esun_uuid'], 'server_uuid': server_uuid, 'captain_email': CAPTAIN_EMAIL, 'server_timestamp': server_timestamp})
@app.route('/inference', methods=['POST'])
def inference():
""" API that return your model predictions when E.SUN calls this API """
data = request.get_json(force=True)
esun_timestamp = data['esun_timestamp'] #自行取用
t = datetime.datetime.now()
ts = str(int(t.utcnow().timestamp()))
server_uuid = generate_server_uuid(CAPTAIN_EMAIL+ts)
try:
answer = predict(data['news'])
except:
raise ValueError('Model error.')
server_timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return jsonify({'esun_timestamp': data['esun_timestamp'], 'server_uuid': server_uuid, 'answer': answer, 'server_timestamp': server_timestamp, 'esun_uuid': data['esun_uuid']})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)