Spaces:
Sleeping
Sleeping
updated app with movies names and genre
Browse files
app.py
CHANGED
@@ -1,89 +1,89 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pickle
|
3 |
-
import numpy as np
|
4 |
-
import pandas as pd
|
5 |
-
|
6 |
-
#
|
7 |
-
with open('final_02_model_biases_factors.pkl', 'rb') as file:
|
8 |
-
model_state = pickle.load(file)
|
9 |
-
|
10 |
-
user_bias = model_state["user_bias"]
|
11 |
-
movie_bias = model_state["movie_bias"]
|
12 |
-
user_factors = model_state["user_factors"]
|
13 |
-
movie_factors = model_state["movie_factors"]
|
14 |
-
movies = pd.read_csv("movies.csv")
|
15 |
-
|
16 |
-
with open('map_movie_to_idx.pkl', 'rb') as file:
|
17 |
-
map_movie_to_idx = pickle.load(file)
|
18 |
-
|
19 |
-
with open('map_idx_to_movie.pkl', 'rb') as file:
|
20 |
-
map_idx_to_movie = pickle.load(file)
|
21 |
-
|
22 |
-
def get_movie_id(title):
|
23 |
-
movie = movies[movies['title'].str.contains(title, case=False, na=False)]
|
24 |
-
if not movie.empty:
|
25 |
-
return movie.iloc[0]['movieId'], movie.iloc[0]['title'], movie.iloc[0]['genres']
|
26 |
-
return None, None, None
|
27 |
-
|
28 |
-
def recommend_movies(movie_title, rating, latent_dim=10, lambda_param=0.1, tau=0.1):
|
29 |
-
movie_id, title, genres = get_movie_id(movie_title)
|
30 |
-
if movie_id is None:
|
31 |
-
return f"Movie '{movie_title}' not found."
|
32 |
-
|
33 |
-
# Map the dummy movie id to its index
|
34 |
-
dummy_movie_index = map_movie_to_idx[movie_id]
|
35 |
-
|
36 |
-
# Get the movie factors and bias for the dummy movie
|
37 |
-
dummy_movie_factors = movie_factors[:, dummy_movie_index]
|
38 |
-
dummy_movie_bias = movie_bias[dummy_movie_index]
|
39 |
-
|
40 |
-
# Initialize dummy user factors
|
41 |
-
dummy_user_factors = np.zeros(latent_dim)
|
42 |
-
|
43 |
-
# Calculate the dummy user factor
|
44 |
-
dummy_user_factor = np.linalg.inv(lambda_param * np.outer(dummy_movie_factors, dummy_movie_factors) +
|
45 |
-
tau * np.eye(latent_dim)) @ (lambda_param * dummy_movie_factors * (rating - dummy_movie_bias))
|
46 |
-
|
47 |
-
# Calculate the score for each movie
|
48 |
-
score = dummy_user_factor @ movie_factors + 0.05 * movie_bias
|
49 |
-
|
50 |
-
# Get the top 10 recommendations
|
51 |
-
recommend_movies_indices = np.argsort(score)[-10:]
|
52 |
-
|
53 |
-
# Map the indices back to movie titles
|
54 |
-
recommended_movies = [movies.loc[movies["movieId"] == map_idx_to_movie[movie]] for movie in recommend_movies_indices]
|
55 |
-
|
56 |
-
# Format the output as HTML
|
57 |
-
html_output = "<div style='font-family: Arial, sans-serif;'>"
|
58 |
-
html_output += f"<div style='margin-bottom: 20px;'><strong>Selected Movie:</strong><br>Title: {title}<br>Genres: {genres}</div>"
|
59 |
-
html_output += "<div style='margin-bottom: 20px;'><strong>Recommended Movies:</strong></div>"
|
60 |
-
for i, movie in enumerate(recommended_movies):
|
61 |
-
title = movie["title"].values[0]
|
62 |
-
genres = movie["genres"].values[0]
|
63 |
-
html_output += f"<div style='margin-bottom: 10px;'><strong>{i+1}. {title}</strong><br>Genres: {genres}</div>"
|
64 |
-
html_output += "</div>"
|
65 |
-
|
66 |
-
return html_output
|
67 |
-
|
68 |
-
# Define the Gradio interface
|
69 |
-
iface = gr.Interface(
|
70 |
-
fn=recommend_movies,
|
71 |
-
inputs=[
|
72 |
-
gr.Textbox(label="Movie Title"),
|
73 |
-
gr.Slider(0, 5, step=0.1, label="Rating")
|
74 |
-
],
|
75 |
-
outputs="html",
|
76 |
-
title="Movie Recommendation System",
|
77 |
-
description="""
|
78 |
-
Enter a movie title and rating to get top 10 movie recommendations.
|
79 |
-
I made this demo as part of a machine learning class taught by Prof. Ulich Paquet at AIMS South Africa.
|
80 |
-
Here are some sample movies you can try out of a database of over 62,000 movies:
|
81 |
-
- Toy Story
|
82 |
-
- Incredibles
|
83 |
-
- Lord of the rings
|
84 |
-
- Star Wars
|
85 |
-
- Terminator
|
86 |
-
"""
|
87 |
-
)
|
88 |
-
|
89 |
iface.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# model weights pickled after training from scratch
|
7 |
+
with open('final_02_model_biases_factors.pkl', 'rb') as file:
|
8 |
+
model_state = pickle.load(file)
|
9 |
+
|
10 |
+
user_bias = model_state["user_bias"]
|
11 |
+
movie_bias = model_state["movie_bias"]
|
12 |
+
user_factors = model_state["user_factors"]
|
13 |
+
movie_factors = model_state["movie_factors"]
|
14 |
+
movies = pd.read_csv("movies.csv")
|
15 |
+
|
16 |
+
with open('map_movie_to_idx.pkl', 'rb') as file:
|
17 |
+
map_movie_to_idx = pickle.load(file)
|
18 |
+
|
19 |
+
with open('map_idx_to_movie.pkl', 'rb') as file:
|
20 |
+
map_idx_to_movie = pickle.load(file)
|
21 |
+
|
22 |
+
def get_movie_id(title):
|
23 |
+
movie = movies[movies['title'].str.contains(title, case=False, na=False)]
|
24 |
+
if not movie.empty:
|
25 |
+
return movie.iloc[0]['movieId'], movie.iloc[0]['title'], movie.iloc[0]['genres']
|
26 |
+
return None, None, None
|
27 |
+
|
28 |
+
def recommend_movies(movie_title, rating, latent_dim=10, lambda_param=0.1, tau=0.1):
|
29 |
+
movie_id, title, genres = get_movie_id(movie_title)
|
30 |
+
if movie_id is None:
|
31 |
+
return f"Movie '{movie_title}' not found."
|
32 |
+
|
33 |
+
# Map the dummy movie id to its index
|
34 |
+
dummy_movie_index = map_movie_to_idx[movie_id]
|
35 |
+
|
36 |
+
# Get the movie factors and bias for the dummy movie
|
37 |
+
dummy_movie_factors = movie_factors[:, dummy_movie_index]
|
38 |
+
dummy_movie_bias = movie_bias[dummy_movie_index]
|
39 |
+
|
40 |
+
# Initialize dummy user factors
|
41 |
+
dummy_user_factors = np.zeros(latent_dim)
|
42 |
+
|
43 |
+
# Calculate the dummy user factor
|
44 |
+
dummy_user_factor = np.linalg.inv(lambda_param * np.outer(dummy_movie_factors, dummy_movie_factors) +
|
45 |
+
tau * np.eye(latent_dim)) @ (lambda_param * dummy_movie_factors * (rating - dummy_movie_bias))
|
46 |
+
|
47 |
+
# Calculate the score for each movie
|
48 |
+
score = dummy_user_factor @ movie_factors + 0.05 * movie_bias
|
49 |
+
|
50 |
+
# Get the top 10 recommendations
|
51 |
+
recommend_movies_indices = np.argsort(score)[-10:]
|
52 |
+
|
53 |
+
# Map the indices back to movie titles
|
54 |
+
recommended_movies = [movies.loc[movies["movieId"] == map_idx_to_movie[movie]] for movie in recommend_movies_indices]
|
55 |
+
|
56 |
+
# Format the output as HTML
|
57 |
+
html_output = "<div style='font-family: Arial, sans-serif;'>"
|
58 |
+
html_output += f"<div style='margin-bottom: 20px;'><strong>Selected Movie:</strong><br>Title: {title}<br>Genres: {genres}</div>"
|
59 |
+
html_output += "<div style='margin-bottom: 20px;'><strong>Recommended Movies:</strong></div>"
|
60 |
+
for i, movie in enumerate(recommended_movies):
|
61 |
+
title = movie["title"].values[0]
|
62 |
+
genres = movie["genres"].values[0]
|
63 |
+
html_output += f"<div style='margin-bottom: 10px;'><strong>{i+1}. {title}</strong><br>Genres: {genres}</div>"
|
64 |
+
html_output += "</div>"
|
65 |
+
|
66 |
+
return html_output
|
67 |
+
|
68 |
+
# Define the Gradio interface
|
69 |
+
iface = gr.Interface(
|
70 |
+
fn=recommend_movies,
|
71 |
+
inputs=[
|
72 |
+
gr.Textbox(label="Movie Title"),
|
73 |
+
gr.Slider(0, 5, step=0.1, label="Rating")
|
74 |
+
],
|
75 |
+
outputs="html",
|
76 |
+
title="Movie Recommendation System",
|
77 |
+
description="""
|
78 |
+
Enter a movie title and rating to get top 10 movie recommendations.
|
79 |
+
I made this demo as part of a machine learning class taught by Prof. Ulich Paquet at AIMS South Africa.
|
80 |
+
Here are some sample movies you can try out of a database of over 62,000 movies:
|
81 |
+
- Toy Story
|
82 |
+
- Incredibles
|
83 |
+
- Lord of the rings
|
84 |
+
- Star Wars
|
85 |
+
- Terminator
|
86 |
+
"""
|
87 |
+
)
|
88 |
+
|
89 |
iface.launch(share=True)
|