AlGouvea commited on
Commit
0648e58
·
1 Parent(s): cdde303

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -65
app.py CHANGED
@@ -1,86 +1,87 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import torch
4
- import torch.nn as nn
5
- from torch.nn import Embedding
6
- from torch.nn.modules.module import Module
7
- import torch.nn.functional as F
8
- from fastai.data.core import DataLoaders
9
- from fastai.collab import CollabDataLoaders
10
 
11
- # Define your CollabNN model class
 
 
 
 
 
 
 
 
 
 
 
 
12
  class CollabNN(Module):
13
- def __init__(self, user_sz, item_sz, y_range=(0, 10.5), n_act=150):
14
  self.user_factors = Embedding(*user_sz)
15
  self.item_factors = Embedding(*item_sz)
16
  self.layers = nn.Sequential(
17
- nn.Linear(user_sz[1] + item_sz[1], n_act),
18
  nn.ReLU(),
19
  nn.Linear(n_act, 1))
20
  self.y_range = y_range
21
-
22
  def forward(self, x):
23
- embs = self.user_factors(x[:, 0]), self.item_factors(x[:, 1])
24
  x = self.layers(torch.cat(embs, dim=1))
25
- return torch.sigmoid(x) * (self.y_range[1] - self.y_range[0]) + self.y_range[0]
26
-
27
- # Load your trained model and provide the correct path
28
- model_path = "model.pkl"
29
- model = torch.load(model_path, map_location=torch.device('cpu'))
30
- model.eval()
31
-
32
- # Load the book dataset
33
- books_path = "BX_Books.csv"
34
- books_df = pd.read_csv(books_path, encoding="latin-1", delimiter=';', usecols=(0,1))
35
-
36
- # Load the book ratings dataset
37
- ratings_path = "BX_Book_Ratings.csv"
38
- ratings_df = pd.read_csv(ratings_path, encoding="latin-1", delimiter=';', usecols=(0,1,2))
39
-
40
- # Merge the ratings and books DataFrames
41
- ratings = ratings_df.merge(books_df, on='ISBN')
42
-
43
- print(ratings_df.columns)
44
- print(books_df.columns)
45
- print(ratings.columns)
46
-
47
- # Create DataLoaders for collaborative filtering
48
- dls = CollabDataLoaders.from_df(ratings, item_name='Book-Title', bs=8192)
49
 
50
- # Define a function to recommend books for a given user
51
- def recommend_books(user_id):
52
- # Filter ratings for the given user
53
- user_ratings = ratings[ratings['User-ID'] == user_id]
54
 
55
- # Create a list of books the user has already rated
56
- rated_books = user_ratings['Book-Title'].tolist()
57
-
58
- # Create a list of books the user has not rated
59
- unrated_books = books_df[~books_df['Book-Title'].isin(rated_books)]
60
-
61
- # Get the titles of unrated books
62
- unrated_titles = unrated_books['Book-Title'].tolist()
63
-
64
- # Create input tensor for the model
65
- user_input = torch.tensor([[int(user_id), title] for title in unrated_titles])
66
-
67
- # Make predictions using the model
68
- predictions = model(user_input).detach().numpy().flatten()
69
-
70
- # Get indices of top 3 predictions
71
- top_indices = predictions.argsort()[-3:][::-1]
72
-
73
- # Get titles of the top 3 recommended books
74
- top_titles = [unrated_titles[i] for i in top_indices]
75
-
76
- return top_titles
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # Create a Gradio interface
79
  iface = gr.Interface(
80
- fn=recommend_books,
81
- inputs="text",
82
- outputs="text",
83
- live=True
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
  # Launch the Gradio app
 
1
  import gradio as gr
2
  import pandas as pd
3
  import torch
 
 
 
 
 
 
4
 
5
+
6
+ ratings = pd.read_csv('BX-Book-Ratings.csv', encoding='latin-1', delimiter=';', usecols=(0,1,2), names=('user_id','isbn','rating'), low_memory=False)
7
+ books = pd.read_csv('BX_Books.csv', encoding='latin-1', delimiter=';', header=None, usecols=(0,1), names=('isbn','title'))
8
+
9
+ # Merge the data
10
+ merged_data = ratings.merge(books)
11
+
12
+ # Load the trained models
13
+ # Assuming 'learn_dot' and 'deep_learner' are already trained instances
14
+ learn_dot.load('learner.pkl') # Replace 'path_to_dot_model' with the actual path
15
+ deep_learner.load('model.pkl') # Replace 'path_to_nn_model' with the actual path
16
+
17
+
18
  class CollabNN(Module):
19
+ def __init__(self, user_sz, item_sz, y_range=(0,10.5), n_act=150):
20
  self.user_factors = Embedding(*user_sz)
21
  self.item_factors = Embedding(*item_sz)
22
  self.layers = nn.Sequential(
23
+ nn.Linear(user_sz[1]+item_sz[1], n_act),
24
  nn.ReLU(),
25
  nn.Linear(n_act, 1))
26
  self.y_range = y_range
27
+
28
  def forward(self, x):
29
+ embs = self.user_factors(x[:,0]),self.item_factors(x[:,1])
30
  x = self.layers(torch.cat(embs, dim=1))
31
+ return sigmoid_range(x, *self.y_range)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
 
 
 
33
 
34
+ class BiasCalc(Module):
35
+ def __init__(self, n_users, n_movies, n_factors, y_range=(0, 10)):
36
+ self.user_factors = Embedding(n_users, n_factors)
37
+ self.user_bias = Embedding(n_users, 1)
38
+ self.book_factors = Embedding(n_books, n_factors)
39
+ self.book_bias = Embedding(n_books, 1)
40
+ self.y_range = y_range
41
+
42
+ def forward(self, x):
43
+ users = self.user_factors(x[:,0])
44
+ books = self.book_factors(x[:,1])
45
+ res = (users * books).sum(dim=1, keepdim=True)
46
+ res += self.user_bias(x[:,0]) + self.book_bias(x[:,1])
47
+ return sigmoid_range(res, *self.y_range)
48
+
49
+ # Function to get top N recommendations for a user using the Dot Product Model
50
+ def get_top_recommendations_dot(user_id, n=3):
51
+ item_ids = dls.classes['title'].o2i[ratings['title']]
52
+ user_ids = [user_id] * len(item_ids)
53
+ user_item_pairs = torch.tensor(list(zip(user_ids, item_ids))).to(learn_dot.model.device)
54
+ predictions_dot = learn_dot.model(user_item_pairs).squeeze().tolist()
55
+ top_indices = sorted(range(len(predictions_dot)), key=lambda i: predictions_dot[i], reverse=True)[:n]
56
+ top_recommendations = [dls.classes['title'].i2o[item_id] for item_id in top_indices]
57
+ return top_recommendations
58
+
59
+ # Function to get top N recommendations for a user using the CollabNN Model
60
+ def get_top_recommendations_nn(user_id, n=3):
61
+ item_ids = dls.classes['title'].o2i[ratings['title']]
62
+ user_ids = [user_id] * len(item_ids)
63
+ user_item_pairs = torch.tensor(list(zip(user_ids, item_ids))).to(deep_learner.model.device)
64
+ predictions_nn = deep_learner.model(user_item_pairs).squeeze().tolist()
65
+ top_indices = sorted(range(len(predictions_nn)), key=lambda i: predictions_nn[i], reverse=True)[:n]
66
+ top_recommendations = [dls.classes['title'].i2o[item_id] for item_id in top_indices]
67
+ return top_recommendations
68
 
69
  # Create a Gradio interface
70
  iface = gr.Interface(
71
+ fn=get_top_recommendations_dot,
72
+ inputs=[gr.Textbox('user_id', label="Enter User ID:")],
73
+ outputs=gr.Textbox(),
74
+ live=True,
75
+ interpretation="default"
76
+ )
77
+
78
+ # Create a Gradio interface for CollabNN Model
79
+ iface_nn = gr.Interface(
80
+ fn=get_top_recommendations_nn,
81
+ inputs=[gr.Textbox('user_id', label="Enter User ID:")],
82
+ outputs=gr.Textbox(),
83
+ live=True,
84
+ interpretation="default"
85
  )
86
 
87
  # Launch the Gradio app