Update app.py
Browse files
app.py
CHANGED
|
@@ -20,16 +20,17 @@ except FileNotFoundError:
|
|
| 20 |
model = None
|
| 21 |
|
| 22 |
# ===================================================================
|
| 23 |
-
# 2. CORE PREDICTION LOGIC
|
| 24 |
# ===================================================================
|
| 25 |
|
| 26 |
-
def
|
| 27 |
"""
|
| 28 |
-
This is the core prediction engine. It takes
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
if model is None:
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
# --- a) Simulate a realistic 1-hour glucose history ---
|
| 35 |
history_size = 12
|
|
@@ -74,12 +75,24 @@ def predict_glucose(current_glucose, hba1c, gender, hr_mean, hrv_rmssd, temp_mea
|
|
| 74 |
|
| 75 |
# --- e) Make Prediction ---
|
| 76 |
prediction = model.predict(input_df)[0]
|
| 77 |
-
return
|
| 78 |
|
| 79 |
# ===================================================================
|
| 80 |
-
# 3.
|
| 81 |
# ===================================================================
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def generate_random_scenario_and_predict():
|
| 84 |
"""Generates random values for all inputs and triggers a prediction."""
|
| 85 |
# Generate random values
|
|
@@ -90,11 +103,16 @@ def generate_random_scenario_and_predict():
|
|
| 90 |
random_hrv = np.random.randint(20, 70)
|
| 91 |
random_temp = np.round(np.random.uniform(32.0, 36.0), 1)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Return all the generated values to update the UI
|
| 97 |
-
return random_glucose, random_hba1c, random_gender, random_hr, random_hrv, random_temp,
|
| 98 |
|
| 99 |
# ===================================================================
|
| 100 |
# 4. CREATE THE GRADIO INTERFACE
|
|
@@ -141,7 +159,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
| 141 |
|
| 142 |
# --- Link UI components to functions ---
|
| 143 |
predict_btn.click(
|
| 144 |
-
fn=
|
| 145 |
inputs=[glucose_input, hba1c_input, gender_input, hr_input, hrv_input, temp_input],
|
| 146 |
outputs=[prediction_output]
|
| 147 |
)
|
|
|
|
| 20 |
model = None
|
| 21 |
|
| 22 |
# ===================================================================
|
| 23 |
+
# 2. CORE PREDICTION LOGIC (THE "ENGINE")
|
| 24 |
# ===================================================================
|
| 25 |
|
| 26 |
+
def predict_glucose_engine(current_glucose, hba1c, gender, hr_mean, hrv_rmssd, temp_mean):
|
| 27 |
"""
|
| 28 |
+
This is the core prediction engine. It takes all inputs, engineers
|
| 29 |
+
the features, and returns a raw numerical prediction.
|
| 30 |
"""
|
| 31 |
if model is None:
|
| 32 |
+
# Return a specific value or raise an error to be handled by callers
|
| 33 |
+
return -1
|
| 34 |
|
| 35 |
# --- a) Simulate a realistic 1-hour glucose history ---
|
| 36 |
history_size = 12
|
|
|
|
| 75 |
|
| 76 |
# --- e) Make Prediction ---
|
| 77 |
prediction = model.predict(input_df)[0]
|
| 78 |
+
return round(prediction) # FIX 1: Return the raw, rounded number
|
| 79 |
|
| 80 |
# ===================================================================
|
| 81 |
+
# 3. WRAPPER FUNCTIONS FOR THE UI
|
| 82 |
# ===================================================================
|
| 83 |
|
| 84 |
+
def predict_and_format_for_ui(current_glucose, hba1c, gender, hr_mean, hrv_rmssd, temp_mean):
|
| 85 |
+
"""
|
| 86 |
+
A wrapper function for the 'Predict' button. It calls the engine
|
| 87 |
+
and then formats the output for display in the textbox.
|
| 88 |
+
"""
|
| 89 |
+
prediction_value = predict_glucose_engine(current_glucose, hba1c, gender, hr_mean, hrv_rmssd, temp_mean)
|
| 90 |
+
|
| 91 |
+
if prediction_value == -1:
|
| 92 |
+
return "Model not loaded. Check file path."
|
| 93 |
+
|
| 94 |
+
return f"{prediction_value} mg/dL" # Format the output string here
|
| 95 |
+
|
| 96 |
def generate_random_scenario_and_predict():
|
| 97 |
"""Generates random values for all inputs and triggers a prediction."""
|
| 98 |
# Generate random values
|
|
|
|
| 103 |
random_hrv = np.random.randint(20, 70)
|
| 104 |
random_temp = np.round(np.random.uniform(32.0, 36.0), 1)
|
| 105 |
|
| 106 |
+
# FIX 2: Call the engine to get the number, then format it separately.
|
| 107 |
+
prediction_value = predict_glucose_engine(random_glucose, random_hba1c, random_gender, random_hr, random_hrv, random_temp)
|
| 108 |
+
|
| 109 |
+
if prediction_value == -1:
|
| 110 |
+
prediction_str = "Model not loaded."
|
| 111 |
+
else:
|
| 112 |
+
prediction_str = f"{prediction_value} mg/dL"
|
| 113 |
|
| 114 |
# Return all the generated values to update the UI
|
| 115 |
+
return random_glucose, random_hba1c, random_gender, random_hr, random_hrv, random_temp, prediction_str
|
| 116 |
|
| 117 |
# ===================================================================
|
| 118 |
# 4. CREATE THE GRADIO INTERFACE
|
|
|
|
| 159 |
|
| 160 |
# --- Link UI components to functions ---
|
| 161 |
predict_btn.click(
|
| 162 |
+
fn=predict_and_format_for_ui, # FIX 3: Use the new UI wrapper function
|
| 163 |
inputs=[glucose_input, hba1c_input, gender_input, hr_input, hrv_input, temp_input],
|
| 164 |
outputs=[prediction_output]
|
| 165 |
)
|