Spaces:
Sleeping
Sleeping
File size: 5,355 Bytes
c45982d |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import gradio as gr
import random
import numpy as np
from PIL import Image
import io
import matplotlib.pyplot as plt
import plotly.graph_objects as go
questions = [
{
"question": "How often do you feel overwhelmed by your daily tasks?",
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
},
{
"question": "How would you rate your sleep quality?",
"options": ["Excellent", "Good", "Fair", "Poor"]
},
{
"question": "How often do you feel anxious or worried?",
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
},
{
"question": "How do you typically handle stressful situations?",
"options": ["Very Well", "Moderately Well", "With Difficulty", "Poorly"]
},
{
"question": "How satisfied are you with your work-life balance?",
"options": ["Very Satisfied", "Satisfied", "Dissatisfied", "Very Dissatisfied"]
},
{
"question": "How often do you engage in relaxing activities?",
"options": ["Daily", "Few times a week", "Rarely", "Never"]
},
{
"question": "How would you describe your energy levels throughout the day?",
"options": ["Consistently High", "Moderate", "Fluctuating", "Usually Low"]
},
{
"question": "How often do you feel supported by others?",
"options": ["Always", "Usually", "Occasionally", "Rarely"]
},
{
"question": "How do you rate your ability to concentrate?",
"options": ["Excellent", "Good", "Fair", "Poor"]
},
{
"question": "How often do you experience physical tension or pain?",
"options": ["Rarely", "Sometimes", "Often", "Very Often"]
}
]
def calculate_stress_score(answers):
stress_values = {
"Rarely": 0, "Sometimes": 1, "Often": 2, "Very Often": 3,
"Excellent": 0, "Good": 1, "Fair": 2, "Poor": 3,
"Very Well": 0, "Moderately Well": 1, "With Difficulty": 2, "Poorly": 3,
"Very Satisfied": 0, "Satisfied": 1, "Dissatisfied": 2, "Very Dissatisfied": 3,
"Daily": 0, "Few times a week": 1, "Rarely": 2, "Never": 3,
"Consistently High": 0, "Moderate": 1, "Fluctuating": 2, "Usually Low": 3,
"Always": 0, "Usually": 1, "Occasionally": 2, "Rarely": 3
}
total = sum(stress_values[ans] for ans in answers)
percentage = (total / (3 * 10)) * 100
return percentage
def get_recommendations(stress_score):
if stress_score < 30:
return [
"Maintain your current stress management practices",
"Continue regular exercise and relaxation routines",
"Keep up with your healthy sleep schedule"
]
elif stress_score < 60:
return [
"Consider incorporating meditation or mindfulness practices",
"Take regular breaks during work hours",
"Establish a consistent sleep routine"
]
else:
return [
"Seek professional support or counseling",
"Practice deep breathing exercises daily",
"Prioritize self-care and stress reduction activities"
]
def create_stress_gauge(score):
fig = go.Figure(go.Indicator(
mode = "gauge+number",
value = score,
title = {'text': "Stress Level"},
gauge = {
'axis': {'range': [0, 100]},
'bar': {'color': "red" if score > 60 else "yellow" if score > 30 else "green"},
'steps': [
{'range': [0, 30], 'color': 'lightgreen'},
{'range': [30, 60], 'color': 'lightyellow'},
{'range': [60, 100], 'color': 'lightcoral'}
]
}
))
return fig
def process_assessment(*answers):
score = calculate_stress_score(answers)
recommendations = get_recommendations(score)
gauge_plot = create_stress_gauge(score)
result_html = f"""
<div style='padding: 20px; background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);'>
<h2 style='color: #2c3e50;'>Assessment Results</h2>
<p style='font-size: 18px;'>Your Stress Level: {score:.1f}%</p>
<h3 style='color: #2c3e50; margin-top: 20px;'>Recommendations:</h3>
<ul style='list-style-type: none; padding: 0;'>
"""
for rec in recommendations:
result_html += f"<li style='margin: 10px 0; padding: 10px; background: #f8f9fa; border-radius: 5px;'>π {rec}</li>"
result_html += "</ul></div>"
return gauge_plot, result_html
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple")) as iface:
gr.Markdown("""
# Psychological Stress Assessment
Answer these questions to evaluate your current stress levels and receive personalized recommendations.
""")
with gr.Group():
questions_components = []
for i, q in enumerate(questions):
gr.Markdown(f"### {i+1}. {q['question']}")
questions_components.append(gr.Radio(choices=q['options'], label=""))
submit_btn = gr.Button("Submit Assessment", variant="primary")
with gr.Row():
gauge_output = gr.Plot()
results_output = gr.HTML()
submit_btn.click(
fn=process_assessment,
inputs=questions_components,
outputs=[gauge_output, results_output]
)
iface.launch()
if __name__ == '__main__':
demo.launch() |