File size: 2,430 Bytes
deb833c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- 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)