Spaces:
Sleeping
Sleeping
Update app.py
Browse filesupload app script
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import joblib
|
|
| 6 |
model = joblib.load('heart_disease_model.joblib')
|
| 7 |
model_columns = joblib.load('model_columns.joblib')
|
| 8 |
|
| 9 |
-
# --- 2. Define the Prediction Function (
|
| 10 |
def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
|
| 11 |
input_data = {
|
| 12 |
'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
|
|
@@ -15,17 +15,21 @@ def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
|
|
| 15 |
}
|
| 16 |
input_df = pd.DataFrame([input_data])
|
| 17 |
|
|
|
|
| 18 |
categorical_cols = ['cp', 'restecg', 'slope', 'thal']
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
prediction = model.predict(input_df)
|
| 27 |
-
prediction_proba = model.predict_proba(input_df)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if prediction[0] == 1:
|
| 30 |
result_text = "<div style='color: #ef4444; font-size: 24px; font-weight: bold;'>High Risk of Heart Disease</div>"
|
| 31 |
confidence = prediction_proba[0][1]
|
|
@@ -35,80 +39,30 @@ def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
|
|
| 35 |
|
| 36 |
return f"{result_text}<br><div style='margin-top: 10px; color: #a1a1aa;'>Confidence: {confidence*100:.2f}%</div>"
|
| 37 |
|
| 38 |
-
# --- 3. Custom CSS for
|
| 39 |
custom_css = """
|
| 40 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
|
| 41 |
.gradio-container { background-color: #1f2937; border-radius: 15px !important; }
|
| 42 |
|
| 43 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
.gradio-slider-track-color { background-color: #3b82f6 !important; }
|
| 45 |
|
| 46 |
-
/*
|
| 47 |
#buttons-row { display: flex; gap: 10px; justify-content: center; width: 100%; }
|
| 48 |
#buttons-row > button { flex-grow: 1; }
|
| 49 |
.gradio-button { transition: all 0.2s ease-in-out !important; transform: scale(1.0); }
|
| 50 |
.gradio-button:hover { transform: scale(1.03); filter: brightness(1.1); }
|
| 51 |
.gradio-button:active { transform: scale(0.98); }
|
| 52 |
-
|
| 53 |
-
/* Submit button is now RED */
|
| 54 |
.gradio-button.primary { background-color: #ef4444 !important; border-color: #ef4444 !important; }
|
| 55 |
.gradio-button.primary:hover { background-color: #dc2626 !important; border-color: #dc2626 !important; }
|
| 56 |
|
| 57 |
-
/* Clear button is now GRAY */
|
| 58 |
-
.gradio-button.secondary { background-color: #6b7280 !important; border-color: #6b7280 !important; }
|
| 59 |
-
.gradio-button.secondary:hover { background-color: #4b5563 !important; border-color: #4b5563 !important; }
|
| 60 |
-
|
| 61 |
-
/* --- Radio Button Color (Selected) --- */
|
| 62 |
-
.gradio-radio-button.selected { background-color: #34d399 !important; color: white !important; border-color: #34d399 !important; }
|
| 63 |
-
|
| 64 |
-
/* --- Permanent Result Box Styling (No changes here) --- */
|
| 65 |
-
#result-box {
|
| 66 |
-
background-color: #374151; border: 1px solid #4b5563; padding: 20px;
|
| 67 |
-
border-radius: 10px; text-align: center; min-height: 120px;
|
| 68 |
-
display: flex; align-items: center; justify-content: center;
|
| 69 |
-
flex-direction: column; color: #e5e7eb;
|
| 70 |
-
}
|
| 71 |
-
"""
|
| 72 |
-
|
| 73 |
-
# --- 4. Define the Custom Theme for ORANGE Labels ---
|
| 74 |
-
# We removed primary_hue and are only setting the label color here.
|
| 75 |
-
theme = gr.themes.Soft().set(
|
| 76 |
-
block_label_text_color_dark="#f97316", # Orange color for labels
|
| 77 |
-
block_label_text_color="#f97316"
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# --- 5. Build the UI with Gradio Blocks ---
|
| 82 |
-
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 83 |
-
gr.Markdown("# ❤️ CardioCompass: Heart Disease Predictor")
|
| 84 |
-
gr.Markdown("Enter patient details to predict the likelihood of having heart disease.")
|
| 85 |
-
|
| 86 |
-
with gr.Row():
|
| 87 |
-
with gr.Column(scale=2):
|
| 88 |
-
age_in = gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years")
|
| 89 |
-
sex_in = gr.Radio(choices=[(0.0, "Female"), (1.0, "Male")], label="Sex", value=0.0)
|
| 90 |
-
cp_in = gr.Radio(choices=[(1.0, "Typical Angina"), (2.0, "Atypical Angina"), (3.0, "Non-anginal Pain"), (4.0, "Asymptomatic")], label="Chest Pain Type (cp)")
|
| 91 |
-
restbp_in = gr.Slider(minimum=94, maximum=200, value=120, label="Resting Blood Pressure (trestbps)", info="in mm Hg")
|
| 92 |
-
chol_in = gr.Slider(minimum=126, maximum=564, value=200, label="Cholesterol (chol)", info="in mg/dl")
|
| 93 |
-
thalach_in = gr.Slider(minimum=71, maximum=202, value=150, label="Max Heart Rate Achieved (thalach)")
|
| 94 |
-
exang_in = gr.Radio(choices=[(0.0, "No"), (1.0, "Yes")], label="Exercise Induced Angina (exang)")
|
| 95 |
-
ca_in = gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)")
|
| 96 |
-
thal_in = gr.Radio(choices=[(3.0, "Normal"), (6.0, "Fixed defect"), (7.0, "Reversible defect")], label="Thalassemia (thal)")
|
| 97 |
-
|
| 98 |
-
with gr.Column(scale=1):
|
| 99 |
-
result_out = gr.HTML(value="<div style='color: #9ca3af;'>Prediction will appear here...</div>", elem_id="result-box")
|
| 100 |
-
with gr.Row(elem_id="buttons-row"):
|
| 101 |
-
clear_button = gr.ClearButton(value="Clear", variant="secondary") # Using secondary variant
|
| 102 |
-
submit_button = gr.Button("Submit", variant="primary") # Using primary variant
|
| 103 |
-
|
| 104 |
-
# --- 6. Connect the components ---
|
| 105 |
-
inputs_list = [age_in, sex_in, cp_in, restbp_in, chol_in, thalach_in, exang_in, ca_in, thal_in]
|
| 106 |
-
submit_button.click(fn=predict_heart_disease, inputs=inputs_list, outputs=result_out)
|
| 107 |
-
clear_button.add(inputs_list)
|
| 108 |
-
|
| 109 |
-
# --- 7. Launch the App ---
|
| 110 |
-
demo.launch(share=True)
|
| 111 |
-
|
| 112 |
|
| 113 |
|
| 114 |
|
|
|
|
| 6 |
model = joblib.load('heart_disease_model.joblib')
|
| 7 |
model_columns = joblib.load('model_columns.joblib')
|
| 8 |
|
| 9 |
+
# --- 2. Define the Prediction Function (with the fix) ---
|
| 10 |
def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
|
| 11 |
input_data = {
|
| 12 |
'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
|
|
|
|
| 15 |
}
|
| 16 |
input_df = pd.DataFrame([input_data])
|
| 17 |
|
| 18 |
+
# --- Preprocess the data just like in your notebook ---
|
| 19 |
categorical_cols = ['cp', 'restecg', 'slope', 'thal']
|
| 20 |
+
input_df_processed = pd.get_dummies(input_df, columns=categorical_cols)
|
| 21 |
|
| 22 |
+
# --- THIS IS THE KEY FIX ---
|
| 23 |
+
# Reindex the processed input to match the model's training columns.
|
| 24 |
+
# This adds any missing dummy columns and fills them with 0.
|
| 25 |
+
input_df_reindexed = input_df_processed.reindex(columns=model_columns, fill_value=0)
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# --- Make the Prediction ---
|
| 29 |
+
prediction = model.predict(input_df_reindexed)
|
| 30 |
+
prediction_proba = model.predict_proba(input_df_reindexed)
|
| 31 |
+
|
| 32 |
+
# --- Format the Output ---
|
| 33 |
if prediction[0] == 1:
|
| 34 |
result_text = "<div style='color: #ef4444; font-size: 24px; font-weight: bold;'>High Risk of Heart Disease</div>"
|
| 35 |
confidence = prediction_proba[0][1]
|
|
|
|
| 39 |
|
| 40 |
return f"{result_text}<br><div style='margin-top: 10px; color: #a1a1aa;'>Confidence: {confidence*100:.2f}%</div>"
|
| 41 |
|
| 42 |
+
# --- 3. Custom CSS for Styling (No changes here) ---
|
| 43 |
custom_css = """
|
| 44 |
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
|
| 45 |
.gradio-container { background-color: #1f2937; border-radius: 15px !important; }
|
| 46 |
|
| 47 |
+
/* Input component labels (e.g., "Age", "Sex") */
|
| 48 |
+
.gradio-form > div > .label { color: #f97316 !important; font-weight: bold !important; font-size: 1.1em !important; }
|
| 49 |
+
|
| 50 |
+
/* Radio Buttons */
|
| 51 |
+
.gradio-radio-button { background-color: #374151; color: #d1d5db; border: 1px solid #4b5563; }
|
| 52 |
+
.gradio-radio-button.selected { background-color: #34d399 !important; color: white !important; border-color: #34d399 !important; }
|
| 53 |
+
|
| 54 |
+
/* Slider Color */
|
| 55 |
.gradio-slider-track-color { background-color: #3b82f6 !important; }
|
| 56 |
|
| 57 |
+
/* Button Colors and Interactivity */
|
| 58 |
#buttons-row { display: flex; gap: 10px; justify-content: center; width: 100%; }
|
| 59 |
#buttons-row > button { flex-grow: 1; }
|
| 60 |
.gradio-button { transition: all 0.2s ease-in-out !important; transform: scale(1.0); }
|
| 61 |
.gradio-button:hover { transform: scale(1.03); filter: brightness(1.1); }
|
| 62 |
.gradio-button:active { transform: scale(0.98); }
|
|
|
|
|
|
|
| 63 |
.gradio-button.primary { background-color: #ef4444 !important; border-color: #ef4444 !important; }
|
| 64 |
.gradio-button.primary:hover { background-color: #dc2626 !important; border-color: #dc2626 !important; }
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
|