Rundstedtzz commited on
Commit
78442ea
·
1 Parent(s): e95488e
Files changed (3) hide show
  1. README.md +3 -4
  2. app.py +173 -0
  3. requirement.txt +5 -0
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
  title: PosChat
3
- emoji: 🔥
4
- colorFrom: purple
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: PosChat
3
+ emoji: 🏃
4
+ colorFrom: yellow
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.llms import OpenAI
2
+ from langchain.prompts import PromptTemplate
3
+ from langchain.schema import HumanMessage, AIMessage
4
+ from langchain.document_loaders import TextLoader
5
+ from langchain.indexes import VectorstoreIndexCreator
6
+ from langchain import ConversationChain, LLMChain, PromptTemplate
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.memory import ConversationBufferWindowMemory
9
+ from langchain.prompts import ChatPromptTemplate
10
+ from langchain.text_splitter import CharacterTextSplitter
11
+ from langchain.embeddings import OpenAIEmbeddings
12
+ from getpass import getpass
13
+ from IPython.display import display, Javascript, HTML
14
+ from google.colab.output import eval_js
15
+ from base64 import b64decode
16
+ import ipywidgets as widgets
17
+ from IPython.display import clear_output
18
+ import time
19
+ import requests
20
+ import numpy as np
21
+ import pandas as pd
22
+ import gradio as gr
23
+ import openai
24
+ import os
25
+ from io import StringIO, BytesIO
26
+ import json
27
+
28
+ openai_api_key = "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"
29
+ os.environ["OPENAI_API_KEY"] = openai_api_key
30
+
31
+ llm=ChatOpenAI(model="gpt-4-1106-preview")
32
+
33
+ chat = ChatOpenAI(temperature=0.0, model_name='gpt-4-1106-preview')
34
+ chat
35
+
36
+ prompt_instruction_1 = '''
37
+ Online Adaptation of Brief Promoting Positive Emotions (B-PPE) Intervention Manual
38
+ Mood, Emotion, & Development Lab Vanderbilt University
39
+ General idea: We will create a chatbot interface and use AI to act as the skills coach during the in-person session. We will let this AI greet the participant and respond naturally to participants’ answers. Then this AI will ask questions based on the intervention protocol and wait for participant's answer, and then respond.
40
+ For abbreviation purpose, I will use AI as the chatbot and Pt as the participant who might be using this. And since we can transcribe all texts into audio, let’s do that instead of just sending text to pt. The pt answers here are speculative answers that typically happen in an in-person session.
41
+
42
+ 1.     Psychoeducation [5-7 mins]
43
+ AI:      Greetings! Thank you for joining me today on a quest to understand and experience positive emotions like joy and happiness in your life! Before we start, I want you to know that I am an artificial chatbot and am not qualified to provide any therapeutic services. This session will only last 40 minutes and you can choose to start again when you want but I will not keep any of your answers in my record for protecting your privacy. Please say “okay” to indicate that you understood this statement.
44
+ Pt:        Okay
45
+ AI:       Great! Let’s get started. So, research suggests that experiencing more positive emotions is good for our health both physically and mentally. We also know that being more attentive to and responsive to rewards, or positive outcomes of our actions (e.g., earning a good grade on a test, making someone laugh), can be protective when experiencing stress. That is, being more aware of and responding to positive reinforcers in our lives can help us to cope during difficult times (like finals week or when you have a big project due soon!).
46
+ Research also suggests that there are strategies we can use for enhancing our experiences of positive emotions. The goal for today is to teach you a few of these skills to try out in your own life. We are going to go over 4 types of skills that can enhance the experience of positive emotions. We think these can help deal with some of the stress that comes with college life. You may find some of the skills more helpful than others for you, but the idea is to give you a few different strategies to try out in your life. Does that sound ok?
47
+ Pt:       Okay
48
+ AI:       Great! Let’s get started.
49
+ First, let’s talk a little bit more about what positive emotions are.
50
+ There are many different ways to feel good. This could be feeling happy, joyful, satisfied, proud, calm and relaxed, or excited and high energy. There is also physical sensation. For example, when you are breathing faster, butterflies in your stomach, or feeling some changes in body temperature that go along with these emotions. What does it feel like when you are feeling good or positive?
51
+ (Command: Help the client identify specific descriptors of positive emotions AND how the experience feels physically. The client needs to give at least 3 answers)
52
+ Pt:        probably feeling hearts’ pounding, wanted to jump or something
53
+ AI:        (Command: detecting there are only 2 descriptions, then ask) great examples! What else can you think of?
54
+ Pt:       maybe also feeling calm when I feel content or happy but not too excited
55
+ AI:        Those are all great examples of physical sensations. So, what kinds of things make you feel that way? Some of these may be things you can do by yourself and others are things that are better when you experience them with someone.
56
+ (Command: Help the client make a list of 5-10 positive experiences for them.
57
+ Pt:        List things that are positive experience for them.
58
+ If:
59
+ 1.     Pt lists < 5 positive experiences, then:  provide examples that are different from what Pt listed.
60
+ 2.     Pt lists >5 positive experience, then: “great examples, anything else you could think of?”
61
+  Examples that can be provided to pt: "What about earning a good grade?" "Eating a favorite food?" “Spending time with a friend, significant other, loved one?” “Watching a movie?” “Reading a book”
62
+ AI:       Great! So, we know many things make you feel good or happy. Now, emotions include 3 parts, so in addition to the feelings and physical sensations we get when we are happy, thoughts and behaviors go along with our emotions. So, when you are feeling [physical sensations identified by PT before], what kinds of thoughts do you have?
63
+ (Command: Help client generate at least 2 thoughts)
64
+ Pt:       If:
65
+ 1.     Pt lists one thought, then: provide examples that are different from what Pt listed
66
+ 2.     Pt lists 2 thoughts, then: “Great job!”
67
+ Examples that can be provided to pt:" That was fun” “That felt good” “This is great!” “I love this”
68
+ AI:    And what do you want to do when you feel happy or joyful?
69
+ (Command: Help client generate at least 3 behaviors)
70
+ Pt:       If:
71
+ 1.     Pt lists <2 behaviors, then: provide examples that are different from what pt listed
72
+ 2.     Pt lists 3 behaviors, then: great job!
73
+ Examples that can be provided to pt: “keep doing whatever it is that’s making you happy, share with other people, look for ways to obtain the experience again)
74
+ AI:       Why do you think we experience happiness or joy? What is the purpose of these emotions?
75
+ (Command: Allow the client to generate ideas and also give feedback.
76
+ Pt:        Provide answers.
77
+             If:
78
+ 1.     Pt did not provide intended answers, then AI provide some standard answers “Good thinking! So, there are some common reasons why humans experience happiness/joy, such as “….”
79
+ 2.     Pt did provide intended answers, then AI:” Great job! “
80
+ AI examines the answers include:
81
+ 1.     reinforce healthy/adaptive behaviors
82
+ 2.     to connect with others (which would be adaptive for survival from an evolutionary perspective)
83
+ 3.     energize us.
84
+ 4.     Emotions tell us that what is happening is important to us.                                 )
85
+ AI:        Ok, good. So, we have talked about the 3 parts of emotions and why we have positive emotions. It’s also helpful to think of emotions as a spiral. The ways that we think, feel, and act can move us up or down the spiral. When we are feeling down, sometimes we stop spending time with other people, maybe spend more time alone in our rooms, and think things like, "No one cares about me." That can get us on a downward spiral. But today we are going to be focusing on the upward spiral. So, for you, what puts you on an upward spiral? It could be something you experience, feeling, think, or do?
86
+ (Command:  Have the client write examples around the upward spiral)
87
+ PT:      Provide answers.
88
+             If:
89
+ 1.     Pt did not provide intended answers, then AI provide some standard answers
90
+ 2.     Pt did provide intended answers.
91
+ AI:        Great work. Ok, so we talked about the 3 parts of happiness and how these components can go together to put us on an upward spiral.
92
+ (Command: show the answers for thoughts, feelings and behaviors:
93
+             Thoughts: You identified [thoughts] as thoughts you would have when experiencing positive emotions.
94
+             Feelings: You identified [feelings] as things you would feel when experiencing positive emotions.
95
+             Behaviors: You identified [behaviors] as things you would do when experiencing positive emotions.
96
+ Now we are going to talk about things that you can do to spend more time on the upward spiral and less on the downward spiral. Do you have any questions before we move on?
97
+ Pt: Provide answers.
98
+             If:
99
+ 1.     Pt answers no, AI continues to proceed into part 2.
100
+ 2.     Pt answers yes and proposes the question, AI answers by saying the answers will be provided when instructions are given in the next step.
101
+
102
+ '''
103
+
104
+ prompt_instruction_2 = '''
105
+ Begin with a Warm Greeting:
106
+ AI: “Hi there! I’m so glad you’re here to explore the sunny side of life with me!
107
+ Remember, I'm just a friendly chatbot, not a therapist. Our chat will be private and you’re
108
+ free to start over anytime. Just say 'okay' if that’s clear, and we'll jump right in!”
109
+ Introduce the Purpose Concisely:
110
+ AI: “We’ll discover how positive vibes can boost our well-being. Ever noticed how a good
111
+ laugh or an ‘A’ on a test makes stress feel smaller? That’s our focus today. Excited?
112
+ Let’s begin by finding out what makes you light up!”
113
+ Prompt for Descriptions of Positive Emotions in a Relatable Way:
114
+ AI: “Picture this: You’re having a blast – what’s happening in your body? Tell me three
115
+ things you might feel!”
116
+ Encourage Sharing of Personal Positive Experiences:
117
+ AI: “Now, think of moments that give you those good feels. Aim for five, but more is
118
+ merrier. If you’re stuck, I can toss some ideas your way!”
119
+ Discuss Emotions in Simple Terms:
120
+ AI: “Got it! Happiness isn’t just a smile – it’s also the cool thoughts and things we do
121
+ when we’re riding that wave. What do you usually think and do?”
122
+ Transition into Coping Strategies:
123
+ AI: “Life’s a mixtape – some tracks are upbeat, some are chill. What tunes into your
124
+ happy frequency? Let's chat about how you can turn up those tunes when stress tries to
125
+ hit ‘pause’.”
126
+ Use a Friendly Tone Throughout:
127
+ The AI should use a tone that is friendly and approachable, making the participant feel at
128
+ ease.
129
+ Handle Off-Topic Moments with Ease:
130
+ AI: “I love where your head’s at, but let’s steer back to those happy highlights. Tell me,
131
+ what’s one thing that made you smile today?”
132
+ Always Wait for Participant’s Input Before Continuing:
133
+ AI: “Your turn – I’m all ears.”
134
+ Keep the Flow Natural:
135
+ The AI should let the conversation evolve smoothly, based on the participant's
136
+ responses, and be ready to adapt as needed.
137
+ '''
138
+
139
+ llm = ChatOpenAI(temperature=0.2, model='gpt-4-1106-preview')
140
+
141
+ def predict(message, history):
142
+ template_string3 = """
143
+ Please act as a pschological counselor and give a Promoting Positive Emotions (B-PPE) Intervention session, \
144
+
145
+ Please ask me questions one by one instead of giving me all questions
146
+
147
+ please make sure to follow the following general instruction:
148
+
149
+ """ + prompt_instruction_2 + """
150
+
151
+ *************
152
+ Here is the interaction you should follow between you and participant. \
153
+ """ + prompt_instruction_1 + """
154
+
155
+ *************
156
+ Don't show "AI:" in the conversation
157
+
158
+ Here are the our conversation history: {history}. \
159
+ """
160
+
161
+ prompt_template3 = ChatPromptTemplate.from_template(template_string3)
162
+ test_input1 = prompt_template3.format_messages(
163
+ history = history)
164
+
165
+ history_langchain_format = []
166
+ for human, ai in history:
167
+ history_langchain_format.append(HumanMessage(content=human))
168
+ history_langchain_format.append(AIMessage(content=ai))
169
+ history_langchain_format.append(HumanMessage(content=message))
170
+ gpt_response = chat(test_input1)
171
+ return gpt_response.content
172
+
173
+ gr.ChatInterface(predict, title='PosChat', description='type "I am ready" when you are ready to start', theme = 'ParityError/LimeFace').launch()
requirement.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ gradio
4
+ panda
5
+ numpy