Spaces:
Sleeping
Sleeping
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() |