Spaces:
Runtime error
Runtime error
Try out new version
Browse files- data/model.csv +2 -2
- data/model.pkl +2 -2
- recommendation-api/learner.py +30 -15
- recommendation-api/server.py +1 -1
- recommendation-api/tracks.py +1 -1
data/model.csv
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:88108b9beaa9863b308ef27805b819cfcc37a14c4c491488a3de9930315a0468
|
3 |
+
size 3561158
|
data/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:fbcaffee79f965e3f8eb2820665bf5a926e317a1b1f8401f50a43ff979914f9c
|
3 |
+
size 5883182
|
recommendation-api/learner.py
CHANGED
@@ -1,16 +1,31 @@
|
|
1 |
-
from fastai.collab import load_learner
|
2 |
-
from fastai.tabular.all import *
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return learn
|
|
|
1 |
+
from fastai.collab import load_learner
|
2 |
+
from fastai.tabular.all import *
|
3 |
+
|
4 |
+
class DotProductBias(Module):
|
5 |
+
def __init__(self, n_users, n_movies, n_factors, y_range=(0,1.1)):
|
6 |
+
self.user_factors = Embedding(n_users, n_factors)
|
7 |
+
self.user_bias = Embedding(n_users, 1)
|
8 |
+
self.movie_factors = Embedding(n_movies, n_factors)
|
9 |
+
self.movie_bias = Embedding(n_movies, 1)
|
10 |
+
self.y_range = y_range
|
11 |
+
|
12 |
+
def forward(self, x):
|
13 |
+
users = self.user_factors(x[:,0])
|
14 |
+
movies = self.movie_factors(x[:,1])
|
15 |
+
res = (users * movies).sum(dim=1, keepdim=True)
|
16 |
+
res += self.user_bias(x[:,0]) + self.movie_bias(x[:,1])
|
17 |
+
return sigmoid_range(res, *self.y_range)
|
18 |
+
|
19 |
+
def custom_accuracy(prediction, target):
|
20 |
+
# set all predictions above 0.95 as true positive (correct prediction)
|
21 |
+
prediction = torch.where(prediction > 0.95, torch.tensor(1.0), prediction)
|
22 |
+
# shape [64, 1] to [64]
|
23 |
+
target = target.squeeze(1)
|
24 |
+
correct = (prediction == target).float()
|
25 |
+
accuracy = correct.sum() / len(target)
|
26 |
+
return accuracy
|
27 |
+
|
28 |
+
async def setup_learner(model_filename: str):
|
29 |
+
learn = load_learner(model_filename)
|
30 |
+
learn.dls.device = 'cpu'
|
31 |
return learn
|
recommendation-api/server.py
CHANGED
@@ -7,7 +7,7 @@ from tracks import get_top_tracks_for_user, get_users_with_track_interactions
|
|
7 |
from recommender import get_recommendations_for_user
|
8 |
|
9 |
# custom_accuracy needs to be imported to the global namespace for Learner to load
|
10 |
-
from learner import setup_learner, custom_accuracy
|
11 |
|
12 |
app = FastAPI()
|
13 |
model_filename = 'data/model.pkl'
|
|
|
7 |
from recommender import get_recommendations_for_user
|
8 |
|
9 |
# custom_accuracy needs to be imported to the global namespace for Learner to load
|
10 |
+
from learner import setup_learner, custom_accuracy, DotProductBias
|
11 |
|
12 |
app = FastAPI()
|
13 |
model_filename = 'data/model.pkl'
|
recommendation-api/tracks.py
CHANGED
@@ -50,6 +50,6 @@ def predictions_to_tracks(entries_and_predictions):
|
|
50 |
track_info = tracks_df[tracks_df['entry'] == entry]
|
51 |
if not track_info.empty:
|
52 |
track_dict = track_info.to_dict('records')[0]
|
53 |
-
track_dict['score'] = score.astype(str)
|
54 |
tracks.append(track_dict)
|
55 |
return tracks
|
|
|
50 |
track_info = tracks_df[tracks_df['entry'] == entry]
|
51 |
if not track_info.empty:
|
52 |
track_dict = track_info.to_dict('records')[0]
|
53 |
+
track_dict['score'] = score[0].astype(str)
|
54 |
tracks.append(track_dict)
|
55 |
return tracks
|