Heart / app.py
Avi8189's picture
Create app.py
fcda1b2
import sklearn
import requests
import pandas as pd
import gradio as gr
url = 'https://data.mendeley.com/public-files/datasets/wmhctcrt5v/files/959a4949-b1c2-4858-b361-2602bb60a07a/file_downloaded'
localfile = 'df.csv'
response = requests.get(url)
with open(localfile, 'wb') as file:
file.write(response.content)
df = pd.read_csv('df.csv')
from sklearn.ensemble import RandomForestClassifier
input, output = df.drop(['Result'], axis=1), df['Result']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(input, output, train_size=.85)
random = RandomForestClassifier(
n_estimators=20,
max_depth=4,
bootstrap=True
)
random.fit(X_train, y_train)
maps = {
'negative':0,
'positive':1
}
dfx = df['Result'].map(maps)
dfx = pd.DataFrame(dfx, columns=['Result'])
def fn(Age, Gender, HeartRate, BP_sys, BP_dias, blood_sugar, ck_mb, troponin):
inputs = [Age, Gender, HeartRate, BP_sys, BP_dias, blood_sugar, ck_mb, troponin]
pred = random.predict([inputs])
if pred[0] == 'positive':
return ('Positive Risk')
else:
return ('No Risk!')
interface = gr.Interface(
fn=fn,
title='Heart Attack Prediction',
inputs=[gr.Slider(1,100, label='Age', ), gr.Text(placeholder='1 for Male 0 for Female'), gr.Slider(0,200, label='Heart Rate'),
gr.Slider(0,200, label='Blood Pressure(Sys)'), gr.Slider(0,200, label='Blood Pressure(dias)'), gr.Slider(50,250,label='Blood Sugar'),
gr.Slider(0,100, label='CK-MB'), gr.Slider(0,5,label='Troponin')],
outputs=gr.Text(placeholder=r'99.9% accurate estimates...'),
description='Fill in the categories and find an estimate for heart attack (/associated risk level)'
)
interface.launch()