Kayeaelne commited on
Commit
a9b0f70
·
verified ·
1 Parent(s): cf81432

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -80
app.py CHANGED
@@ -2,92 +2,107 @@ import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
 
5
- # Constants for Galvus (Vildagliptin) effects based on studies
6
- post_meal_reduction_mean = 63.6 # mg/dL
7
- post_meal_reduction_std = 74 # mg/dL
8
- fasting_glucose_reduction_mean = 27.7 # mg/dL
9
- fasting_glucose_reduction_std = 43.4 # mg/dL
10
-
11
- # Meal types and their carbs (in mL)
12
- meal_options = {
13
- "Light Meal": 30, # mL of fast carbs
14
- "Standard Meal": 50, # mL of fast carbs
15
- "Heavy Meal": 70, # mL of fast carbs
16
- "Fast Carbs (Juice/Milk)": 120 # mL of fast carbs (for quick glucose rise)
17
- }
18
-
19
- # Function to calculate blood glucose prediction
20
- def predict_glucose(current_glucose, meal_type, meal_time, galvus_dose, exercise_duration, fast_carb_mL):
21
- # Convert meal time to hours and minutes
22
- if "min ago" in meal_time:
23
- minutes_ago = int(meal_time.replace(" min ago", ""))
24
- meal_time_hours = minutes_ago // 60
25
- meal_time_minutes = minutes_ago % 60
26
- else:
27
- # Default to 0 if no valid input
28
- meal_time_hours, meal_time_minutes = 0, 0
29
 
30
- # Calculate predicted glucose
31
- # 1. Post-Meal Prediction (after 2 hours)
32
- if current_glucose > 100: # Adjust if high glucose
33
- post_meal_pred = current_glucose - post_meal_reduction_mean
 
 
 
 
 
 
 
 
 
 
 
 
34
  else:
35
- post_meal_pred = current_glucose # No significant effect if glucose is normal/low
36
 
37
- # 2. Fasting Prediction (after 6-12 hours)
38
- fasting_pred = current_glucose - fasting_glucose_reduction_mean
39
-
40
- # 3. Fast Carb Effect (adding juice/milk carbs)
41
- carb_effect = fast_carb_mL * 2 # Simplified assumption: 2 mg/dL rise per 10 mL of liquid carb
42
-
43
- # Adjusting for exercise (duration, in minutes)
44
- exercise_effect = exercise_duration * 0.5 # Assuming each minute of exercise reduces glucose by 0.5 mg/dL
45
-
46
- # Final prediction outputs after 1 hour, 3 hours
47
- glucose_1hr = post_meal_pred + carb_effect - exercise_effect
48
- glucose_3hr = fasting_pred + carb_effect - exercise_effect
49
-
50
- # Create a graph showing blood glucose over time
51
- times = [0, 1, 3]
52
- glucose_levels = [current_glucose, glucose_1hr, glucose_3hr]
53
-
54
- plt.plot(times, glucose_levels, marker='o')
55
- plt.xlabel('Time (hours)')
56
- plt.ylabel('Blood Glucose (mg/dL)')
57
- plt.title('Blood Glucose Prediction Over Time')
58
-
59
- # Save plot as an image file
60
- plt.savefig("/tmp/blood_glucose_prediction.png")
 
 
61
  plt.close()
62
 
63
- # Return results
64
- return glucose_1hr, glucose_3hr, "/tmp/blood_glucose_prediction.png"
65
 
66
 
67
- # Gradio interface to input data
68
- def update_fast_carb_visibility(meal_type):
69
- if meal_type == "Fast Carbs (Juice/Milk)":
70
- return gr.Slider(minimum=0, maximum=300, default=120, label="Amount of Fast Carbs (mL)")
71
- else:
72
- return gr.Slider(minimum=0, maximum=0, default=0, label="Amount of Fast Carbs (mL)")
73
-
74
-
75
- iface = gr.Interface(
76
- fn=predict_glucose,
77
- inputs=[
78
- gr.Number(label="Current Blood Glucose (mg/dL)"),
79
- gr.Dropdown(choices=list(meal_options.keys()), label="Meal Type"),
80
- gr.Textbox(label="Time of Last Meal (e.g., '15 min ago')"),
81
- gr.Number(label="Galvus Dose (50 mg)"),
82
- gr.Number(label="Exercise Duration (minutes)"),
83
- gr.Slider(minimum=0, maximum=300, default=120, label="Amount of Fast Carbs (mL)"),
84
- ],
85
- outputs=[
86
- gr.Textbox(label="Blood Glucose After 1 Hour (mg/dL)"),
87
- gr.Textbox(label="Blood Glucose After 3 Hours (mg/dL)"),
88
- gr.Image(type="file", label="Blood Glucose Prediction Graph")
89
- ],
90
- live=True
91
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
 
 
93
  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_hours, meal_time_minutes, galvus_dose, galvus_time_hours, galvus_time_minutes, 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
+ # Convert meal and Galvus times to total minutes
15
+ meal_time_total = meal_time_hours * 60 + meal_time_minutes
16
+ galvus_time_total = galvus_time_hours * 60 + galvus_time_minutes
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 Carbs (Juice/Milk)':
26
+ glucose_after_meal = current_glucose + carb_effect # Explicitly handle fast carbs meal
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_total - galvus_time_total) / 60 # Convert back to hours
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')
47
+ plt.title("Blood Glucose Prediction Over Time")
48
+ plt.xlabel("Time (Hours)")
49
+ plt.ylabel("Blood Glucose (mg/dL)")
50
+ plt.xticks([0, 1, 2, 3])
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 Carbs (Juice/Milk)"], label="Meal Type", value="Normal")
71
+
72
+ with gr.Row():
73
+ meal_time_hours = gr.Number(label="Last Meal Time (Hours Ago)", value=2)
74
+ meal_time_minutes = gr.Number(label="Last Meal Time (Minutes Ago)", value=0)
75
+
76
+ with gr.Row():
77
+ galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50)
78
+ galvus_time_hours = gr.Number(label="Galvus Time of Administration (Hours Ago)", value=0)
79
+ galvus_time_minutes = gr.Number(label="Galvus Time of Administration (Minutes Ago)", value=0)
80
+
81
+ with gr.Row():
82
+ exercise_duration = gr.Number(label="Exercise Duration (min)", value=0)
83
+ fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0)
84
+
85
+ # Output predictions and graph
86
+ glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)")
87
+ glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)")
88
+ glucose_graph = gr.Image(label="Blood Glucose Prediction Graph")
89
+
90
+ # Button to trigger prediction
91
+ predict_button = gr.Button("Predict Blood Glucose")
92
+
93
+ # Set button action
94
+ predict_button.click(
95
+ predict_glucose,
96
+ inputs=[
97
+ current_glucose, meal_type, meal_time_hours, meal_time_minutes, galvus_dose,
98
+ galvus_time_hours, galvus_time_minutes, exercise_duration, fast_carbs_ml
99
+ ],
100
+ outputs=[glucose_1hr_output, glucose_3hr_output, glucose_graph]
101
+ )
102
+
103
+ return iface
104
+
105
 
106
+ # Build and launch the Gradio interface
107
+ iface = build_interface()
108
  iface.launch()