File size: 1,830 Bytes
feec403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9e8351
feec403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab61732
feec403
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import _pickle as cPickle
import bz2

import gradio as gr
import pandas as pd


def decompress_model(file):
    data = bz2.BZ2File(file, 'rb')
    data = cPickle.load(data)
    return data


movies_list = decompress_model("./movies/movies_model.pbz2")
movies_similarity = decompress_model("./movies/movies_similarity.pbz2")
best_movies = pd.read_csv("./movies/best_movies.csv")
movie_data = pd.read_csv("./movies/movie_data.csv")


def recommend(movie_title):
    movie_index = movies_list[movies_list["title"] == movie_title].index[0]
    distances = movies_similarity[movie_index]
    sorted_movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:120]
    recommended_movies, recommended_posters = [], []
    unique_movies = set()
    for i in sorted_movie_list:
        poster_path = movies_list["poster_path"][i[0]]
        recommended_movie = movies_list.iloc[i[0]].title
        if recommended_movie not in unique_movies:
            unique_movies.add(recommended_movie)
            recommended_movies.append(recommended_movie)
            recommended_posters.append("https://image.tmdb.org/t/p/original" + poster_path)
    return recommended_movies, recommended_posters


def get_movie_details(title):
    movie_details = movie_data[movie_data["title"] == title]
    return movie_details.to_dict(orient="records")


def get_recommendation(movie):
    recommendation, movie_posters = recommend(movie)
    movie_details = [get_movie_details(movie) for movie in recommendation]
    return recommendation, movie_posters, movie_details


iface = gr.Interface(
    fn=get_recommendation,
    inputs="text",
    outputs="json",
    title="Movie Recommender",
    description="Enter a movie title to get recommendations.",
    examples=[["The Dark Knight"]],
    allow_flagging=False
)

iface.launch()