File size: 2,395 Bytes
35b3531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, app, jsonify, render_template
import numpy as np
import pandas as pd
from joblib import dump, load
from src.utils import LoadClassifierThreshold
import os

app = Flask(__name__)

model_path = os.path.join('artifacts', 'best_model.pkl')
threshold_path = os.path.join('artifacts', 'threshold.txt')
preprocessor_path = os.path.join('artifacts', 'preprocessor.pkl')
# Load the model and preprocessor
model = LoadClassifierThreshold(model_path= model_path,
                                threshold_path=threshold_path)
preprocessor = load(preprocessor_path)

# Create first route
@app.route('/')
def home():
    return render_template('home.html')

@app.route('/predict_api', methods=['POST'])
def predict_api():
    data = request.json['data']
    # Create a DataFrame from the JSON data
    data_df = pd.DataFrame(data, index=[0])
    print(data_df)
    new_data = preprocessor.transform(data_df)
    output = model.predict_with_threshold(new_data)
    # Convert the output to an integer
    prediction = int(output[0])
    return jsonify(prediction)

@app.route('/predict', methods=['POST'])
def predict():
    # Get data from the HTML form and create a DataFrame
    data = {
        'gender': [request.form['gender']],
        'age': [int(request.form['age'])],
        'hypertension': [int(request.form['hypertension'])],
        'heart_disease': [int(request.form['heart_disease'])],
        'ever_married': [request.form['ever_married']],
        'work_type': [request.form['work_type']],
        'Residence_type': [request.form['Residence_type']],
        'avg_glucose_level': [float(request.form['avg_glucose_level'])],
        'bmi': [float(request.form['bmi'])],
        'smoking_status': [request.form['smoking_status']]
    }
    data_df = pd.DataFrame(data)
    print(data_df)

    # Transform the data using the preprocessor
    final_input = preprocessor.transform(data_df)

    # Make predictions using the model
    output = model.predict_with_threshold(final_input)
    prediction = int(output[0])

    # # Define the prediction message
    # if prediction == 1:
    #     prediction_message = "There is an indication of stroke."
    # else:
    #     prediction_message = "There is no indication of stroke."

    return render_template("home.html", prediction_text=prediction)



if __name__ == "__main__":
    app.run(host="0.0.0.0",port=5000)