Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
# Prediction function
|
| 5 |
+
def predict_glucose(current_glucose, meal_type, carbs, exercise, sleep_hours, medication):
|
| 6 |
+
# Define risk factors for hypoglycemia/hyperglycemia
|
| 7 |
+
risk_score = 0
|
| 8 |
+
|
| 9 |
+
# Carbohydrates effect
|
| 10 |
+
if carbs < 20:
|
| 11 |
+
risk_score -= 1 # Risk of hypoglycemia
|
| 12 |
+
elif carbs > 100:
|
| 13 |
+
risk_score += 1 # Risk of hyperglycemia
|
| 14 |
+
|
| 15 |
+
# Exercise effect
|
| 16 |
+
if exercise > 30:
|
| 17 |
+
risk_score -= 1 # More exercise may lower glucose
|
| 18 |
+
|
| 19 |
+
# Sleep effect
|
| 20 |
+
if sleep_hours < 4:
|
| 21 |
+
risk_score += 1 # Poor sleep can increase glucose
|
| 22 |
+
|
| 23 |
+
# Medication effect
|
| 24 |
+
if medication == "Insulin" or medication == "Sulfonylurea":
|
| 25 |
+
risk_score -= 2 # These medications increase hypoglycemia risk
|
| 26 |
+
|
| 27 |
+
# Estimate next glucose level
|
| 28 |
+
predicted_glucose = current_glucose + (risk_score * 10)
|
| 29 |
+
predicted_glucose = max(40, min(300, predicted_glucose)) # Keep within realistic limits
|
| 30 |
+
|
| 31 |
+
# Return prediction
|
| 32 |
+
if predicted_glucose < 70:
|
| 33 |
+
status = "⚠️ Risk of Hypoglycemia"
|
| 34 |
+
elif predicted_glucose > 180:
|
| 35 |
+
status = "⚠️ Risk of Hyperglycemia"
|
| 36 |
+
else:
|
| 37 |
+
status = "✅ Normal Range"
|
| 38 |
+
|
| 39 |
+
return f"Predicted Glucose: {predicted_glucose} mg/dL\n{status}"
|
| 40 |
+
|
| 41 |
+
# Define Gradio interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=predict_glucose,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Number(label="Current Blood Glucose (mg/dL)", value=100),
|
| 46 |
+
gr.Dropdown(["Breakfast", "Lunch", "Dinner", "Snack"], label="Meal Type"),
|
| 47 |
+
gr.Number(label="Carbohydrates (grams)", value=50),
|
| 48 |
+
gr.Number(label="Exercise (minutes)", value=0),
|
| 49 |
+
gr.Number(label="Sleep (hours)", value=7),
|
| 50 |
+
gr.Dropdown(["None", "Metformin", "Insulin", "Sulfonylurea"], label="Medication"),
|
| 51 |
+
],
|
| 52 |
+
outputs="text",
|
| 53 |
+
title="Blood Glucose Prediction",
|
| 54 |
+
description="Enter your current glucose, meal, and lifestyle details to predict your future blood sugar level."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Run app
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
iface.launch()
|