-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (46 loc) · 1.27 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
import os
import sys
import logging
from flask import Flask, request, jsonify
from flask_cors import CORS
from serve import get_model_api
# define the app
app = Flask(__name__)
CORS(app) # needed for cross-domain requests, allow everything by default
# load the model
model_api = get_model_api()
# API route
@app.route('/hw/mathreco', methods=['POST'])
def mathreco():
"""API function
All model-specific logic to be defined in the get_model_api()
function
"""
input_data = request.json
app.logger.debug("api_input: " + str(input_data))
output_data = model_api(input_data)
app.logger.debug("api_output: " + str(output_data))
response = jsonify(output_data)
return response
@app.route('/')
def index():
return "Index API"
# HTTP Errors handlers
@app.errorhandler(404)
def url_error(e):
return """
Wrong URL!
<pre>{}</pre>""".format(e), 404
@app.errorhandler(500)
def server_error(e):
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
try:
mathreco_port=int(os.environ["MATHRECO_PORT"])
except:
mathreco_port=8080;
# This is used when running locally.
app.run(host='0.0.0.0',port=mathreco_port, debug=True)