File size: 7,482 Bytes
b3391d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import xgboost as xgb
import pandas as pd
from huggingface_hub import hf_hub_download

xgboostmodel_id = "Sannidhi/stress_prediction_xgboost_model"
xgboost_model = None
 
def load_xgboost_model():
    global xgboost_model
    try:
        # Download the model from Hugging Face using huggingface_hub
        model_path = hf_hub_download(repo_id="Sannidhi/stress_prediction_xgboost_model", filename="xgboost_model.json")
       
        # Load the model into XGBoost
        xgboost_model = xgb.Booster()
        xgboost_model.load_model(model_path)  # Load the model into the Booster object
       
        return True
    except Exception as e:
        st.error(f"Error loading XGBoost model from Hugging Face: {e}")
        return False
 
def display_predict_stress():
    st.title("Predict Stress Level")
    st.markdown("Answer the questions below to predict your stress level.")
 
    # Sidebar for navigation
    with st.sidebar:
        go_home = st.button("Back to Home")
        if go_home:
            st.session_state.page = "home"
            st.experimental_rerun()  # Go back to homepage
    
    load_xgboost_model()
 
    # Define the form with dropdowns for user input
    with st.form(key="stress_form"):
        # Define the questions and their options
        stress_questions = {
            "How many fruits or vegetables do you eat every day?": ["0", "1", "2", "3", "4", "5"],
            "How many new places do you visit in an year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many people are very close to you?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many people do you help achieve a better life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "With how many people do you interact with during a typical day?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many remarkable achievements are you proud of?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many times do you donate your time or money to good causes?": ["0", "1", "2", "3", "4", "5"],
            "How well do you complete your weekly to-do lists?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "In a typical day, how many hours do you experience 'FLOW'? (Flow is defined as the mental state, in which you are fully immersed in performing an activity. You then experience a feeling of energized focus, full involvement, and enjoyment in the process of this activity)": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many steps (in thousands) do you typically walk everyday?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "For how many years ahead is your life vision very clear for?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "About how long do you typically sleep?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many days of vacation do you typically lose every year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How often do you shout or sulk at somebody?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": ["1", "2"],
            "How many recognitions have you received in your life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "How many hours do you spend everyday doing what you are passionate about?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "In a typical week, how many times do you have the opportunity to think about yourself?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            "Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": ["1", "2", "3", "4"],
            "Gender (1 = 'Female', 0 = 'Male')": ["0", "1"]
        }

        # Map the question strings to model feature names
        question_to_feature_map = {
            "How many fruits or vegetables do you eat every day?": "FRUITS_VEGGIES",
            "How many new places do you visit in an year?": "PLACES_VISITED",
            "How many people are very close to you?": "CORE_CIRCLE",
            "How many people do you help achieve a better life?": "SUPPORTING_OTHERS",
            "With how many people do you interact with during a typical day?": "SOCIAL_NETWORK",
            "How many remarkable achievements are you proud of?": "ACHIEVEMENT",
            "How many times do you donate your time or money to good causes?": "DONATION",
            "How well do you complete your weekly to-do lists?": "TODO_COMPLETED",
            "In a typical day, how many hours do you experience 'FLOW'? (Flow is defined as the mental state, in which you are fully immersed in performing an activity. You then experience a feeling of energized focus, full involvement, and enjoyment in the process of this activity)": "FLOW",
            "How many steps (in thousands) do you typically walk everyday?": "DAILY_STEPS",
            "For how many years ahead is your life vision very clear for?": "LIVE_VISION",
            "About how long do you typically sleep?": "SLEEP_HOURS",
            "How many days of vacation do you typically lose every year?": "LOST_VACATION",
            "How often do you shout or sulk at somebody?": "DAILY_SHOUTING",
            "How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": "SUFFICIENT_INCOME",
            "How many recognitions have you received in your life?": "PERSONAL_AWARDS",
            "How many hours do you spend everyday doing what you are passionate about?": "TIME_FOR_PASSION",
            "In a typical week, how many times do you have the opportunity to think about yourself?": "WEEKLY_MEDITATION",
            "Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": "AGE",
            "Gender (1 = 'Female', 0 = 'Male')": "GENDER"
        }

        # Map the responses to numerical values
        response_map = {str(i): i for i in range(11)}  # Mapping 0-10 to 0-10
        response_map.update({"1": 1, "2": 2})  # Mapping "1" and "2" for certain questions

        # Store user responses
        responses = {}
        for question, options in stress_questions.items():
            responses[question] = st.selectbox(question, options)

        # Submit button
        submit_button = st.form_submit_button("Submit")

        # When submit is clicked, process the responses and make a prediction
        if submit_button:
            # Convert responses to feature dictionary based on the feature names
            feature_dict = {question_to_feature_map[q]: response_map[responses[q]] for q in stress_questions.keys()}

            # Convert to pandas DataFrame
            feature_df = pd.DataFrame([feature_dict])

            # Make prediction
            try:
                dmatrix = xgb.DMatrix(feature_df)
                prediction = xgboost_model.predict(dmatrix)
                st.markdown(f"### Predicted Stress Level: {prediction[0]:.2f}")
                st.markdown("Higher values indicate higher stress levels.")
            except Exception as e:
                st.error(f"Error making prediction: {e}")