Spaces:
Runtime error
Runtime error
sourav11295
commited on
Commit
•
60c2768
1
Parent(s):
bf9c84f
Upload 4 files
Browse files- links.csv +0 -0
- movie_recommendation.py +86 -0
- movies.csv +0 -0
- requirements.txt +4 -0
links.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
movie_recommendation.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Movie Recommendation.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/16mb8GFViCsAzCEZxBKLbV12h3pQEoU_l
|
8 |
+
"""
|
9 |
+
|
10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
11 |
+
# %pip install gradio
|
12 |
+
|
13 |
+
import pandas as pd
|
14 |
+
import requests
|
15 |
+
|
16 |
+
movies_df = pd.read_csv('./movies.csv')
|
17 |
+
links_df = pd.read_csv('./links.csv')
|
18 |
+
combined_df = pd.concat([movies_df, links_df[['imdbId','tmdbId']]], axis=1)
|
19 |
+
combined_df = combined_df.set_index('title')
|
20 |
+
|
21 |
+
combined_df.head()
|
22 |
+
|
23 |
+
df = movies_df[['title','genres']]
|
24 |
+
df.head()
|
25 |
+
|
26 |
+
print(df.isnull().sum())
|
27 |
+
|
28 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
29 |
+
|
30 |
+
tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=0, stop_words='english')
|
31 |
+
|
32 |
+
matrix = tf.fit_transform(df['genres'])
|
33 |
+
|
34 |
+
from sklearn.metrics.pairwise import linear_kernel
|
35 |
+
|
36 |
+
cosine_similarities = linear_kernel(matrix,matrix)
|
37 |
+
|
38 |
+
movie_title = df['title']
|
39 |
+
|
40 |
+
indices = pd.Series(df.index, index=df['title'])
|
41 |
+
|
42 |
+
def movie_recommend(original_title):
|
43 |
+
|
44 |
+
id = 'tt'+str(combined_df.loc[[original_title]].imdbId.values).replace('[','').replace(']','').zfill(7)
|
45 |
+
|
46 |
+
URL = f"http://www.omdbapi.com/?i={id}&apikey=3bd2165d"
|
47 |
+
|
48 |
+
# sending get request and saving the response as response object
|
49 |
+
r = requests.get(url = URL)
|
50 |
+
|
51 |
+
# extracting data in json format
|
52 |
+
data = r.json()
|
53 |
+
|
54 |
+
poster_url = data['Poster']
|
55 |
+
|
56 |
+
idx = indices[original_title]
|
57 |
+
|
58 |
+
sim_scores = list(enumerate(cosine_similarities[idx]))
|
59 |
+
|
60 |
+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
61 |
+
|
62 |
+
sim_scores = sim_scores[2:12]
|
63 |
+
|
64 |
+
movie_indices = [i[0] for i in sim_scores]
|
65 |
+
|
66 |
+
results = pd.DataFrame(list(data.items()), columns=['Key','Value']).head(20)
|
67 |
+
|
68 |
+
movies = pd.DataFrame(movie_title.iloc[movie_indices].reset_index(drop=True))
|
69 |
+
|
70 |
+
return results, movies, poster_url
|
71 |
+
|
72 |
+
import gradio as gr
|
73 |
+
|
74 |
+
with gr.Blocks(title='Movie Recommendation') as Intf:
|
75 |
+
gr.Markdown(value='Content Based Recommendation System')
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
inp = gr.Dropdown(choices=list(df['title']), label="Choose Movie")
|
79 |
+
btn = gr.Button("Run")
|
80 |
+
gr.Markdown(value='Movie Details')
|
81 |
+
results = gr.DataFrame()
|
82 |
+
poster = gr.Image(label="Poster Image")
|
83 |
+
recomms = gr.DataFrame(label='Recommended Content, Similar to this Movie')
|
84 |
+
btn.click(fn=movie_recommend, inputs=inp, outputs=[results,recomms,poster])
|
85 |
+
|
86 |
+
Intf.launch(debug=False)
|
movies.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pandas
|
3 |
+
requests
|
4 |
+
scikit_learn
|