saherPervaiz commited on
Commit
c718b76
Β·
verified Β·
1 Parent(s): b0960d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -76
app.py CHANGED
@@ -22,99 +22,95 @@ def get_personalized_advice(user_data):
22
  st.error(f"Error fetching advice from Groq: {str(e)}")
23
  return None
24
 
25
- def stress_level_to_string(stress_level):
26
- """Convert numerical stress level (0, 1, 2) to a string representation."""
27
- if stress_level == 0:
28
- return "Low"
29
- elif stress_level == 1:
30
- return "Moderate"
31
- else:
32
- return "High"
33
-
34
- # Chatbot interface for conversation-like experience
35
  def chatbot_interface():
36
  st.title("πŸŽ“ Student Well-being Chatbot")
37
  st.write("Hello! I’m here to assist you with well-being advice based on your responses.")
38
-
39
- # Initialize session state
40
- if 'messages' not in st.session_state:
41
- st.session_state.messages = [
42
- {"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! 😊"}
43
- ]
44
 
45
- def next_step(user_input=None):
46
- # Save the user's message to the chat history
47
- if user_input:
48
- st.session_state.messages.append({"role": "user", "content": user_input})
49
-
50
- # Proceed to next step in the conversation
51
- if len(st.session_state.messages) == 1:
52
- st.session_state.messages.append({"role": "bot", "content": "On a scale of 1 to 10, how would you rate your anxiety level?"})
53
- elif len(st.session_state.messages) == 2:
54
- # Validate anxiety level input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  try:
56
- anxiety_level = int(user_input)
57
  if anxiety_level < 1 or anxiety_level > 10:
58
  raise ValueError
59
- st.session_state.user_data = {'anxiety_level': anxiety_level}
60
- st.session_state.messages.append({"role": "bot", "content": f"Got it! Your anxiety level is {anxiety_level}. Now, on a scale of 1 to 10, how would you rate your self-esteem?"})
61
  except ValueError:
62
- st.error("Please enter a valid number between 1 and 10 for anxiety level.")
63
- elif len(st.session_state.messages) == 3:
64
- # Validate self-esteem input
 
 
 
 
65
  try:
66
- self_esteem = int(user_input)
67
  if self_esteem < 1 or self_esteem > 10:
68
  raise ValueError
69
  st.session_state.user_data['self_esteem'] = self_esteem
70
- st.session_state.messages.append({"role": "bot", "content": f"Got it! Your self-esteem level is {self_esteem}. Please select your mental health history from the options below."})
71
  except ValueError:
72
- st.error("Please enter a valid number between 1 and 10 for self-esteem.")
73
- elif len(st.session_state.messages) == 4:
74
- st.session_state.user_data['mental_health_history'] = user_input
75
- st.session_state.messages.append({"role": "bot", "content": "Thanks! Now, on a scale of 0 to 2, how would you rate your stress level?"})
76
- elif len(st.session_state.messages) == 5:
77
- try:
78
- stress_level = int(user_input)
79
- if stress_level < 0 or stress_level > 2:
80
- raise ValueError
81
- st.session_state.user_data['stress_level'] = stress_level
82
- st.session_state.messages.append({"role": "bot", "content": "Thank you for providing the information! Let me get your personalized advice..."})
83
- # Get the advice from Groq API
84
- advice = get_personalized_advice(st.session_state.user_data)
85
- st.session_state.messages.append({"role": "bot", "content": advice or "I couldn't retrieve specific advice right now, but please check back later."})
86
- except ValueError:
87
- st.error("Please enter a valid number between 0 and 2 for stress level.")
88
- elif len(st.session_state.messages) == 6:
89
- st.session_state.messages.append({"role": "bot", "content": "Would you like to start over?"})
90
-
91
- # Display conversation history
92
- for message in st.session_state.messages:
93
- if message['role'] == 'bot':
94
- st.markdown(f"**Bot**: {message['content']}")
95
- else:
96
- st.markdown(f"**You**: {message['content']}")
97
-
98
- # User input field for chat interaction
99
- user_input = ""
100
- if len(st.session_state.messages) == 3:
101
- # Mental health history (checkbox options)
102
- mental_health_options = ["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
103
- mental_health_history = st.multiselect("Select your mental health history:", mental_health_options)
104
  if mental_health_history:
105
- user_input = ", ".join(mental_health_history)
106
- elif len(st.session_state.messages) == 4:
107
- # Stress level (radio button)
108
- user_input = st.radio("On a scale of 0 to 2, how would you rate your stress level?", [0, 1, 2])
109
-
110
- if user_input:
111
- next_step(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- # Option to restart the conversation
114
- if len(st.session_state.messages) >= 6:
115
  restart_button = st.button("Start Over")
116
  if restart_button:
117
- st.session_state.messages = [{"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! 😊"}]
 
118
 
119
  # Main function to run the chatbot
120
  def main():
 
22
  st.error(f"Error fetching advice from Groq: {str(e)}")
23
  return None
24
 
25
+ # Function for chatbot interaction
 
 
 
 
 
 
 
 
 
26
  def chatbot_interface():
27
  st.title("πŸŽ“ Student Well-being Chatbot")
28
  st.write("Hello! I’m here to assist you with well-being advice based on your responses.")
 
 
 
 
 
 
29
 
30
+ # Initialize session state
31
+ if 'step' not in st.session_state:
32
+ st.session_state.step = 0
33
+ st.session_state.user_data = {}
34
+
35
+ def next_step():
36
+ st.session_state.step += 1
37
+
38
+ def show_input_error(message="Please provide a valid input."):
39
+ st.error(message)
40
+ return False
41
+
42
+ # Step 0: Welcome message and initial question
43
+ if st.session_state.step == 0:
44
+ st.write("Bot: Hi there! I'm here to help with your well-being. Let's start! 😊")
45
+ next_step_button = st.button("Start Chat")
46
+ if next_step_button:
47
+ next_step()
48
+
49
+ # Step 1: Ask for anxiety level
50
+ elif st.session_state.step == 1:
51
+ st.write("Bot: On a scale of 1 to 10, how would you rate your anxiety level?")
52
+ anxiety_level = st.text_input("Your Anxiety Level:", "")
53
+ if anxiety_level:
54
  try:
55
+ anxiety_level = int(anxiety_level)
56
  if anxiety_level < 1 or anxiety_level > 10:
57
  raise ValueError
58
+ st.session_state.user_data['anxiety_level'] = anxiety_level
59
+ next_step()
60
  except ValueError:
61
+ show_input_error("Please enter a number between 1 and 10 for anxiety level.")
62
+
63
+ # Step 2: Ask for self-esteem
64
+ elif st.session_state.step == 2:
65
+ st.write("Bot: On a scale of 1 to 10, how would you rate your self-esteem?")
66
+ self_esteem = st.text_input("Your Self-Esteem Level:", "")
67
+ if self_esteem:
68
  try:
69
+ self_esteem = int(self_esteem)
70
  if self_esteem < 1 or self_esteem > 10:
71
  raise ValueError
72
  st.session_state.user_data['self_esteem'] = self_esteem
73
+ next_step()
74
  except ValueError:
75
+ show_input_error("Please enter a number between 1 and 10 for self-esteem.")
76
+
77
+ # Step 3: Ask for mental health history
78
+ elif st.session_state.step == 3:
79
+ st.write("Bot: How would you describe your mental health history?")
80
+ mental_health_history = st.multiselect(
81
+ "Select your mental health history:",
82
+ ["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
83
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  if mental_health_history:
85
+ st.session_state.user_data['mental_health_history'] = ", ".join(mental_health_history)
86
+ next_step()
87
+
88
+ # Step 4: Ask for stress level
89
+ elif st.session_state.step == 4:
90
+ st.write("Bot: On a scale of 0 to 2, how would you rate your stress level?")
91
+ stress_level = st.radio("Your Stress Level:", [0, 1, 2])
92
+ st.session_state.user_data['stress_level'] = stress_level
93
+ next_step()
94
+
95
+ # Step 5: Show advice based on user input
96
+ elif st.session_state.step == 5:
97
+ st.write("Bot: Thank you for providing the information! Here’s your personalized advice:")
98
+ user_data = st.session_state.user_data
99
+ st.write(f"Your data: {user_data}")
100
+
101
+ # Fetch personalized advice from Groq
102
+ st.subheader("πŸ”” Here's Your Personalized Well-being Advice:")
103
+ advice = get_personalized_advice(user_data)
104
+ if advice:
105
+ st.write(advice)
106
+ else:
107
+ st.write("I couldn't retrieve specific advice right now, but please check back later.")
108
 
109
+ # Option to restart the chat
 
110
  restart_button = st.button("Start Over")
111
  if restart_button:
112
+ st.session_state.step = 0
113
+ st.session_state.user_data = {}
114
 
115
  # Main function to run the chatbot
116
  def main():