ML_Entrega07 / app.py
AlGouvea's picture
Update app.py
3f2ee66
Raw History Blame Contribute Delete
5.3 kB
import gradio as gr
import pandas as pd
import torch
from fastai.learner import load_learner
from torch.nn import Module
from fastai.collab import CollabDataLoaders
from fastai.layers import sigmoid_range
ratings = pd.read_csv('BX_Book_Ratings.csv', encoding='latin-1', delimiter=';', usecols=(0,1,2), names=('user_id','isbn','rating'), low_memory=False, skiprows=[0])
books = pd.read_csv('BX_Books.csv', encoding='latin-1', delimiter=';', header=None, usecols=(0,1), names=('isbn','title'), skiprows=[0])
# Merge the data
ratings = ratings.merge(books)
user_ids = ratings['user_id'].unique().astype(int)
ids_list = user_ids.tolist()[:15]
print(ids_list[:5])
ratings['title'] = ratings['title'].astype('category')
ratings['user_id'] = ratings['user_id'].astype('int')
ratings['rating'] = ratings['rating'].astype('int')
dls = CollabDataLoaders.from_df(ratings, item_name='title', bs=8192)
class CollabNN(Module):
def __init__(self, user_sz, item_sz, y_range=(0,10.5), n_act=150):
self.user_factors = Embedding(*user_sz)
self.item_factors = Embedding(*item_sz)
self.layers = nn.Sequential(
nn.Linear(user_sz[1]+item_sz[1], n_act),
nn.ReLU(),
nn.Linear(n_act, 1))
self.y_range = y_range
def forward(self, x):
embs = self.user_factors(x[:,0]),self.item_factors(x[:,1])
x = self.layers(torch.cat(embs, dim=1))
return sigmoid_range(x, *self.y_range)
class BiasCalc(Module):
def __init__(self, n_users, n_movies, n_factors, y_range=(0, 10)):
self.user_factors = Embedding(n_users, n_factors)
self.user_bias = Embedding(n_users, 1)
self.book_factors = Embedding(n_books, n_factors)
self.book_bias = Embedding(n_books, 1)
self.y_range = y_range
def forward(self, x):
users = self.user_factors(x[:,0])
books = self.book_factors(x[:,1])
res = (users * books).sum(dim=1, keepdim=True)
res += self.user_bias(x[:,0]) + self.book_bias(x[:,1])
return sigmoid_range(res, *self.y_range)
# Load the trained models
# Assuming 'learn_dot' and 'deep_learner' are already trained instances
learn_dot = load_learner('learner.pkl') # Replace 'path_to_dot_model' with the actual path
deep_learner = load_learner('model.pkl') # Replace 'path_to_nn_model' with the actual path
def recommend_dot(user_id):
test_users = [user_id for _ in range(5)]
test_items = [ratings['title'].sample().iloc[0] for _ in range(5)]
user_ids = [dls.classes['user_id'].o2i[user] for user in test_users]
item_ids = [dls.classes['title'].o2i[item] for item in test_items]
user_item_pairs = list(zip(user_ids, item_ids))
user_item_pairs = torch.tensor(user_item_pairs)
# Move the input tensor to the same device as the model
device = next(learn_dot.model.parameters()).device
user_item_pairs = user_item_pairs.to(device)
predictions_dot = learn_dot.model(user_item_pairs)
predictions_dot = predictions_dot.squeeze().tolist()
recommendations = [(item, prediction) for item, prediction in zip(test_items, predictions_dot)]
print(recommendations[:5])
recommendations.sort(key=lambda x: x[1], reverse=True)
top_recommendations = recommendations[:5]
print(top_recommendations)
return top_recommendations
# Function to get top N recommendations for a user using the CollabNN Model
def recommend_nn(user_id):
# Sample user and item pairs
test_users = [user_id for _ in range(5)]
test_items = [ratings['title'].sample().iloc[0] for _ in range(5)]
user_ids = [dls.classes['user_id'].o2i[user] for user in test_users]
item_ids = [dls.classes['title'].o2i[item] for item in test_items]
user_item_pairs = list(zip(user_ids, item_ids))
user_item_pairs = torch.tensor(user_item_pairs)
# Move the input tensor to the same device as the model
device = next(deep_learner.model.parameters()).device
user_item_pairs = user_item_pairs.to(device)
# Get predictions
predictions_nn = deep_learner.model(user_item_pairs)
predictions_nn = predictions_nn.squeeze().tolist()
# Get the top 3 recommended books
recommendations = [(item, prediction) for item, prediction in zip(test_items, predictions_nn)]
print(recommendations[:5])
recommendations.sort(key=lambda x: x[1], reverse=True)
top_recommendations = recommendations[:5]
print(top_recommendations)
return top_recommendations
def get_recommendations(user_id):
pure_nn = recommend_nn(user_id)
pure_dot = recommend_dot(user_id)
nn = [(t[0]) for t in pure_nn]
dot = [(t[0]) for t in pure_dot]
return f"De acordo com o modelo NN esse usuário deve gostar desses livros: {nn}\nDe acordo com o modelo DotProduct esse usuário deve gostar desses livros: {dot}"
# Create a Gradio interface
#iface = gr.Interface(
# fn=get_top_recommendations_dot,
# inputs=[gr.Textbox('user_id', label="Enter User ID:")],
# outputs=gr.Textbox(),
# live=True,
#)
# Create a Gradio interface for CollabNN Model
iface_nn = gr.Interface(
fn=get_recommendations,
inputs=gr.Dropdown(choices=ids_list, type="value", label="Select User ID"),
outputs=gr.Textbox(),
)
# Launch the Gradio app
iface_nn.launch()