Spaces:
Sleeping
Sleeping
File size: 3,333 Bytes
d9a636f b9433b1 d9a636f b9433b1 d9a636f b9433b1 d9a636f b9433b1 d9a636f b9433b1 d9a636f b9433b1 d9a636f b9433b1 | 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 | import gradio as gr
import numpy as np
import joblib
# Load model
model = joblib.load("model.pkl")
# Value mapping
bmi_map = {'Normal': 0, 'Overweight': 1, 'Obese': 2}
caffeine_map = {'Yes': 1, 'No': 0}
# Prediction logic
def predict_sleep_quality(age, sleep_duration, physical_activity, stress_level,
heart_rate, bmi_category, screen_time, caffeine):
input_data = np.array([[age, sleep_duration, physical_activity, stress_level,
heart_rate, bmi_map[bmi_category], screen_time, caffeine_map[caffeine]]])
prediction = model.predict(input_data)[0]
# Tips
tips = []
if sleep_duration < 7:
tips.append("π Try to get at least 7β8 hours of sleep.")
if stress_level > 6:
tips.append("π§ Practice meditation or light exercise.")
if physical_activity < 40:
tips.append("πββοΈ Increase daily activity.")
if caffeine == 'Yes':
tips.append("β Avoid caffeine after 6 PM.")
if screen_time > 2:
tips.append("π΅ Reduce screen time before bed.")
tip_text = "\n".join(f"β’ {tip}" for tip in tips) if tips else "β
Your habits are already sleep-friendly!"
return f"ποΈ Predicted Sleep Quality: {round(prediction, 2)} / 10", tip_text
# Gradio UI with custom styles
with gr.Blocks(css="""
body {
background: linear-gradient(to right, #dbeafe, #e0e7ff);
}
.gradio-container {
font-family: 'Segoe UI', sans-serif;
}
h1, h2, h3 {
color: #1e3a8a;
}
.gr-button {
background: linear-gradient(to right, #6366f1, #4f46e5);
color: white;
border-radius: 10px;
padding: 10px 20px;
}
.gr-button:hover {
background: #4338ca;
}
.gr-input, .gr-dropdown, .gr-slider {
background-color: white !important;
border: 1px solid #c7d2fe !important;
border-radius: 10px !important;
}
""") as demo:
gr.Markdown("# π€ SleepOptimizer")
gr.Markdown("Predict your sleep quality and get personalized suggestions based on your lifestyle habits.")
with gr.Row():
with gr.Column():
age = gr.Slider(10, 100, value=25, label="Age")
sleep_duration = gr.Slider(0.0, 12.0, value=6.5, label="Sleep Duration (hrs)")
physical_activity = gr.Slider(0, 100, value=40, label="Physical Activity (0β100)")
stress_level = gr.Slider(1, 10, value=5, label="Stress Level (1β10)")
with gr.Column():
heart_rate = gr.Slider(40, 120, value=75, label="Heart Rate (bpm)")
bmi_category = gr.Dropdown(['Normal', 'Overweight', 'Obese'], label="BMI Category")
screen_time = gr.Slider(0.0, 10.0, value=3.0, label="Screen Time Before Bed (hrs)")
caffeine = gr.Dropdown(['Yes', 'No'], label="Caffeine After 6 PM?")
predict_btn = gr.Button("π Predict Sleep Quality")
output_score = gr.Textbox(label="π€ Sleep Quality Score")
output_tips = gr.Textbox(label="π‘ Suggestions for Better Sleep", lines=5)
predict_btn.click(
fn=predict_sleep_quality,
inputs=[age, sleep_duration, physical_activity, stress_level,
heart_rate, bmi_category, screen_time, caffeine],
outputs=[output_score, output_tips]
)
gr.Markdown("---")
gr.Markdown("π©βπ» Built with π by Harshita Suri")
demo.launch()
|