-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (44 loc) · 1.93 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
import os
import datetime
import pickle
import pandas as pd
from flask import Flask, request, jsonify
app = Flask(__name__)
FOLDER = 'data'
with open('./content_based_recsys.pkl', 'rb') as f:
model = pickle.load(f)
train_df = pd.read_csv(os.path.join(FOLDER, 'train_df.csv'))
train_df[['contentId', 'personId']] = train_df[['contentId', 'personId']].astype(str)
items_df = pd.read_csv(os.path.join(FOLDER, 'shared_articles.csv'))
items_df['contentId'] = items_df['contentId'].astype(str)
def predict(user_id, topn):
interacted_items = train_df[train_df['personId']==user_id]['contentId']
itemid_to_ignore = set(interacted_items.values)
recommend_df = model.recommend_items(user_id, itemid_to_ignore, topn=10000, verbose=False)
itemid_to_recommend = set(recommend_df['contentId'].tolist())
itemname_to_ignore = items_df[items_df['contentId'].isin(itemid_to_ignore)]['title'].tolist()
itemname_to_recommend = items_df[items_df['contentId'].isin(itemid_to_recommend)]['title'].tolist()
return itemname_to_ignore, itemname_to_recommend
@app.route('/inference', methods=['POST'])
def inference():
params = request.get_json(force=True)
# request parameters
user_id = str(params['userid'])
topn = params['topn']
result = {
'user_id': user_id,
'topn': topn
}
try:
itemname_to_ignore = predict(user_id, topn)
itemname_to_ignore, itemid_to_recommend = predict(user_id, topn)
result['articles_the_user_interacted'] = itemname_to_ignore
result['articles_recommend_to_user'] = itemname_to_ignore
result['status'] = 'success'
except Exception as e:
result['message'] = str(e)
result['status'] = 'failed'
result['timestamp'] = str(datetime.datetime.now())
return jsonify(result)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int("5000"), debug=True) #map port to 5000