jrno commited on
Commit
aad08d3
1 Parent(s): 43b611c

Try out new version

Browse files
data/model.csv CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:1b511f072d4b52c995e69aebc2b3f10fff7aa5a860d4c75d8fc7d47ef9194a6d
3
- size 10292501
 
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:fdc02a0cbeace0ced62cac81640773c36e1325ea59fd85afd12037100d1d1187
3
- size 7986862
 
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
- 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)
7
- # shape [64, 1] to [64]
8
- target = target.squeeze(1)
9
- correct = (prediction == target).float()
10
- accuracy = correct.sum() / len(target)
11
- return accuracy
12
-
13
- async def setup_learner(model_filename: str):
14
- learn = load_learner(model_filename)
15
- learn.dls.device = 'cpu'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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