-
Notifications
You must be signed in to change notification settings - Fork 164
/
web.py
40 lines (31 loc) · 1002 Bytes
/
web.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
from flask.ext.api import FlaskAPI
from flask import request, current_app, abort
from functools import wraps
app = FlaskAPI(__name__)
app.config.from_object('settings')
def token_auth(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if request.headers.get('X-API-TOKEN', None) != current_app.config['API_TOKEN']:
abort(403)
return f(*args, **kwargs)
return decorated_function
@app.route('/predict', methods=['POST'])
@token_auth
def predict():
from engines import content_engine
item = request.data.get('item')
num_predictions = request.data.get('num', 10)
if not item:
return []
return content_engine.predict(str(item), num_predictions)
@app.route('/train')
@token_auth
def train():
from engines import content_engine
data_url = request.data.get('data-url', None)
content_engine.train(data_url)
return {"message": "Success!", "success": 1}
if __name__ == '__main__':
app.debug = True
app.run()