Kayeaelne commited on
Commit
1e120d7
·
verified ·
1 Parent(s): cc5a88a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -2,45 +2,52 @@ import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
 
5
- # Function to simulate blood glucose changes over time
6
- def predict_glucose(current_glucose, meal_type, meal_time_hr, meal_time_min, galvus_dose, galvus_time_hr, galvus_time_min, exercise_duration, fast_carbs_ml, prediction_time=3):
7
- # Convert hours and minutes to total hours
8
- meal_time = meal_time_hr + (meal_time_min / 60)
9
- galvus_time = galvus_time_hr + (galvus_time_min / 60)
10
-
11
- # Constants for glucose reduction effects
12
- post_meal_reduction = 63.6 # mg/dL (avg reduction for Vildagliptin in 2 hours)
13
- fasting_reduction = 27.7 # mg/dL (avg reduction over 6-12 hours)
 
14
 
15
  # Adjust for fast carbs (milk, juice, etc.)
16
  carb_effect = fast_carbs_ml * 1.5 # Approximate glucose rise per mL of fast carbs
17
 
18
- # Calculate blood glucose over time considering meal type, Galvus dose, and exercise
19
  if meal_type == 'High-carb':
20
- glucose_after_meal = current_glucose + carb_effect
21
  elif meal_type == 'Protein-heavy':
22
- glucose_after_meal = current_glucose + 20 # Small glucose increase due to protein
23
  elif meal_type == 'Low-carb':
24
- glucose_after_meal = current_glucose - 10 # Small reduction due to low-carb meal
25
- elif meal_type == 'Fast-carb':
26
- glucose_after_meal = current_glucose + carb_effect * 1.2 # Fast carbs spike blood sugar
 
 
 
 
 
 
 
 
 
27
  else:
28
- glucose_after_meal = current_glucose # Normal meal with moderate carbs
29
 
30
- # Simulate blood glucose levels over 1 hour and 3 hours
31
- glucose_1hr = glucose_after_meal - post_meal_reduction + carb_effect * 0.5 # Adjust for carb effect
32
- glucose_3hr = glucose_1hr - fasting_reduction
33
 
34
- # Adjust the effects of Galvus based on its administration time (medication effect starts at galvus_time + 1-2 hours)
35
- time_since_galvus = meal_time - galvus_time
36
- if time_since_galvus >= 1: # After 1 hour, the effects of Galvus start kicking in
37
- glucose_3hr -= fasting_reduction # Galvus effect after 3 hours
38
 
39
- # Exercise effect on glucose (hypothetical value, may vary based on intensity)
40
- glucose_3hr -= exercise_duration * 2 # Exercise reduces glucose by 2 mg/dL per minute
41
 
42
- # Plotting the graph of glucose prediction over time
43
- time_points = [0, 1, 3] # Time: 0 hours, 1 hour, 3 hours
44
  glucose_values = [current_glucose, glucose_1hr, glucose_3hr]
45
 
46
  plt.plot(time_points, glucose_values, marker='o', color='b')
@@ -51,40 +58,34 @@ def predict_glucose(current_glucose, meal_type, meal_time_hr, meal_time_min, gal
51
  plt.grid(True)
52
  plt.tight_layout()
53
 
54
- # Save the graph as a file to show it in Gradio
55
  plt.savefig('/tmp/blood_glucose_prediction.png')
56
  plt.close()
57
 
58
- # Return glucose predictions and the image file path
59
  return glucose_1hr, glucose_3hr, '/tmp/blood_glucose_prediction.png'
60
 
61
-
62
  # Gradio Interface
63
  def build_interface():
64
  with gr.Blocks() as iface:
65
- gr.Markdown("# Blood Glucose Prediction Model (With Vildagliptin Effects)")
66
 
67
- # Inputs for current glucose, meal info, medication dose, exercise, fast carbs, and Galvus time
68
  with gr.Row():
69
  current_glucose = gr.Number(label="Current Blood Glucose (mg/dL)", value=150)
70
- meal_type = gr.Radio(choices=["Normal", "High-carb", "Protein-heavy", "Low-carb", "Fast-carb"], label="Meal Type", value="Normal")
71
  meal_time_hr = gr.Number(label="Last Meal Time (Hours)", value=2)
72
  meal_time_min = gr.Number(label="Last Meal Time (Minutes)", value=0)
73
  galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50)
74
- galvus_time_hr = gr.Number(label="Galvus Administration Time (Hours)", value=0)
75
- galvus_time_min = gr.Number(label="Galvus Administration Time (Minutes)", value=0)
76
- exercise_duration = gr.Number(label="Exercise Duration (min)", value=0)
77
  fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0)
78
 
79
- # Output predictions and graph
80
  glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)")
81
  glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)")
82
  glucose_graph = gr.Image(label="Blood Glucose Prediction Graph")
83
 
84
- # Button to trigger prediction
85
  predict_button = gr.Button("Predict Blood Glucose")
86
 
87
- # Set button action
88
  predict_button.click(
89
  predict_glucose,
90
  inputs=[current_glucose, meal_type, meal_time_hr, meal_time_min, galvus_dose, galvus_time_hr, galvus_time_min, exercise_duration, fast_carbs_ml],
@@ -93,7 +94,5 @@ def build_interface():
93
 
94
  return iface
95
 
96
-
97
- # Build and launch the Gradio interface
98
  iface = build_interface()
99
  iface.launch()
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
 
5
+ # Function to simulate blood glucose changes
6
+ def predict_glucose(current_glucose, meal_type, meal_time_hr, meal_time_min, galvus_dose, galvus_time_hr, galvus_time_min, exercise_duration, fast_carbs_ml):
7
+ # Convert time to total minutes
8
+ meal_time = meal_time_hr * 60 + meal_time_min
9
+ galvus_time = galvus_time_hr * 60 + galvus_time_min
10
+
11
+ # Constants for glucose changes
12
+ post_meal_reduction = 60 # mg/dL (avg reduction from Galvus over 2 hours)
13
+ fasting_reduction_rate = 0.48 # mg/dL per hour for first 3 hours fasting
14
+ fasting_reduction = fasting_reduction_rate * 3 # Total fasting drop in 3 hours (~1.44 mg/dL)
15
 
16
  # Adjust for fast carbs (milk, juice, etc.)
17
  carb_effect = fast_carbs_ml * 1.5 # Approximate glucose rise per mL of fast carbs
18
 
19
+ # Meal effect on glucose
20
  if meal_type == 'High-carb':
21
+ glucose_after_meal = current_glucose + 50 + carb_effect # High-carb spike
22
  elif meal_type == 'Protein-heavy':
23
+ glucose_after_meal = current_glucose + 20 # Small rise due to protein
24
  elif meal_type == 'Low-carb':
25
+ glucose_after_meal = current_glucose - 10 # Small reduction
26
+ elif meal_type == 'Fast carbs':
27
+ glucose_after_meal = current_glucose + carb_effect # Fast carbs spike
28
+ else:
29
+ glucose_after_meal = current_glucose # Normal meal
30
+
31
+ # Time since Galvus administration
32
+ time_since_galvus = (meal_time - galvus_time) / 60 # Convert back to hours
33
+
34
+ # Apply Galvus effect if taken more than 1 hour ago
35
+ if time_since_galvus >= 1:
36
+ glucose_3hr = glucose_after_meal - post_meal_reduction
37
  else:
38
+ glucose_3hr = glucose_after_meal # No Galvus effect yet
39
 
40
+ # Apply fasting glucose reduction
41
+ glucose_3hr -= fasting_reduction
 
42
 
43
+ # Apply exercise effect
44
+ glucose_3hr -= exercise_duration * 2 # Exercise reduces ~2 mg/dL per minute
 
 
45
 
46
+ # Calculate 1-hour glucose level
47
+ glucose_1hr = (glucose_after_meal + glucose_3hr) / 2 # Approximate midpoint glucose level
48
 
49
+ # Plotting glucose changes
50
+ time_points = [0, 1, 3] # Time: Now, 1 hour, 3 hours
51
  glucose_values = [current_glucose, glucose_1hr, glucose_3hr]
52
 
53
  plt.plot(time_points, glucose_values, marker='o', color='b')
 
58
  plt.grid(True)
59
  plt.tight_layout()
60
 
61
+ # Save the graph
62
  plt.savefig('/tmp/blood_glucose_prediction.png')
63
  plt.close()
64
 
 
65
  return glucose_1hr, glucose_3hr, '/tmp/blood_glucose_prediction.png'
66
 
 
67
  # Gradio Interface
68
  def build_interface():
69
  with gr.Blocks() as iface:
70
+ gr.Markdown("# Blood Glucose Prediction Model (With Galvus & Fasting Effects)")
71
 
 
72
  with gr.Row():
73
  current_glucose = gr.Number(label="Current Blood Glucose (mg/dL)", value=150)
74
+ meal_type = gr.Radio(choices=["Normal", "High-carb", "Protein-heavy", "Low-carb", "Fast carbs"], label="Meal Type", value="Normal")
75
  meal_time_hr = gr.Number(label="Last Meal Time (Hours)", value=2)
76
  meal_time_min = gr.Number(label="Last Meal Time (Minutes)", value=0)
77
  galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50)
78
+ galvus_time_hr = gr.Number(label="Galvus Time (Hours)", value=0)
79
+ galvus_time_min = gr.Number(label="Galvus Time (Minutes)", value=0)
80
+ exercise_duration = gr.Number(label="Exercise Duration (Minutes)", value=0)
81
  fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0)
82
 
 
83
  glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)")
84
  glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)")
85
  glucose_graph = gr.Image(label="Blood Glucose Prediction Graph")
86
 
 
87
  predict_button = gr.Button("Predict Blood Glucose")
88
 
 
89
  predict_button.click(
90
  predict_glucose,
91
  inputs=[current_glucose, meal_type, meal_time_hr, meal_time_min, galvus_dose, galvus_time_hr, galvus_time_min, exercise_duration, fast_carbs_ml],
 
94
 
95
  return iface
96
 
 
 
97
  iface = build_interface()
98
  iface.launch()