Movie_Recommendation / movie_recommendation.py
stu1180's picture
Duplicate from sourav11295/Movie_Recommendation
deb833c
# -*- coding: utf-8 -*-
"""Movie Recommendation.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16mb8GFViCsAzCEZxBKLbV12h3pQEoU_l
"""
# Commented out IPython magic to ensure Python compatibility.
# %pip install gradio
import pandas as pd
import requests
movies_df = pd.read_csv('./movies.csv')
links_df = pd.read_csv('./links.csv')
combined_df = pd.concat([movies_df, links_df[['imdbId','tmdbId']]], axis=1)
combined_df = combined_df.set_index('title')
combined_df.head()
df = movies_df[['title','genres']]
df.head()
print(df.isnull().sum())
from sklearn.feature_extraction.text import TfidfVectorizer
tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=0, stop_words='english')
matrix = tf.fit_transform(df['genres'])
from sklearn.metrics.pairwise import linear_kernel
cosine_similarities = linear_kernel(matrix,matrix)
movie_title = df['title']
indices = pd.Series(df.index, index=df['title'])
def movie_recommend(original_title):
id = 'tt'+str(combined_df.loc[[original_title]].imdbId.values).replace('[','').replace(']','').zfill(7)
URL = f"http://www.omdbapi.com/?i={id}&apikey=3bd2165d"
# sending get request and saving the response as response object
r = requests.get(url = URL)
# extracting data in json format
data = r.json()
poster_url = data['Poster']
idx = indices[original_title]
sim_scores = list(enumerate(cosine_similarities[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[2:12]
movie_indices = [i[0] for i in sim_scores]
results = pd.DataFrame(list(data.items()), columns=['Key','Value']).head(20)
movies = pd.DataFrame(movie_title.iloc[movie_indices].reset_index(drop=True))
return results, movies, poster_url
import gradio as gr
with gr.Blocks(title='Movie Recommendation') as Intf:
gr.Markdown(value='Content Based Recommendation System')
with gr.Row():
with gr.Column():
inp = gr.Dropdown(choices=list(df['title']), label="Choose Movie")
btn = gr.Button("Run")
gr.Markdown(value='Movie Details')
results = gr.DataFrame()
poster = gr.Image(label="Poster Image")
recomms = gr.DataFrame(label='Recommended Content, Similar to this Movie')
btn.click(fn=movie_recommend, inputs=inp, outputs=[results,recomms,poster])
Intf.launch(debug=False)