Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ css = """
|
|
| 13 |
|
| 14 |
with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as demo:
|
| 15 |
|
| 16 |
-
# The user_state now includes a 'username' field
|
| 17 |
user_state = gr.State({"email": None, "id": None, "logged_in": False, "gender": None, "username": None})
|
| 18 |
|
| 19 |
# --- Static Header (Always Visible) ---
|
|
@@ -93,7 +92,7 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 93 |
signup_btn = gr.Button("Sign Up", variant="primary")
|
| 94 |
show_login_btn = gr.Button("Already have an account? Login.", variant="secondary")
|
| 95 |
|
| 96 |
-
# --- Prediction Tab ---
|
| 97 |
with gr.TabItem("Prediction", id="prediction_tab"):
|
| 98 |
|
| 99 |
with gr.Column(visible=False) as prediction_view:
|
|
@@ -116,6 +115,11 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 116 |
predict_btn = gr.Button("Predict My Risk", variant="primary")
|
| 117 |
|
| 118 |
result_output = gr.Textbox(label="Prediction Result", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 120 |
|
| 121 |
with gr.Column(visible=True) as logged_out_view:
|
|
@@ -129,71 +133,20 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 129 |
)
|
| 130 |
|
| 131 |
|
| 132 |
-
# --- Component Logic (Event Handlers) ---
|
| 133 |
-
def handle_create_report(user, p, g, b, i, bm, a, result):
|
| 134 |
-
if not result:
|
| 135 |
-
return gr.update(visible=False) # Don't show if there's no result
|
| 136 |
-
# Structure the input data for the report
|
| 137 |
-
input_data = {
|
| 138 |
-
"Pregnancies": p if p is not None else 0,
|
| 139 |
-
"Glucose": g,
|
| 140 |
-
"Blood Pressure": b,
|
| 141 |
-
"Insulin": i,
|
| 142 |
-
"BMI": bm,
|
| 143 |
-
"Age": a
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
# Call the generator function
|
| 147 |
-
file_path = create_report(user, input_data, result)
|
| 148 |
-
|
| 149 |
-
# Return the file path to make it downloadable
|
| 150 |
-
return gr.update(value=file_path, visible=True)
|
| 151 |
-
|
| 152 |
-
predict_btn.click(
|
| 153 |
-
fn=predict,
|
| 154 |
-
inputs=[preg, glucose, bp, insulin, bmi, age, user_state],
|
| 155 |
-
outputs=[result_output]
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
# NEW: When the prediction is done, show the "Download Report" button
|
| 159 |
-
result_output.change(
|
| 160 |
-
fn=lambda x: gr.update(visible=bool(x)), # Show button only if result_output is not empty
|
| 161 |
-
inputs=result_output,
|
| 162 |
-
outputs=generate_report_btn
|
| 163 |
-
)
|
| 164 |
|
|
|
|
| 165 |
def switch_to_signup(): return gr.update(visible=False), gr.update(visible=True)
|
| 166 |
def switch_to_login(): return gr.update(visible=True), gr.update(visible=False)
|
| 167 |
show_signup_btn.click(fn=switch_to_signup, outputs=[login_col, signup_col])
|
| 168 |
show_login_btn.click(fn=switch_to_login, outputs=[login_col, signup_col])
|
| 169 |
-
|
| 170 |
-
signup_btn.click(
|
| 171 |
-
fn=register,
|
| 172 |
-
inputs=[email_signup, pwd_signup, gender_signup, username_signup],
|
| 173 |
-
outputs=[auth_message, email_signup, pwd_signup, gender_signup, username_signup]
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
login_btn.click(fn=login, inputs=[email_login, pwd_login, user_state], outputs=[auth_message, user_state, main_tabs])
|
| 177 |
-
|
| 178 |
-
logout_btn.click(
|
| 179 |
-
fn=logout,
|
| 180 |
-
inputs=[user_state],
|
| 181 |
-
outputs=[
|
| 182 |
-
auth_message, user_state, main_tabs,
|
| 183 |
-
email_login, pwd_login,
|
| 184 |
-
preg, glucose, bp, insulin, bmi, age, result_output,
|
| 185 |
-
pregnancies_row
|
| 186 |
-
]
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
predict_btn.click(fn=predict, inputs=[preg, glucose, bp, insulin, bmi, age, user_state], outputs=[result_output])
|
| 190 |
|
|
|
|
| 191 |
def handle_user_state_change(user_data):
|
| 192 |
is_logged_in = user_data.get("logged_in", False)
|
| 193 |
is_female = user_data.get("gender") == "Female"
|
| 194 |
username = user_data.get("username", "User")
|
| 195 |
welcome_text = f"### 👋 Welcome, {username}!"
|
| 196 |
-
|
| 197 |
return (
|
| 198 |
gr.update(visible=is_logged_in),
|
| 199 |
gr.update(visible=not is_logged_in),
|
|
@@ -201,12 +154,27 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 201 |
gr.update(value=welcome_text),
|
| 202 |
gr.update(visible=is_logged_in)
|
| 203 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
user_state.change(
|
| 206 |
-
fn=handle_user_state_change,
|
| 207 |
-
inputs=user_state,
|
| 208 |
-
outputs=[prediction_view, logged_out_view, pregnancies_row, welcome_message, logged_in_header]
|
| 209 |
-
)
|
| 210 |
|
| 211 |
# --- Launch the App ---
|
| 212 |
if __name__ == "__main__":
|
|
|
|
| 13 |
|
| 14 |
with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as demo:
|
| 15 |
|
|
|
|
| 16 |
user_state = gr.State({"email": None, "id": None, "logged_in": False, "gender": None, "username": None})
|
| 17 |
|
| 18 |
# --- Static Header (Always Visible) ---
|
|
|
|
| 92 |
signup_btn = gr.Button("Sign Up", variant="primary")
|
| 93 |
show_login_btn = gr.Button("Already have an account? Login.", variant="secondary")
|
| 94 |
|
| 95 |
+
# --- Prediction Tab (Correctly Ordered) ---
|
| 96 |
with gr.TabItem("Prediction", id="prediction_tab"):
|
| 97 |
|
| 98 |
with gr.Column(visible=False) as prediction_view:
|
|
|
|
| 115 |
predict_btn = gr.Button("Predict My Risk", variant="primary")
|
| 116 |
|
| 117 |
result_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 118 |
+
|
| 119 |
+
# The report generation UI components are defined here
|
| 120 |
+
generate_report_btn = gr.Button("Download Report as PDF", variant="secondary", visible=False)
|
| 121 |
+
report_file_output = gr.File(label="Your Report", visible=False)
|
| 122 |
+
|
| 123 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 124 |
|
| 125 |
with gr.Column(visible=True) as logged_out_view:
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
|
| 136 |
+
# --- Component Logic (Event Handlers) (Correctly Ordered) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
# --- Handlers for UI switching ---
|
| 139 |
def switch_to_signup(): return gr.update(visible=False), gr.update(visible=True)
|
| 140 |
def switch_to_login(): return gr.update(visible=True), gr.update(visible=False)
|
| 141 |
show_signup_btn.click(fn=switch_to_signup, outputs=[login_col, signup_col])
|
| 142 |
show_login_btn.click(fn=switch_to_login, outputs=[login_col, signup_col])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# --- Handler for main state changes (login/logout UI updates) ---
|
| 145 |
def handle_user_state_change(user_data):
|
| 146 |
is_logged_in = user_data.get("logged_in", False)
|
| 147 |
is_female = user_data.get("gender") == "Female"
|
| 148 |
username = user_data.get("username", "User")
|
| 149 |
welcome_text = f"### 👋 Welcome, {username}!"
|
|
|
|
| 150 |
return (
|
| 151 |
gr.update(visible=is_logged_in),
|
| 152 |
gr.update(visible=not is_logged_in),
|
|
|
|
| 154 |
gr.update(value=welcome_text),
|
| 155 |
gr.update(visible=is_logged_in)
|
| 156 |
)
|
| 157 |
+
user_state.change(fn=handle_user_state_change, inputs=user_state, outputs=[prediction_view, logged_out_view, pregnancies_row, welcome_message, logged_in_header])
|
| 158 |
+
|
| 159 |
+
# --- Handlers for core auth actions ---
|
| 160 |
+
signup_btn.click(fn=register, inputs=[email_signup, pwd_signup, gender_signup, username_signup], outputs=[auth_message, email_signup, pwd_signup, gender_signup, username_signup])
|
| 161 |
+
login_btn.click(fn=login, inputs=[email_login, pwd_login, user_state], outputs=[auth_message, user_state, main_tabs])
|
| 162 |
+
logout_btn.click(fn=logout, inputs=[user_state], outputs=[auth_message, user_state, main_tabs, email_login, pwd_login, preg, glucose, bp, insulin, bmi, age, result_output, pregnancies_row])
|
| 163 |
+
|
| 164 |
+
# --- Handlers for prediction and report generation ---
|
| 165 |
+
def handle_create_report(user, p, g, b, i, bm, a, result):
|
| 166 |
+
if not result:
|
| 167 |
+
return gr.update(visible=False)
|
| 168 |
+
input_data = {"Pregnancies": p if p is not None else 0, "Glucose": g, "Blood Pressure": b, "Insulin": i, "BMI": bm, "Age": a}
|
| 169 |
+
file_path = create_report(user, input_data, result)
|
| 170 |
+
return gr.update(value=file_path, visible=True)
|
| 171 |
+
|
| 172 |
+
predict_btn.click(fn=predict, inputs=[preg, glucose, bp, insulin, bmi, age, user_state], outputs=[result_output])
|
| 173 |
+
|
| 174 |
+
result_output.change(fn=lambda x: gr.update(visible=bool(x)), inputs=result_output, outputs=generate_report_btn)
|
| 175 |
+
|
| 176 |
+
generate_report_btn.click(fn=handle_create_report, inputs=[user_state, preg, glucose, bp, insulin, bmi, age, result_output], outputs=[report_file_output])
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
# --- Launch the App ---
|
| 180 |
if __name__ == "__main__":
|