Spaces:
Sleeping
Sleeping
File size: 11,047 Bytes
d5d88eb 98849e2 d5d88eb 73b109e d5d88eb 73b109e d5d88eb 73b109e d5d88eb 73b109e d5d88eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# -*- coding: utf-8 -*-
"""LastwebSiteforAlzahimer.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/14idWYo0QCi5Bokd4Fhb6Apz8XdBPIa4b
"""
import gradio as gr
import numpy as np
sections = {
"Mediterranean Diet Assessment (Yes/No)": [
"1. Use olive oil as main cooking fat?",
"2. Eat more than or equal 2 servings of vegetables daily?",
"3. Eat more than or equal 3 servings of fruit daily?",
"4. Eat fish/seafood more than or equal 3 times/week?",
"5. Eat legumes more than or equal 2 times/week?",
"6. Eat nuts more than or equal 3 times/week?",
"7. Limit red/processed meats?",
"8. Choose whole grains?",
"9. Avoid sugary snacks/drinks?",
"10. Drink 6-8 glasses water/day?"
],
"Sleep Quality Assessment": [
"1. Trouble falling asleep more 30 mins?",
"2. Wake up more 1 time/night?",
"3. Restless/disturbed sleep?",
"4. Poor sleep quality?",
"5. Daytime fatigue?"
],
"Health Metrics": [
"Weight (kg)",
"Height (cm)",
"Diabetes?",
"Smoker?"
]
}
def calculate_total_score(*answers):
try:
# التحقق من المدخلات
if any(a is None for a in answers[:10]):
return "❌ Please answer all Mediterranean Diet questions.", 0
if any(a is None for a in answers[10:15]):
return "❌ Please answer all Sleep Quality questions.", 0
# حساب النتائج
med_diet = sum([1 if a == "Yes" else 0 for a in answers[0:10]])
sleep_score = sum([2 if a == "Yes" else 0 for a in answers[10:15]])
# معالجة النشاط البدني
vigorous_days, vigorous_hrs, vigorous_mins, \
moderate_days, moderate_hrs, moderate_mins, \
walk_days, walk_hrs, walk_mins, \
sitting_hrs, sitting_mins = [a or 0 for a in answers[15:26]]
vigorous = vigorous_days * (vigorous_hrs*60 + vigorous_mins)
moderate = moderate_days * (moderate_hrs*60 + moderate_mins)
walking = walk_days * (walk_hrs*60 + walk_mins)
met = (vigorous*8.0) + (moderate*4.0) + (walking*3.3)
activity = 0
if met >= 6000: activity = 10
elif met >= 3000: activity = 9
elif met >= 1500: activity = 7
elif met >= 600: activity = 5
elif met >= 100: activity = 3
# حساب الBMI
weight, height = answers[25:27]
bmi_score = 10 if (weight / ((height / 100) ** 2)) >= 30 else 0
# عوامل الخطر
diabetes, smoker = answers[27:29]
risk = 5*(diabetes == "Yes") + 5*(smoker == "Yes")
total_score = med_diet + sleep_score + activity + bmi_score + risk
final_score = int(round((total_score/50)*10))
# إنشاء التقرير
if final_score >= 7:
status_color = "#2ecc71"
status_title = "Low Alzheimer's Risk - Excellent"
status_icon = "🛡️"
elif final_score >= 4:
status_color = "#f1c40f"
status_title = "Moderate Risk - Needs Improvement"
status_icon = "⚠️"
else:
status_color = "#e74c3c"
status_title = "High Alzheimer's Risk - Take Action"
status_icon = "🚨"
# تحليل الفئات
categories = {
"Diet": {
"score": med_diet,
"max": 10,
"advice": {
"good": ["Maintain your healthy eating habits!", "Increase fish and olive oil consumption"],
"bad": ["Reduce red meat intake", "Choose whole grains over refined carbs"]
}
},
"Sleep": {
"score": sleep_score,
"max": 10,
"advice": {
"good": ["Maintain consistent sleep schedule"],
"bad": ["Avoid caffeine before bedtime", "Limit screen time before sleep"]
}
},
"Activity": {
"score": activity,
"max": 10,
"advice": {
"good": ["Great activity level! Keep it up!"],
"bad": ["Aim for 150 mins exercise/week", "Take walking breaks regularly"]
}
},
"Health": {
"score": 20 - (bmi_score + risk),
"max": 20,
"advice": {
"good": ["Maintain healthy weight"],
"bad": ["Monitor blood sugar levels", "Consider smoking cessation programs"]
}
}
}
category_cards = ""
for name, data in categories.items():
percent = (data['score']/data['max'])*100
color = "#2ecc71" if percent >= 60 else "#f1c40f" if percent >= 40 else "#e74c3c"
category_cards += f"""
<div style='background:white; padding:15px; border-radius:8px; margin-bottom:15px; box-shadow:0 2px 5px rgba(0,0,0,0.1);'>
<h3 style='margin:0 0 10px 0; color:{color};'>{name}</h3>
<div style='background:#eee; height:8px; border-radius:4px;'>
<div style='width:{percent}%; background:{color}; height:100%; border-radius:4px;'></div>
</div>
<ul style='padding-left:20px; margin:10px 0 0 0;'>
{''.join([f'<li>{tip}</li>' for tip in (data["advice"]["good"] if percent >= 60 else data["advice"]["bad"][:2])])}
</ul>
</div>
"""
report = f"""
<div style='padding:20px; border-radius:10px; background:#f8f9fa;'>
<h2 style='text-align:center; color:{status_color}; margin-bottom:20px;'>
{status_icon} {status_title}
</h2>
<div style='columns:2; gap:20px; margin-bottom:20px;'>
{category_cards}
</div>
<div style='background:#e8f5e9; padding:15px; border-radius:8px;'>
<h3 style='margin-top:0;'>General Recommendations:</h3>
<ul>
<li>Maintain regular physical activity</li>
<li>Follow a balanced Mediterranean diet</li>
<li>Ensure 7-8 hours of quality sleep</li>
<li>Monitor weight and blood sugar levels</li>
</ul>
</div>
</div>
"""
return report, final_score
except Exception as e:
error_html = f"""
<div style='color:white; background:#e74c3c; padding:15px; border-radius:8px; text-align:center;'>
<h3>❌ Calculation Error</h3>
<p>{str(e)}</p>
<p>Please verify all input values</p>
</div>
"""
return error_html, 0
# بناء الواجهة
with gr.Blocks(theme=gr.themes.Soft(), css="""
.full-width-image img { width:100% !important; height:auto; }
.section { border:1px solid #ddd; padding:15px; margin:10px 0; border-radius:8px; }
""") as demo:
diet_score_state = gr.State(value=0)
with gr.Tabs():
with gr.Tab("Alzheimer's on Health Assessment"):
gr.Image(value="Images/diet.jpg",
height=300,
container=False,
elem_classes="full-width-image")
gr.Markdown("""
<h1 style='text-align: center; color: #2E8B57;'>🧠 Alzheimer's Risk Assessment</h1>
<p style='text-align: center;'>Answer all questions honestly</p>
""")
inputs = []
# Mediterranean Diet
with gr.Group(elem_classes="section"):
gr.Markdown("### Mediterranean Diet (Yes/No)")
for q in sections["Mediterranean Diet Assessment (Yes/No)"]:
inputs.append(gr.Radio(["Yes", "No"], label=q))
# Sleep Quality
with gr.Group(elem_classes="section"):
gr.Markdown("### Sleep Quality Evaluation")
for q in sections["Sleep Quality Assessment"]:
inputs.append(gr.Radio(["Yes", "No"], label=q))
# Physical Activity - IPAQ
with gr.Group(elem_classes="section"):
gr.Markdown("### International Physical Activity Questionnaire (IPAQ – Short Form)")
gr.Markdown("**1. Vigorous Physical Activity**")
gr.Markdown("During the last 7 days, on how many days did you do vigorous physical activities (e.g., heavy lifting, aerobics, fast bicycling)?")
inputs.append(gr.Number(label="Days per week", precision=0, minimum=0, maximum=7))
inputs.append(gr.Number(label="Hours per day", precision=0, minimum=0))
inputs.append(gr.Number(label="Minutes per hour", precision=0, minimum=0, maximum=59))
gr.Markdown("**2. Moderate Physical Activity**")
gr.Markdown("During the last 7 days, on how many days did you do moderate physical activities (e.g., carrying light loads, cycling at regular pace, doubles tennis)?")
inputs.append(gr.Number(label="Days per week", precision=0, minimum=0, maximum=7))
inputs.append(gr.Number(label="Hours per day", precision=0, minimum=0))
inputs.append(gr.Number(label="Minutes per hour", precision=0, minimum=0, maximum=59))
gr.Markdown("**3. Walking**")
gr.Markdown("During the last 7 days, on how many days did you walk for at least 10 minutes at a time (including work, home, travel, and leisure)?")
inputs.append(gr.Number(label="Days per week", precision=0, minimum=0, maximum=7))
inputs.append(gr.Number(label="Hours per day", precision=0, minimum=0))
inputs.append(gr.Number(label="Minutes per hour", precision=0, minimum=0, maximum=59))
gr.Markdown("**4. Sitting Time**")
gr.Markdown("During the last 7 days, how much time did you spend sitting on a weekday?")
inputs.append(gr.Number(label="Hours per day", precision=0, minimum=0))
inputs.append(gr.Number(label="Minutes per hour", precision=0, minimum=0, maximum=59))
# Health Metrics
with gr.Group(elem_classes="section"):
gr.Markdown("### Health Indicators")
for q in sections["Health Metrics"]:
if q in ["Diabetes?", "Smoker?"]:
inputs.append(gr.Dropdown(["Yes", "No"], label=q))
else:
inputs.append(gr.Number(label=q, precision=1))
submit_btn = gr.Button("Generate Risk Report", variant="primary")
output = gr.HTML(label="Risk Assessment Report")
submit_btn.click(fn=calculate_total_score, inputs=inputs, outputs=[output, diet_score_state])
demo.launch(share=True) |