Kayeaelne commited on
Commit
1103a86
·
verified ·
1 Parent(s): dc36cbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -72
app.py CHANGED
@@ -2,69 +2,39 @@ import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
 
5
- # Function to simulate blood glucose changes using our exact manual method
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, chocolate_time_hr, chocolate_time_min):
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
- chocolate_time = chocolate_time_hr * 60 + chocolate_time_min
11
-
12
- # Constants based on manual logic
13
- galvus_total_effect = 60 # mg/dL drop over 2 hours
14
- galvus_rate = galvus_total_effect / 2 # 30 mg/dL per hour
15
- fasting_drop_first_3h = 0.48 # mg/dL per hour for first 3 hours
16
- fasting_drop_after_3h = 2 # mg/dL per hour after 3 hours
17
- carb_effect_per_ml = 1.5 # mg/dL per mL of fast carbs
18
- exercise_reduction_rate = 2 # mg/dL per minute of exercise
19
- chocolate_spike = 40 # mg/dL rise from chocolate bar
20
-
21
- # Step 1: Calculate Meal Impact
22
  if meal_type == 'High-carb':
23
- glucose_after_meal = current_glucose + 50 + (fast_carbs_ml * carb_effect_per_ml)
24
  elif meal_type == 'Protein-heavy':
25
- glucose_after_meal = current_glucose + 20
26
  elif meal_type == 'Low-carb':
27
- glucose_after_meal = current_glucose - 10
28
- elif meal_type == 'Fast carbs':
29
- glucose_after_meal = current_glucose + (fast_carbs_ml * carb_effect_per_ml)
30
  else:
31
- glucose_after_meal = current_glucose # No big change for normal meals
32
 
33
- # Step 2: Apply Fasting Effect
34
- time_since_last_meal = (meal_time_hr * 60 + meal_time_min) / 60 # Convert to hours
35
- if time_since_last_meal <= 3:
36
- fasting_effect = time_since_last_meal * fasting_drop_first_3h
37
- else:
38
- fasting_effect = (3 * fasting_drop_first_3h) + ((time_since_last_meal - 3) * fasting_drop_after_3h)
39
-
40
- glucose_after_fasting = glucose_after_meal - fasting_effect
41
-
42
- # Step 3: Apply Galvus Effect
43
- time_since_galvus = (meal_time - galvus_time) / 60 # Convert back to hours
44
- if time_since_galvus >= 1:
45
- glucose_after_galvus = glucose_after_fasting - galvus_rate
46
- else:
47
- glucose_after_galvus = glucose_after_fasting # No effect yet
48
-
49
- # Step 4: Apply Chocolate Effect
50
- time_since_chocolate = (meal_time - chocolate_time) / 60 # Convert back to hours
51
- if time_since_chocolate >= 0: # Chocolate was eaten after meal
52
- glucose_after_chocolate = glucose_after_galvus + chocolate_spike
53
- else:
54
- glucose_after_chocolate = glucose_after_galvus
55
-
56
- # Step 5: Apply Second Hour Galvus Effect
57
- glucose_after_2hr = glucose_after_chocolate - galvus_rate # Second hour of Galvus effect
58
 
59
- # Step 6: Apply Exercise Effect
60
- glucose_after_exercise = glucose_after_2hr - (exercise_duration * exercise_reduction_rate)
 
 
61
 
62
- # Step 7: Predict 1-Hour and 3-Hour Levels
63
- glucose_1hr = glucose_after_chocolate # Since we assume peak at 1-hour
64
- glucose_3hr = glucose_after_exercise # After all effects take place
65
 
66
- # Graphing the glucose change
67
- time_points = [0, 1, 3]
68
  glucose_values = [current_glucose, glucose_1hr, glucose_3hr]
69
 
70
  plt.plot(time_points, glucose_values, marker='o', color='b')
@@ -75,47 +45,47 @@ def predict_glucose(current_glucose, meal_type, meal_time_hr, meal_time_min, gal
75
  plt.grid(True)
76
  plt.tight_layout()
77
 
78
- # Save the graph
79
  plt.savefig('/tmp/blood_glucose_prediction.png')
80
  plt.close()
81
 
 
82
  return glucose_1hr, glucose_3hr, '/tmp/blood_glucose_prediction.png'
83
 
 
84
  # Gradio Interface
85
  def build_interface():
86
  with gr.Blocks() as iface:
87
- gr.Markdown("# Blood Glucose Prediction Model (Step-by-Step Thinking)")
88
 
 
89
  with gr.Row():
90
- current_glucose = gr.Number(label="Current Blood Glucose (mg/dL)", value=150)
91
- meal_type = gr.Radio(choices=["Normal", "High-carb", "Protein-heavy", "Low-carb", "Fast carbs"], label="Meal Type", value="Normal")
92
- meal_time_hr = gr.Number(label="Last Meal Time (Hours)", value=2)
93
- meal_time_min = gr.Number(label="Last Meal Time (Minutes)", value=0)
94
  galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50)
95
- galvus_time_hr = gr.Number(label="Galvus Time (Hours)", value=0)
96
- galvus_time_min = gr.Number(label="Galvus Time (Minutes)", value=0)
97
- exercise_duration = gr.Number(label="Exercise Duration (Minutes)", value=0)
98
  fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0)
99
- chocolate_time_hr = gr.Number(label="Chocolate Bar Time (Hours)", value=0)
100
- chocolate_time_min = gr.Number(label="Chocolate Bar Time (Minutes)", value=0)
101
 
 
102
  glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)")
103
  glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)")
104
  glucose_graph = gr.Image(label="Blood Glucose Prediction Graph")
105
 
 
106
  predict_button = gr.Button("Predict Blood Glucose")
107
 
108
- def on_predict(*inputs):
109
- return predict_glucose(*inputs)
110
-
111
  predict_button.click(
112
- on_predict,
113
- inputs=[current_glucose, meal_type, meal_time_hr, meal_time_min, galvus_dose, galvus_time_hr, galvus_time_min, exercise_duration, fast_carbs_ml, chocolate_time_hr, chocolate_time_min],
114
  outputs=[glucose_1hr_output, glucose_3hr_output, glucose_graph]
115
  )
116
 
117
  return iface
118
 
119
- # Launch the interface
 
120
  iface = build_interface()
121
  iface.launch()
 
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, galvus_dose, galvus_time, exercise_duration, fast_carbs_ml, prediction_time=3):
7
+ # Constants for glucose reduction effects
8
+ post_meal_reduction = 63.6 # mg/dL (avg reduction for Vildagliptin in 2 hours)
9
+ fasting_reduction = 27.7 # mg/dL (avg reduction over 6-12 hours)
10
+
11
+ # Adjust for fast carbs (milk, juice, etc.)
12
+ carb_effect = fast_carbs_ml * 1.5 # Approximate glucose rise per mL of fast carbs
13
+
14
+ # Calculate blood glucose over time considering meal type, Galvus dose, and exercise
 
 
 
 
 
 
 
15
  if meal_type == 'High-carb':
16
+ glucose_after_meal = current_glucose + carb_effect
17
  elif meal_type == 'Protein-heavy':
18
+ glucose_after_meal = current_glucose + 20 # Small glucose increase due to protein
19
  elif meal_type == 'Low-carb':
20
+ glucose_after_meal = current_glucose - 10 # Small reduction due to low-carb meal
 
 
21
  else:
22
+ glucose_after_meal = current_glucose # Normal meal with moderate carbs
23
 
24
+ # Simulate blood glucose levels over 1 hour and 3 hours
25
+ glucose_1hr = glucose_after_meal - post_meal_reduction + carb_effect * 0.5 # Adjust for carb effect
26
+ glucose_3hr = glucose_1hr - fasting_reduction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ # Adjust the effects of Galvus based on its administration time (medication effect starts at galvus_time + 1-2 hours)
29
+ time_since_galvus = meal_time - galvus_time
30
+ if time_since_galvus >= 1: # After 1 hour, the effects of Galvus start kicking in
31
+ glucose_3hr -= fasting_reduction # Galvus effect after 3 hours
32
 
33
+ # Exercise effect on glucose (hypothetical value, may vary based on intensity)
34
+ glucose_3hr -= exercise_duration * 2 # Exercise reduces glucose by 2 mg/dL per minute
 
35
 
36
+ # Plotting the graph of glucose prediction over time
37
+ time_points = [0, 1, 3] # Time: 0 hours, 1 hour, 3 hours
38
  glucose_values = [current_glucose, glucose_1hr, glucose_3hr]
39
 
40
  plt.plot(time_points, glucose_values, marker='o', color='b')
 
45
  plt.grid(True)
46
  plt.tight_layout()
47
 
48
+ # Save the graph as a file to show it in Gradio
49
  plt.savefig('/tmp/blood_glucose_prediction.png')
50
  plt.close()
51
 
52
+ # Return glucose predictions and the image file path
53
  return glucose_1hr, glucose_3hr, '/tmp/blood_glucose_prediction.png'
54
 
55
+
56
  # Gradio Interface
57
  def build_interface():
58
  with gr.Blocks() as iface:
59
+ gr.Markdown("# Blood Glucose Prediction Model (With Vildagliptin Effects)")
60
 
61
+ # Inputs for current glucose, meal info, medication dose, exercise, fast carbs, and Galvus time
62
  with gr.Row():
63
+ current_glucose = gr.Number(label="Current Blood Glucose (mg/dL)", value=105)
64
+ meal_type = gr.Radio(choices=["Normal", "High-carb", "Protein-heavy", "Low-carb"], label="Meal Type", value="Low-carb")
65
+ meal_time = gr.Number(label="Last Meal Time (in hours)", value=6)
 
66
  galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50)
67
+ galvus_time = gr.Number(label="Galvus Time of Administration (hours)", value=2) # Input for when Galvus was taken
68
+ exercise_duration = gr.Number(label="Exercise Duration (min)", value=60)
 
69
  fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0)
 
 
70
 
71
+ # Output predictions and graph
72
  glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)")
73
  glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)")
74
  glucose_graph = gr.Image(label="Blood Glucose Prediction Graph")
75
 
76
+ # Button to trigger prediction
77
  predict_button = gr.Button("Predict Blood Glucose")
78
 
79
+ # Set button action
 
 
80
  predict_button.click(
81
+ predict_glucose,
82
+ inputs=[current_glucose, meal_type, meal_time, galvus_dose, galvus_time, exercise_duration, fast_carbs_ml],
83
  outputs=[glucose_1hr_output, glucose_3hr_output, glucose_graph]
84
  )
85
 
86
  return iface
87
 
88
+
89
+ # Build and launch the Gradio interface
90
  iface = build_interface()
91
  iface.launch()