Antony-gitau commited on
Commit
6640c83
·
1 Parent(s): 4276044

initial deployment

Browse files
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
+ import pandas as pd
5
+
6
+
7
+ # Load your model and data
8
+ with open('final_02_model_biases_factors.pkl', 'rb') as file:
9
+ model_state = pickle.load(file)
10
+
11
+ user_bias = model_state["user_bias"]
12
+ movie_bias = model_state["movie_bias"]
13
+ user_factors = model_state["user_factors"]
14
+ movie_factors = model_state["movie_factors"]
15
+ movies = pd.read_csv("movies.csv")
16
+
17
+ with open('map_movie_to_idx.pkl', 'rb') as file:
18
+ map_movie_to_idx = pickle.load(file)
19
+
20
+ with open('map_idx_to_movie.pkl', 'rb') as file:
21
+ map_idx_to_movie = pickle.load(file)
22
+
23
+ def recommend_movies(dummy_movie_id, rating, latent_dim=10, lambda_param=0.1, tau=0.1):
24
+ # Map the dummy movie id to its index
25
+ dummy_movie_index = map_movie_to_idx[dummy_movie_id]
26
+
27
+ # Get the movie factors and bias for the dummy movie
28
+ dummy_movie_factors = movie_factors[:, dummy_movie_index]
29
+ dummy_movie_bias = movie_bias[dummy_movie_index]
30
+
31
+ # Initialize dummy user factors
32
+ dummy_user_factors = np.zeros(latent_dim)
33
+
34
+ # Calculate the dummy user factor
35
+ dummy_user_factor = np.linalg.inv(lambda_param * np.outer(dummy_movie_factors, dummy_movie_factors) +
36
+ tau * np.eye(latent_dim)) @ (lambda_param * dummy_movie_factors * (rating - dummy_movie_bias))
37
+
38
+ # Calculate the score for each movie
39
+ score = dummy_user_factor @ movie_factors + 0.05 * movie_bias
40
+
41
+ # Get the top 10 recommendations
42
+ recommend_movies_indices = np.argsort(score)[-10:]
43
+
44
+ # Map the indices back to movie titles
45
+ recommended_movies = [movies.loc[movies["movieId"] == map_idx_to_movie[movie]] for movie in recommend_movies_indices]
46
+
47
+ # return recommended_movies
48
+
49
+ # Format the output as HTML
50
+ html_output = "<div style='font-family: Arial, sans-serif;'>"
51
+ for i, movie in enumerate(recommended_movies):
52
+ title = movie["title"].values[0]
53
+ genres = movie["genres"].values[0]
54
+ html_output += f"<div style='margin-bottom: 10px;'><strong>{i+1}. {title}</strong><br>Genres: {genres}</div>"
55
+ html_output += "</div>"
56
+
57
+ return html_output
58
+
59
+ # Define the Gradio interface
60
+ iface = gr.Interface(
61
+ fn=recommend_movies,
62
+ inputs=[
63
+ gr.Number(label="Movie ID"),
64
+ gr.Slider(0, 5, step=0.1, label="Rating")
65
+ ],
66
+ outputs="html",
67
+ title="Movie Recommendation System",
68
+ description="Enter a movie ID and rating to get top 10 movie recommendations."
69
+ )
70
+
71
+ iface.launch(share=True)
final_02_model_biases_factors.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0d3b60725c32f7aefef716ffdff2d5570747c365b46d7270b4b1cc6d2af0c199
3
+ size 19500750
map_idx_to_movie.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d5f6ec3b73ab6d76843caadd26f1b99616aef2cec935fe9d71b57e10cb1695d3
3
+ size 1122266
map_movie_to_idx.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:320c8f1d8c625c0f9449271101e2ab7bbecd061ae4eca24c767148c523bb4ee1
3
+ size 1299169
movies.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ pandas
3
+ numpy