File size: 6,141 Bytes
8219bab
 
 
 
c550c93
 
8219bab
 
 
c550c93
8219bab
 
 
 
 
 
 
 
 
c550c93
8219bab
c550c93
 
 
 
8219bab
c550c93
 
8219bab
c550c93
8219bab
c550c93
8219bab
 
c550c93
8219bab
 
c550c93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8219bab
c550c93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb9198e
5a6c80e
 
 
 
 
 
 
509ac8f
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import gradio as gr
import pandas as pd
import joblib

# --- Load the Saved Model and Columns ---
# Ensure these files are in the same directory as your app.py
model = joblib.load('heart_disease_model.joblib')
model_columns = joblib.load('model_columns.joblib')

# --- Define the Prediction Function ---
def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
    input_data = {
        'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
        'fbs': 0.0, 'restecg': 0.0, 'thalach': thalach, 'exang': exang,
        'oldpeak': 1.0, 'slope': 2.0, 'ca': ca, 'thal': thal
    }
    input_df = pd.DataFrame([input_data])

    categorical_cols = ['cp', 'restecg', 'slope', 'thal']
    input_df = pd.get_dummies(input_df, columns=categorical_cols)

    for col in model_columns:
        if col not in input_df.columns:
            input_df[col] = 0
    input_df = input_df[model_columns]

    prediction = model.predict(input_df)
    prediction_proba = model.predict_proba(input_df)

    # --- Format the Output with HTML for styling ---
    if prediction[0] == 1:
        result_text = "<span style='color: #FF6347; font-weight: bold;'>High Risk of Heart Disease</span>"
        confidence = prediction_proba[0][1]
    else:
        result_text = "<span style='color: #3CB371; font-weight: bold;'>Low Risk of Heart Disease</span>"
        confidence = prediction_proba[0][0]

    return f"{result_text}<br>Confidence: {confidence*100:.2f}%"

# --- Gradio Interface Setup ---

# Define the input components for the UI
# We're making some inputs more visually appealing and grouped
inputs = [
    gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years"),
    gr.Radio(choices=[0.0, 1.0], label="Sex", value=0.0, info="0=Female, 1=Male"),
    gr.Radio(choices=[1.0, 2.0, 3.0, 4.0], label="Chest Pain Type (cp)", value=1.0,
             info="1: Typical Angina, 2: Atypical Angina, 3: Non-anginal Pain, 4: Asymptomatic"),
    gr.Slider(minimum=94, maximum=200, value=120, label="Resting Blood Pressure (trestbps)", info="in mm Hg"),
    gr.Slider(minimum=126, maximum=564, value=200, label="Cholesterol (chol)", info="in mg/dl"),
    gr.Slider(minimum=71, maximum=202, value=150, label="Max Heart Rate Achieved (thalach)", info="Max heart rate during exercise"),
    gr.Radio(choices=[0.0, 1.0], label="Exercise Induced Angina (exang)", value=0.0,
             info="1=Yes, 0=No"),
    gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)", value=0.0,
             info="Number of major vessels (0-3) colored by fluoroscopy"),
    gr.Radio(choices=[3.0, 6.0, 7.0], label="Thalassemia (thal)", value=3.0,
             info="3: Normal, 6: Fixed defect, 7: Reversible defect")
]

# Output component - using gr.HTML to display rich text, avoiding the scrollbar
outputs = gr.HTML(label="Prediction Result") # Use HTML to render our styled output

# --- Custom CSS for a more attractive and colorful look ---
custom_css = """
body {
    background-color: #2c2c2c; /* Dark background */
    color: #e0e0e0; /* Light text */
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.gradio-container {
    background-color: #3a3a3a;
    border-radius: 15px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
    padding: 30px;
}
.gradio-header {
    background-color: #e91e63; /* Pink header */
    color: white;
    padding: 20px;
    border-radius: 10px 10px 0 0;
    font-size: 2.2em;
    font-weight: bold;
    text-align: center;
    margin-bottom: 20px;
}
.gradio-description {
    color: #bdbdbd;
    text-align: center;
    margin-bottom: 30px;
    font-size: 1.1em;
}
.gradio-label {
    color: #81c784; /* Green for labels */
    font-weight: bold;
    font-size: 1.1em;
    margin-bottom: 5px;
}
.gradio-input label { /* Targeting specific input labels */
    color: #81c784 !important;
}
.gradio-input input[type='number'],
.gradio-input input[type='text'] {
    background-color: #4a4a4a;
    color: #e0e0e0;
    border: 1px solid #5a5a5a;
    border-radius: 5px;
}
.gradio-slider-track-color { /* Slider color */
    background-color: #e91e63 !important;
}
.gradio-radio-button.selected { /* Selected radio button */
    background-color: #e91e63 !important;
    color: white !important;
    border-color: #e91e63 !important;
}
.gradio-button {
    background-color: #e91e63 !important; /* Pink button */
    color: white !important;
    border: none;
    padding: 12px 25px;
    font-size: 1.1em;
    border-radius: 8px;
    transition: background-color 0.3s ease;
}
.gradio-button:hover {
    background-color: #c2185b !important; /* Darker pink on hover */
}
/* Style for the output box */
.gradio-output-text {
    background-color: #4a4a4a;
    padding: 20px;
    border-radius: 10px;
    font-size: 1.2em;
    text-align: center;
    margin-top: 20px;
    min-height: 80px; /* Ensure sufficient height */
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
/* Further refined styling for the HTML output */
#component-20 { /* Adjust this ID if your output component ID changes */
    background-color: #4a4a4a;
    padding: 20px;
    border-radius: 10px;
    font-size: 1.2em;
    text-align: center;
    margin-top: 20px;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #e0e0e0; /* Default text color for the output box */
}
"""

# --- Create the Interface and Launch ---
iface = gr.Interface(
    fn=predict_heart_disease,
    inputs=inputs,
    outputs=outputs,
    title="❤️ CardioCompass: Heart Disease Predictor",
    description="Enter patient details to predict the likelihood of having heart disease. Based on the UCI Heart Disease dataset.",
    css=custom_css, # Apply our custom CSS
    theme=gr.themes.Soft() # Use a modern Gradio theme as a base
)

# Launch the app. The 'share=True' will create a temporary public link in Colab!
# For Hugging Face Spaces, you'd typically remove share=True as the space itself is public.
iface.launch(share=True)