-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
371 lines (319 loc) · 12.5 KB
/
app.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import pandas as pd
import streamlit as st
import pickle
from PIL import Image
import google.generativeai as genai
# Check if running in Streamlit Cloud
is_streamlit_cloud = 'STREAMLIT_SERVER' in os.environ
if is_streamlit_cloud:
# Fetch API key from Streamlit secrets
api_key = st.secrets["api_key"]
else:
# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()
# Fetch API key from environment variables
api_key = os.getenv('API_KEY')
# Configure the generative AI model
genai.configure(api_key=api_key)
generation_config = {
"temperature": 0.9,
"top_p": 1,
"max_output_tokens": 2048,
"response_mime_type": "text/plain",
}
generation_model = genai.GenerativeModel(
model_name="gemini-1.0-pro",
generation_config=generation_config
)
# Load the pre-trained model
with open('model/stress_detection.pkl', 'rb') as file:
reg = pickle.load(file)
# Function to generate personalized suggestions
def generate_personalized_suggestions(features):
# Construct prompt for the generative AI model
stress_level = features['stress_level']
prompt = f"Given the stress level category '{stress_level}', provide short personalized recommendations and suggestions for stress management based on the following features: {features}."
chat_session = generation_model.start_chat(history=[])
response = chat_session.send_message(prompt)
return response.text.strip()
# Prediction tab
def prediction_tab():
st.title('Stress Level Predictor')
st.markdown(
"""
<style>
.stApp {
background-color: burlywood;
color: black;
text-align: justify;
}
p {
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
\nWelcome to the Stress Level Predictor! Input physiological and environmental features to predict stress levels. Explore different scenarios, experiment with varying inputs, and gain insights into potential stress levels. Make informed decisions and take proactive measures to manage stress effectively in this interactive and exploratory Stress Level Predictor.
""",
unsafe_allow_html=True,
)
# Take user input for features
st.sidebar.title("Enter Feature Values")
selected_sr = st.sidebar.number_input("Snoring Rate:")
selected_rr = st.sidebar.number_input("Respiration Rate:")
selected_t = st.sidebar.number_input("Body Temperature:")
selected_lm = st.sidebar.number_input("Limb Movement:")
selected_bo = st.sidebar.number_input("Blood Oxygen Level:")
selected_rem = st.sidebar.number_input("Rapid Eye Movement:")
selected_sh = st.sidebar.number_input("Sleep Hours:")
selected_hr = st.sidebar.number_input("Heart Rate:")
# Create user input DataFrame
user_input = pd.DataFrame({
'sr': [selected_sr],
'rr': [selected_rr],
't': [selected_t],
'lm': [selected_lm],
'bo': [selected_bo],
'rem': [selected_rem],
'sh': [selected_sh],
'hr': [selected_hr]
})
# Define function for prediction
def predict_stress_level(user_input):
user_pred = reg.predict(user_input)
return user_pred
# Create button for prediction
if st.button("Predict"):
# Get prediction
prediction = predict_stress_level(user_input)
# Define thresholds for stress levels
threshold_low = 1.0
threshold_medium_low = 2.0
threshold_medium_high = 3.0
threshold_high = 4.0
# Determine stress level category
if prediction < threshold_low:
predicted_stress_level = 'Low/Normal'
elif threshold_low <= prediction < threshold_medium_low:
predicted_stress_level = 'Medium Low'
elif threshold_medium_low <= prediction < threshold_medium_high:
predicted_stress_level = 'Medium'
elif threshold_medium_high <= prediction < threshold_high:
predicted_stress_level = 'Medium High'
else:
predicted_stress_level = 'High'
# Generate personalized suggestions
features = {
'snoring_rate': selected_sr,
'respiration_rate': selected_rr,
'body_temperature': selected_t,
'limb_movement': selected_lm,
'blood_oxygen_level': selected_bo,
'rapid_eye_movement': selected_rem,
'sleep_hours': selected_sh,
'heart_rate': selected_hr,
'stress_level': predicted_stress_level
}
suggestions = generate_personalized_suggestions(features)
# Display prediction result and suggestions
st.subheader('Predicted Stress Level:')
st.write(predicted_stress_level)
st.subheader('Personalized Suggestions:')
st.write(suggestions)
st.markdown(
"""
\nIf you'd like to talk to someone or need more personalized support, check out our Somebody tab. There, you'll find your AI assistant ready to chat and offer guidance. 😊
""",
unsafe_allow_html=True,
)
# AI Interaction function
def ai_interaction(prompt):
try:
# Check if there is an existing chat session
if 'chat_session' not in st.session_state:
# Start a new chat session
chat_session = generation_model.start_chat()
st.session_state.chat_session = chat_session
else:
chat_session = st.session_state.chat_session
# Send user prompt and get response
response = chat_session.send_message(prompt)
# Extract the text from the response
response_text = response.text.strip()
# Append response to the history
st.session_state.chat_history.append(f"Somebody: {response_text}")
return response_text
except Exception as e:
st.session_state.chat_history.append(f"Somebody: An error occurred: {e}")
return f"An error occurred: {e}"
# Somebody tab
def somebody_tab():
st.title('Somebody: Your AI Assistant')
st.markdown(
"""
<style>
body { }
.stApp {
background-color: burlywood;
color: black;
text-align: justify;
}
p {
font-size: 18px;
}
.chat-message {
font-size: 18px;
margin: 5px 0;
}
.chat-label {
font-weight: bold;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
\nMeet Somebody, your AI assistant! There's somebody for everyone, and Somebody will be your friend and guide in managing stress and answering your questions. Engage in a friendly chat with Somebody, and get personalized advice and support to help you navigate through stressful situations.
""",
unsafe_allow_html=True,
)
# Initialize chat history
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
# Chat input and interaction
user_input = st.text_input("Type your message here:")
if st.button("Send"):
if user_input:
st.session_state.chat_history.append(f"You: {user_input}")
ai_response = ai_interaction(user_input)
# Display chat history
st.subheader("Chat History")
for message in st.session_state.chat_history:
st.markdown(f"<p class='chat-message'>{message}</p>", unsafe_allow_html=True)
# Feedback tab
def feedback_tab():
st.title('Share Your Thoughts')
st.markdown(
"""
<style>
.stApp {
background-color: #DFEAF0;
color: black;
text-align: justify;
}
p {
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
\nWe'd love to hear your thoughts and suggestions on Stress Detection Explorer! Your feedback helps us improve and enhance the platform to better serve your needs. Whether it's a feature request, bug report, or general comment, we value your input and appreciate your contribution to making Stress Detection Explorer even better.
\nPlease share your feedback in the form below. Thank you for helping us create a more user-friendly and effective Stress Detection Explorer!
""",
unsafe_allow_html=True,
)
# Feedback form
name = st.text_input("Name:", max_chars=50)
email = st.text_input("Email:", max_chars=50)
feedback = st.text_area("Please provide your feedback here:", height=200)
if st.button("Submit Feedback"):
# Process feedback (can be stored in a database or file)
st.success(f"Thank you, {name}! We appreciate your feedback. We'll review your input and work towards improving Stress Detection Explorer.")
# About tab
def about_tab():
st.title('About the Stress Detection Model')
st.markdown(
"""
<style>
.stApp {
background-color: #E6C8BC;
color: black;
text-align: justify;
}
p {
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
\nOur stress detection model utilizes a RandomForestClassifier, trained on physiological and environmental features to predict stress levels. Key features include:
- Snoring Rate
- Respiration Rate
- Body Temperature
- Limb Movement
- Blood Oxygen Level
- Rapid Eye Movement
- Sleep Hours
- Heart Rate
\nThe RandomForestClassifier is an ensemble learning method that builds multiple decision trees and aggregates their results for accurate predictions. This model helps identify stress levels categorized as Low/Normal, Medium Low, Medium, Medium High, and High, providing valuable insights for effective stress management.
\nExplore our tool to understand and manage your stress levels better. Your well-being is our priority!
""",
unsafe_allow_html=True,
)
# Home tab
def home_tab():
st.title('Welcome to Happify :)')
st.markdown(
"""
<style>
.stApp {
background-color: #ADDCF4;
color: black;
text-align: justify;
}
p {
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
"""
\nHappify is an interactive platform designed to predict stress levels based on physiological and environmental features. It provides users with insights into potential stress levels and enables them to explore different scenarios by inputting specific feature values.
""",
unsafe_allow_html=True,
)
# Display image
img = Image.open('Images/stress.jpg')
st.image(img, width=100, use_column_width=True)
st.markdown(
"""
\nNavigate through the tabs to explore features:
\n- **Home:** Get introduced to the system and view the welcome image.
\n- **About:** Learn more about how the stress detection model works.
\n- **Prediction:** Input your data to get stress level predictions and personalized suggestions.
\n- **Somebody:** Chat with the AI assistant for personalized support and guidance.
\n- **Feedback:** Share your thoughts and provide feedback to help us improve the system.
\nStart exploring and take control of your stress management today!
""",
unsafe_allow_html=True,
)
# Main application
def main():
st.sidebar.title("Navigation")
selection = st.sidebar.radio("Go to", ["Home", "About", "Prediction", "Somebody", "Feedback"])
if selection == "Home":
home_tab()
elif selection == "About":
about_tab()
elif selection == "Prediction":
prediction_tab()
elif selection == "Somebody":
somebody_tab()
elif selection == "Feedback":
feedback_tab()
if __name__ == "__main__":
main()