Spaces:
Build error
Build error
Commit
·
ca97150
0
Parent(s):
Duplicate from Jaffermirza17/ProjectPythonClass
Browse filesCo-authored-by: Jaffer Mirza <Jaffermirza17@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +128 -0
- heart_xgb.pkl +3 -0
- requirements.txt +10 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: ProjectPythonClass
|
| 3 |
+
emoji: 📊
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.27.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
duplicated_from: Jaffermirza17/ProjectPythonClass
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import shap
|
| 4 |
+
from shap.plots._force_matplotlib import draw_additive_plot
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# load the model from disk
|
| 10 |
+
loaded_model = pickle.load(open("heart_xgb.pkl", 'rb'))
|
| 11 |
+
|
| 12 |
+
# Setup SHAP
|
| 13 |
+
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 14 |
+
|
| 15 |
+
# Create the main function for server
|
| 16 |
+
def main_func(age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall):
|
| 17 |
+
new_row = pd.DataFrame.from_dict({'age':age,'sex':sex,
|
| 18 |
+
'cp':cp,'trtbps':trtbps,'chol':chol,
|
| 19 |
+
'fbs':fbs, 'restecg':restecg,'thalachh':thalachh,'exng':exng,
|
| 20 |
+
'oldpeak':oldpeak,'slp':slp,'caa':caa,'thall':thall},
|
| 21 |
+
orient = 'index').transpose()
|
| 22 |
+
|
| 23 |
+
prob = loaded_model.predict_proba(new_row)
|
| 24 |
+
|
| 25 |
+
shap_values = explainer(new_row)
|
| 26 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
| 27 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
| 28 |
+
plot = shap.plots.bar(shap_values[0], max_display=8, order=shap.Explanation.abs, show_data='auto', show=False)
|
| 29 |
+
|
| 30 |
+
plt.tight_layout()
|
| 31 |
+
local_plot = plt.gcf()
|
| 32 |
+
plt.close()
|
| 33 |
+
|
| 34 |
+
return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
|
| 35 |
+
|
| 36 |
+
# Create the UI
|
| 37 |
+
title = "**Heart Attack Predictor & Interpreter** 🪐"
|
| 38 |
+
description1 = """This app takes info from subjects and predicts their heart attack likelihood. Do not use these results for an actual medical diagnosis."""
|
| 39 |
+
|
| 40 |
+
description2 = """
|
| 41 |
+
To use the app, simply adjust the inputs and click the "Analyze" button. You can also click one of the examples below to see how it's done!
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(title=title) as demo:
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
gr.Markdown(f"# {title}")
|
| 49 |
+
gr.Markdown(f"## How does it work?")
|
| 50 |
+
gr.Markdown(description1)
|
| 51 |
+
gr.Markdown("""---""")
|
| 52 |
+
gr.Markdown(description2)
|
| 53 |
+
|
| 54 |
+
gr.Markdown("""---""")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
gr.Markdown(f"## Edit the Inputs Below:")
|
| 59 |
+
gr.Markdown("""---""")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
age = gr.Number(label="Age", info="How old are you?", value=40)
|
| 63 |
+
# sex = gr.Radio(["Male", "Female"], label = "What Gender are you?", type = "index")
|
| 64 |
+
sex = gr.Radio(["Male", "Female"], label="Sex", info="What gender are you?", type="index")
|
| 65 |
+
# sex = gr.Radio(choices=["Male", "Female"])
|
| 66 |
+
|
| 67 |
+
cp = gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest Pain", info="What kind of chest pain do you have?", type="index")
|
| 68 |
+
# cp = gr.Slider(label="Chest Pain Type", minimum=1, maximum=5, value=4, step=1)
|
| 69 |
+
# trtbps = gr.Slider(label="Resting blood pressure (in mm Hg)", minimum=1, maximum=200, value=4, step=1)
|
| 70 |
+
trtbps = gr.Number(label="trtbps", value=100)
|
| 71 |
+
chol = gr.Number(label="chol", value=70)
|
| 72 |
+
fbs = gr.Radio(["False", "True"], label="fbs", info="Is your fasting blood sugar > 120 mg/dl?" , type="index")
|
| 73 |
+
|
| 74 |
+
# restecg = gr.Slider(label="Resting ECG Score", minimum=1, maximum=5, value=4, step=1)
|
| 75 |
+
restecg = gr.Dropdown(["Normal", "Having ST-T wave abnormality", "Showing probable or definite left ventricular hypertrophy by Estes' criteria"], label="rest_ecg", type="index")
|
| 76 |
+
thalachh = gr.Slider(label="thalach Score", minimum=1, maximum=205, value=4, step=1)
|
| 77 |
+
exng = gr.Radio(["No", "Yes"], label="Exercise Induced Angina", type="index")
|
| 78 |
+
oldpeak = gr.Slider(label="Oldpeak Score", minimum=1, maximum=10, value=4, step=1)
|
| 79 |
+
slp = gr.Slider(label="Slp Score", minimum=1, maximum=5, value=4, step=1)
|
| 80 |
+
caa = gr.Slider(label="Number of Major Vessels", minimum=1, maximum=3, value=3, step=1)
|
| 81 |
+
thall = gr.Slider(label="Thall Score", minimum=1, maximum=5, value=4, step=1)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
with gr.Column():
|
| 88 |
+
gr.Markdown(f"## Output:")
|
| 89 |
+
gr.Markdown("""---""")
|
| 90 |
+
with gr.Column(visible=True) as output_col:
|
| 91 |
+
label = gr.Label(label = "Predicted Label")
|
| 92 |
+
local_plot = gr.Plot(label = 'Shap:')
|
| 93 |
+
|
| 94 |
+
gr.Markdown(f"## Examples:")
|
| 95 |
+
gr.Markdown("""---""")
|
| 96 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 97 |
+
gr.Examples([[24,"Male","Typical Angina",4,5,"True","Normal",4,"No",5,1,2,3], [24,"Female","Asymptomatic",4,5,"False","Normal",2,"Yes",1,1,2,3]], [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall], [label,local_plot], main_func, cache_examples=True)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
submit_btn = gr.Button("Analyze", variant="primary")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
gr.Markdown("""---""")
|
| 104 |
+
gr.Markdown(f"## Data Dictionary:")
|
| 105 |
+
gr.Markdown("""
|
| 106 |
+
|
| 107 |
+
Age : Age of the patient
|
| 108 |
+
Sex : Sex of the patient
|
| 109 |
+
trtbps : resting blood pressure (in mm Hg)
|
| 110 |
+
chol : cholestoral in mg/dl fetched via BMI sensor
|
| 111 |
+
fbs : (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
|
| 112 |
+
rest_ecg : resting electrocardiographic results
|
| 113 |
+
Value 0: normal
|
| 114 |
+
Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or depression of > 0.05 mV)
|
| 115 |
+
Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria
|
| 116 |
+
|
| 117 |
+
thalach : maximum heart rate achieved
|
| 118 |
+
target : 0 = less chance of heart attack 1= more chance of heart attack""")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
submit_btn.click(
|
| 122 |
+
main_func,
|
| 123 |
+
[age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall],
|
| 124 |
+
[label,local_plot], api_name="Heart_Predictor"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
demo.launch()
|
heart_xgb.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c89450258148337aaf93e65ca5526dcabe996126869ad0c6f4f2a7d37ffb28f8
|
| 3 |
+
size 132944
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.1.3
|
| 2 |
+
Pillow
|
| 3 |
+
yake
|
| 4 |
+
pandas
|
| 5 |
+
sklearn
|
| 6 |
+
shap
|
| 7 |
+
xgboost
|
| 8 |
+
matplotlib
|
| 9 |
+
numpy
|
| 10 |
+
streamlit
|