Spaces:
Runtime error
Runtime error
refresh model
Browse files
recommendation-api/learner.py
CHANGED
@@ -1,25 +1,6 @@
|
|
1 |
from fastai.collab import load_learner
|
2 |
from fastai.tabular.all import *
|
3 |
|
4 |
-
def create_params(size):
|
5 |
-
return nn.Parameter(torch.zeros(*size).normal_(0, 0.01))
|
6 |
-
|
7 |
-
class DotProductBias(Module):
|
8 |
-
def __init__(self, n_users, n_items, n_factors, y_range=(0, 1.5)):
|
9 |
-
super().__init__()
|
10 |
-
self.user_factors = create_params([n_users, n_factors])
|
11 |
-
self.user_bias = create_params([n_users])
|
12 |
-
self.item_factors = create_params([n_items, n_factors])
|
13 |
-
self.item_bias = create_params([n_items])
|
14 |
-
self.y_range = y_range
|
15 |
-
|
16 |
-
def forward(self, x):
|
17 |
-
users = self.user_factors[x[:, 0]]
|
18 |
-
items = self.item_factors[x[:, 1]]
|
19 |
-
res = (users * items).sum(dim=1)
|
20 |
-
res += self.user_bias[x[:, 0]] + self.item_bias[x[:, 1]]
|
21 |
-
return sigmoid_range(res, *self.y_range)
|
22 |
-
|
23 |
def custom_accuracy(prediction, target):
|
24 |
# set all predictions above 0.95 as true positive (correct prediction)
|
25 |
prediction = torch.where(prediction > 0.95, torch.tensor(1.0), prediction)
|
|
|
1 |
from fastai.collab import load_learner
|
2 |
from fastai.tabular.all import *
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def custom_accuracy(prediction, target):
|
5 |
# set all predictions above 0.95 as true positive (correct prediction)
|
6 |
prediction = torch.where(prediction > 0.95, torch.tensor(1.0), prediction)
|
recommendation-api/model.pkl
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3ac0f088fb42f998622c2dbb6f9f060fa7fb9cfd0de1e66f876fa9aa95228ce
|
3 |
+
size 9239534
|
recommendation-api/recommender.py
CHANGED
@@ -5,6 +5,7 @@ from tracks import get_unlistened_tracks_for_user, predictions_to_tracks
|
|
5 |
|
6 |
def get_recommendations_for_user(learn: Learner, user_id: str, limit: int = 5):
|
7 |
not_listened_tracks = get_unlistened_tracks_for_user(user_id)
|
|
|
8 |
|
9 |
# Get predictions for the tracks user hasn't listened yet
|
10 |
input_dataframe = pd.DataFrame({'user_id': [user_id] * len(not_listened_tracks), 'entry': not_listened_tracks})
|
@@ -15,6 +16,8 @@ def get_recommendations_for_user(learn: Learner, user_id: str, limit: int = 5):
|
|
15 |
tracks_with_predictions = list(zip(not_listened_tracks, predictions[0].numpy()))
|
16 |
tracks_with_predictions.sort(key=lambda x: x[1], reverse=True)
|
17 |
|
|
|
|
|
18 |
# Pick n and return as full tracks
|
19 |
recommendations = predictions_to_tracks(tracks_with_predictions[:limit])
|
20 |
|
|
|
5 |
|
6 |
def get_recommendations_for_user(learn: Learner, user_id: str, limit: int = 5):
|
7 |
not_listened_tracks = get_unlistened_tracks_for_user(user_id)
|
8 |
+
print(len(not_listened_tracks))
|
9 |
|
10 |
# Get predictions for the tracks user hasn't listened yet
|
11 |
input_dataframe = pd.DataFrame({'user_id': [user_id] * len(not_listened_tracks), 'entry': not_listened_tracks})
|
|
|
16 |
tracks_with_predictions = list(zip(not_listened_tracks, predictions[0].numpy()))
|
17 |
tracks_with_predictions.sort(key=lambda x: x[1], reverse=True)
|
18 |
|
19 |
+
print(tracks_with_predictions[:limit])
|
20 |
+
|
21 |
# Pick n and return as full tracks
|
22 |
recommendations = predictions_to_tracks(tracks_with_predictions[:limit])
|
23 |
|