-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (48 loc) · 2.17 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
#importing necessary libraries/modules
from flask import Flask,render_template,request
import pickle
import numpy as np
import importlib
import itertools
#loading data from pickle files into variables
clean_ratings = pickle.load(open('C:/Users/M Sheharyar/Desktop/Rec_Sys/clean_ratings.pkl','rb'))
pvTable = pickle.load(open('C:/Users/M Sheharyar/Desktop/Rec_Sys/pvTable.pkl','rb'))
model = pickle.load(open('C:/Users/M Sheharyar/Desktop/Rec_Sys/model.pkl','rb'))
#basic Flask app boilerplate code
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
'''definition of function to take input from text field
using http POST method for the kNN model and
print out a list of similarly rated movies
according to first movie containing the input genre tag'''
@app.route('/recommend_movies',methods=['post'])
def recommend():
try:
name = request.form.get('query')
check = clean_ratings['title'].str.contains(name,case=False,regex=True)
idx = check[check].index[0]
nom = clean_ratings.at[idx,'title']
mov_id = np.where(pvTable.index == nom)[0][0]
distances, recommendations = model.kneighbors(pvTable.iloc[mov_id].dropna().values.reshape(1, -1), n_neighbors=10)
data = []
links = []
l = ''
for i in range(len(recommendations)):
for n in range(len(recommendations[i])):
data.append(pvTable.index[recommendations[i][n]])
l = str(clean_ratings.at[clean_ratings[clean_ratings['title'] == (pvTable.index[recommendations[i][n]])].index[0],'imdbId'])
if len(l) == 5:
l = ('{}'+l).format('tt00')
elif len(l) == 6:
l = ('{}'+l).format('tt0')
else:
l = ('{}'+l).format('tt')
links.append(l)
return render_template('index.html',name=name,d_l=zip(data,links))
except IndexError:
error_msg = "Sorry, this movie isn't in the database."
return render_template('index.html',error_msg = error_msg)
if __name__ == "__main__":
app.run()