-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
188 lines (148 loc) · 6.19 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
178
179
180
181
182
183
184
185
186
187
188
from flask import Flask, render_template, flash, request, redirect, url_for
from flask_cors import CORS
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from flask_wtf.recaptcha import RecaptchaField, Recaptcha
from flask_wtf.csrf import CSRFProtect
from wtforms import TextField, TextAreaField, StringField, SubmitField, SelectField, IntegerField, FieldList, FormField, FloatField
from wtforms.validators import DataRequired, Length, NumberRange
from wtforms import validators
import pandas as pd
from math import ceil
import json
from MovieAlgo.movie_info import MovieInfo, get_movie_name_year
from MovieAlgo.knn import KNN
'''
CS 598 - Practical Statistical Learning Project 3
UIUC, Fall 2020
@authors:
- Pranav Velamakanni (pranavv2@illinois.edu)
- Tarik Koric (koric1@illinois.edu)
'''
# App config.
app = Flask(__name__)
CORS(app)
CSRFProtect(app)
Bootstrap(app)
app.config.from_object(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a'
# Load metadata
with open('data/movie_data.json') as f:
movie_names_id = json.loads(f.read())
# Pre-load model and data for System 2
model = KNN()
class MovieRecommendation1(FlaskForm):
genre_coices = sorted(['Animation',
'Romance',
'Action',
'Crime',
'Film-Noir',
'War',
'Fantasy',
'Drama',
'Musical',
'Comedy',
'Mystery',
'Documentary',
'Thriller',
'Western',
'Adventure',
'Horror',
"Children's",
'Sci-Fi'])
algo_choice = ['Number of reviews', 'Mean rating', 'Weighted rating']
genre = SelectField('Genre', choices = genre_coices, description = "Pick a genre to receive recommendations for.", validators = [DataRequired()])
algo = SelectField('Rated by', choices = algo_choice, description = "Pick a rating algorithm.", validators = [DataRequired()])
number = IntegerField('Recommendations', default = 5, description = "Number of recommendations, limited to 20.", validators = [DataRequired(), NumberRange(min=1, max=20, message = 'Must be within 1 and 20, default is 5.')])
submit = SubmitField()
@app.route('/', methods = ('GET', 'POST'))
def index():
return render_template('index.html')
@app.errorhandler(404)
def page_not_found(error):
return render_template('confused.html'), 404
@app.route('/system1', methods = ('GET', 'POST'))
def system1():
'''
Serves the /system1 end point. Redirects to /results for POST.
'''
form = MovieRecommendation1()
if request.method == 'POST':
if form.validate_on_submit():
return redirect(url_for('results1', genre = request.form.get('genre'), algo = request.form.get('algo'), number = request.form.get('number')))
return render_template('system1.html', form = form)
@app.route('/results/<genre>/<algo>/<number>', methods = ['GET'])
def results1(genre, algo, number):
'''
Processes requests for System 1.
NOTE: This end point is not intended to be used like an API to avoid maxing out resource limits.
'''
#if not request.referrer:
# return render_template('confused.html'), 401
#####
## Movie analysis - System 1
#####
algo_map = {'Number of reviews': 'size', 'Mean rating': 'mean_ratings', 'Weighted rating': 'weighted_rating'}
data = pd.read_csv('data/final_ratings.csv')
results = data[(data['genre 1'] == genre) | (data['genre 2'] == genre) | (data['genre 3'] == genre) | (data['genre 4'] == genre) | (data['genre 5'] == genre) | (data['genre 6'] == genre)].sort_values(by=[algo_map.get(algo)], ascending=False).head(int(number))
movie_year_list = [*map(get_movie_name_year, list(results.movie))]
MovieStats = MovieInfo()
final_data = MovieStats.prepare_movie_list(movie_year_list)
return render_template('success.html', data = final_data)
class MovieForm(FlaskForm):
movie_name = SelectField(
'Movie',
description = 'Pick a movie to rate',
choices = sorted(movie_names_id.keys()),
validators = [DataRequired()]
)
rating = FloatField(
'Rating',
description = 'Rate the movie from 1.0 to 5.0',
validators=[NumberRange(min = 1.0, max = 5.0, message='Value must be between 1 and 5')]
)
class System2(FlaskForm):
'''
System 2 parent form.
'''
movies = FieldList(
FormField(MovieForm),
min_entries=1,
max_entries=10
)
number = IntegerField(
'Recommendations',
default = 5,
description = "Number of recommendations to display, limited to 20.",
validators = [NumberRange(min=1, max=20, message = 'Must be within 1 and 20, default is 5.')]
)
class RequestFormResponse2:
def __init__(self, form):
self.raw_data = form
self.recommendations = int(form['number'])
self.form_data = self.prepare_data(form)
def prepare_data(self, data):
final_data = list()
for i in range(ceil((len(data) - 3) / 2)):
temp = dict()
temp['movie_name'] = data['movies-{}-movie_name'.format(i)]
temp['movie_id'] = movie_names_id.get(temp['movie_name'])
temp['rating'] = data['movies-{}-rating'.format(i)]
final_data.append(temp)
return final_data
@app.route('/system2', methods=['POST', 'GET'])
def system2():
form = System2()
template_form = MovieForm(prefix='movies-_-')
if request.method == 'POST':
if form.validate_on_submit():
data = RequestFormResponse2(request.form)
results = model.predict(data.form_data)
movie_year_list = [*map(get_movie_name_year, results[:data.recommendations])]
MovieStats = MovieInfo()
final_data = MovieStats.prepare_movie_list(movie_year_list)
return render_template('success.html', data = final_data)
return render_template('system2.html', form=form, _template = template_form)
if __name__ == '__main__':
app.run(host = '0.0.0.0', threaded = True, port = 5000)