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()