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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1GVLBcgTt2EmZTg1eSA2_ez5k25akVsN-
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import joblib
|
| 13 |
+
|
| 14 |
+
# --- Load the Saved Model and Columns ---
|
| 15 |
+
# Ensure these files are in the same directory as your app.py
|
| 16 |
+
model = joblib.load('heart_disease_model.joblib')
|
| 17 |
+
model_columns = joblib.load('model_columns.joblib')
|
| 18 |
+
|
| 19 |
+
# --- Define the Prediction Function ---
|
| 20 |
+
def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
|
| 21 |
+
input_data = {
|
| 22 |
+
'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
|
| 23 |
+
'fbs': 0.0, 'restecg': 0.0, 'thalach': thalach, 'exang': exang,
|
| 24 |
+
'oldpeak': 1.0, 'slope': 2.0, 'ca': ca, 'thal': thal
|
| 25 |
+
}
|
| 26 |
+
input_df = pd.DataFrame([input_data])
|
| 27 |
+
|
| 28 |
+
categorical_cols = ['cp', 'restecg', 'slope', 'thal']
|
| 29 |
+
input_df = pd.get_dummies(input_df, columns=categorical_cols)
|
| 30 |
+
|
| 31 |
+
for col in model_columns:
|
| 32 |
+
if col not in input_df.columns:
|
| 33 |
+
input_df[col] = 0
|
| 34 |
+
input_df = input_df[model_columns]
|
| 35 |
+
|
| 36 |
+
prediction = model.predict(input_df)
|
| 37 |
+
prediction_proba = model.predict_proba(input_df)
|
| 38 |
+
|
| 39 |
+
# --- Format the Output with HTML for styling ---
|
| 40 |
+
if prediction[0] == 1:
|
| 41 |
+
result_text = "<span style='color: #FF6347; font-weight: bold;'>High Risk of Heart Disease</span>"
|
| 42 |
+
confidence = prediction_proba[0][1]
|
| 43 |
+
else:
|
| 44 |
+
result_text = "<span style='color: #3CB371; font-weight: bold;'>Low Risk of Heart Disease</span>"
|
| 45 |
+
confidence = prediction_proba[0][0]
|
| 46 |
+
|
| 47 |
+
return f"{result_text}<br>Confidence: {confidence*100:.2f}%"
|
| 48 |
+
|
| 49 |
+
# --- Gradio Interface Setup ---
|
| 50 |
+
|
| 51 |
+
# Define the input components for the UI
|
| 52 |
+
# We're making some inputs more visually appealing and grouped
|
| 53 |
+
inputs = [
|
| 54 |
+
gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years"),
|
| 55 |
+
gr.Radio(choices=[0.0, 1.0], label="Sex", value=0.0, info="0=Female, 1=Male"),
|
| 56 |
+
gr.Radio(choices=[1.0, 2.0, 3.0, 4.0], label="Chest Pain Type (cp)", value=1.0,
|
| 57 |
+
info="1: Typical Angina, 2: Atypical Angina, 3: Non-anginal Pain, 4: Asymptomatic"),
|
| 58 |
+
gr.Slider(minimum=94, maximum=200, value=120, label="Resting Blood Pressure (trestbps)", info="in mm Hg"),
|
| 59 |
+
gr.Slider(minimum=126, maximum=564, value=200, label="Cholesterol (chol)", info="in mg/dl"),
|
| 60 |
+
gr.Slider(minimum=71, maximum=202, value=150, label="Max Heart Rate Achieved (thalach)", info="Max heart rate during exercise"),
|
| 61 |
+
gr.Radio(choices=[0.0, 1.0], label="Exercise Induced Angina (exang)", value=0.0,
|
| 62 |
+
info="1=Yes, 0=No"),
|
| 63 |
+
gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)", value=0.0,
|
| 64 |
+
info="Number of major vessels (0-3) colored by fluoroscopy"),
|
| 65 |
+
gr.Radio(choices=[3.0, 6.0, 7.0], label="Thalassemia (thal)", value=3.0,
|
| 66 |
+
info="3: Normal, 6: Fixed defect, 7: Reversible defect")
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
# Output component - using gr.HTML to display rich text, avoiding the scrollbar
|
| 70 |
+
outputs = gr.HTML(label="Prediction Result") # Use HTML to render our styled output
|
| 71 |
+
|
| 72 |
+
# --- Custom CSS for a more attractive and colorful look ---
|
| 73 |
+
custom_css = """
|
| 74 |
+
body {
|
| 75 |
+
background-color: #2c2c2c; /* Dark background */
|
| 76 |
+
color: #e0e0e0; /* Light text */
|
| 77 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 78 |
+
}
|
| 79 |
+
.gradio-container {
|
| 80 |
+
background-color: #3a3a3a;
|
| 81 |
+
border-radius: 15px;
|
| 82 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
|
| 83 |
+
padding: 30px;
|
| 84 |
+
}
|
| 85 |
+
.gradio-header {
|
| 86 |
+
background-color: #e91e63; /* Pink header */
|
| 87 |
+
color: white;
|
| 88 |
+
padding: 20px;
|
| 89 |
+
border-radius: 10px 10px 0 0;
|
| 90 |
+
font-size: 2.2em;
|
| 91 |
+
font-weight: bold;
|
| 92 |
+
text-align: center;
|
| 93 |
+
margin-bottom: 20px;
|
| 94 |
+
}
|
| 95 |
+
.gradio-description {
|
| 96 |
+
color: #bdbdbd;
|
| 97 |
+
text-align: center;
|
| 98 |
+
margin-bottom: 30px;
|
| 99 |
+
font-size: 1.1em;
|
| 100 |
+
}
|
| 101 |
+
.gradio-label {
|
| 102 |
+
color: #81c784; /* Green for labels */
|
| 103 |
+
font-weight: bold;
|
| 104 |
+
font-size: 1.1em;
|
| 105 |
+
margin-bottom: 5px;
|
| 106 |
+
}
|
| 107 |
+
.gradio-input label { /* Targeting specific input labels */
|
| 108 |
+
color: #81c784 !important;
|
| 109 |
+
}
|
| 110 |
+
.gradio-input input[type='number'],
|
| 111 |
+
.gradio-input input[type='text'] {
|
| 112 |
+
background-color: #4a4a4a;
|
| 113 |
+
color: #e0e0e0;
|
| 114 |
+
border: 1px solid #5a5a5a;
|
| 115 |
+
border-radius: 5px;
|
| 116 |
+
}
|
| 117 |
+
.gradio-slider-track-color { /* Slider color */
|
| 118 |
+
background-color: #e91e63 !important;
|
| 119 |
+
}
|
| 120 |
+
.gradio-radio-button.selected { /* Selected radio button */
|
| 121 |
+
background-color: #e91e63 !important;
|
| 122 |
+
color: white !important;
|
| 123 |
+
border-color: #e91e63 !important;
|
| 124 |
+
}
|
| 125 |
+
.gradio-button {
|
| 126 |
+
background-color: #e91e63 !important; /* Pink button */
|
| 127 |
+
color: white !important;
|
| 128 |
+
border: none;
|
| 129 |
+
padding: 12px 25px;
|
| 130 |
+
font-size: 1.1em;
|
| 131 |
+
border-radius: 8px;
|
| 132 |
+
transition: background-color 0.3s ease;
|
| 133 |
+
}
|
| 134 |
+
.gradio-button:hover {
|
| 135 |
+
background-color: #c2185b !important; /* Darker pink on hover */
|
| 136 |
+
}
|
| 137 |
+
/* Style for the output box */
|
| 138 |
+
.gradio-output-text {
|
| 139 |
+
background-color: #4a4a4a;
|
| 140 |
+
padding: 20px;
|
| 141 |
+
border-radius: 10px;
|
| 142 |
+
font-size: 1.2em;
|
| 143 |
+
text-align: center;
|
| 144 |
+
margin-top: 20px;
|
| 145 |
+
min-height: 80px; /* Ensure sufficient height */
|
| 146 |
+
display: flex;
|
| 147 |
+
align-items: center;
|
| 148 |
+
justify-content: center;
|
| 149 |
+
flex-direction: column;
|
| 150 |
+
}
|
| 151 |
+
/* Further refined styling for the HTML output */
|
| 152 |
+
#component-20 { /* Adjust this ID if your output component ID changes */
|
| 153 |
+
background-color: #4a4a4a;
|
| 154 |
+
padding: 20px;
|
| 155 |
+
border-radius: 10px;
|
| 156 |
+
font-size: 1.2em;
|
| 157 |
+
text-align: center;
|
| 158 |
+
margin-top: 20px;
|
| 159 |
+
min-height: 80px;
|
| 160 |
+
display: flex;
|
| 161 |
+
align-items: center;
|
| 162 |
+
justify-content: center;
|
| 163 |
+
flex-direction: column;
|
| 164 |
+
color: #e0e0e0; /* Default text color for the output box */
|
| 165 |
+
}
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
# --- Create the Interface and Launch ---
|
| 169 |
+
iface = gr.Interface(
|
| 170 |
+
fn=predict_heart_disease,
|
| 171 |
+
inputs=inputs,
|
| 172 |
+
outputs=outputs,
|
| 173 |
+
title="❤️ CardioCompass: Heart Disease Predictor",
|
| 174 |
+
description="Enter patient details to predict the likelihood of having heart disease. Based on the UCI Heart Disease dataset.",
|
| 175 |
+
css=custom_css, # Apply our custom CSS
|
| 176 |
+
theme=gr.themes.Soft() # Use a modern Gradio theme as a base
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Launch the app. The 'share=True' will create a temporary public link in Colab!
|
| 180 |
+
# For Hugging Face Spaces, you'd typically remove share=True as the space itself is public.
|
| 181 |
+
iface.launch(share=True)
|
| 182 |
+
|