Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,19 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import joblib
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
model = joblib.load("model.pkl")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
bmi_map = {'Normal': 0, 'Overweight': 1, 'Obese': 2}
|
| 10 |
caffeine_map = {'Yes': 1, 'No': 0}
|
| 11 |
|
| 12 |
-
# Prediction
|
| 13 |
def predict_sleep_quality(age, sleep_duration, physical_activity, stress_level,
|
| 14 |
heart_rate, bmi_category, screen_time, caffeine):
|
| 15 |
-
# Preprocess
|
| 16 |
input_data = np.array([[age, sleep_duration, physical_activity, stress_level,
|
| 17 |
heart_rate, bmi_map[bmi_category], screen_time, caffeine_map[caffeine]]])
|
|
|
|
| 18 |
prediction = model.predict(input_data)[0]
|
| 19 |
|
| 20 |
# Tips
|
|
@@ -22,37 +22,73 @@ def predict_sleep_quality(age, sleep_duration, physical_activity, stress_level,
|
|
| 22 |
if sleep_duration < 7:
|
| 23 |
tips.append("π Try to get at least 7β8 hours of sleep.")
|
| 24 |
if stress_level > 6:
|
| 25 |
-
tips.append("π§
|
| 26 |
if physical_activity < 40:
|
| 27 |
-
tips.append("π Increase daily activity
|
| 28 |
if caffeine == 'Yes':
|
| 29 |
tips.append("β Avoid caffeine after 6 PM.")
|
| 30 |
if screen_time > 2:
|
| 31 |
-
tips.append("π΅ Reduce screen time before
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return f"ποΈ Predicted Sleep Quality: {round(prediction, 2)} / 10",
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import joblib
|
| 4 |
|
| 5 |
+
# Load model
|
| 6 |
model = joblib.load("model.pkl")
|
| 7 |
|
| 8 |
+
# Value mapping
|
| 9 |
bmi_map = {'Normal': 0, 'Overweight': 1, 'Obese': 2}
|
| 10 |
caffeine_map = {'Yes': 1, 'No': 0}
|
| 11 |
|
| 12 |
+
# Prediction logic
|
| 13 |
def predict_sleep_quality(age, sleep_duration, physical_activity, stress_level,
|
| 14 |
heart_rate, bmi_category, screen_time, caffeine):
|
|
|
|
| 15 |
input_data = np.array([[age, sleep_duration, physical_activity, stress_level,
|
| 16 |
heart_rate, bmi_map[bmi_category], screen_time, caffeine_map[caffeine]]])
|
| 17 |
+
|
| 18 |
prediction = model.predict(input_data)[0]
|
| 19 |
|
| 20 |
# Tips
|
|
|
|
| 22 |
if sleep_duration < 7:
|
| 23 |
tips.append("π Try to get at least 7β8 hours of sleep.")
|
| 24 |
if stress_level > 6:
|
| 25 |
+
tips.append("π§ Practice meditation or light exercise.")
|
| 26 |
if physical_activity < 40:
|
| 27 |
+
tips.append("πββοΈ Increase daily activity.")
|
| 28 |
if caffeine == 'Yes':
|
| 29 |
tips.append("β Avoid caffeine after 6 PM.")
|
| 30 |
if screen_time > 2:
|
| 31 |
+
tips.append("π΅ Reduce screen time before bed.")
|
| 32 |
+
|
| 33 |
+
tip_text = "\n".join(f"β’ {tip}" for tip in tips) if tips else "β
Your habits are already sleep-friendly!"
|
| 34 |
+
return f"ποΈ Predicted Sleep Quality: {round(prediction, 2)} / 10", tip_text
|
| 35 |
+
|
| 36 |
+
# Gradio UI with custom styles
|
| 37 |
+
with gr.Blocks(css="""
|
| 38 |
+
body {
|
| 39 |
+
background: linear-gradient(to right, #dbeafe, #e0e7ff);
|
| 40 |
+
}
|
| 41 |
+
.gradio-container {
|
| 42 |
+
font-family: 'Segoe UI', sans-serif;
|
| 43 |
+
}
|
| 44 |
+
h1, h2, h3 {
|
| 45 |
+
color: #1e3a8a;
|
| 46 |
+
}
|
| 47 |
+
.gr-button {
|
| 48 |
+
background: linear-gradient(to right, #6366f1, #4f46e5);
|
| 49 |
+
color: white;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
padding: 10px 20px;
|
| 52 |
+
}
|
| 53 |
+
.gr-button:hover {
|
| 54 |
+
background: #4338ca;
|
| 55 |
+
}
|
| 56 |
+
.gr-input, .gr-dropdown, .gr-slider {
|
| 57 |
+
background-color: white !important;
|
| 58 |
+
border: 1px solid #c7d2fe !important;
|
| 59 |
+
border-radius: 10px !important;
|
| 60 |
+
}
|
| 61 |
+
""") as demo:
|
| 62 |
+
|
| 63 |
+
gr.Markdown("# π€ SleepOptimizer")
|
| 64 |
+
gr.Markdown("Predict your sleep quality and get personalized suggestions based on your lifestyle habits.")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
age = gr.Slider(10, 100, value=25, label="Age")
|
| 69 |
+
sleep_duration = gr.Slider(0.0, 12.0, value=6.5, label="Sleep Duration (hrs)")
|
| 70 |
+
physical_activity = gr.Slider(0, 100, value=40, label="Physical Activity (0β100)")
|
| 71 |
+
stress_level = gr.Slider(1, 10, value=5, label="Stress Level (1β10)")
|
| 72 |
+
with gr.Column():
|
| 73 |
+
heart_rate = gr.Slider(40, 120, value=75, label="Heart Rate (bpm)")
|
| 74 |
+
bmi_category = gr.Dropdown(['Normal', 'Overweight', 'Obese'], label="BMI Category")
|
| 75 |
+
screen_time = gr.Slider(0.0, 10.0, value=3.0, label="Screen Time Before Bed (hrs)")
|
| 76 |
+
caffeine = gr.Dropdown(['Yes', 'No'], label="Caffeine After 6 PM?")
|
| 77 |
+
|
| 78 |
+
predict_btn = gr.Button("π Predict Sleep Quality")
|
| 79 |
+
|
| 80 |
+
output_score = gr.Textbox(label="π€ Sleep Quality Score")
|
| 81 |
+
output_tips = gr.Textbox(label="π‘ Suggestions for Better Sleep", lines=5)
|
| 82 |
+
|
| 83 |
+
predict_btn.click(
|
| 84 |
+
fn=predict_sleep_quality,
|
| 85 |
+
inputs=[age, sleep_duration, physical_activity, stress_level,
|
| 86 |
+
heart_rate, bmi_category, screen_time, caffeine],
|
| 87 |
+
outputs=[output_score, output_tips]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
gr.Markdown("---")
|
| 91 |
+
gr.Markdown("π©βπ» Built with π by Harshita Suri")
|
| 92 |
+
|
| 93 |
+
demo.launch()
|
| 94 |
+
|