-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
34 lines (26 loc) · 941 Bytes
/
server.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
from flask import Flask, request, jsonify
from item_knn import ItemKnnRecommendAlgorithm
from popularity import PopularityBasedRecommendAlgorithm
import pandas as pd
app = Flask(__name__)
models = {
"popularity": PopularityBasedRecommendAlgorithm(),
"item_knn": ItemKnnRecommendAlgorithm()
}
@app.route("/model/<model_name>/test", methods=['POST'])
def model_test(model_name):
if model_name in models:
recommendations = models[model_name].recommend_items(
request.json['session'], pd.read_json(request.json['source_items']), request.json['k']
)
# fix int64 not serializable
new_r = []
for r in recommendations:
new_r.append(int(r))
return jsonify(new_r)
else:
return jsonify([])
if __name__ == '__main__':
for name in models:
models[name].load_model()
app.run(host='127.0.0.1', port=6789)