-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (51 loc) · 1.59 KB
/
app.py
File metadata and controls
54 lines (51 loc) · 1.59 KB
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
from flask import Flask, render_template, request
import pickle
from datetime import datetime
import numpy as np
import pandas as pd
app = Flask(__name__, template_folder='templates', static_folder='images')
data = pd.read_csv('./dataset/maxdata.csv')
modelfile = open('./model/rfbest.pkl', 'rb')
model = pickle.load(modelfile)
label = pickle.load(modelfile)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
val = [x for x in request.form.values()]
s = val[0]
d = val[1]
c = val[2]
ct1 = val[3]
ct2 = val[4]
df = data[(data.source == s) & (data.destination == d)]
if c == '2':
n = ct2
elif c == '1':
n = ct1
if df.empty:
res = -1
elif s == '--Choose a Source--' or d == '--Choose a Destination--' or c == '--Choose a Cab--' or (ct1 == '--Choose a Cab type--' and ct2 == '--Choose a Cab type--'):
res = -1
else:
distance = df[(df.name == n)]['distance'].iloc[0]
price = df[(df.name == n)]['price'].iloc[0]
ct = np.where(label == n)
t = datetime.now()
hour = int(t.strftime('%H'))
wd = t.weekday()
if wd > 4:
is_weekday = 0
else:
is_weekday = 1
if 7 <= hour <= 19:
is_oh = 1
else:
is_oh = 0
features = np.array([distance, price, ct[0][0], is_weekday, is_oh]).reshape(1, 5)
pred = model.predict(features)
res = round(pred[0], 2)
return render_template('index.html', result=res)
if __name__ == '__main__':
app.run()