Spaces:
Build error
Build error
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.preprocessing import StandardScaler
|
4 |
+
import joblib
|
5 |
+
|
6 |
+
# Load the trained model
|
7 |
+
model = joblib.load('your_trained_model.pkl')
|
8 |
+
|
9 |
+
# Define the function to preprocess user inputs
|
10 |
+
def preprocess_input(age, sex, cp_type, resting_bp, cholesterol, fasting_bs, ecg, max_hr, exercise_angina, oldpeak, st_slope):
|
11 |
+
# Convert categorical features to one-hot encoding
|
12 |
+
sex_mapping = {'F': [0, 1], 'M': [1, 0]}
|
13 |
+
sex_encoded = sex_mapping.get(sex, [0, 0])
|
14 |
+
|
15 |
+
cp_mapping = {'ASY': [1, 0, 0, 0], 'ATA': [0, 1, 0, 0], 'NAP': [0, 0, 1, 0], 'TA': [0, 0, 0, 1]}
|
16 |
+
cp_encoded = cp_mapping.get(cp_type, [0, 0, 0, 0])
|
17 |
+
|
18 |
+
ecg_mapping = {'Normal': [1, 0, 0], 'ST': [0, 1, 0], 'LVH': [0, 0, 1]}
|
19 |
+
ecg_encoded = ecg_mapping.get(ecg, [0, 0, 0])
|
20 |
+
|
21 |
+
# Normalize numerical features
|
22 |
+
numerical_features = [age, resting_bp, cholesterol, max_hr, oldpeak]
|
23 |
+
numerical_features = np.array(numerical_features).reshape(1, -1)
|
24 |
+
scaler = StandardScaler()
|
25 |
+
numerical_features_scaled = scaler.fit_transform(numerical_features)
|
26 |
+
|
27 |
+
# Combine all features
|
28 |
+
processed_input = np.concatenate([numerical_features_scaled, [fasting_bs], sex_encoded, cp_encoded, ecg_encoded, [exercise_angina], [st_slope]], axis=1)
|
29 |
+
|
30 |
+
return processed_input
|
31 |
+
|
32 |
+
# Define the function to make predictions
|
33 |
+
def predict_heart_disease(age, sex, cp_type, resting_bp, cholesterol, fasting_bs, ecg, max_hr, exercise_angina, oldpeak, st_slope):
|
34 |
+
processed_input = preprocess_input(age, sex, cp_type, resting_bp, cholesterol, fasting_bs, ecg, max_hr, exercise_angina, oldpeak, st_slope)
|
35 |
+
prediction = model.predict(processed_input)[0]
|
36 |
+
return "Heart Disease Likely" if prediction == 1 else "No Heart Disease"
|
37 |
+
|
38 |
+
# Create Gradio interface
|
39 |
+
inputs = [
|
40 |
+
gr.inputs.Slider(0, 100, default=50, label="Age"),
|
41 |
+
gr.inputs.Radio(['F', 'M'], label="Sex"),
|
42 |
+
gr.inputs.Dropdown(['TA', 'ATA', 'NAP', 'ASY'], label="Chest Pain Type"),
|
43 |
+
gr.inputs.Slider(80, 200, default=120, label="Resting Blood Pressure"),
|
44 |
+
gr.inputs.Slider(100, 600, default=200, label="Cholesterol"),
|
45 |
+
gr.inputs.Checkbox(label="Fasting Blood Sugar > 120 mg/dl"),
|
46 |
+
gr.inputs.Dropdown(['Normal', 'ST', 'LVH'], label="Resting ECG"),
|
47 |
+
gr.inputs.Slider(60, 202, default=100, label="Maximum Heart Rate Achieved"),
|
48 |
+
gr.inputs.Radio(['Y', 'N'], label="Exercise-induced Angina"),
|
49 |
+
gr.inputs.Slider(0, 10, default=5, label="Oldpeak"),
|
50 |
+
gr.inputs.Dropdown(['Up', 'Flat', 'Down'], label="ST Slope")
|
51 |
+
]
|
52 |
+
|
53 |
+
output = gr.outputs.Textbox(label="Heart Disease Prediction")
|
54 |
+
|
55 |
+
gr.Interface(predict_heart_disease, inputs, output, title="Heart Disease Prediction", description="Enter patient information to predict likelihood of heart disease.").launch()
|