This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
177 lines (149 loc) · 6.13 KB
/
app.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from flask import Flask, render_template, request
import numpy as np
import pandas as pd
import joblib
import imdb
import os
MoviesData= joblib.load('Movies_Datase.pkl')
X = joblib.load('Movies_Learned_Features.pkl')
my_ratings = np.zeros((97,1))
my_movies=[]
my_added_movies=[]
def computeCost(X, y, theta):
m=y.size
s=np.dot(X,theta)-y
j=(1/(2*m))*(np.dot(np.transpose(s),s))
print(j)
return j
def gradientDescent(X, y, theta, alpha, num_iters):
m = float(y.shape[0])
theta = theta.copy()
for i in range(num_iters):
theta=(theta)-(alpha/m)*(np.dot(np.transpose((np.dot(X,theta)-y)),X))
return theta
def checkAndAdd(movie,rating):
try:
if isinstance(int(rating), str):
pass
except ValueError:
return (3)
if (int(rating) <= 5 and int(rating) >= 0):
movie = movie.lower()
movie=movie+' '
if movie not in MoviesData['movie_title'].unique():
return(1)
else:
index=MoviesData[MoviesData['movie_title']==movie].index.values[0]
my_ratings[index] = rating
movieid=MoviesData.loc[MoviesData['movie_title']==movie, 'movie_id']
if movie in my_added_movies:
return(2)
my_movies.append(movieid)
my_added_movies.append(movie)
return(0)
else:
return(-1)
def url_clean(url):
base, ext = os.path.splitext(url)
i = url.count('@')
s2 = url.split('@')[0]
url = s2 + '@' * i + ext
return url
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/addMovie/', methods=['GET','POST'])
def addMovie():
val=request.form.get('movie_name')
rating=request.form.get('rating')
flag=checkAndAdd(val,rating)
if (flag==1):
processed_text="Sorry! The movie you requested is not in our database. Please check the spelling or try with some other movies"
return render_template('home.html',processed_text=processed_text)
elif (flag==-1):
processed_text="please enter rating between 1-5 this application follows five-star rating system"
return render_template('home.html',processed_text=processed_text)
elif (flag==2):
processed_text="The movie has already been added by you"
return render_template('home.html',processed_text=processed_text)
elif (flag==3):
processed_text="Invalid Input! Please enter a number between 0-5 in the rating field"
return render_template('home.html',processed_text=processed_text)
else:
processed_text="Successfully added movie to your rated movies"
movie_text=", you've rated "+rating+" stars to movie: "+val
return render_template('home.html',processed_text=processed_text,movie_text=movie_text,my_added_movies=my_added_movies)
@app.route('/reset/', methods=['GET','POST'])
def reset():
global my_ratings
global my_movies
global my_added_movies
my_ratings = np.zeros((9724,1))
my_movies=[]
my_added_movies=[]
processed_text='Successfull reset'
return render_template('home.html',processed_text=processed_text)
@app.route('/predict_advance/', methods=['GET','POST'])
def predict_advance():
if (len(my_added_movies)==0):
processed_text="Yikes! you've to add some movies before predicting anything "
return render_template('home.html',processed_text=processed_text)
data=predict(flag=1)
links=[]
data=data[:12]
data=data.reset_index(drop=True)
titles=data['movie_title'].values.tolist()
access = imdb.IMDb()
for movie in titles:
name=movie
movie = access.search_movie(name)[1]
data=data.values.tolist()
return render_template('result.html',data=data)
@app.route('/predict/', methods=['GET','POST'])
def predict(flag=None):
if request.method == "POST":
if (len(my_added_movies)==0):
processed_text="Yikes! you've to add some movies before predicting anything "
return render_template('home.html',processed_text=processed_text)
if(flag==1):
if (len(my_added_movies)==0):
processed_text="Yikes! you've to add some movies before predicting anything "
return render_template('home.html',processed_text=processed_text)
out_arr = my_ratings[np.nonzero(my_ratings)]
out_arr=out_arr.reshape(-1,1)
idx = np.where(my_ratings)[0]
X_1=[X[x] for x in idx]
X_1=np.array(X_1)
y=out_arr
y=np.reshape(y, -1)
theta = gradientDescent(X_1,y,np.zeros((100)),0.001,10000)
p = X @ theta.T
p=np.reshape(p, -1)
predictedData=MoviesData.copy()
predictedData['Pridiction']=p
sorted_data=predictedData.sort_values(by=['Pridiction'],ascending=False)
sorted_data=sorted_data[~sorted_data.movie_title.isin(my_added_movies)]
sorted_data=sorted_data[:40]
return(sorted_data)
out_arr = my_ratings[np.nonzero(my_ratings)]
out_arr=out_arr.reshape(-1,1)
idx = np.where(my_ratings)[0]
X_1=[X[x] for x in idx]
X_1=np.array(X_1)
y=out_arr
y=np.reshape(y, -1)
theta = gradientDescent(X_1,y,np.zeros((100)),0.001,10000)
p = X @ theta.T
p=np.reshape(p, -1)
predictedData=MoviesData.copy()
predictedData['Pridiction']=p
sorted_data=predictedData.sort_values(by=['Pridiction'],ascending=False)
sorted_data=sorted_data[~sorted_data.movie_title.isin(my_added_movies)]
sorted_data=sorted_data[:40]
my_list=sorted_data.values.tolist()
return render_template('result.html',my_list=my_list)
pass
if __name__ == '__main__':
app.debug = True
app.run()