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"""
Your Stress Level: {score:.1f}%