Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Function to simulate blood glucose changes over time | |
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_min, prediction_time=3): | |
# Constants for glucose reduction effects | |
post_meal_reduction = 63.6 # mg/dL (avg reduction for Vildagliptin in 2 hours) | |
fasting_reduction = 27.7 # mg/dL (avg reduction over 6-12 hours) | |
# Convert meal time and Galvus time to minutes for easier calculations | |
meal_time_total_min = meal_time_hr * 60 + meal_time_min | |
galvus_time_total_min = galvus_time_hr * 60 + galvus_time_min | |
# Adjust for fast carbs (milk, juice, etc.) | |
carb_effect = fast_carbs_ml * 1.5 # Approximate glucose rise per mL of fast carbs | |
# Calculate blood glucose over time considering meal type, Galvus dose, and exercise | |
if meal_type == 'High-carb': | |
glucose_after_meal = current_glucose + carb_effect + 60 # Higher glucose increase due to carbs | |
elif meal_type == 'Protein-heavy': | |
glucose_after_meal = current_glucose + 20 # Small glucose increase due to protein | |
elif meal_type == 'Low-carb': | |
glucose_after_meal = current_glucose - 10 # Small reduction due to low-carb meal | |
elif meal_type == 'Fast carb': | |
glucose_after_meal = current_glucose + carb_effect * 1.5 # Fast carbs like juice, milk increase glucose quickly | |
else: | |
glucose_after_meal = current_glucose # Normal meal with moderate carbs | |
# Calculate glucose levels over 1 hour and 3 hours | |
glucose_1hr = glucose_after_meal - post_meal_reduction + carb_effect * 0.5 # Adjust for carb effect | |
glucose_3hr = glucose_1hr - fasting_reduction | |
# Adjust the effects of Galvus based on its administration time (medication effect starts at galvus_time + 1-2 hours) | |
time_since_galvus = meal_time_total_min - galvus_time_total_min | |
if time_since_galvus >= 60: # After 1 hour, the effects of Galvus start kicking in | |
glucose_3hr -= fasting_reduction # Galvus effect after 3 hours | |
# Exercise effect on glucose (hypothetical value, may vary based on intensity) | |
glucose_3hr -= exercise_duration * 2 # Exercise reduces glucose by 2 mg/dL per minute | |
# Chocolate bar effect | |
if chocolate_time_min <= 60: # If the chocolate bar is eaten within 1 hour | |
glucose_1hr += 40 # Approximate glucose rise from chocolate | |
glucose_3hr += 30 # This would still contribute to an increase at 3 hours | |
# Plotting the graph of glucose prediction over time | |
time_points = [0, 1, 3] # Time: 0 hours, 1 hour, 3 hours | |
glucose_values = [current_glucose, glucose_1hr, glucose_3hr] | |
plt.plot(time_points, glucose_values, marker='o', color='b') | |
plt.title("Blood Glucose Prediction Over Time") | |
plt.xlabel("Time (Hours)") | |
plt.ylabel("Blood Glucose (mg/dL)") | |
plt.xticks([0, 1, 2, 3]) | |
plt.grid(True) | |
plt.tight_layout() | |
# Save the graph as a file to show it in Gradio | |
plt.savefig('/tmp/blood_glucose_prediction.png') | |
plt.close() | |
# Return glucose predictions and the image file path | |
return glucose_1hr, glucose_3hr, '/tmp/blood_glucose_prediction.png' | |
# Gradio Interface | |
def build_interface(): | |
with gr.Blocks() as iface: | |
gr.Markdown("# Blood Glucose Prediction Model (With Vildagliptin Effects)") | |
# Inputs for current glucose, meal info, medication dose, exercise, fast carbs, and Galvus time | |
with gr.Row(): | |
current_glucose = gr.Number(label="Current Blood Glucose (mg/dL)", value=105) | |
meal_type = gr.Radio(choices=["Normal", "High-carb", "Protein-heavy", "Low-carb", "Fast carb"], label="Meal Type", value="Low-carb") | |
meal_time_hr = gr.Number(label="Last Meal Time (hours)", value=6) | |
meal_time_min = gr.Number(label="Last Meal Time (minutes)", value=0) | |
galvus_dose = gr.Number(label="Galvus Dose (mg)", value=50) | |
galvus_time_hr = gr.Number(label="Galvus Time of Administration (hours)", value=2) | |
galvus_time_min = gr.Number(label="Galvus Time of Administration (minutes)", value=0) | |
exercise_duration = gr.Number(label="Exercise Duration (min)", value=60) | |
fast_carbs_ml = gr.Number(label="Fast Carbs (mL)", value=0) | |
chocolate_time_min = gr.Number(label="Time After Meal When Chocolate Bar Is Eaten (min)", value=60) | |
# Output predictions and graph | |
glucose_1hr_output = gr.Textbox(label="Predicted Glucose Level in 1 Hour (mg/dL)") | |
glucose_3hr_output = gr.Textbox(label="Predicted Glucose Level in 3 Hours (mg/dL)") | |
glucose_graph = gr.Image(label="Blood Glucose Prediction Graph") | |
# Button to trigger prediction | |
predict_button = gr.Button("Predict Blood Glucose") | |
# Set button action | |
predict_button.click( | |
predict_glucose, | |
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_min], | |
outputs=[glucose_1hr_output, glucose_3hr_output, glucose_graph] | |
) | |
return iface | |
# Build and launch the Gradio interface | |
iface = build_interface() | |
iface.launch() |