Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,6 +61,7 @@ def home():
|
|
| 61 |
@app.route('/predict', methods=['POST'])
|
| 62 |
def predict():
|
| 63 |
global model
|
|
|
|
| 64 |
if model is None:
|
| 65 |
try:
|
| 66 |
model = joblib.load('stroke_prediction_model.pkl')
|
|
@@ -71,16 +72,19 @@ def predict():
|
|
| 71 |
|
| 72 |
try:
|
| 73 |
data = request.json
|
|
|
|
| 74 |
if not data or 'input' not in data:
|
|
|
|
| 75 |
return jsonify({'success': False, 'error': 'Invalid input data'}), 400
|
| 76 |
input_data = data['input']
|
| 77 |
-
print(f"
|
| 78 |
|
|
|
|
| 79 |
features = {
|
| 80 |
'age': float(input_data.get('age', 0)),
|
| 81 |
'gender': input_data.get('gender', 'Unknown'),
|
| 82 |
'ever_married': input_data.get('ever_married', 'No'),
|
| 83 |
-
'
|
| 84 |
'work_type': input_data.get('work_type', 'Unknown'),
|
| 85 |
'hypertension': int(input_data.get('hypertension', 0)),
|
| 86 |
'heart_disease': int(input_data.get('heart_disease', 0)),
|
|
@@ -88,15 +92,17 @@ def predict():
|
|
| 88 |
'bmi': float(input_data.get('bmi', 0)),
|
| 89 |
'smoking_status': input_data.get('smoking_status', 'Unknown')
|
| 90 |
}
|
| 91 |
-
print(f"
|
| 92 |
|
|
|
|
| 93 |
df = pd.DataFrame([features])
|
| 94 |
-
categorical_cols = ['gender', 'ever_married', '
|
| 95 |
df = pd.get_dummies(df, columns=categorical_cols, drop_first=True)
|
| 96 |
|
|
|
|
| 97 |
expected_columns = model.feature_names_in_ if hasattr(model, 'feature_names_in_') else [
|
| 98 |
'age', 'hypertension', 'heart_disease', 'avg_glucose_level', 'bmi',
|
| 99 |
-
'gender_Male', 'gender_Other', 'ever_married_Yes', '
|
| 100 |
'work_type_Govt_job', 'work_type_Never_worked', 'work_type_Private',
|
| 101 |
'work_type_Self-employed',
|
| 102 |
'smoking_status_formerly smoked', 'smoking_status_never smoked',
|
|
@@ -104,22 +110,25 @@ def predict():
|
|
| 104 |
]
|
| 105 |
print(f"Expected columns: {expected_columns}")
|
| 106 |
|
|
|
|
| 107 |
for col in expected_columns:
|
| 108 |
if col not in df.columns:
|
| 109 |
df[col] = 0
|
| 110 |
df = df[expected_columns]
|
| 111 |
|
|
|
|
| 112 |
probability = model.predict_proba(df)[0][1] * 100
|
| 113 |
risk_level = 'High' if probability > 50 else 'Moderate' if probability > 25 else 'Low'
|
| 114 |
|
|
|
|
| 115 |
contributing_factors = {
|
| 116 |
-
'age': features['age'] > 45,
|
| 117 |
-
'hypertension': features['hypertension'] == 1,
|
| 118 |
-
'heart_disease': features['heart_disease'] == 1,
|
| 119 |
'glucose': features['avg_glucose_level'] > 140,
|
|
|
|
|
|
|
| 120 |
'smoking': features['smoking_status'] in ['smokes', 'formerly smoked']
|
| 121 |
}
|
| 122 |
|
|
|
|
| 123 |
conn = sqlite3.connect('/tmp/submissions.db')
|
| 124 |
c = conn.cursor()
|
| 125 |
c.execute('''
|
|
@@ -133,7 +142,7 @@ def predict():
|
|
| 133 |
features['age'],
|
| 134 |
features['gender'],
|
| 135 |
features['ever_married'],
|
| 136 |
-
features['
|
| 137 |
features['work_type'],
|
| 138 |
features['hypertension'],
|
| 139 |
features['heart_disease'],
|
|
@@ -146,6 +155,7 @@ def predict():
|
|
| 146 |
conn.commit()
|
| 147 |
conn.close()
|
| 148 |
|
|
|
|
| 149 |
return jsonify({
|
| 150 |
'success': True,
|
| 151 |
'probability': round(probability),
|
|
|
|
| 61 |
@app.route('/predict', methods=['POST'])
|
| 62 |
def predict():
|
| 63 |
global model
|
| 64 |
+
print("Received predict request")
|
| 65 |
if model is None:
|
| 66 |
try:
|
| 67 |
model = joblib.load('stroke_prediction_model.pkl')
|
|
|
|
| 72 |
|
| 73 |
try:
|
| 74 |
data = request.json
|
| 75 |
+
print(f"Received JSON data: {data}")
|
| 76 |
if not data or 'input' not in data:
|
| 77 |
+
print("Invalid input data format")
|
| 78 |
return jsonify({'success': False, 'error': 'Invalid input data'}), 400
|
| 79 |
input_data = data['input']
|
| 80 |
+
print(f"Processed input data: {input_data}")
|
| 81 |
|
| 82 |
+
# Validate and convert input data
|
| 83 |
features = {
|
| 84 |
'age': float(input_data.get('age', 0)),
|
| 85 |
'gender': input_data.get('gender', 'Unknown'),
|
| 86 |
'ever_married': input_data.get('ever_married', 'No'),
|
| 87 |
+
'residence_type': input_data.get('residence_type', 'Unknown'), # Matches form
|
| 88 |
'work_type': input_data.get('work_type', 'Unknown'),
|
| 89 |
'hypertension': int(input_data.get('hypertension', 0)),
|
| 90 |
'heart_disease': int(input_data.get('heart_disease', 0)),
|
|
|
|
| 92 |
'bmi': float(input_data.get('bmi', 0)),
|
| 93 |
'smoking_status': input_data.get('smoking_status', 'Unknown')
|
| 94 |
}
|
| 95 |
+
print(f"Converted features: {features}")
|
| 96 |
|
| 97 |
+
# Create DataFrame and handle categorical variables
|
| 98 |
df = pd.DataFrame([features])
|
| 99 |
+
categorical_cols = ['gender', 'ever_married', 'residence_type', 'work_type', 'smoking_status']
|
| 100 |
df = pd.get_dummies(df, columns=categorical_cols, drop_first=True)
|
| 101 |
|
| 102 |
+
# Define expected columns based on model training
|
| 103 |
expected_columns = model.feature_names_in_ if hasattr(model, 'feature_names_in_') else [
|
| 104 |
'age', 'hypertension', 'heart_disease', 'avg_glucose_level', 'bmi',
|
| 105 |
+
'gender_Male', 'gender_Other', 'ever_married_Yes', 'residence_type_Urban',
|
| 106 |
'work_type_Govt_job', 'work_type_Never_worked', 'work_type_Private',
|
| 107 |
'work_type_Self-employed',
|
| 108 |
'smoking_status_formerly smoked', 'smoking_status_never smoked',
|
|
|
|
| 110 |
]
|
| 111 |
print(f"Expected columns: {expected_columns}")
|
| 112 |
|
| 113 |
+
# Align DataFrame columns
|
| 114 |
for col in expected_columns:
|
| 115 |
if col not in df.columns:
|
| 116 |
df[col] = 0
|
| 117 |
df = df[expected_columns]
|
| 118 |
|
| 119 |
+
# Make prediction
|
| 120 |
probability = model.predict_proba(df)[0][1] * 100
|
| 121 |
risk_level = 'High' if probability > 50 else 'Moderate' if probability > 25 else 'Low'
|
| 122 |
|
| 123 |
+
# Determine contributing factors
|
| 124 |
contributing_factors = {
|
|
|
|
|
|
|
|
|
|
| 125 |
'glucose': features['avg_glucose_level'] > 140,
|
| 126 |
+
'hypertension': features['hypertension'] == 1,
|
| 127 |
+
'heartDisease': features['heart_disease'] == 1, # Matches index.html
|
| 128 |
'smoking': features['smoking_status'] in ['smokes', 'formerly smoked']
|
| 129 |
}
|
| 130 |
|
| 131 |
+
# Store in database
|
| 132 |
conn = sqlite3.connect('/tmp/submissions.db')
|
| 133 |
c = conn.cursor()
|
| 134 |
c.execute('''
|
|
|
|
| 142 |
features['age'],
|
| 143 |
features['gender'],
|
| 144 |
features['ever_married'],
|
| 145 |
+
features['residence_type'],
|
| 146 |
features['work_type'],
|
| 147 |
features['hypertension'],
|
| 148 |
features['heart_disease'],
|
|
|
|
| 155 |
conn.commit()
|
| 156 |
conn.close()
|
| 157 |
|
| 158 |
+
print(f"Prediction success: probability={probability}%, risk_level={risk_level}")
|
| 159 |
return jsonify({
|
| 160 |
'success': True,
|
| 161 |
'probability': round(probability),
|