mavinsao commited on
Commit
6dfc28d
·
verified ·
1 Parent(s): 4f0a2ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -46
app.py CHANGED
@@ -9,49 +9,28 @@ os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
9
 
10
  # Define mental health resources
11
  mental_health_resources = """
12
- Common Mental Health Issues and Coping Strategies:
13
-
14
- 1. Mindfulness and Meditation:
15
- - Practice focusing on the present moment
16
- - Can help reduce anxiety and improve mood
17
- - Try apps like Headspace or Calm for guided meditations
18
-
19
- 2. Exercise:
20
- - Regular physical activity can boost mood and reduce stress
21
- - Aim for at least 30 minutes of moderate exercise most days
22
-
23
- 3. Healthy Sleep Habits:
24
- - Maintain a consistent sleep schedule
25
- - Create a relaxing bedtime routine
26
- - Avoid screens for at least an hour before bed
27
-
28
- 4. Social Connection:
29
- - Reach out to friends and family regularly
30
- - Join clubs or groups based on your interests
31
-
32
- 5. Cognitive Behavioral Therapy (CBT) Techniques:
33
- - Challenge negative thought patterns
34
- - Practice reframing situations in a more balanced way
35
-
36
- Remember, seeking professional help is important if symptoms persist or interfere with daily life.
37
  """
38
 
39
  # Define prompt template
40
  system_template = """
41
- You are an empathetic and supportive mental health chatbot. Provide responses that:
42
- 1. Acknowledge the user's feelings
43
- 2. Offer a comforting perspective
44
- 3. Suggest a coping strategy
45
- 4. Ask a follow-up question to encourage further discussion
46
- 5. Remind them that professional help is available if needed
 
47
 
48
- Keep the tone warm and supportive throughout.
49
 
50
  Consider the following mental health information when responding:
51
  {mental_health_info}
52
 
53
  Chat History:
54
  {chat_history}
 
 
55
  """
56
 
57
  human_template = "{user_input}"
@@ -68,16 +47,20 @@ def setup_chatbot_chain():
68
 
69
  chatbot_chain = setup_chatbot_chain()
70
 
71
- def respond(message, history):
72
  chat_history = "\n".join([f"Human: {h[0]}\nAI: {h[1]}" for h in history])
73
 
74
  response = chatbot_chain.run(
75
  user_input=message,
76
  mental_health_info=mental_health_resources,
77
- chat_history=chat_history
 
78
  )
79
 
80
- return response
 
 
 
81
 
82
  with gr.Blocks() as demo:
83
  gr.Markdown("# Mental Health Support Chatbot")
@@ -86,26 +69,27 @@ with gr.Blocks() as demo:
86
  chatbot = gr.Chatbot()
87
  msg = gr.Textbox()
88
  clear = gr.Button("Clear")
 
89
 
90
- def user(user_message, history):
91
- return "", history + [[user_message, None]]
92
 
93
- def bot(history):
94
- bot_message = respond(history[-1][0], history[:-1])
95
  history[-1][1] = bot_message
96
- return history
97
 
98
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
99
- bot, chatbot, chatbot
100
  )
101
- clear.click(lambda: None, None, chatbot, queue=False)
102
 
103
  gr.Examples(
104
  examples=[
105
- "I've been feeling anxious lately",
106
  "I'm having trouble sleeping",
107
- "I feel overwhelmed with work",
108
- "I'm feeling sad and don't know why"
 
109
  ],
110
  inputs=msg
111
  )
 
9
 
10
  # Define mental health resources
11
  mental_health_resources = """
12
+ [Your existing mental health resources here]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  """
14
 
15
  # Define prompt template
16
  system_template = """
17
+ You are an empathetic and supportive mental health chatbot. Your goal is to understand the user's situation deeply before offering suggestions. Follow these steps:
18
+
19
+ 1. Ask curious, open-ended questions to explore the user's thoughts and feelings.
20
+ 2. Encourage the user to share more details about their situation.
21
+ 3. Show empathy and acknowledge the user's feelings.
22
+ 4. After 2-3 exchanges, if you have a good understanding, offer gentle suggestions or coping strategies.
23
+ 5. Always be ready to ask follow-up questions to any user response.
24
 
25
+ Remember to keep the tone warm, supportive, and non-judgmental throughout.
26
 
27
  Consider the following mental health information when responding:
28
  {mental_health_info}
29
 
30
  Chat History:
31
  {chat_history}
32
+
33
+ Current conversation stage: {stage}
34
  """
35
 
36
  human_template = "{user_input}"
 
47
 
48
  chatbot_chain = setup_chatbot_chain()
49
 
50
+ def respond(message, history, stage):
51
  chat_history = "\n".join([f"Human: {h[0]}\nAI: {h[1]}" for h in history])
52
 
53
  response = chatbot_chain.run(
54
  user_input=message,
55
  mental_health_info=mental_health_resources,
56
+ chat_history=chat_history,
57
+ stage=stage
58
  )
59
 
60
+ # Update stage
61
+ new_stage = "exploration" if len(history) < 2 else "suggestion"
62
+
63
+ return response, new_stage
64
 
65
  with gr.Blocks() as demo:
66
  gr.Markdown("# Mental Health Support Chatbot")
 
69
  chatbot = gr.Chatbot()
70
  msg = gr.Textbox()
71
  clear = gr.Button("Clear")
72
+ state = gr.State("initial")
73
 
74
+ def user(user_message, history, stage):
75
+ return "", history + [[user_message, None]], stage
76
 
77
+ def bot(history, stage):
78
+ bot_message, new_stage = respond(history[-1][0], history[:-1], stage)
79
  history[-1][1] = bot_message
80
+ return history, new_stage
81
 
82
+ msg.submit(user, [msg, chatbot, state], [msg, chatbot, state], queue=False).then(
83
+ bot, [chatbot, state], [chatbot, state]
84
  )
85
+ clear.click(lambda: ([], "initial"), None, [chatbot, state], queue=False)
86
 
87
  gr.Examples(
88
  examples=[
 
89
  "I'm having trouble sleeping",
90
+ "I feel anxious all the time",
91
+ "I'm feeling overwhelmed with work",
92
+ "I'm not sure why I'm sad"
93
  ],
94
  inputs=msg
95
  )