saherPervaiz commited on
Commit
7877a46
Β·
verified Β·
1 Parent(s): b30668d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -8,9 +8,9 @@ groq_api_key = "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"
8
  # Initialize Groq client
9
  client = Groq(api_key=groq_api_key)
10
 
11
- # Define a function to fetch personalized advice using Groq API
12
  def get_personalized_advice(user_data):
13
- """Fetch personalized advice using the Groq API based on user data"""
14
  query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self esteem: {user_data['self_esteem']}, mental health history: {user_data['mental_health_history']}, stress level: {user_data['stress_level']}, provide personalized mental health advice."
15
 
16
  try:
@@ -32,11 +32,12 @@ def stress_level_to_string(stress_level):
32
  else:
33
  return "High"
34
 
 
35
  def chatbot_interface():
36
  st.title("πŸŽ“ Student Well-being Chatbot")
37
- st.write("I can help you with personalized well-being advice based on your responses. Let's get started!")
38
-
39
- # Initializing session state
40
  if 'step' not in st.session_state:
41
  st.session_state.step = 0
42
  st.session_state.user_data = {}
@@ -44,9 +45,13 @@ def chatbot_interface():
44
  def next_step():
45
  st.session_state.step += 1
46
 
 
 
 
 
47
  # Step 0: Welcome message and initial question
48
  if st.session_state.step == 0:
49
- st.write("Hi! I am your well-being assistant. I'll ask you some questions to understand how you're feeling.")
50
  next_step_button = st.button("Start Chat")
51
  if next_step_button:
52
  next_step()
@@ -55,19 +60,32 @@ def chatbot_interface():
55
  elif st.session_state.step == 1:
56
  anxiety_level = st.text_input("On a scale of 1 to 10, how would you rate your anxiety level?", "")
57
  if anxiety_level:
58
- st.session_state.user_data['anxiety_level'] = int(anxiety_level)
59
- next_step()
 
 
 
 
 
 
60
 
61
  # Step 2: Ask for self-esteem
62
  elif st.session_state.step == 2:
63
  self_esteem = st.text_input("On a scale of 1 to 10, how would you rate your self-esteem?", "")
64
  if self_esteem:
65
- st.session_state.user_data['self_esteem'] = int(self_esteem)
66
- next_step()
 
 
 
 
 
 
67
 
68
  # Step 3: Ask for mental health history
69
  elif st.session_state.step == 3:
70
- mental_health_history = st.selectbox("How would you describe your mental health history?", ["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
 
71
  st.session_state.user_data['mental_health_history'] = mental_health_history
72
  next_step()
73
 
@@ -83,15 +101,16 @@ def chatbot_interface():
83
  user_data = st.session_state.user_data
84
  st.write(f"Your data: {user_data}")
85
 
86
- st.subheader("πŸ”” Personalized Health Advice")
 
87
  advice = get_personalized_advice(user_data)
88
  if advice:
89
  st.write(advice)
90
  else:
91
- st.write("No advice available at the moment.")
92
 
93
  # Option to restart the chat
94
- restart_button = st.button("Restart Chat")
95
  if restart_button:
96
  st.session_state.step = 0
97
  st.session_state.user_data = {}
 
8
  # Initialize Groq client
9
  client = Groq(api_key=groq_api_key)
10
 
11
+ # Function to fetch personalized advice using Groq API
12
  def get_personalized_advice(user_data):
13
+ """Fetch personalized advice using the Groq API based on user data."""
14
  query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self esteem: {user_data['self_esteem']}, mental health history: {user_data['mental_health_history']}, stress level: {user_data['stress_level']}, provide personalized mental health advice."
15
 
16
  try:
 
32
  else:
33
  return "High"
34
 
35
+ # Chatbot interface for conversation-like experience
36
  def chatbot_interface():
37
  st.title("πŸŽ“ Student Well-being Chatbot")
38
+ st.write("Hello! I’m here to assist you with well-being advice based on your responses.")
39
+
40
+ # Initialize session state
41
  if 'step' not in st.session_state:
42
  st.session_state.step = 0
43
  st.session_state.user_data = {}
 
45
  def next_step():
46
  st.session_state.step += 1
47
 
48
+ def show_input_error(message="Please provide a valid input."):
49
+ st.error(message)
50
+ return False
51
+
52
  # Step 0: Welcome message and initial question
53
  if st.session_state.step == 0:
54
+ st.write("I'm going to ask you a few questions to understand your well-being. Let's get started!")
55
  next_step_button = st.button("Start Chat")
56
  if next_step_button:
57
  next_step()
 
60
  elif st.session_state.step == 1:
61
  anxiety_level = st.text_input("On a scale of 1 to 10, how would you rate your anxiety level?", "")
62
  if anxiety_level:
63
+ try:
64
+ anxiety_level = int(anxiety_level)
65
+ if anxiety_level < 1 or anxiety_level > 10:
66
+ raise ValueError
67
+ st.session_state.user_data['anxiety_level'] = anxiety_level
68
+ next_step()
69
+ except ValueError:
70
+ show_input_error("Please enter a number between 1 and 10 for anxiety level.")
71
 
72
  # Step 2: Ask for self-esteem
73
  elif st.session_state.step == 2:
74
  self_esteem = st.text_input("On a scale of 1 to 10, how would you rate your self-esteem?", "")
75
  if self_esteem:
76
+ try:
77
+ self_esteem = int(self_esteem)
78
+ if self_esteem < 1 or self_esteem > 10:
79
+ raise ValueError
80
+ st.session_state.user_data['self_esteem'] = self_esteem
81
+ next_step()
82
+ except ValueError:
83
+ show_input_error("Please enter a number between 1 and 10 for self-esteem.")
84
 
85
  # Step 3: Ask for mental health history
86
  elif st.session_state.step == 3:
87
+ mental_health_history = st.selectbox("How would you describe your mental health history?",
88
+ ["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
89
  st.session_state.user_data['mental_health_history'] = mental_health_history
90
  next_step()
91
 
 
101
  user_data = st.session_state.user_data
102
  st.write(f"Your data: {user_data}")
103
 
104
+ # Show a conversational summary of the user's well-being status
105
+ st.subheader("πŸ”” Here's Your Personalized Well-being Advice:")
106
  advice = get_personalized_advice(user_data)
107
  if advice:
108
  st.write(advice)
109
  else:
110
+ st.write("I couldn't retrieve specific advice right now, but please check back later.")
111
 
112
  # Option to restart the chat
113
+ restart_button = st.button("Start Over")
114
  if restart_button:
115
  st.session_state.step = 0
116
  st.session_state.user_data = {}