Spaces:
Running
Running
Update predict_stress.py
Browse files- predict_stress.py +192 -192
predict_stress.py
CHANGED
@@ -1,193 +1,193 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import xgboost as xgb
|
3 |
-
import pandas as pd
|
4 |
-
from huggingface_hub import hf_hub_download
|
5 |
-
import itertools
|
6 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
7 |
-
import os
|
8 |
-
from transformers import pipeline
|
9 |
-
from langchain_core.prompts import PromptTemplate
|
10 |
-
from langchain_core.output_parsers import StrOutputParser
|
11 |
-
|
12 |
-
xgboostmodel_id = "Sannidhi/stress_prediction_xgboost_model"
|
13 |
-
xgboost_model = None
|
14 |
-
model_id = "
|
15 |
-
generator = pipeline("text-generation", model=model_id)
|
16 |
-
|
17 |
-
def get_llm_response(prompt_text, model_id="meta-llama/Llama-3.2-3B-Instruct", max_new_tokens=256, temperature=0.5):
|
18 |
-
"""Generates a response from the Hugging Face model for a given prompt text."""
|
19 |
-
try:
|
20 |
-
llm = HuggingFaceEndpoint(
|
21 |
-
repo_id=model_id,
|
22 |
-
max_new_tokens=max_new_tokens,
|
23 |
-
temperature=temperature,
|
24 |
-
token=os.getenv("HF_TOKEN")
|
25 |
-
)
|
26 |
-
|
27 |
-
system_message = "Rephrase the following text without adding any comments, feedback, or suggestions. Return only the rephrased text exactly as requested."
|
28 |
-
|
29 |
-
prompt = PromptTemplate.from_template("{system_message}\n\n{user_text}")
|
30 |
-
|
31 |
-
chat = prompt | llm.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
32 |
-
|
33 |
-
response = chat.invoke(input=dict(system_message=system_message, user_text=prompt_text))
|
34 |
-
|
35 |
-
return response
|
36 |
-
|
37 |
-
except Exception as e:
|
38 |
-
return f"Error generating response: {e}"
|
39 |
-
|
40 |
-
def load_xgboost_model():
|
41 |
-
global xgboost_model
|
42 |
-
try:
|
43 |
-
model_path = hf_hub_download(repo_id="Sannidhi/stress_prediction_xgboost_model", filename="xgboost_model.json")
|
44 |
-
|
45 |
-
xgboost_model = xgb.Booster()
|
46 |
-
xgboost_model.load_model(model_path)
|
47 |
-
|
48 |
-
return True
|
49 |
-
except Exception as e:
|
50 |
-
st.error(f"Error loading XGBoost model from Hugging Face: {e}")
|
51 |
-
return False
|
52 |
-
|
53 |
-
def display_predict_stress():
|
54 |
-
st.title("Analyse Current Stress")
|
55 |
-
st.markdown("Answer the questions below to predict your stress level.")
|
56 |
-
|
57 |
-
with st.sidebar:
|
58 |
-
go_home = st.button("Back to Home")
|
59 |
-
if go_home:
|
60 |
-
st.session_state.page = "home"
|
61 |
-
|
62 |
-
load_xgboost_model()
|
63 |
-
|
64 |
-
with st.form(key="stress_form"):
|
65 |
-
stress_questions = {
|
66 |
-
"How many fruits or vegetables do you eat every day?": ["0", "1", "2", "3", "4", "5"],
|
67 |
-
"How many new places do you visit in an year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
68 |
-
"How many people are very close to you?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
69 |
-
"How many people do you help achieve a better life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
70 |
-
"With how many people do you interact with during a typical day?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
71 |
-
"How many remarkable achievements are you proud of?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
72 |
-
"How many times do you donate your time or money to good causes?": ["0", "1", "2", "3", "4", "5"],
|
73 |
-
"How well do you complete your weekly to-do lists?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
74 |
-
"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"],
|
75 |
-
"How many steps (in thousands) do you typically walk everyday?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
76 |
-
"For how many years ahead is your life vision very clear for?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
77 |
-
"About how long do you typically sleep?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
78 |
-
"How many days of vacation do you typically lose every year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
79 |
-
"How often do you shout or sulk at somebody?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
80 |
-
"How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": ["1", "2"],
|
81 |
-
"How many recognitions have you received in your life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
82 |
-
"How many hours do you spend every week doing what you are passionate about?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
83 |
-
"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"],
|
84 |
-
"Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": ["1", "2", "3", "4"],
|
85 |
-
"Gender (1 = 'Female', 0 = 'Male')": ["0", "1"]
|
86 |
-
}
|
87 |
-
|
88 |
-
question_to_feature_map = {
|
89 |
-
"How many fruits or vegetables do you eat every day?": "FRUITS_VEGGIES",
|
90 |
-
"How many new places do you visit in an year?": "PLACES_VISITED",
|
91 |
-
"How many people are very close to you?": "CORE_CIRCLE",
|
92 |
-
"How many people do you help achieve a better life?": "SUPPORTING_OTHERS",
|
93 |
-
"With how many people do you interact with during a typical day?": "SOCIAL_NETWORK",
|
94 |
-
"How many remarkable achievements are you proud of?": "ACHIEVEMENT",
|
95 |
-
"How many times do you donate your time or money to good causes?": "DONATION",
|
96 |
-
"How well do you complete your weekly to-do lists?": "TODO_COMPLETED",
|
97 |
-
"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",
|
98 |
-
"How many steps (in thousands) do you typically walk everyday?": "DAILY_STEPS",
|
99 |
-
"For how many years ahead is your life vision very clear for?": "LIVE_VISION",
|
100 |
-
"About how long do you typically sleep?": "SLEEP_HOURS",
|
101 |
-
"How many days of vacation do you typically lose every year?": "LOST_VACATION",
|
102 |
-
"How often do you shout or sulk at somebody?": "DAILY_SHOUTING",
|
103 |
-
"How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": "SUFFICIENT_INCOME",
|
104 |
-
"How many recognitions have you received in your life?": "PERSONAL_AWARDS",
|
105 |
-
"How many hours do you spend every week doing what you are passionate about?": "TIME_FOR_PASSION",
|
106 |
-
"In a typical week, how many times do you have the opportunity to think about yourself?": "WEEKLY_MEDITATION",
|
107 |
-
"Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": "AGE",
|
108 |
-
"Gender (1 = 'Female', 0 = 'Male')": "GENDER"
|
109 |
-
}
|
110 |
-
|
111 |
-
response_map = {str(i): i for i in range(11)}
|
112 |
-
response_map.update({"1": 1, "2": 2})
|
113 |
-
|
114 |
-
responses = {}
|
115 |
-
for question, options in stress_questions.items():
|
116 |
-
responses[question] = st.selectbox(question, options)
|
117 |
-
|
118 |
-
submit_button = st.form_submit_button("Submit")
|
119 |
-
|
120 |
-
if submit_button:
|
121 |
-
feature_dict = {question_to_feature_map[q]: response_map[responses[q]] for q in stress_questions.keys()}
|
122 |
-
feature_df = pd.DataFrame([feature_dict])
|
123 |
-
|
124 |
-
try:
|
125 |
-
dmatrix = xgb.DMatrix(feature_df)
|
126 |
-
prediction = xgboost_model.predict(dmatrix)
|
127 |
-
st.markdown(f"### Predicted Stress Level: {prediction[0]:.2f}")
|
128 |
-
if prediction[0] <= 1:
|
129 |
-
st.markdown("Your stress level is within a healthy range. Keep up the good work, and aim to maintain it for continued good health!")
|
130 |
-
else:
|
131 |
-
weekly_meditation_input = feature_dict["WEEKLY_MEDITATION"]
|
132 |
-
sleep_hours_input = feature_dict["SLEEP_HOURS"]
|
133 |
-
time_for_passion_input = feature_dict["TIME_FOR_PASSION"]
|
134 |
-
places_visited_input = feature_dict["PLACES_VISITED"]
|
135 |
-
daily_steps_input = feature_dict["DAILY_STEPS"]
|
136 |
-
|
137 |
-
weekly_meditation_upper_bound = min(10, weekly_meditation_input + 3)
|
138 |
-
sleep_hours_upper_bound = min(10, sleep_hours_input + 3)
|
139 |
-
time_for_passion_upper_bound = min(10, time_for_passion_input + 3)
|
140 |
-
places_visited_upper_bound = min(10, places_visited_input + 3)
|
141 |
-
daily_steps_upper_bound = min(10, daily_steps_input + 3)
|
142 |
-
|
143 |
-
weekly_meditation_range = range(weekly_meditation_input, weekly_meditation_upper_bound + 1)
|
144 |
-
sleep_hours_range = range(sleep_hours_input, sleep_hours_upper_bound + 1)
|
145 |
-
time_for_passion_range = range(time_for_passion_input, time_for_passion_upper_bound + 1)
|
146 |
-
places_visited_range = range(places_visited_input, places_visited_upper_bound + 1)
|
147 |
-
daily_steps_range = range(daily_steps_input, daily_steps_upper_bound + 1)
|
148 |
-
|
149 |
-
all_combinations = itertools.product(weekly_meditation_range, sleep_hours_range, time_for_passion_range, places_visited_range, daily_steps_range)
|
150 |
-
|
151 |
-
best_combination = None
|
152 |
-
min_diff = float('inf')
|
153 |
-
|
154 |
-
for combination in all_combinations:
|
155 |
-
adjusted_feature_dict = feature_dict.copy()
|
156 |
-
adjusted_feature_dict["WEEKLY_MEDITATION"] = combination[0]
|
157 |
-
adjusted_feature_dict["SLEEP_HOURS"] = combination[1]
|
158 |
-
adjusted_feature_dict["TIME_FOR_PASSION"] = combination[2]
|
159 |
-
adjusted_feature_dict["PLACES_VISITED"] = combination[3]
|
160 |
-
adjusted_feature_dict["DAILY_STEPS"] = combination[4]
|
161 |
-
|
162 |
-
adjusted_feature_df = pd.DataFrame([adjusted_feature_dict])
|
163 |
-
|
164 |
-
dmatrix = xgb.DMatrix(adjusted_feature_df)
|
165 |
-
adjusted_prediction = xgboost_model.predict(dmatrix)
|
166 |
-
if adjusted_prediction[0] <= 1:
|
167 |
-
diff = sum(abs(adjusted_feature_dict[feature] - feature_dict[feature]) for feature in adjusted_feature_dict)
|
168 |
-
if diff < min_diff:
|
169 |
-
min_diff = diff
|
170 |
-
best_combination = adjusted_feature_dict
|
171 |
-
if best_combination:
|
172 |
-
best_sleep = best_combination["SLEEP_HOURS"]
|
173 |
-
best_meditation = best_combination["WEEKLY_MEDITATION"]
|
174 |
-
best_passion = best_combination["TIME_FOR_PASSION"]
|
175 |
-
best_places = best_combination["PLACES_VISITED"]
|
176 |
-
best_steps = best_combination["DAILY_STEPS"]
|
177 |
-
best_stress_level = xgboost_model.predict(xgb.DMatrix(pd.DataFrame([best_combination])))[0]
|
178 |
-
|
179 |
-
prompt = f"Your stress level appears a bit elevated. To help bring it to a healthier range, try getting {best_sleep} hours of sleep each night, spend around {best_passion} hours each week doing something you’re passionate about, set aside {best_meditation} hours weekly for meditation, aim for {best_steps} thousand steps a day, and plan to explore {best_places} new places this year. These small changes can make a meaningful difference and help you reach a stress level of {best_stress_level}."
|
180 |
-
model_response = get_llm_response(prompt)
|
181 |
-
if model_response:
|
182 |
-
st.markdown(model_response)
|
183 |
-
else:
|
184 |
-
st.markdown("Your stress seems a bit high.")
|
185 |
-
else:
|
186 |
-
prompt = f"Your stress level seems a bit high. To help bring it down, aim for up to {sleep_hours_upper_bound} hours of sleep each night, spend around {time_for_passion_upper_bound} hours each week on activities you enjoy, set aside {weekly_meditation_upper_bound} hours for meditation each week, try to reach {daily_steps_upper_bound} thousand steps daily, and plan to explore {places_visited_upper_bound} new places this year. These small adjustments can have a positive impact on your stress levels and overall well-being."
|
187 |
-
model_response = get_llm_response(prompt)
|
188 |
-
if model_response:
|
189 |
-
st.markdown(model_response)
|
190 |
-
else:
|
191 |
-
st.markdown("Your stress seems a bit high.")
|
192 |
-
except Exception as e:
|
193 |
st.error(f"Error making prediction: {e}")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import xgboost as xgb
|
3 |
+
import pandas as pd
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import itertools
|
6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
7 |
+
import os
|
8 |
+
from transformers import pipeline
|
9 |
+
from langchain_core.prompts import PromptTemplate
|
10 |
+
from langchain_core.output_parsers import StrOutputParser
|
11 |
+
|
12 |
+
xgboostmodel_id = "Sannidhi/stress_prediction_xgboost_model"
|
13 |
+
xgboost_model = None
|
14 |
+
model_id = "unsloth/Llama-3.2-1B-Instruct"
|
15 |
+
generator = pipeline("text-generation", model=model_id)
|
16 |
+
|
17 |
+
def get_llm_response(prompt_text, model_id="meta-llama/Llama-3.2-3B-Instruct", max_new_tokens=256, temperature=0.5):
|
18 |
+
"""Generates a response from the Hugging Face model for a given prompt text."""
|
19 |
+
try:
|
20 |
+
llm = HuggingFaceEndpoint(
|
21 |
+
repo_id=model_id,
|
22 |
+
max_new_tokens=max_new_tokens,
|
23 |
+
temperature=temperature,
|
24 |
+
token=os.getenv("HF_TOKEN")
|
25 |
+
)
|
26 |
+
|
27 |
+
system_message = "Rephrase the following text without adding any comments, feedback, or suggestions. Return only the rephrased text exactly as requested."
|
28 |
+
|
29 |
+
prompt = PromptTemplate.from_template("{system_message}\n\n{user_text}")
|
30 |
+
|
31 |
+
chat = prompt | llm.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
32 |
+
|
33 |
+
response = chat.invoke(input=dict(system_message=system_message, user_text=prompt_text))
|
34 |
+
|
35 |
+
return response
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error generating response: {e}"
|
39 |
+
|
40 |
+
def load_xgboost_model():
|
41 |
+
global xgboost_model
|
42 |
+
try:
|
43 |
+
model_path = hf_hub_download(repo_id="Sannidhi/stress_prediction_xgboost_model", filename="xgboost_model.json")
|
44 |
+
|
45 |
+
xgboost_model = xgb.Booster()
|
46 |
+
xgboost_model.load_model(model_path)
|
47 |
+
|
48 |
+
return True
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"Error loading XGBoost model from Hugging Face: {e}")
|
51 |
+
return False
|
52 |
+
|
53 |
+
def display_predict_stress():
|
54 |
+
st.title("Analyse Current Stress")
|
55 |
+
st.markdown("Answer the questions below to predict your stress level.")
|
56 |
+
|
57 |
+
with st.sidebar:
|
58 |
+
go_home = st.button("Back to Home")
|
59 |
+
if go_home:
|
60 |
+
st.session_state.page = "home"
|
61 |
+
|
62 |
+
load_xgboost_model()
|
63 |
+
|
64 |
+
with st.form(key="stress_form"):
|
65 |
+
stress_questions = {
|
66 |
+
"How many fruits or vegetables do you eat every day?": ["0", "1", "2", "3", "4", "5"],
|
67 |
+
"How many new places do you visit in an year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
68 |
+
"How many people are very close to you?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
69 |
+
"How many people do you help achieve a better life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
70 |
+
"With how many people do you interact with during a typical day?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
71 |
+
"How many remarkable achievements are you proud of?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
72 |
+
"How many times do you donate your time or money to good causes?": ["0", "1", "2", "3", "4", "5"],
|
73 |
+
"How well do you complete your weekly to-do lists?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
74 |
+
"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"],
|
75 |
+
"How many steps (in thousands) do you typically walk everyday?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
76 |
+
"For how many years ahead is your life vision very clear for?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
77 |
+
"About how long do you typically sleep?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
78 |
+
"How many days of vacation do you typically lose every year?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
79 |
+
"How often do you shout or sulk at somebody?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
80 |
+
"How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": ["1", "2"],
|
81 |
+
"How many recognitions have you received in your life?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
82 |
+
"How many hours do you spend every week doing what you are passionate about?": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
|
83 |
+
"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"],
|
84 |
+
"Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": ["1", "2", "3", "4"],
|
85 |
+
"Gender (1 = 'Female', 0 = 'Male')": ["0", "1"]
|
86 |
+
}
|
87 |
+
|
88 |
+
question_to_feature_map = {
|
89 |
+
"How many fruits or vegetables do you eat every day?": "FRUITS_VEGGIES",
|
90 |
+
"How many new places do you visit in an year?": "PLACES_VISITED",
|
91 |
+
"How many people are very close to you?": "CORE_CIRCLE",
|
92 |
+
"How many people do you help achieve a better life?": "SUPPORTING_OTHERS",
|
93 |
+
"With how many people do you interact with during a typical day?": "SOCIAL_NETWORK",
|
94 |
+
"How many remarkable achievements are you proud of?": "ACHIEVEMENT",
|
95 |
+
"How many times do you donate your time or money to good causes?": "DONATION",
|
96 |
+
"How well do you complete your weekly to-do lists?": "TODO_COMPLETED",
|
97 |
+
"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",
|
98 |
+
"How many steps (in thousands) do you typically walk everyday?": "DAILY_STEPS",
|
99 |
+
"For how many years ahead is your life vision very clear for?": "LIVE_VISION",
|
100 |
+
"About how long do you typically sleep?": "SLEEP_HOURS",
|
101 |
+
"How many days of vacation do you typically lose every year?": "LOST_VACATION",
|
102 |
+
"How often do you shout or sulk at somebody?": "DAILY_SHOUTING",
|
103 |
+
"How sufficient is your income to cover basic life expenses (1 for insufficient, 2 for sufficient)?": "SUFFICIENT_INCOME",
|
104 |
+
"How many recognitions have you received in your life?": "PERSONAL_AWARDS",
|
105 |
+
"How many hours do you spend every week doing what you are passionate about?": "TIME_FOR_PASSION",
|
106 |
+
"In a typical week, how many times do you have the opportunity to think about yourself?": "WEEKLY_MEDITATION",
|
107 |
+
"Age (1 = 'Less than 20' 2 = '21 to 35' 3 = '36 to 50' 4 = '51 or more')": "AGE",
|
108 |
+
"Gender (1 = 'Female', 0 = 'Male')": "GENDER"
|
109 |
+
}
|
110 |
+
|
111 |
+
response_map = {str(i): i for i in range(11)}
|
112 |
+
response_map.update({"1": 1, "2": 2})
|
113 |
+
|
114 |
+
responses = {}
|
115 |
+
for question, options in stress_questions.items():
|
116 |
+
responses[question] = st.selectbox(question, options)
|
117 |
+
|
118 |
+
submit_button = st.form_submit_button("Submit")
|
119 |
+
|
120 |
+
if submit_button:
|
121 |
+
feature_dict = {question_to_feature_map[q]: response_map[responses[q]] for q in stress_questions.keys()}
|
122 |
+
feature_df = pd.DataFrame([feature_dict])
|
123 |
+
|
124 |
+
try:
|
125 |
+
dmatrix = xgb.DMatrix(feature_df)
|
126 |
+
prediction = xgboost_model.predict(dmatrix)
|
127 |
+
st.markdown(f"### Predicted Stress Level: {prediction[0]:.2f}")
|
128 |
+
if prediction[0] <= 1:
|
129 |
+
st.markdown("Your stress level is within a healthy range. Keep up the good work, and aim to maintain it for continued good health!")
|
130 |
+
else:
|
131 |
+
weekly_meditation_input = feature_dict["WEEKLY_MEDITATION"]
|
132 |
+
sleep_hours_input = feature_dict["SLEEP_HOURS"]
|
133 |
+
time_for_passion_input = feature_dict["TIME_FOR_PASSION"]
|
134 |
+
places_visited_input = feature_dict["PLACES_VISITED"]
|
135 |
+
daily_steps_input = feature_dict["DAILY_STEPS"]
|
136 |
+
|
137 |
+
weekly_meditation_upper_bound = min(10, weekly_meditation_input + 3)
|
138 |
+
sleep_hours_upper_bound = min(10, sleep_hours_input + 3)
|
139 |
+
time_for_passion_upper_bound = min(10, time_for_passion_input + 3)
|
140 |
+
places_visited_upper_bound = min(10, places_visited_input + 3)
|
141 |
+
daily_steps_upper_bound = min(10, daily_steps_input + 3)
|
142 |
+
|
143 |
+
weekly_meditation_range = range(weekly_meditation_input, weekly_meditation_upper_bound + 1)
|
144 |
+
sleep_hours_range = range(sleep_hours_input, sleep_hours_upper_bound + 1)
|
145 |
+
time_for_passion_range = range(time_for_passion_input, time_for_passion_upper_bound + 1)
|
146 |
+
places_visited_range = range(places_visited_input, places_visited_upper_bound + 1)
|
147 |
+
daily_steps_range = range(daily_steps_input, daily_steps_upper_bound + 1)
|
148 |
+
|
149 |
+
all_combinations = itertools.product(weekly_meditation_range, sleep_hours_range, time_for_passion_range, places_visited_range, daily_steps_range)
|
150 |
+
|
151 |
+
best_combination = None
|
152 |
+
min_diff = float('inf')
|
153 |
+
|
154 |
+
for combination in all_combinations:
|
155 |
+
adjusted_feature_dict = feature_dict.copy()
|
156 |
+
adjusted_feature_dict["WEEKLY_MEDITATION"] = combination[0]
|
157 |
+
adjusted_feature_dict["SLEEP_HOURS"] = combination[1]
|
158 |
+
adjusted_feature_dict["TIME_FOR_PASSION"] = combination[2]
|
159 |
+
adjusted_feature_dict["PLACES_VISITED"] = combination[3]
|
160 |
+
adjusted_feature_dict["DAILY_STEPS"] = combination[4]
|
161 |
+
|
162 |
+
adjusted_feature_df = pd.DataFrame([adjusted_feature_dict])
|
163 |
+
|
164 |
+
dmatrix = xgb.DMatrix(adjusted_feature_df)
|
165 |
+
adjusted_prediction = xgboost_model.predict(dmatrix)
|
166 |
+
if adjusted_prediction[0] <= 1:
|
167 |
+
diff = sum(abs(adjusted_feature_dict[feature] - feature_dict[feature]) for feature in adjusted_feature_dict)
|
168 |
+
if diff < min_diff:
|
169 |
+
min_diff = diff
|
170 |
+
best_combination = adjusted_feature_dict
|
171 |
+
if best_combination:
|
172 |
+
best_sleep = best_combination["SLEEP_HOURS"]
|
173 |
+
best_meditation = best_combination["WEEKLY_MEDITATION"]
|
174 |
+
best_passion = best_combination["TIME_FOR_PASSION"]
|
175 |
+
best_places = best_combination["PLACES_VISITED"]
|
176 |
+
best_steps = best_combination["DAILY_STEPS"]
|
177 |
+
best_stress_level = xgboost_model.predict(xgb.DMatrix(pd.DataFrame([best_combination])))[0]
|
178 |
+
|
179 |
+
prompt = f"Your stress level appears a bit elevated. To help bring it to a healthier range, try getting {best_sleep} hours of sleep each night, spend around {best_passion} hours each week doing something you’re passionate about, set aside {best_meditation} hours weekly for meditation, aim for {best_steps} thousand steps a day, and plan to explore {best_places} new places this year. These small changes can make a meaningful difference and help you reach a stress level of {best_stress_level}."
|
180 |
+
model_response = get_llm_response(prompt)
|
181 |
+
if model_response:
|
182 |
+
st.markdown(model_response)
|
183 |
+
else:
|
184 |
+
st.markdown("Your stress seems a bit high.")
|
185 |
+
else:
|
186 |
+
prompt = f"Your stress level seems a bit high. To help bring it down, aim for up to {sleep_hours_upper_bound} hours of sleep each night, spend around {time_for_passion_upper_bound} hours each week on activities you enjoy, set aside {weekly_meditation_upper_bound} hours for meditation each week, try to reach {daily_steps_upper_bound} thousand steps daily, and plan to explore {places_visited_upper_bound} new places this year. These small adjustments can have a positive impact on your stress levels and overall well-being."
|
187 |
+
model_response = get_llm_response(prompt)
|
188 |
+
if model_response:
|
189 |
+
st.markdown(model_response)
|
190 |
+
else:
|
191 |
+
st.markdown("Your stress seems a bit high.")
|
192 |
+
except Exception as e:
|
193 |
st.error(f"Error making prediction: {e}")
|