saherPervaiz commited on
Commit
0017779
Β·
verified Β·
1 Parent(s): ede3059

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -9,15 +9,14 @@ client = Groq(api_key=groq_api_key)
9
 
10
  # Function to fetch personalized advice using Groq API
11
  def get_personalized_advice(user_data):
12
- """Fetch personalized advice using the Groq API based on user data."""
13
  query = f"""
14
- Based on the following user's responses:
15
  - Anxiety level: {user_data['anxiety_level']}
16
  - Self-esteem: {user_data['self_esteem']}
17
  - Mental health history: {user_data['mental_health_history']}
18
  - Stress level: {user_data['stress_level']}
19
-
20
- Please provide professional, personalized mental health advice tailored to these responses.
21
  """
22
 
23
  try:
@@ -27,11 +26,11 @@ def get_personalized_advice(user_data):
27
  )
28
  return response.choices[0].message.content
29
  except Exception as e:
30
- st.error(f"Error fetching advice from Groq: {str(e)}")
31
  return None
32
 
33
  def stress_level_to_string(stress_level):
34
- """Convert numerical stress level (0, 1, 2) to a string representation."""
35
  if stress_level == 0:
36
  return "Low"
37
  elif stress_level == 1:
@@ -39,10 +38,10 @@ def stress_level_to_string(stress_level):
39
  else:
40
  return "High"
41
 
42
- # Chatbot interface for professional user experience
43
  def chatbot_interface():
44
- st.title("πŸŽ“ Professional Student Well-being Chatbot")
45
- st.write("Welcome to the Student Well-being Chatbot. I’m here to assist you in improving your mental well-being based on your responses. Please answer the following questions.")
46
 
47
  # Initialize session state
48
  if 'step' not in st.session_state:
@@ -63,29 +62,30 @@ def chatbot_interface():
63
 
64
  # Step 0: Welcome message and initial question
65
  if st.session_state.step == 0:
66
- st.write("Bot: Thank you for choosing to take this well-being assessment. We will gather some insights to provide personalized advice.")
67
- st.write("Bot: Please answer the following questions as accurately as possible.")
68
- next_step_button = st.button("Start Assessment")
69
  clear_button = st.button("Clear All")
70
- if next_step_button:
71
  next_step()
72
  if clear_button:
73
  clear_data()
74
 
75
  # Step 1: Ask for anxiety level
76
  elif st.session_state.step == 1:
77
- st.write("Bot: On a scale from 1 to 10, how would you rate your current anxiety level?")
78
  anxiety_level = st.slider("Anxiety Level", 1, 10, 5)
79
- if anxiety_level:
80
- st.session_state.user_data['anxiety_level'] = anxiety_level
 
81
  next_step()
82
 
83
  # Step 2: Ask for self-esteem
84
  elif st.session_state.step == 2:
85
  st.write("Bot: On a scale from 1 to 10, how would you rate your current self-esteem?")
86
  self_esteem = st.slider("Self-esteem Level", 1, 10, 5)
87
- if self_esteem:
88
- st.session_state.user_data['self_esteem'] = self_esteem
 
89
  next_step()
90
 
91
  # Step 3: Ask for mental health history
@@ -96,30 +96,36 @@ def chatbot_interface():
96
  ["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
97
  )
98
  st.session_state.user_data['mental_health_history'] = mental_health_history
99
- next_step()
 
 
100
 
101
  # Step 4: Ask for stress level
102
  elif st.session_state.step == 4:
103
  st.write("Bot: On a scale from 0 to 2, how would you rate your current stress level?")
104
- stress_level = st.radio("Stress Level (0 = Low, 1 = Moderate, 2 = High)", [0, 1, 2])
105
  st.session_state.user_data['stress_level'] = stress_level
106
- next_step()
 
 
107
 
108
- # Step 5: Show advice based on user input
109
  elif st.session_state.step == 5:
110
- st.write("Bot: Thank you for providing the information.")
111
  user_data = st.session_state.user_data
112
- st.write(f"Bot: Based on your inputs, here is a summary of your responses: {user_data}")
 
 
 
113
 
114
- # Show professional, personalized advice
115
- st.subheader("πŸ”” Professional Well-being Advice:")
116
  advice = get_personalized_advice(user_data)
117
  if advice:
118
  st.write(f"Bot: {advice}")
119
  else:
120
- st.write("Bot: Unfortunately, I couldn't retrieve specific advice at this moment, but feel free to try again later.")
121
-
122
- # Option to restart the chat
123
  restart_button = st.button("Start Over")
124
  clear_button = st.button("Clear All")
125
  if restart_button:
 
9
 
10
  # Function to fetch personalized advice using Groq API
11
  def get_personalized_advice(user_data):
12
+ """Fetch personalized advice based on user data."""
13
  query = f"""
14
+ Based on the following student's responses:
15
  - Anxiety level: {user_data['anxiety_level']}
16
  - Self-esteem: {user_data['self_esteem']}
17
  - Mental health history: {user_data['mental_health_history']}
18
  - Stress level: {user_data['stress_level']}
19
+ Provide professional, personalized mental health advice.
 
20
  """
21
 
22
  try:
 
26
  )
27
  return response.choices[0].message.content
28
  except Exception as e:
29
+ st.error(f"Error fetching advice: {str(e)}")
30
  return None
31
 
32
  def stress_level_to_string(stress_level):
33
+ """Convert stress level to string representation."""
34
  if stress_level == 0:
35
  return "Low"
36
  elif stress_level == 1:
 
38
  else:
39
  return "High"
40
 
41
+ # Chatbot interface
42
  def chatbot_interface():
43
+ st.title("πŸŽ“ Student Health and Stress Chatbot")
44
+ st.write("Hello! I’m here to assist you in assessing your well-being and provide helpful advice. Let's get started! 😊")
45
 
46
  # Initialize session state
47
  if 'step' not in st.session_state:
 
62
 
63
  # Step 0: Welcome message and initial question
64
  if st.session_state.step == 0:
65
+ st.write("Bot: Welcome to the Student Health and Stress Assessment. Let's start with some simple questions.")
66
+ start_button = st.button("Start Assessment")
 
67
  clear_button = st.button("Clear All")
68
+ if start_button:
69
  next_step()
70
  if clear_button:
71
  clear_data()
72
 
73
  # Step 1: Ask for anxiety level
74
  elif st.session_state.step == 1:
75
+ st.write("Bot: On a scale of 1 to 10, how would you rate your current anxiety level?")
76
  anxiety_level = st.slider("Anxiety Level", 1, 10, 5)
77
+ st.session_state.user_data['anxiety_level'] = anxiety_level
78
+ next_step_button = st.button("Next")
79
+ if next_step_button:
80
  next_step()
81
 
82
  # Step 2: Ask for self-esteem
83
  elif st.session_state.step == 2:
84
  st.write("Bot: On a scale from 1 to 10, how would you rate your current self-esteem?")
85
  self_esteem = st.slider("Self-esteem Level", 1, 10, 5)
86
+ st.session_state.user_data['self_esteem'] = self_esteem
87
+ next_step_button = st.button("Next")
88
+ if next_step_button:
89
  next_step()
90
 
91
  # Step 3: Ask for mental health history
 
96
  ["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
97
  )
98
  st.session_state.user_data['mental_health_history'] = mental_health_history
99
+ next_step_button = st.button("Next")
100
+ if next_step_button:
101
+ next_step()
102
 
103
  # Step 4: Ask for stress level
104
  elif st.session_state.step == 4:
105
  st.write("Bot: On a scale from 0 to 2, how would you rate your current stress level?")
106
+ stress_level = st.radio("Stress Level", [0, 1, 2])
107
  st.session_state.user_data['stress_level'] = stress_level
108
+ next_step_button = st.button("Next")
109
+ if next_step_button:
110
+ next_step()
111
 
112
+ # Step 5: Provide personalized advice based on responses
113
  elif st.session_state.step == 5:
114
+ st.write("Bot: Thank you for providing the information! Here's a summary of your responses:")
115
  user_data = st.session_state.user_data
116
+ st.write(f"Bot: Your Anxiety Level: {user_data['anxiety_level']}")
117
+ st.write(f"Bot: Your Self-Esteem Level: {user_data['self_esteem']}")
118
+ st.write(f"Bot: Mental Health History: {user_data['mental_health_history']}")
119
+ st.write(f"Bot: Your Stress Level: {stress_level_to_string(user_data['stress_level'])}")
120
 
121
+ st.subheader("πŸ”” Personalized Mental Health Advice:")
 
122
  advice = get_personalized_advice(user_data)
123
  if advice:
124
  st.write(f"Bot: {advice}")
125
  else:
126
+ st.write("Bot: I'm unable to retrieve personalized advice at the moment, but feel free to try again later.")
127
+
128
+ # Option to restart or clear data
129
  restart_button = st.button("Start Over")
130
  clear_button = st.button("Clear All")
131
  if restart_button: