Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import gradio as gr
|
|
| 4 |
from components.auth import register, login, logout
|
| 5 |
from components.predict import predict
|
| 6 |
|
|
|
|
| 7 |
css = """
|
| 8 |
.gradio-container { max-width: 1000px !important; margin: auto !important; }
|
| 9 |
.centered-text { text-align: center; }
|
|
@@ -11,16 +12,59 @@ css = """
|
|
| 11 |
|
| 12 |
with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as demo:
|
| 13 |
|
|
|
|
| 14 |
user_state = gr.State({"email": None, "id": None, "logged_in": False, "gender": None})
|
| 15 |
|
|
|
|
| 16 |
gr.Markdown("# 🩺 Welcome to DiaSpark: An AI-based Diabetes Detector")
|
| 17 |
gr.Markdown("#### Your Proactive Partner in Health Awareness")
|
| 18 |
|
|
|
|
| 19 |
with gr.Tabs() as main_tabs:
|
| 20 |
-
with gr.TabItem("Home", id="home_tab"):
|
| 21 |
-
# Homepage content... (omitted for brevity)
|
| 22 |
-
gr.Markdown("Content...")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
with gr.TabItem("Login / Signup", id="auth_tab"):
|
| 25 |
auth_message = gr.Markdown()
|
| 26 |
|
|
@@ -34,21 +78,30 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 34 |
with gr.Column(visible=False) as signup_col:
|
| 35 |
gr.Markdown("### Create a New Account")
|
| 36 |
email_signup = gr.Textbox(label="Email", placeholder="Enter your email")
|
| 37 |
-
pwd_signup = gr.Textbox(
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
signup_btn = gr.Button("Sign Up", variant="primary")
|
| 41 |
show_login_btn = gr.Button("Already have an account? Login.", variant="secondary")
|
| 42 |
|
|
|
|
| 43 |
with gr.TabItem("Prediction", id="prediction_tab"):
|
|
|
|
|
|
|
| 44 |
with gr.Column(visible=False) as prediction_view:
|
| 45 |
gr.Markdown("### Enter Your Health Metrics for Prediction")
|
| 46 |
gr.Markdown("---")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
with gr.Row(visible=False) as pregnancies_row:
|
| 50 |
preg = gr.Number(label="Pregnancies", info="Number of times pregnant")
|
| 51 |
|
|
|
|
| 52 |
with gr.Row():
|
| 53 |
with gr.Column():
|
| 54 |
glucose = gr.Number(label="Glucose", info="Plasma glucose concentration")
|
|
@@ -64,10 +117,20 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 64 |
result_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 65 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 66 |
|
|
|
|
| 67 |
with gr.Column(visible=True) as logged_out_view:
|
| 68 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# --- Event Handlers ---
|
| 71 |
def switch_to_signup():
|
| 72 |
return gr.update(visible=False), gr.update(visible=True)
|
| 73 |
|
|
@@ -92,25 +155,30 @@ with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as dem
|
|
| 92 |
auth_message, user_state, main_tabs,
|
| 93 |
email_login, pwd_login,
|
| 94 |
preg, glucose, bp, insulin, bmi, age, result_output,
|
| 95 |
-
pregnancies_row
|
| 96 |
]
|
| 97 |
)
|
| 98 |
|
| 99 |
predict_btn.click(fn=predict, inputs=[preg, glucose, bp, insulin, bmi, age, user_state], outputs=[result_output])
|
| 100 |
|
|
|
|
| 101 |
def handle_user_state_change(user_data):
|
| 102 |
"""Controls visibility of UI elements based on login state and gender."""
|
| 103 |
is_logged_in = user_data.get("logged_in", False)
|
| 104 |
is_female = user_data.get("gender") == "Female"
|
| 105 |
-
|
| 106 |
-
#
|
|
|
|
| 107 |
return gr.update(visible=is_logged_in), gr.update(visible=not is_logged_in), gr.update(visible=is_female)
|
| 108 |
|
|
|
|
| 109 |
user_state.change(
|
| 110 |
fn=handle_user_state_change,
|
| 111 |
inputs=user_state,
|
| 112 |
outputs=[prediction_view, logged_out_view, pregnancies_row]
|
| 113 |
)
|
| 114 |
|
|
|
|
|
|
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.launch()
|
|
|
|
| 4 |
from components.auth import register, login, logout
|
| 5 |
from components.predict import predict
|
| 6 |
|
| 7 |
+
# --- Style and Theme ---
|
| 8 |
css = """
|
| 9 |
.gradio-container { max-width: 1000px !important; margin: auto !important; }
|
| 10 |
.centered-text { text-align: center; }
|
|
|
|
| 12 |
|
| 13 |
with gr.Blocks(title="DiaSpark: Your spark for early detection", css=css) as demo:
|
| 14 |
|
| 15 |
+
# The user_state now includes a 'gender' field, which is populated on login
|
| 16 |
user_state = gr.State({"email": None, "id": None, "logged_in": False, "gender": None})
|
| 17 |
|
| 18 |
+
# --- Header ---
|
| 19 |
gr.Markdown("# 🩺 Welcome to DiaSpark: An AI-based Diabetes Detector")
|
| 20 |
gr.Markdown("#### Your Proactive Partner in Health Awareness")
|
| 21 |
|
| 22 |
+
# --- Main Tabs ---
|
| 23 |
with gr.Tabs() as main_tabs:
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# --- Homepage Tab ---
|
| 26 |
+
with gr.TabItem("Home", id="home_tab"):
|
| 27 |
+
gr.Markdown(
|
| 28 |
+
"<h2 class='centered-text'>A Smart First Step in Understanding Your Health</h2>",
|
| 29 |
+
elem_classes="centered-text"
|
| 30 |
+
)
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
DiaSpark is a confidential tool that uses machine learning to provide an educational analysis of your diabetes risk factors.
|
| 34 |
+
Our goal is to promote health awareness, not to provide a medical diagnosis.
|
| 35 |
+
"""
|
| 36 |
+
)
|
| 37 |
+
gr.Markdown("---")
|
| 38 |
+
|
| 39 |
+
with gr.Accordion("🎯 Our Mission: The 'Why'", open=True):
|
| 40 |
+
gr.Markdown(
|
| 41 |
+
"""
|
| 42 |
+
We believe that awareness is the first and most powerful step towards a healthier life.
|
| 43 |
+
This tool was built to help you:
|
| 44 |
+
* **Gain Insight:** Understand how key health metrics can contribute to your overall risk profile.
|
| 45 |
+
* **Encourage Proactive Care:** Use this information as a starting point for meaningful conversations with your doctor.
|
| 46 |
+
* **Do It All Privately:** Your health data is sensitive. Our secure login ensures your prediction history is for your eyes only.
|
| 47 |
+
"""
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
with gr.Accordion("🚀 Getting Started: The 'How'", open=False):
|
| 51 |
+
gr.Markdown(
|
| 52 |
+
"""
|
| 53 |
+
1. **Create Your Secure Account:** Navigate to the **'Login / Signup'** tab. The process is quick and requires only an email, password, and gender.
|
| 54 |
+
2. **Enter Your Health Metrics:** Once logged in, go to the **'Prediction'** tab. Fill in the fields with your most recent health information.
|
| 55 |
+
3. **Receive Your Instant Analysis:** Click the 'Predict My Risk' button. Our AI model will provide an immediate, educational assessment based on your data.
|
| 56 |
+
"""
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
with gr.Accordion("⚠️ Important Disclaimer", open=False):
|
| 60 |
+
gr.Markdown(
|
| 61 |
+
"""
|
| 62 |
+
**This tool is NOT a substitute for professional medical advice, diagnosis, or treatment.**
|
| 63 |
+
The results from DiaSpark are for informational purposes only. Always seek the advice of your physician or another qualified health provider with any questions you may have regarding a medical condition. Never disregard professional medical advice or delay in seeking it because of something you have read or seen on this application.
|
| 64 |
+
"""
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# --- Authentication Tab ---
|
| 68 |
with gr.TabItem("Login / Signup", id="auth_tab"):
|
| 69 |
auth_message = gr.Markdown()
|
| 70 |
|
|
|
|
| 78 |
with gr.Column(visible=False) as signup_col:
|
| 79 |
gr.Markdown("### Create a New Account")
|
| 80 |
email_signup = gr.Textbox(label="Email", placeholder="Enter your email")
|
| 81 |
+
pwd_signup = gr.Textbox(
|
| 82 |
+
label="Password",
|
| 83 |
+
type="password",
|
| 84 |
+
placeholder="Create a password",
|
| 85 |
+
info="Must be at least 6 characters long."
|
| 86 |
+
)
|
| 87 |
+
# NEW: Gender radio buttons for signup
|
| 88 |
+
gender_signup = gr.Radio(["Female", "Male"], label="Gender", info="This determines if the 'Pregnancies' field is shown.")
|
| 89 |
signup_btn = gr.Button("Sign Up", variant="primary")
|
| 90 |
show_login_btn = gr.Button("Already have an account? Login.", variant="secondary")
|
| 91 |
|
| 92 |
+
# --- Prediction Tab ---
|
| 93 |
with gr.TabItem("Prediction", id="prediction_tab"):
|
| 94 |
+
|
| 95 |
+
# This container is shown to logged-in users
|
| 96 |
with gr.Column(visible=False) as prediction_view:
|
| 97 |
gr.Markdown("### Enter Your Health Metrics for Prediction")
|
| 98 |
gr.Markdown("---")
|
| 99 |
|
| 100 |
+
# NEW: The pregnancies field is now in its own row to control its visibility
|
| 101 |
with gr.Row(visible=False) as pregnancies_row:
|
| 102 |
preg = gr.Number(label="Pregnancies", info="Number of times pregnant")
|
| 103 |
|
| 104 |
+
# The rest of the prediction fields
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
| 107 |
glucose = gr.Number(label="Glucose", info="Plasma glucose concentration")
|
|
|
|
| 117 |
result_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 118 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 119 |
|
| 120 |
+
# This container is shown to logged-out users
|
| 121 |
with gr.Column(visible=True) as logged_out_view:
|
| 122 |
+
gr.Markdown(
|
| 123 |
+
"""
|
| 124 |
+
<div style="text-align:center; padding: 50px;">
|
| 125 |
+
<h2>🔒 Please log in to access the prediction tool.</h2>
|
| 126 |
+
<p>You can create an account or log in via the 'Login / Signup' tab.</p>
|
| 127 |
+
</div>
|
| 128 |
+
"""
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# --- Component Logic (Event Handlers) ---
|
| 133 |
|
|
|
|
| 134 |
def switch_to_signup():
|
| 135 |
return gr.update(visible=False), gr.update(visible=True)
|
| 136 |
|
|
|
|
| 155 |
auth_message, user_state, main_tabs,
|
| 156 |
email_login, pwd_login,
|
| 157 |
preg, glucose, bp, insulin, bmi, age, result_output,
|
| 158 |
+
pregnancies_row # Add pregnancies_row to be cleared/hidden on logout
|
| 159 |
]
|
| 160 |
)
|
| 161 |
|
| 162 |
predict_btn.click(fn=predict, inputs=[preg, glucose, bp, insulin, bmi, age, user_state], outputs=[result_output])
|
| 163 |
|
| 164 |
+
# NEW: Updated function to handle visibility of both prediction view AND pregnancies row
|
| 165 |
def handle_user_state_change(user_data):
|
| 166 |
"""Controls visibility of UI elements based on login state and gender."""
|
| 167 |
is_logged_in = user_data.get("logged_in", False)
|
| 168 |
is_female = user_data.get("gender") == "Female"
|
| 169 |
+
|
| 170 |
+
# The main prediction form is visible only if the user is logged in.
|
| 171 |
+
# The pregnancies row is visible only if the user is logged in AND their gender is 'Female'.
|
| 172 |
return gr.update(visible=is_logged_in), gr.update(visible=not is_logged_in), gr.update(visible=is_female)
|
| 173 |
|
| 174 |
+
# The user_state listener now controls three components
|
| 175 |
user_state.change(
|
| 176 |
fn=handle_user_state_change,
|
| 177 |
inputs=user_state,
|
| 178 |
outputs=[prediction_view, logged_out_view, pregnancies_row]
|
| 179 |
)
|
| 180 |
|
| 181 |
+
|
| 182 |
+
# --- Launch the App ---
|
| 183 |
if __name__ == "__main__":
|
| 184 |
demo.launch()
|