-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
51 lines (41 loc) · 2.2 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
from flask import Flask, render_template, request, redirect
from flask_cors import CORS, cross_origin
import pickle
import numpy as np
import pandas as pd
app = Flask(__name__, template_folder='template')
cors=CORS(app)
car_data = pd.read_csv('dataset/cleaned_car_data.csv')
model = pickle.load(open('random_forest_model.pickle', 'rb'))
@app.route('/', methods=['GET', 'POST'])
@cross_origin()
def home():
year = sorted(car_data['year'].unique(), reverse=True)
make = sorted(car_data['make'].unique())
number_of_doors = sorted(car_data['number_of_doors'].unique())
vehicle_style = sorted(car_data['vehicle_style'].unique())
vehicle_size = sorted(car_data['vehicle_size'].unique())
driven_wheels = sorted(car_data['driven_wheels'].unique())
engine_fuel_type = sorted(car_data['engine_fuel_type'].unique())
transmission_type = sorted(car_data['transmission_type'].unique())
make.insert(0, 'Select brand')
return render_template('index.html', year=year, make=make,number_of_doors=number_of_doors, vehicle_style=vehicle_style,vehicle_size=vehicle_size, driven_wheels=driven_wheels,engine_fuel_type=engine_fuel_type, transmission_type= transmission_type)
@app.route("/predict", methods=['POST'])
@cross_origin()
def predict():
"""rendering result to html gui"""
year = int(request.form.get('year'))
make = request.form.get('make')
number_of_doors = int(request.form.get('number_of_doors'))
vehicle_style = request.form.get('vehicle_style')
vehicle_size = request.form.get('vehicle_size')
driven_wheels = request.form.get('driven_wheels')
engine_fuel_type = request.form.get('engine_fuel_type')
engine_hp = int(request.form.get('engine_hp'))
transmission_type = request.form.get('transmission_type')
prediction = model.predict(pd.DataFrame(columns=['make', 'year','engine_fuel_type', 'engine_hp', 'transmission_type', 'driven_wheels',
'number_of_doors', 'vehicle_size', 'vehicle_style'], data=np.array([make, year, engine_fuel_type, engine_hp, transmission_type, driven_wheels, number_of_doors, vehicle_size, vehicle_style]).reshape(1,9)))
print(prediction)
return str(np.round(prediction[0],2))
if __name__ == "__main__":
app.run(debug=True)