Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import _pickle as cPickle
|
2 |
+
import bz2
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
|
8 |
+
def decompress_model(file):
|
9 |
+
data = bz2.BZ2File(file, 'rb')
|
10 |
+
data = cPickle.load(data)
|
11 |
+
return data
|
12 |
+
|
13 |
+
|
14 |
+
movies_list = decompress_model("./movies/movies_model.pbz2")
|
15 |
+
movies_similarity = decompress_model("./movies/movies_similarity.pbz2")
|
16 |
+
best_movies = pd.read_csv("./movies/best_movies.csv")
|
17 |
+
movie_data = pd.read_csv("./movies/movie_data.csv")
|
18 |
+
|
19 |
+
|
20 |
+
def recommend(movie_title):
|
21 |
+
movie_index = movies_list[movies_list["title"] == movie_title].index[0]
|
22 |
+
distances = movies_similarity[movie_index]
|
23 |
+
sorted_movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:120]
|
24 |
+
recommended_movies, recommended_posters = [], []
|
25 |
+
unique_movies = set()
|
26 |
+
for i in sorted_movie_list:
|
27 |
+
poster_path = movies_list["poster_path"][i[0]]
|
28 |
+
recommended_movie = movies_list.iloc[i[0]].title
|
29 |
+
if recommended_movie not in unique_movies:
|
30 |
+
unique_movies.add(recommended_movie)
|
31 |
+
recommended_movies.append(recommended_movie)
|
32 |
+
recommended_posters.append("https://image.tmdb.org/t/p/original" + poster_path)
|
33 |
+
return recommended_movies, recommended_posters
|
34 |
+
|
35 |
+
|
36 |
+
def get_movie_details(title):
|
37 |
+
movie_details = movie_data[movie_data["title"] == title]
|
38 |
+
return movie_details.to_dict(orient="records")
|
39 |
+
|
40 |
+
|
41 |
+
def get_recommendation(movie):
|
42 |
+
recommendation, movie_posters = recommend(movie)
|
43 |
+
movie_details = [get_movie_details(movie) for movie in recommendation]
|
44 |
+
|
45 |
+
# Extract the desired outputs
|
46 |
+
recommendation = recommendation[:3]
|
47 |
+
movie_posters = movie_posters[:3]
|
48 |
+
movie_details = movie_details[:3]
|
49 |
+
|
50 |
+
return recommendation, movie_posters, movie_details
|
51 |
+
|
52 |
+
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=get_recommendation,
|
55 |
+
inputs="text",
|
56 |
+
outputs="json",
|
57 |
+
title="Movie Recommender",
|
58 |
+
description="Enter a movie title to get recommendations.",
|
59 |
+
examples=[["The Dark Knight"]],
|
60 |
+
allow_flagging=False
|
61 |
+
)
|
62 |
+
|
63 |
+
iface.launch()
|