HarshitaSuri commited on
Commit
b9433b1
Β·
verified Β·
1 Parent(s): 1b5654d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -34
app.py CHANGED
@@ -2,19 +2,19 @@ import gradio as gr
2
  import numpy as np
3
  import joblib
4
 
5
- # Load the model
6
  model = joblib.load("model.pkl")
7
 
8
- # BMI and caffeine mapping
9
  bmi_map = {'Normal': 0, 'Overweight': 1, 'Obese': 2}
10
  caffeine_map = {'Yes': 1, 'No': 0}
11
 
12
- # Prediction function
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("🧘 Reduce stress through meditation or light exercise.")
26
  if physical_activity < 40:
27
- tips.append("πŸƒ Increase daily activity for better rest.")
28
  if caffeine == 'Yes':
29
  tips.append("β˜• Avoid caffeine after 6 PM.")
30
  if screen_time > 2:
31
- tips.append("πŸ“΅ Reduce screen time before sleep.")
32
-
33
- tips_output = "\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", tips_output
35
-
36
- # Interface
37
- iface = gr.Interface(
38
- fn=predict_sleep_quality,
39
- inputs=[
40
- gr.Slider(10, 100, value=25, label="Age"),
41
- gr.Slider(0.0, 12.0, value=6.5, label="Sleep Duration (hours)"),
42
- gr.Slider(0, 100, value=40, label="Physical Activity Level"),
43
- gr.Slider(1, 10, value=5, label="Stress Level"),
44
- gr.Slider(40, 120, value=75, label="Heart Rate (bpm)"),
45
- gr.Dropdown(['Normal', 'Overweight', 'Obese'], label="BMI Category"),
46
- gr.Slider(0.0, 10.0, value=3.0, label="Screen Time Before Bed (hours)"),
47
- gr.Dropdown(['Yes', 'No'], label="Caffeine After 6 PM?")
48
- ],
49
- outputs=[
50
- gr.Textbox(label="Sleep Quality Score"),
51
- gr.Textbox(label="Tips to Improve Sleep")
52
- ],
53
- title="πŸ’€ SleepOptimizer",
54
- description="AI-powered sleep quality predictor with personalized suggestions.",
55
- theme=gr.themes.Soft()
56
- )
57
-
58
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+