-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (45 loc) · 1.31 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
import os
import sys
import logging
from flask import Flask, request, jsonify
from flask_cors import CORS
# from TemplateQA.main import evaluate_line
from serve import get_model_api
app = Flask(__name__)
CORS(app) # needed for cross-domain requests, allow everything by default
# logging for heroku
if 'DYNO' in os.environ:
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.INFO)
# API route
@app.route('/api', methods=['POST'])
def api():
"""API function
All model-specific logic to be defined in the get_model_api()
function
"""
input_data = request.json
app.logger.info("api_input: " + str(input_data))
# output_data = evaluate_line(input_data)
output_data = get_model_api(input_data)
app.logger.info("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__':
# This is used when running locally.
app.run(host='127.0.0.1', debug=True)