nefro313 commited on
Commit ·
c2e7ffa
1
Parent(s): 2f8f59f
graido config
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
|
| 6 |
+
model_p01 = load_model("notebook/models_artifacts/lstm_model_patient01.keras")
|
| 7 |
+
model_p02 = load_model("notebook/models_artifacts/lstm_model_patient02.keras")
|
| 8 |
+
model_p03 = load_model("notebook/models_artifacts/lstm_model_patient03.keras")
|
| 9 |
+
model_p04 = load_model("notebook/models_artifacts/lstm_model_patient04.keras")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def predict_glucose(p_num,time_of_day,BG2h45, BG2h30, BG2h15, BG2h, BG1h45, BG1h30, BG1h15, BG1h, BG45m, BG30m, BG15m,BG0m):
|
| 14 |
+
|
| 15 |
+
day = {"Morning":1, "Afternoon":2, "Evening":3, "Night":0}
|
| 16 |
+
|
| 17 |
+
predicted = 0.0
|
| 18 |
+
alert = ""
|
| 19 |
+
inputs = np.expand_dims([day[time_of_day],
|
| 20 |
+
BG2h45, BG2h30, BG2h15,
|
| 21 |
+
BG2h, BG1h45, BG1h30, BG1h15, BG1h,
|
| 22 |
+
BG45m, BG30m, BG15m, BG0m
|
| 23 |
+
,],axis=0)
|
| 24 |
+
if p_num == "Patient 1":
|
| 25 |
+
predicted = float(model_p01.predict(inputs)[0][0])
|
| 26 |
+
elif p_num == "Patient 2":
|
| 27 |
+
predicted = float(model_p02.predict(inputs)[0][0])
|
| 28 |
+
elif p_num == "Patient 3":
|
| 29 |
+
predicted = float(model_p03.predict(inputs)[0][0])
|
| 30 |
+
elif p_num == "Patient 4":
|
| 31 |
+
predicted = float(model_p04.predict(inputs)[0][0])
|
| 32 |
+
|
| 33 |
+
# Determine alert level
|
| 34 |
+
if predicted <= 3.0:
|
| 35 |
+
alert = "Severe hypoglycemia: urgent alert!"
|
| 36 |
+
elif predicted <= 4.0:
|
| 37 |
+
alert = "Mild hypoglycemia: consume 15g fast-acting carbs"
|
| 38 |
+
elif predicted >= 13.9:
|
| 39 |
+
alert = "Severe hyperglycemia: urgent alert!"
|
| 40 |
+
elif predicted >= 10.0:
|
| 41 |
+
alert = "Mild hyperglycemia: hydrate and adjust treatment"
|
| 42 |
+
else:
|
| 43 |
+
alert = "Normal range"
|
| 44 |
+
|
| 45 |
+
# Trend-based alert
|
| 46 |
+
last_bg = BG0m # most recent measurement (15m ago)
|
| 47 |
+
slope = (predicted - last_bg) / 60.0 # mmol/L per minute
|
| 48 |
+
if slope > 0.1:
|
| 49 |
+
alert += " | Rapid rise"
|
| 50 |
+
elif slope < -0.1:
|
| 51 |
+
alert += " | Rapid drop"
|
| 52 |
+
|
| 53 |
+
return float(np.round(predicted, 2)), alert
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# Blood Glucose Prediction Interface")
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
BG2h45 = gr.Number(label="BG 2h45m ago (mmol/L)",value=8.0)
|
| 60 |
+
BG2h30 = gr.Number(label="BG 2h30m ago (mmol/L)",value=11.0)
|
| 61 |
+
BG2h15 = gr.Number(label="BG 2h15m ago (mmol/L)",value=13.0)
|
| 62 |
+
BG2h = gr.Number(label="BG 2h ago (mmol/L)",value=10.0)
|
| 63 |
+
BG1h45 = gr.Number(label="BG 1h45m ago (mmol/L)",value=9.0)
|
| 64 |
+
BG1h30 = gr.Number(label="BG 1h30m ago (mmol/L)",value=8.7)
|
| 65 |
+
with gr.Column():
|
| 66 |
+
BG1h15 = gr.Number(label="BG 1h15m ago (mmol/L)",value=7.9)
|
| 67 |
+
BG1h = gr.Number(label="BG 1h ago (mmol/L)",value=11.8)
|
| 68 |
+
BG45m = gr.Number(label="BG 45m ago (mmol/L)",value=7.4)
|
| 69 |
+
BG30m = gr.Number(label="BG 30m ago (mmol/L)",value=18.7)
|
| 70 |
+
BG15m = gr.Number(label="BG 15m ago (mmol/L)",value=13.8)
|
| 71 |
+
BG0m = gr.Number(label="BG now (mmol/L) (mmol/L)",value=15.0)
|
| 72 |
+
time_of_day = gr.Radio(["Morning", "Afternoon", "Evening", "Night"], label="Time of Day")
|
| 73 |
+
p_num = gr.Radio(["Patient 1", "Patient 2", "Patient 3", "Patient 4"], label="Patient")
|
| 74 |
+
predict_btn = gr.Button("Predict")
|
| 75 |
+
output_pred = gr.Number(label="Predicted BG in 1h (mmol/L)")
|
| 76 |
+
output_alert = gr.Textbox(label="Alerts")
|
| 77 |
+
predict_btn.click(predict_glucose,
|
| 78 |
+
inputs=[p_num,time_of_day,BG2h45, BG2h30, BG2h15, BG2h, BG1h45, BG1h30, BG1h15, BG1h, BG45m, BG30m, BG15m,BG0m],
|
| 79 |
+
outputs=[output_pred, output_alert])
|
| 80 |
+
|
| 81 |
+
demo.launch()
|