Gopala Krishna
commited on
Commit
·
028e00f
1
Parent(s):
4918d38
- .vs/MovieRecommendations/v17/.wsuo +0 -0
- app.py +16 -10
.vs/MovieRecommendations/v17/.wsuo
CHANGED
Binary files a/.vs/MovieRecommendations/v17/.wsuo and b/.vs/MovieRecommendations/v17/.wsuo differ
|
|
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
from scipy.sparse import csr_matrix
|
5 |
from sklearn.neighbors import NearestNeighbors
|
6 |
|
|
|
7 |
def create_matrix(df):
|
8 |
N = len(df['userId'].unique())
|
9 |
M = len(df['movieId'].unique())
|
@@ -18,6 +20,7 @@ def create_matrix(df):
|
|
18 |
X = csr_matrix((df["rating"], (movie_index, user_index)), shape=(M, N))
|
19 |
return X, user_mapper, movie_mapper, user_inv_mapper, movie_inv_mapper
|
20 |
|
|
|
21 |
def find_similar_movies(movie_id, X, k, metric='cosine', show_distance=False):
|
22 |
neighbour_ids = []
|
23 |
movie_ind = movie_mapper[movie_id]
|
@@ -25,22 +28,25 @@ def find_similar_movies(movie_id, X, k, metric='cosine', show_distance=False):
|
|
25 |
k += 1
|
26 |
kNN = NearestNeighbors(n_neighbors=k, algorithm="brute", metric=metric)
|
27 |
kNN.fit(X)
|
28 |
-
movie_vec = movie_vec.reshape(1
|
29 |
neighbour = kNN.kneighbors(movie_vec, return_distance=show_distance)
|
30 |
-
for i in range(0,k):
|
31 |
n = neighbour.item(i)
|
32 |
neighbour_ids.append(movie_inv_mapper[n])
|
33 |
neighbour_ids.pop(0)
|
34 |
return neighbour_ids
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
similar_ids = find_similar_movies(movie_id, X, k=10)
|
38 |
-
|
39 |
-
recommendations = []
|
40 |
-
for i in similar_ids:
|
41 |
-
recommendations.append(movie_titles[i])
|
42 |
return recommendations
|
43 |
|
|
|
44 |
# Load data
|
45 |
ratings = pd.read_csv("https://s3-us-west-2.amazonaws.com/recommender-tutorial/ratings.csv")
|
46 |
movies = pd.read_csv("https://s3-us-west-2.amazonaws.com/recommender-tutorial/movies.csv")
|
@@ -58,12 +64,12 @@ X, user_mapper, movie_mapper, user_inv_mapper, movie_inv_mapper = create_matrix(
|
|
58 |
movie_titles = dict(zip(movies['movieId'], movies['title']))
|
59 |
|
60 |
# Set up Gradio interface
|
61 |
-
|
62 |
iface = gr.Interface(
|
63 |
fn=recommend_movies,
|
64 |
-
inputs=
|
65 |
outputs="text",
|
66 |
title="Movie Recommender System",
|
67 |
-
description="Enter a movie
|
68 |
)
|
69 |
iface.launch()
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
from scipy.sparse import csr_matrix
|
6 |
from sklearn.neighbors import NearestNeighbors
|
7 |
|
8 |
+
|
9 |
def create_matrix(df):
|
10 |
N = len(df['userId'].unique())
|
11 |
M = len(df['movieId'].unique())
|
|
|
20 |
X = csr_matrix((df["rating"], (movie_index, user_index)), shape=(M, N))
|
21 |
return X, user_mapper, movie_mapper, user_inv_mapper, movie_inv_mapper
|
22 |
|
23 |
+
|
24 |
def find_similar_movies(movie_id, X, k, metric='cosine', show_distance=False):
|
25 |
neighbour_ids = []
|
26 |
movie_ind = movie_mapper[movie_id]
|
|
|
28 |
k += 1
|
29 |
kNN = NearestNeighbors(n_neighbors=k, algorithm="brute", metric=metric)
|
30 |
kNN.fit(X)
|
31 |
+
movie_vec = movie_vec.reshape(1, -1)
|
32 |
neighbour = kNN.kneighbors(movie_vec, return_distance=show_distance)
|
33 |
+
for i in range(0, k):
|
34 |
n = neighbour.item(i)
|
35 |
neighbour_ids.append(movie_inv_mapper[n])
|
36 |
neighbour_ids.pop(0)
|
37 |
return neighbour_ids
|
38 |
|
39 |
+
|
40 |
+
def recommend_movies(movie_name):
|
41 |
+
movie_id = [k for k, v in movie_titles.items() if movie_name.lower() in v.lower()]
|
42 |
+
if len(movie_id) == 0:
|
43 |
+
return ["Movie not found"]
|
44 |
+
movie_id = movie_id[0]
|
45 |
similar_ids = find_similar_movies(movie_id, X, k=10)
|
46 |
+
recommendations = [movie_titles[i] for i in similar_ids]
|
|
|
|
|
|
|
47 |
return recommendations
|
48 |
|
49 |
+
|
50 |
# Load data
|
51 |
ratings = pd.read_csv("https://s3-us-west-2.amazonaws.com/recommender-tutorial/ratings.csv")
|
52 |
movies = pd.read_csv("https://s3-us-west-2.amazonaws.com/recommender-tutorial/movies.csv")
|
|
|
64 |
movie_titles = dict(zip(movies['movieId'], movies['title']))
|
65 |
|
66 |
# Set up Gradio interface
|
67 |
+
movie_name = gr.inputs.Textbox(default="The Shawshank Redemption", label="Movie Name")
|
68 |
iface = gr.Interface(
|
69 |
fn=recommend_movies,
|
70 |
+
inputs=movie_name,
|
71 |
outputs="text",
|
72 |
title="Movie Recommender System",
|
73 |
+
description="Enter a movie name and get recommendations for similar movies."
|
74 |
)
|
75 |
iface.launch()
|