Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
questions = [
|
9 |
+
{
|
10 |
+
"question": "How often do you feel overwhelmed by your daily tasks?",
|
11 |
+
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"question": "How would you rate your sleep quality?",
|
15 |
+
"options": ["Excellent", "Good", "Fair", "Poor"]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"question": "How often do you feel anxious or worried?",
|
19 |
+
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"question": "How do you typically handle stressful situations?",
|
23 |
+
"options": ["Very Well", "Moderately Well", "With Difficulty", "Poorly"]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"question": "How satisfied are you with your work-life balance?",
|
27 |
+
"options": ["Very Satisfied", "Satisfied", "Dissatisfied", "Very Dissatisfied"]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"question": "How often do you engage in relaxing activities?",
|
31 |
+
"options": ["Daily", "Few times a week", "Rarely", "Never"]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"question": "How would you describe your energy levels throughout the day?",
|
35 |
+
"options": ["Consistently High", "Moderate", "Fluctuating", "Usually Low"]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"question": "How often do you feel supported by others?",
|
39 |
+
"options": ["Always", "Usually", "Occasionally", "Rarely"]
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"question": "How do you rate your ability to concentrate?",
|
43 |
+
"options": ["Excellent", "Good", "Fair", "Poor"]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"question": "How often do you experience physical tension or pain?",
|
47 |
+
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
|
48 |
+
}
|
49 |
+
]
|
50 |
+
def calculate_stress_score(answers):
|
51 |
+
stress_values = {
|
52 |
+
"Rarely": 0, "Sometimes": 1, "Often": 2, "Very Often": 3,
|
53 |
+
"Excellent": 0, "Good": 1, "Fair": 2, "Poor": 3,
|
54 |
+
"Very Well": 0, "Moderately Well": 1, "With Difficulty": 2, "Poorly": 3,
|
55 |
+
"Very Satisfied": 0, "Satisfied": 1, "Dissatisfied": 2, "Very Dissatisfied": 3,
|
56 |
+
"Daily": 0, "Few times a week": 1, "Rarely": 2, "Never": 3,
|
57 |
+
"Consistently High": 0, "Moderate": 1, "Fluctuating": 2, "Usually Low": 3,
|
58 |
+
"Always": 0, "Usually": 1, "Occasionally": 2, "Rarely": 3
|
59 |
+
}
|
60 |
+
total = sum(stress_values[ans] for ans in answers)
|
61 |
+
percentage = (total / (3 * 10)) * 100
|
62 |
+
return percentage
|
63 |
+
def get_recommendations(stress_score):
|
64 |
+
if stress_score < 30:
|
65 |
+
return [
|
66 |
+
"Maintain your current stress management practices",
|
67 |
+
"Continue regular exercise and relaxation routines",
|
68 |
+
"Keep up with your healthy sleep schedule"
|
69 |
+
]
|
70 |
+
elif stress_score < 60:
|
71 |
+
return [
|
72 |
+
"Consider incorporating meditation or mindfulness practices",
|
73 |
+
"Take regular breaks during work hours",
|
74 |
+
"Establish a consistent sleep routine"
|
75 |
+
]
|
76 |
+
else:
|
77 |
+
return [
|
78 |
+
"Seek professional support or counseling",
|
79 |
+
"Practice deep breathing exercises daily",
|
80 |
+
"Prioritize self-care and stress reduction activities"
|
81 |
+
]
|
82 |
+
def create_stress_gauge(score):
|
83 |
+
fig = go.Figure(go.Indicator(
|
84 |
+
mode = "gauge+number",
|
85 |
+
value = score,
|
86 |
+
title = {'text': "Stress Level"},
|
87 |
+
gauge = {
|
88 |
+
'axis': {'range': [0, 100]},
|
89 |
+
'bar': {'color': "red" if score > 60 else "yellow" if score > 30 else "green"},
|
90 |
+
'steps': [
|
91 |
+
{'range': [0, 30], 'color': 'lightgreen'},
|
92 |
+
{'range': [30, 60], 'color': 'lightyellow'},
|
93 |
+
{'range': [60, 100], 'color': 'lightcoral'}
|
94 |
+
]
|
95 |
+
}
|
96 |
+
))
|
97 |
+
return fig
|
98 |
+
def process_assessment(*answers):
|
99 |
+
score = calculate_stress_score(answers)
|
100 |
+
recommendations = get_recommendations(score)
|
101 |
+
gauge_plot = create_stress_gauge(score)
|
102 |
+
result_html = f"""
|
103 |
+
<div style='padding: 20px; background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);'>
|
104 |
+
<h2 style='color: #2c3e50;'>Assessment Results</h2>
|
105 |
+
<p style='font-size: 18px;'>Your Stress Level: {score:.1f}%</p>
|
106 |
+
<h3 style='color: #2c3e50; margin-top: 20px;'>Recommendations:</h3>
|
107 |
+
<ul style='list-style-type: none; padding: 0;'>
|
108 |
+
"""
|
109 |
+
for rec in recommendations:
|
110 |
+
result_html += f"<li style='margin: 10px 0; padding: 10px; background: #f8f9fa; border-radius: 5px;'>🌟 {rec}</li>"
|
111 |
+
result_html += "</ul></div>"
|
112 |
+
return gauge_plot, result_html
|
113 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple")) as iface:
|
114 |
+
gr.Markdown("""
|
115 |
+
# Psychological Stress Assessment
|
116 |
+
Answer these questions to evaluate your current stress levels and receive personalized recommendations.
|
117 |
+
""")
|
118 |
+
with gr.Group():
|
119 |
+
questions_components = []
|
120 |
+
for i, q in enumerate(questions):
|
121 |
+
gr.Markdown(f"### {i+1}. {q['question']}")
|
122 |
+
questions_components.append(gr.Radio(choices=q['options'], label=""))
|
123 |
+
submit_btn = gr.Button("Submit Assessment", variant="primary")
|
124 |
+
with gr.Row():
|
125 |
+
gauge_output = gr.Plot()
|
126 |
+
results_output = gr.HTML()
|
127 |
+
submit_btn.click(
|
128 |
+
fn=process_assessment,
|
129 |
+
inputs=questions_components,
|
130 |
+
outputs=[gauge_output, results_output]
|
131 |
+
)
|
132 |
+
iface.launch()
|
133 |
+
|
134 |
+
if __name__ == '__main__':
|
135 |
+
demo.launch()
|