-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.py
38 lines (31 loc) · 1.34 KB
/
helpers.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
from flask import request, jsonify
from hybrid.hybrid import calcRecommendations, getNotes
from soundcloud.soundcloud import getSongs
def getResponse():
weights = getHTTPParamArray("weights")
context = getHTTPParamArray("context")
if (len(context) != 8 or len(weights) != 3):
response = {"error":"context should consist exactly out of 8 features, weights should be exactly 3"}
else:
recommendations = calcRecommendations(weights, context)
response = formResponse(recommendations)
return jsonify(response)
def getHTTPParamArray(param):
return list(map(float, request.args.get(param).split(',')))
def getRecommendationOutput(name, recommendation, flag):
return {
"name":name,
"recommendation": getNotes(recommendation.tolist()),
"is_reliable":flag
}
def formResponse(recs):
(ind_recommendations,hybrid, flags) = recs
response = {"recommenders":[], "songs":[]}
# response for individual recommenders
for i, recommendation in enumerate(ind_recommendations):
response["recommenders"].append(getRecommendationOutput("recommender_"+str(i+1),recommendation,flags[i]))
# responses for hybrid recommender
response["recommenders"].append(getRecommendationOutput("hybrid",hybrid,1))
# responses with recommended tracks
response["songs"] = getSongs(hybrid.tolist())
return response