File size: 3,030 Bytes
b1d4757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ef3f60
 
b1d4757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# coding: utf-8

# In[15]:


import csv
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
import imdb
import random
import gradio as gr


# In[2]:


file1=open("movies.csv",'r')
file2=open("movies3.csv",'r')
csvreader1 = csv.reader(file1)
csvreader2 = csv.reader(file2)


# In[3]:


pd_dict={'movie':[],'time_minute':[],'imdb_rating':[],'Action':[],'Adventure':[],'Fantasy':[],
         'Sci-Fi':[],'Animation':[],'Comedy':[],'Family':[],'Mystery':[],'Romance':[],'Drama':[],
         'Crime':[],'Thriller':[],'War':[],'Musical':[],'Biography':[]}

lst=['Action','Adventure','Fantasy','Sci-Fi','Animation','Comedy',
     'Family','Mystery','Romance','Drama','Crime','Thriller','War','Musical','Biography']


# In[4]:


j=0
for row in csvreader2:
    if j==0:
        j=1
        continue
    for i in range(len(lst)):
        pd_dict[lst[i]].append(int(row[i+1]))
j=0
for row in csvreader1:
    if j==0:
        j=1
        continue
    if j==250:
        break
    pd_dict['movie'].append(row[1])
    pd_dict['time_minute'].append(int(row[3].split()[0]))
    pd_dict['imdb_rating'].append(float(row[4]))
    j+=1


# In[5]:


df=pd.DataFrame(pd_dict)
X=np.array(df.drop(['movie']))


# In[6]:


kmeans = KMeans(max_iter=300).fit(X)


# In[9]:


movie_name="Oppenheimer"
ia=imdb.IMDb()
items = ia.search_movie(movie_name)
code = items[0].getID()
print(code)
series = ia.get_movie(code)
genre = series.data['genres']
time_minute=series.data['runtimes']
rating=series.data['rating']
x=[int(time_minute[0]),float(rating)]
for i in lst:
    if i in genre:
        x.append(1)
    else:
        x.append(0)
print(x)


# In[13]:


i=random.randint(1,249)
test_x=X[i].reshape(1,-1)
x=np.array(x)
x=x.reshape(1,-1)
# print(kmeans.predict(test_x))
# print(kmeans.predict(x))
pred=kmeans.predict(x)
pred_test=kmeans.predict(test_x)
while pred!=pred_test:
    i=random.randint(1,249)
    test_x=X[i].reshape(1,-1)
    x=np.array(x)
    x=x.reshape(1,-1)
    pred_test=kmeans.predict(test_x)
print('Another movie like',movie_name,'is:',df['movie'][i])


# In[16]:


def movie(txt):
    ia=imdb.IMDb()
    items = ia.search_movie(movie_name)
    code = items[0].getID()
    series = ia.get_movie(code)
    genre = series.data['genres']
    time_minute=series.data['runtimes']
    rating=series.data['rating']
    x=[int(time_minute[0]),float(rating)]
    for i in lst:
        if i in genre:
            x.append(1)
        else:
            x.append(0)
            
    i=random.randint(1,249)
    test_x=X[i].reshape(1,-1)
    x=np.array(x)
    x=x.reshape(1,-1)
    # print(kmeans.predict(test_x))
    # print(kmeans.predict(x))
    pred=kmeans.predict(x)
    pred_test=kmeans.predict(test_x)
    while pred!=pred_test:
        i=random.randint(1,249)
        test_x=X[i].reshape(1,-1)
        x=np.array(x)
        x=x.reshape(1,-1)
        pred_test=kmeans.predict(test_x)
    return df['movie'][i]

iface = gr.Interface(fn=movie, inputs="text", outputs="text")
iface.launch()


# In[ ]: