Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# 1. Download your model from your repo
|
| 7 |
+
model_path = hf_hub_download(repo_id="Aaravkumar/heart-disease-prediction", filename="heart_disease_model.joblib")
|
| 8 |
+
model = joblib.load(model_path)
|
| 9 |
+
|
| 10 |
+
# 2. Define the prediction function
|
| 11 |
+
def predict(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
| 12 |
+
# Create a dataframe matching your training columns
|
| 13 |
+
input_data = pd.DataFrame([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]],
|
| 14 |
+
columns=['Age', 'Sex', 'Chest pain type', 'BP', 'Cholesterol', 'FBS over 120',
|
| 15 |
+
'EKG results', 'Max HR', 'Exercise angina', 'ST depression',
|
| 16 |
+
'Slope of ST', 'Number of vessels fluro', 'Thallium'])
|
| 17 |
+
|
| 18 |
+
prediction = model.predict(input_data)[0]
|
| 19 |
+
probability = model.predict_proba(input_data)[0][1]
|
| 20 |
+
|
| 21 |
+
result = "❤️ Heart Disease Detected" if prediction == 1 else "✅ No Heart Disease Detected"
|
| 22 |
+
return f"{result} (Confidence: {probability:.2%})"
|
| 23 |
+
|
| 24 |
+
# 3. Create a beautiful UI
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=predict,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Slider(20, 90, value=50, label="Age"),
|
| 29 |
+
gr.Radio([0, 1], label="Sex (1=M, 0=F)"),
|
| 30 |
+
gr.Slider(1, 4, step=1, label="Chest Pain Type"),
|
| 31 |
+
gr.Number(label="Resting BP (trestbps)"),
|
| 32 |
+
gr.Number(label="Cholesterol"),
|
| 33 |
+
gr.Radio([0, 1], label="Fasting Blood Sugar > 120 (1=True)"),
|
| 34 |
+
gr.Slider(0, 2, step=1, label="Resting EKG"),
|
| 35 |
+
gr.Number(label="Max Heart Rate"),
|
| 36 |
+
gr.Radio([0, 1], label="Exercise Induced Angina"),
|
| 37 |
+
gr.Number(label="ST Depression (oldpeak)"),
|
| 38 |
+
gr.Slider(1, 3, step=1, label="Slope of ST"),
|
| 39 |
+
gr.Slider(0, 3, step=1, label="Major Vessels (ca)"),
|
| 40 |
+
gr.Slider(1, 3, step=1, label="Thallium")
|
| 41 |
+
],
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="Heart Disease Risk Analyzer",
|
| 44 |
+
description="Enter patient vitals to predict cardiac risk using Aarav's Random Forest Model."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|