JaloCardiAI / app.py
MaxJalo's picture
Update app.py
69d6037 verified
from huggingface_hub import hf_hub_download
import gradio as gr
import pandas as pd
import tensorflow as tf
import numpy as np
from dateutil.utils import today
from datasets import load_dataset
model_path = hf_hub_download(repo_id="MaxJalo/CardioAI", filename="cardioai_model.keras")
model = tf.keras.models.load_model(model_path)
heart = load_dataset("MaxJalo/CardioAI", split = 'train')
data = pd.DataFrame(heart, columns=["age","gender","height","weight","ap_hi","ap_lo","cholesterol","gluc","smoke","alco","active",'cardio'])
X_for_train = data.drop(['cardio'], axis=1).values
X_min = np.min(X_for_train, axis=0)
X_max = np.max(X_for_train, axis=0)
def webai(user_input):
user_input_clear = user_input
input_data = [user_input_clear]
input_data_scaled = (input_data - X_min) / (X_max - X_min)
# Получаем предсказание от модели
predicted_result_scaled = model.predict(input_data_scaled)
otv = round(predicted_result_scaled[0][0] * 100, 2)
if otv < 0:
otv = 0
elif otv > 100:
otv = 100
chans = ''
if otv >=0 and otv < 30:
chans = "Низкий"
elif otv >=30 and otv <50:
chans = "Средний"
elif otv >=50 and otv <70:
chans = "Высокий, обратитесь к кардиологу"
else:
chans = "Крайне высокий, обязательно обратитесь к кардиологу"
return f'Вероятность заболевания: {round(otv, 2)}%. Шанс вашего заболевания: ' + chans
def pomoch(age, gender, height, weight, ap_hi, ap_lo, cholesterol, gluc, smoke, alco, active):
try:
X = [int(age), gender, int(height), int(weight), int(ap_hi), int(ap_lo), float(cholesterol), float(gluc), smoke,
alco, active]
X[0] = today().year - X[0]
if X[1] == "Мужской":
X[1] = 0
else:
X[1] = 1
for i in range(8, 11):
if X[i] == "Да":
X[i] = 1
else:
X[i] = 0
if X[6] <= 5:
X[6] = 1
else:
if X[6] >= 7.8:
X[6] = 3
else:
X[6] = 2
if X[7] <= 5.5 and X[7] >= 3.3:
X[7] = 1
else:
if (X[7] > 5.5 and X[7] < 11) or X[7] < 3.3:
X[7] = 2
else:
X[7] = 3
return webai(X)
except ValueError:
return "Пожалуйста, убедитесь, что все значения числовые."
demo = gr.Interface(
pomoch,
[
gr.Slider(1900, 2010, value=1990, step=1, label="Год рождения"),
gr.Radio(["Мужской", "Женский"], label="Пол", ),
gr.Textbox(label="Рост(см)"),
gr.Textbox(label="Вес(кг)"),
gr.Textbox(label="Верхнее Давление"),
gr.Textbox(label="Нижнее Давление"),
gr.Textbox(label="Холестерин(ммоль/л)"),
gr.Textbox(label="Глюкоза(ммоль/л)", ),
gr.Radio(["Да", "Нет"], label="Курение", ),
gr.Radio(["Да", "Нет"], label="Алкоголь", ),
gr.Radio(["Да", "Нет"], label="Активность", ),
],
'text')
demo.launch()