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()