Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- Dockerfile +17 -0
- app.py +71 -0
- artifacts/best_model.pkl +3 -0
- artifacts/healthcare-dataset-stroke-data-cleaned.csv +0 -0
- artifacts/preprocessor.pkl +3 -0
- artifacts/threshold.txt +1 -0
- requirements.txt +0 -0
- templates/home.html +179 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a slim Python image as the base
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory to /app
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy only the necessary files to the container
|
| 8 |
+
COPY app.py requirements.txt ./
|
| 9 |
+
COPY src ./src
|
| 10 |
+
COPY artifacts ./artifacts
|
| 11 |
+
COPY templates ./templates
|
| 12 |
+
|
| 13 |
+
# Install packages and clean up
|
| 14 |
+
RUN pip install -r requirements.txt && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
| 15 |
+
|
| 16 |
+
# Run your Flask app with Gunicorn
|
| 17 |
+
CMD ["gunicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, app, jsonify, render_template
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from joblib import dump, load
|
| 5 |
+
from src.utils import LoadClassifierThreshold
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
model_path = os.path.join('artifacts', 'best_model.pkl')
|
| 11 |
+
threshold_path = os.path.join('artifacts', 'threshold.txt')
|
| 12 |
+
preprocessor_path = os.path.join('artifacts', 'preprocessor.pkl')
|
| 13 |
+
# Load the model and preprocessor
|
| 14 |
+
model = LoadClassifierThreshold(model_path= model_path,
|
| 15 |
+
threshold_path=threshold_path)
|
| 16 |
+
preprocessor = load(preprocessor_path)
|
| 17 |
+
|
| 18 |
+
# Create first route
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def home():
|
| 21 |
+
return render_template('home.html')
|
| 22 |
+
|
| 23 |
+
@app.route('/predict_api', methods=['POST'])
|
| 24 |
+
def predict_api():
|
| 25 |
+
data = request.json['data']
|
| 26 |
+
# Create a DataFrame from the JSON data
|
| 27 |
+
data_df = pd.DataFrame(data, index=[0])
|
| 28 |
+
print(data_df)
|
| 29 |
+
new_data = preprocessor.transform(data_df)
|
| 30 |
+
output = model.predict_with_threshold(new_data)
|
| 31 |
+
# Convert the output to an integer
|
| 32 |
+
prediction = int(output[0])
|
| 33 |
+
return jsonify(prediction)
|
| 34 |
+
|
| 35 |
+
@app.route('/predict', methods=['POST'])
|
| 36 |
+
def predict():
|
| 37 |
+
# Get data from the HTML form and create a DataFrame
|
| 38 |
+
data = {
|
| 39 |
+
'gender': [request.form['gender']],
|
| 40 |
+
'age': [int(request.form['age'])],
|
| 41 |
+
'hypertension': [int(request.form['hypertension'])],
|
| 42 |
+
'heart_disease': [int(request.form['heart_disease'])],
|
| 43 |
+
'ever_married': [request.form['ever_married']],
|
| 44 |
+
'work_type': [request.form['work_type']],
|
| 45 |
+
'Residence_type': [request.form['Residence_type']],
|
| 46 |
+
'avg_glucose_level': [float(request.form['avg_glucose_level'])],
|
| 47 |
+
'bmi': [float(request.form['bmi'])],
|
| 48 |
+
'smoking_status': [request.form['smoking_status']]
|
| 49 |
+
}
|
| 50 |
+
data_df = pd.DataFrame(data)
|
| 51 |
+
print(data_df)
|
| 52 |
+
|
| 53 |
+
# Transform the data using the preprocessor
|
| 54 |
+
final_input = preprocessor.transform(data_df)
|
| 55 |
+
|
| 56 |
+
# Make predictions using the model
|
| 57 |
+
output = model.predict_with_threshold(final_input)
|
| 58 |
+
prediction = int(output[0])
|
| 59 |
+
|
| 60 |
+
# # Define the prediction message
|
| 61 |
+
# if prediction == 1:
|
| 62 |
+
# prediction_message = "There is an indication of stroke."
|
| 63 |
+
# else:
|
| 64 |
+
# prediction_message = "There is no indication of stroke."
|
| 65 |
+
|
| 66 |
+
return render_template("home.html", prediction_text=prediction)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
app.run(host="0.0.0.0",port=5000)
|
artifacts/best_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:261538acd5a93027d8a8f9ac6703a47e042707cc88f20e6d19f08588f73adfd2
|
| 3 |
+
size 14859001
|
artifacts/healthcare-dataset-stroke-data-cleaned.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
artifacts/preprocessor.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d1dbb821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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<title>Stroke Prediction</title>
|
| 7 |
+
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
|
| 8 |
+
<link href='https://fonts.googleapis.com/css?family=Arimo' rel='stylesheet' type='text/css'>
|
| 9 |
+
<link href='https://fonts.googleapis.com/css?family=Hind:300' rel='stylesheet' type='text/css'>
|
| 10 |
+
<link href='https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
|
| 11 |
+
<style>
|
| 12 |
+
.login {
|
| 13 |
+
width: 300px;
|
| 14 |
+
margin: 0 auto;
|
| 15 |
+
padding: 20px;
|
| 16 |
+
border: 1px solid #ccc;
|
| 17 |
+
border-radius: 5px;
|
| 18 |
+
background: #f9f9f9;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
.login h1 {
|
| 22 |
+
font-family: 'Pacifico', cursive;
|
| 23 |
+
font-size: 28px;
|
| 24 |
+
text-align: center;
|
| 25 |
+
color: #333;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
.login form {
|
| 29 |
+
font-family: 'Arimo', sans-serif;
|
| 30 |
+
font-size: 14px;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.login form select,
|
| 34 |
+
.login form input {
|
| 35 |
+
width: 100%;
|
| 36 |
+
padding: 10px;
|
| 37 |
+
margin-bottom: 10px;
|
| 38 |
+
border: 1px solid #ccc;
|
| 39 |
+
border-radius: 3px;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
.login form select {
|
| 43 |
+
height: 35px;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
.login form button {
|
| 47 |
+
width: 100%;
|
| 48 |
+
padding: 10px;
|
| 49 |
+
background: #333;
|
| 50 |
+
border: none;
|
| 51 |
+
border-radius: 3px;
|
| 52 |
+
color: #fff;
|
| 53 |
+
font-weight: bold;
|
| 54 |
+
cursor: pointer;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.login form button:hover {
|
| 58 |
+
background: #555;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
.prediction-result {
|
| 62 |
+
text-align: center;
|
| 63 |
+
font-family: 'Arimo', sans-serif;
|
| 64 |
+
font-size: 18px;
|
| 65 |
+
margin-top: 20px;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.indication {
|
| 69 |
+
color: red;
|
| 70 |
+
font-weight: bold;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.no-indication {
|
| 74 |
+
color: green;
|
| 75 |
+
font-weight: bold;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
.error {
|
| 79 |
+
color: #FF5733;
|
| 80 |
+
font-weight: bold;
|
| 81 |
+
}
|
| 82 |
+
</style>
|
| 83 |
+
</head>
|
| 84 |
+
|
| 85 |
+
<body>
|
| 86 |
+
<div class="login">
|
| 87 |
+
<h1>Stroke Prediction</h1>
|
| 88 |
+
|
| 89 |
+
<!-- Main Input For Receiving Query to our ML -->
|
| 90 |
+
<form action="{{ url_for('predict') }}" method="post">
|
| 91 |
+
<!-- Section 1: Gender -->
|
| 92 |
+
<label for="gender">Select Gender:</label>
|
| 93 |
+
<select name="gender">
|
| 94 |
+
<option value="Male">Male</option>
|
| 95 |
+
<option value="Female">Female</option>
|
| 96 |
+
<option value="Other">Other</option>
|
| 97 |
+
</select><br>
|
| 98 |
+
|
| 99 |
+
<!-- Section 2: Age -->
|
| 100 |
+
<label for="age">Enter Age:</label>
|
| 101 |
+
<input type="number" step="1" name="age" placeholder="Age" required="required" /><br>
|
| 102 |
+
|
| 103 |
+
<!-- Section 3: Average Glucose Level -->
|
| 104 |
+
<label for="avg_glucose_level">Enter Average Glucose Level:</label>
|
| 105 |
+
<input type="number" step="any" name="avg_glucose_level" placeholder="Average Glucose Level" required="required" /><br>
|
| 106 |
+
|
| 107 |
+
<!-- Section 4: BMI (Body Mass Index) -->
|
| 108 |
+
<label for "bmi">Enter BMI (Body Mass Index):</label>
|
| 109 |
+
<input type="number" step="any" name="bmi" placeholder="BMI" required="required" /><br>
|
| 110 |
+
|
| 111 |
+
<!-- Section 5: Hypertension -->
|
| 112 |
+
<label for="hypertension">Select Hypertension Status:</label>
|
| 113 |
+
<select name="hypertension">
|
| 114 |
+
<option value="0">No Hypertension</option>
|
| 115 |
+
<option value="1">Hypertension</option>
|
| 116 |
+
</select><br>
|
| 117 |
+
|
| 118 |
+
<!-- Section 6: Heart Disease -->
|
| 119 |
+
<label for="heart_disease">Select Heart Disease Status:</label>
|
| 120 |
+
<select name="heart_disease">
|
| 121 |
+
<option value="0">No Heart Disease</option>
|
| 122 |
+
<option value="1">Heart Disease</option>
|
| 123 |
+
</select><br>
|
| 124 |
+
|
| 125 |
+
<!-- Section 7: Ever Married -->
|
| 126 |
+
<label for="ever_married">Select Marital Status:</label>
|
| 127 |
+
<select name="ever_married">
|
| 128 |
+
<option value="Yes">Yes</option>
|
| 129 |
+
<option value="No">No</option>
|
| 130 |
+
</select><br>
|
| 131 |
+
|
| 132 |
+
<!-- Section 8: Work Type -->
|
| 133 |
+
<label for="work_type">Select Work Type:</label>
|
| 134 |
+
<select name="work_type">
|
| 135 |
+
<option value="Private">Private</option>
|
| 136 |
+
<option value="Self-employed">Self-employed</option>
|
| 137 |
+
<option value="Govt_job">Govt_job</option>
|
| 138 |
+
<option value="children">Children</option>
|
| 139 |
+
<option value="Never_worked">Never_worked</option>
|
| 140 |
+
</select><br>
|
| 141 |
+
|
| 142 |
+
<!-- Section 9: Residence Type -->
|
| 143 |
+
<label for="Residence_type">Select Residence Type:</label>
|
| 144 |
+
<select name="Residence_type">
|
| 145 |
+
<option value="Urban">Urban</option>
|
| 146 |
+
<option value="Rural">Rural</option>
|
| 147 |
+
</select><br>
|
| 148 |
+
|
| 149 |
+
<!-- Section 10: Smoking Status -->
|
| 150 |
+
<label for="smoking_status">Select Smoking Status:</label>
|
| 151 |
+
<select name="smoking_status">
|
| 152 |
+
<option value="formerly smoked">Formerly Smoked</option>
|
| 153 |
+
<option value="never smoked">Never Smoked</option>
|
| 154 |
+
<option value="smokes">Smokes</option>
|
| 155 |
+
<option value="Unknown">Unknown</option>
|
| 156 |
+
</select><br>
|
| 157 |
+
|
| 158 |
+
<button type="submit" class="btn btn-primary btn-block btn-large">Do The Prediction</button>
|
| 159 |
+
</form>
|
| 160 |
+
|
| 161 |
+
<br>
|
| 162 |
+
<br>
|
| 163 |
+
|
| 164 |
+
<!-- Section 11: Prediction Result -->
|
| 165 |
+
<div class="prediction-result">
|
| 166 |
+
<p>Prediction Result:</p>
|
| 167 |
+
{% if prediction_text == 1 %}
|
| 168 |
+
<p class="indication">There is an indication of stroke.</p>
|
| 169 |
+
{% elif prediction_text == 0 %}
|
| 170 |
+
<p class="no-indication">There is no indication of stroke.</p>
|
| 171 |
+
{% else %}
|
| 172 |
+
<p class="error">Prediction result unavailable.</p>
|
| 173 |
+
{% endif %}
|
| 174 |
+
</div>
|
| 175 |
+
</div>
|
| 176 |
+
</body>
|
| 177 |
+
|
| 178 |
+
</html>
|
| 179 |
+
|