Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,8 +64,33 @@ class BiasCalc(Module):
|
|
| 64 |
learn_dot = load_learner('learner.pkl') # Replace 'path_to_dot_model' with the actual path
|
| 65 |
deep_learner = load_learner('model.pkl') # Replace 'path_to_nn_model' with the actual path
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Function to get top N recommendations for a user using the CollabNN Model
|
| 68 |
-
def
|
| 69 |
# Sample user and item pairs
|
| 70 |
test_users = [user_id for _ in range(5)]
|
| 71 |
test_items = [ratings['title'].sample().iloc[0] for _ in range(5)]
|
|
@@ -93,6 +118,15 @@ def get_recommendations(user_id):
|
|
| 93 |
|
| 94 |
return top_recommendations
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# Create a Gradio interface
|
| 97 |
#iface = gr.Interface(
|
| 98 |
# fn=get_top_recommendations_dot,
|
|
|
|
| 64 |
learn_dot = load_learner('learner.pkl') # Replace 'path_to_dot_model' with the actual path
|
| 65 |
deep_learner = load_learner('model.pkl') # Replace 'path_to_nn_model' with the actual path
|
| 66 |
|
| 67 |
+
def recommend_dot(user_id):
|
| 68 |
+
test_users = [user_id for _ in range(5)]
|
| 69 |
+
test_items = [ratings['title'].sample().iloc[0] for _ in range(5)]
|
| 70 |
+
|
| 71 |
+
user_ids = [dls.classes['user_id'].o2i[user] for user in test_users]
|
| 72 |
+
item_ids = [dls.classes['title'].o2i[item] for item in test_items]
|
| 73 |
+
|
| 74 |
+
user_item_pairs = list(zip(user_ids, item_ids))
|
| 75 |
+
user_item_pairs = torch.tensor(user_item_pairs)
|
| 76 |
+
|
| 77 |
+
# Move the input tensor to the same device as the model
|
| 78 |
+
device = next(learn_dot.model.parameters()).device
|
| 79 |
+
user_item_pairs = user_item_pairs.to(device)
|
| 80 |
+
|
| 81 |
+
predictions_dot = learn_dot.model(user_item_pairs)
|
| 82 |
+
predictions_dot = predictions_dot.squeeze().tolist()
|
| 83 |
+
|
| 84 |
+
recommendations = [(item, prediction) for item, prediction in zip(test_items, predictions_nn)]
|
| 85 |
+
print(recommendations[:5])
|
| 86 |
+
recommendations.sort(key=lambda x: x[1], reverse=True)
|
| 87 |
+
top_recommendations = recommendations[:5]
|
| 88 |
+
print(top_recommendations)
|
| 89 |
+
|
| 90 |
+
return top_recommendations
|
| 91 |
+
|
| 92 |
# Function to get top N recommendations for a user using the CollabNN Model
|
| 93 |
+
def recommend_nn(user_id):
|
| 94 |
# Sample user and item pairs
|
| 95 |
test_users = [user_id for _ in range(5)]
|
| 96 |
test_items = [ratings['title'].sample().iloc[0] for _ in range(5)]
|
|
|
|
| 118 |
|
| 119 |
return top_recommendations
|
| 120 |
|
| 121 |
+
def get_recommendations(user_id):
|
| 122 |
+
pure_nn = recommend_nn(user_id)
|
| 123 |
+
pure_dot = recommend_dot(user_id)
|
| 124 |
+
|
| 125 |
+
nn = [(t[0]) for t in pure_nn]
|
| 126 |
+
dot = [(t[0]) for t in pure_dot]
|
| 127 |
+
|
| 128 |
+
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}"
|
| 129 |
+
|
| 130 |
# Create a Gradio interface
|
| 131 |
#iface = gr.Interface(
|
| 132 |
# fn=get_top_recommendations_dot,
|