-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (56 loc) · 1.96 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
from flask import Flask, request, render_template
import numpy as np
import pickle
import pandas as pd
app = Flask(__name__)
# Load the model and data
pipe = pickle.load(open('models/pipe.pkl', 'rb'))
df = pickle.load(open('models/df.pkl', 'rb'))
# Convert 'Embarked' column to string to handle mixed types
df['Embarked'] = df['Embarked'].astype(str)
@app.route('/', methods=['GET'])
def index():
# Sort the unique values
Pclasss = sorted(df['Pclass'].unique())
Sexs = sorted(df['Sex'].unique())
Embarkeds = sorted(df['Embarked'].unique())
return render_template('index.html',
Pclasss=Pclasss,
Sexs=Sexs,
Embarkeds=Embarkeds)
@app.route('/predict', methods=['POST'])
def predict():
# Retrieve form data
Pclass = int(request.form['Pclass'])
Sex = request.form['Sex']
Age = float(request.form['Age'])
SibSp = int(request.form['SibSp'])
Parch = int(request.form['Parch'])
Fare = float(request.form['Fare'])
Embarked = request.form['Embarked']
# Create a DataFrame from the input data for the model
query = pd.DataFrame([[Pclass, Sex, Age, SibSp, Parch, Fare, Embarked]],
columns=['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked'])
# Predict survival
prediction = pipe.predict(query)[0]
# Sort the unique values again for consistent dropdown options
Pclasss = sorted(df['Pclass'].unique())
Sexs = sorted(df['Sex'].unique())
Embarkeds = sorted(df['Embarked'].unique())
# Render template with prediction and input values
return render_template(
'index.html',
survived=prediction,
Pclasss=Pclasss,
Sexs=Sexs,
Embarkeds=Embarkeds,
Pclass=Pclass,
Sex=Sex,
Age=Age,
SibSp=SibSp,
Parch=Parch,
Fare=Fare,
Embarked=Embarked
)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)