CardioCompass / app.py
Nadeem005's picture
Update app.py
c550c93 verified
Raw History Blame Contribute Delete
6.14 kB
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)