Wanswan commited on
Commit
fb99a1d
·
verified ·
1 Parent(s): 907efc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -47
app.py CHANGED
@@ -17,25 +17,25 @@ SET_I = [
17
  "What would constitute a 'perfect' day for you?",
18
  "When did you last sing to yourself? To someone else?",
19
  "If you were able to live to the age of 90 and retain either the mind or body of a 30-year-old for the last 60 years of your life, which would you want?",
20
- "Do you have a secret hunch about how you will die?",
21
  "Name three things you and me appear to have in common.",
22
  "For what in your life do you feel most grateful?",
23
  "If you could change anything about the way you were raised, what would it be?",
24
- "Take one minutes and tell me some of your life stories in as much detail as possible.",
25
  "If you could wake up tomorrow having gained any one quality or ability, what would it be?"
26
  ]
27
 
28
  SET_II = [
29
  "If a crystal ball could tell you the truth about yourself, your life, the future or anything else, what would you want to know?",
30
  "Is there something that you’ve dreamed of doing for a long time? Why haven’t you done it?",
31
- "What is the greatest accomplishment of your life?",
32
  "What do you value most in a friendship?",
33
  "What is your most treasured memory?",
34
  "What is your most terrible memory?",
35
  "If you knew that in one year you would die suddenly, would you change anything about the way you are now living? Why?",
36
  "What does friendship mean to you?",
37
  "What roles do love and affection play in your life?",
38
- "Alternate sharing something you consider a positive characteristic of me, I will be appreciate.",
39
  "How close and warm is your family? Do you feel your childhood was happier than most other people’s?",
40
  "How do you feel about your relationship with your mother?"
41
  ]
@@ -44,16 +44,17 @@ SET_III = [
44
  "Let's try something fun: Can you write three sentences that start with 'We' and describe something true about us — you and me, chatting here together? (For example: 'We are both curious about each other.')",
45
  "Let’s complete this sentence together: 'I wish I had someone I could share ___ with.' What would you say?",
46
  "If you were going to become a close friend with me, please share what would be important for me to know.",
47
- "Tell me what you like about me; be very honest this time, saying things that you might not say to someone you’ve just met.",
48
  "Share with me an embarrassing moment in your life.",
49
  "When did you last cry in front of another person? By yourself?",
50
  "Tell me something that you like about me already.",
51
  "What, if anything, is too serious to be joked about?",
52
- "If you were to die this evening with no opportunity to communicate with anyone, what would you most regret not having told someone? Why haven’t you told them yet?",
53
  "Your house, containing everything you own, catches fire. After saving your loved ones and pets, you have time to safely make a final dash to save any one item. What would it be? Why?",
54
- "Of all the people in your family, whose death would you find most disturbing? Why?",
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
56
  ]
 
57
  def generate_question_set():
58
  return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
59
 
@@ -70,19 +71,28 @@ async def gpt_translate(text, target_lang):
70
 
71
  async def respond(user_input, history, step, language, questions, followup_mode):
72
  history.append({"role": "user", "content": user_input})
 
73
  if language == "":
74
  user_language = user_input.strip().capitalize()
75
  questions_en = generate_question_set()
76
  translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
77
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
78
- history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"})
 
 
 
79
  return history, "", 1, user_language, translated_questions, False
 
80
  if step >= len(questions):
81
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
82
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
83
- history.append({"role": "assistant", "content": thank_you})
84
- history.append({"role": "assistant", "content": end_note})
 
 
 
85
  return history, "", step, language, questions, False
 
86
  if followup_mode:
87
  comment_prompt = [
88
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
@@ -95,39 +105,62 @@ async def respond(user_input, history, step, language, questions, followup_mode)
95
  temperature=0.7
96
  )
97
  comment = comment_response.choices[0].message.content.strip()
98
- history.append({"role": "assistant", "content": ""})
99
- for c in comment:
100
- history[-1]["content"] += c
101
- await asyncio.sleep(0.03)
102
- next_question = f"{step+1}. {questions[step]}"
103
- history.append({"role": "assistant", "content": ""})
104
- for c in next_question:
105
- history[-1]["content"] += c
106
- await asyncio.sleep(0.03)
107
- return history, "", step + 1, language, questions, False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  comment_prompt = [
110
- {"role": "system", "content": f"You are Robin, a warm and engaging conversational AI companion who chats naturally with the user, asks questions, and responds thoughtfully to their answers. Write 1–2 natural follow-up questions based on the user's answer. Do NOT number the questions. Write a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
111
  history[-2],
112
  history[-1]
113
  ]
114
  comment_response = client.chat.completions.create(
115
  model="gpt-3.5-turbo",
116
  messages=comment_prompt,
117
- temperature=0.8
118
  )
119
- bot_reply = comment_response.choices[0].message.content.strip()
120
 
121
- followup_count = 0
122
  rand = random.random()
123
- if rand < 0.1:
124
- followup_count = 2
125
- elif rand < 0.5:
126
- followup_count = 1
127
-
128
- if followup_count > 0:
129
  followup_prompt = [
130
- {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}."},
131
  history[-2],
132
  history[-1]
133
  ]
@@ -136,27 +169,37 @@ async def respond(user_input, history, step, language, questions, followup_mode)
136
  messages=followup_prompt,
137
  temperature=0.7
138
  )
139
- bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
 
 
 
 
 
 
140
  history.append({"role": "assistant", "content": ""})
141
- for c in bot_reply:
142
  history[-1]["content"] += c
143
  await asyncio.sleep(0.03)
 
144
  return history, "", step, language, questions, True
 
 
 
 
 
145
 
146
- history.append({"role": "assistant", "content": ""})
147
- for c in bot_reply:
148
- history[-1]["content"] += c
149
- await asyncio.sleep(0.03)
150
- next_question = f"{step+1}. {questions[step]}"
151
- history.append({"role": "assistant", "content": ""})
152
- for c in next_question:
153
- history[-1]["content"] += c
154
- await asyncio.sleep(0.03)
155
- return history, "", step + 1, language, questions, False
156
 
157
  def init():
158
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
159
 
 
160
  with gr.Blocks(css="""
161
  .send-btn {
162
  background-color: #25D366 !important;
@@ -188,11 +231,9 @@ with gr.Blocks(css="""
188
  #chatbox .message.assistant { background-color: #ffffff !important; }
189
  footer { display: none !important; }
190
  .svelte-1ipelgc { display: none !important; }
191
-
192
  .icon-button-wrapper {
193
  display: none !important;
194
  }
195
-
196
  label.svelte-1to105q {
197
  display: none !important;
198
  }
@@ -245,5 +286,4 @@ label.svelte-1to105q {
245
  send_btn.click(respond, [user_input, state, step, language, questions, followup_mode],
246
  [chatbot, user_input, step, language, questions, followup_mode])
247
 
248
- demo.queue().launch()
249
-
 
17
  "What would constitute a 'perfect' day for you?",
18
  "When did you last sing to yourself? To someone else?",
19
  "If you were able to live to the age of 90 and retain either the mind or body of a 30-year-old for the last 60 years of your life, which would you want?",
20
+ "Have you ever thought about how you would like to be remembered in the future?",
21
  "Name three things you and me appear to have in common.",
22
  "For what in your life do you feel most grateful?",
23
  "If you could change anything about the way you were raised, what would it be?",
24
+ "Could you please tell me one of your life stories in detail?",
25
  "If you could wake up tomorrow having gained any one quality or ability, what would it be?"
26
  ]
27
 
28
  SET_II = [
29
  "If a crystal ball could tell you the truth about yourself, your life, the future or anything else, what would you want to know?",
30
  "Is there something that you’ve dreamed of doing for a long time? Why haven’t you done it?",
31
+ "What is the greatest accomplishment of your life you think?",
32
  "What do you value most in a friendship?",
33
  "What is your most treasured memory?",
34
  "What is your most terrible memory?",
35
  "If you knew that in one year you would die suddenly, would you change anything about the way you are now living? Why?",
36
  "What does friendship mean to you?",
37
  "What roles do love and affection play in your life?",
38
+ "Please share what you think are my strengths so far, I would be grateful.",
39
  "How close and warm is your family? Do you feel your childhood was happier than most other people’s?",
40
  "How do you feel about your relationship with your mother?"
41
  ]
 
44
  "Let's try something fun: Can you write three sentences that start with 'We' and describe something true about us — you and me, chatting here together? (For example: 'We are both curious about each other.')",
45
  "Let’s complete this sentence together: 'I wish I had someone I could share ___ with.' What would you say?",
46
  "If you were going to become a close friend with me, please share what would be important for me to know.",
47
+ "Tell me how you feel about me",
48
  "Share with me an embarrassing moment in your life.",
49
  "When did you last cry in front of another person? By yourself?",
50
  "Tell me something that you like about me already.",
51
  "What, if anything, is too serious to be joked about?",
52
+ "If you were leaving for Mars tonight and wouldn’t be able to contact anyone again, what would you most regret not having told someone? Why haven’t you told them yet?",
53
  "Your house, containing everything you own, catches fire. After saving your loved ones and pets, you have time to safely make a final dash to save any one item. What would it be? Why?",
54
+ "If one member of your family had to go on a long mission to Mars and you wouldn’t see them for many years, whose absence would affect you the most? Why?",
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
56
  ]
57
+
58
  def generate_question_set():
59
  return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
60
 
 
71
 
72
  async def respond(user_input, history, step, language, questions, followup_mode):
73
  history.append({"role": "user", "content": user_input})
74
+
75
  if language == "":
76
  user_language = user_input.strip().capitalize()
77
  questions_en = generate_question_set()
78
  translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
79
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
80
+ history.append({"role": "assistant", "content": ""})
81
+ for c in f"{welcome}\n\n1. {translated_questions[0]}":
82
+ history[-1]["content"] += c
83
+ await asyncio.sleep(0.03)
84
  return history, "", 1, user_language, translated_questions, False
85
+
86
  if step >= len(questions):
87
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
88
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
89
+ for msg in [thank_you, end_note]:
90
+ history.append({"role": "assistant", "content": ""})
91
+ for c in msg:
92
+ history[-1]["content"] += c
93
+ await asyncio.sleep(0.03)
94
  return history, "", step, language, questions, False
95
+
96
  if followup_mode:
97
  comment_prompt = [
98
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
 
105
  temperature=0.7
106
  )
107
  comment = comment_response.choices[0].message.content.strip()
108
+
109
+ rand = random.random()
110
+ if rand < 0.1:
111
+ followup_prompt = [
112
+ {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's last answer."},
113
+ history[-2],
114
+ history[-1]
115
+ ]
116
+ followup_response = client.chat.completions.create(
117
+ model="gpt-3.5-turbo",
118
+ messages=followup_prompt,
119
+ temperature=0.7
120
+ )
121
+ followup_question = followup_response.choices[0].message.content.strip()
122
+
123
+ history.append({"role": "assistant", "content": ""})
124
+ for c in comment:
125
+ history[-1]["content"] += c
126
+ await asyncio.sleep(0.03)
127
+
128
+ history.append({"role": "assistant", "content": ""})
129
+ for c in followup_question:
130
+ history[-1]["content"] += c
131
+ await asyncio.sleep(0.03)
132
+
133
+ return history, "", step, language, questions, True
134
+ else:
135
+ history.append({"role": "assistant", "content": ""})
136
+ for c in comment:
137
+ history[-1]["content"] += c
138
+ await asyncio.sleep(0.03)
139
+
140
+ next_question = f"{step+1}. {questions[step]}"
141
+ history.append({"role": "assistant", "content": ""})
142
+ for c in next_question:
143
+ history[-1]["content"] += c
144
+ await asyncio.sleep(0.03)
145
+
146
+ return history, "", step + 1, language, questions, False
147
 
148
  comment_prompt = [
149
+ {"role": "system", "content": f"Write a short, personalized comment in {language} responding to the user's answer. Add a light emoji."},
150
  history[-2],
151
  history[-1]
152
  ]
153
  comment_response = client.chat.completions.create(
154
  model="gpt-3.5-turbo",
155
  messages=comment_prompt,
156
+ temperature=0.7
157
  )
158
+ comment = comment_response.choices[0].message.content.strip()
159
 
 
160
  rand = random.random()
161
+ if rand < 0.4:
 
 
 
 
 
162
  followup_prompt = [
163
+ {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's answer."},
164
  history[-2],
165
  history[-1]
166
  ]
 
169
  messages=followup_prompt,
170
  temperature=0.7
171
  )
172
+ followup_question = followup_response.choices[0].message.content.strip()
173
+
174
+ history.append({"role": "assistant", "content": ""})
175
+ for c in comment:
176
+ history[-1]["content"] += c
177
+ await asyncio.sleep(0.03)
178
+
179
  history.append({"role": "assistant", "content": ""})
180
+ for c in followup_question:
181
  history[-1]["content"] += c
182
  await asyncio.sleep(0.03)
183
+
184
  return history, "", step, language, questions, True
185
+ else:
186
+ history.append({"role": "assistant", "content": ""})
187
+ for c in comment:
188
+ history[-1]["content"] += c
189
+ await asyncio.sleep(0.03)
190
 
191
+ next_question = f"{step+1}. {questions[step]}"
192
+ history.append({"role": "assistant", "content": ""})
193
+ for c in next_question:
194
+ history[-1]["content"] += c
195
+ await asyncio.sleep(0.03)
196
+
197
+ return history, "", step + 1, language, questions, False
 
 
 
198
 
199
  def init():
200
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
201
 
202
+
203
  with gr.Blocks(css="""
204
  .send-btn {
205
  background-color: #25D366 !important;
 
231
  #chatbox .message.assistant { background-color: #ffffff !important; }
232
  footer { display: none !important; }
233
  .svelte-1ipelgc { display: none !important; }
 
234
  .icon-button-wrapper {
235
  display: none !important;
236
  }
 
237
  label.svelte-1to105q {
238
  display: none !important;
239
  }
 
286
  send_btn.click(respond, [user_input, state, step, language, questions, followup_mode],
287
  [chatbot, user_input, step, language, questions, followup_mode])
288
 
289
+ demo.queue().launch()