Wanswan commited on
Commit
def17ef
·
verified ·
1 Parent(s): a342e4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -59
app.py CHANGED
@@ -11,51 +11,50 @@ WELCOME_MESSAGE_EN = "Hi! I'm Robin, your dialogue partner.\nWe're about to begi
11
 
12
  # === Normal Question Bank ===
13
  NORMAL_SET_I = [
14
- "When was the last time you walked for more than an hour? Describe where you went and what you saw.",
15
  "What is the best gift you have ever received? Why?",
16
  "If you had to leave your current city, where would you move to? What do you miss most about your current city?",
17
- "How did you celebrate Halloween last year?",
18
- "Do you read newspapers often? Which newspapers do you like? Why?",
19
  "What is the ideal number of students to share a house with? Why?",
20
  "If you could invent a new flavor of ice cream, what flavor would you create?",
21
  "What is the best restaurant you have been to in the past month? Please describe it.",
22
- "Describe the pet you last got.",
23
  "What is your favorite holiday? Why?",
24
- "Tell me the funniest thing that happened when you were with a child."
25
  ]
26
  NORMAL_SET_II = [
27
  "What gift did you get on your last birthday?",
28
- "Describe your last trip to the zoo.",
29
  "Is there someone in your family you feel especially close to? What makes your bond strong?",
30
- "Let’s play a quick word game! I’ll say a word, and you respond with a word that starts with the last letter of mine. We’ll go back and forth for a few rounds — no need to form sentences, just have fun!",
31
  "Do you like to wake up early or stay up late? Has anything interesting ever happened to you because of this?",
32
- "Where are you from? Tell me some places you have lived.",
33
  "What is your favorite subject in school and why?",
34
  "What is your plan for the next holiday?",
35
- "What gifts did you receive last Christmas/Hanukkah?",
36
  "Who is your favorite actor or actress? ",
37
  "What was your impression of your university when you first came to it?",
38
- "What is the best TV show you have seen in the past month? Describe it a little bit.",
39
  "What is your favorite holiday? Why?"
40
  ]
41
  NORMAL_SET_III = [
42
- "Where did you go to high school? What was it like?",
43
- "What is the best book you have read in the past three months? Describe it a little bit.",
44
  "What country would you most like to visit? What attracts you to it?",
45
  "Do you prefer digital or analog watches/clocks? Why?",
46
  "Can you think of someone your mother or father is really close to? What do you think makes their friendship special?"
47
  "What are the pros and cons of artificial Christmas trees?",
48
- "How often do you get your hair cut? Where do you go? Have you ever had a bad haircut?",
49
  "Did you have a class pet in elementary school? Do you remember its name?",
50
  "Do you think left-handed people are more creative than right-handed people?",
51
- "What was the last concert you attended? How many albums of the band do you have? Have you seen them perform before? Where?",
52
  "What magazines do you subscribe to? What have you subscribed to in the past?",
53
- "Have you ever participated in a school play? What role did you play? What was the plot? Did anything funny happen during the play?"
54
  ]
55
 
56
-
57
  def generate_question_set():
58
- return random.sample(NORMAL_SET_I, 3) + random.sample(NORMAL_SET_II, 3) + random.sample(NORMAL_SET_III, 3)
59
 
60
  async def gpt_translate(text, target_lang):
61
  if target_lang == "English":
@@ -70,19 +69,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 +103,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 +167,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;
@@ -186,19 +227,14 @@ with gr.Blocks(css="""
186
  }
187
  #chatbox .message.user { background-color: #DCF8C6 !important; }
188
  #chatbox .message.assistant { background-color: #ffffff !important; }
189
-
190
  footer { display: none !important; }
191
  .svelte-1ipelgc { display: none !important; }
192
-
193
  .icon-button-wrapper {
194
  display: none !important;
195
  }
196
-
197
  label.svelte-1to105q {
198
  display: none !important;
199
  }
200
-
201
-
202
  """) as demo:
203
  gr.HTML("""
204
  <script>
@@ -248,4 +284,4 @@ label.svelte-1to105q {
248
  send_btn.click(respond, [user_input, state, step, language, questions, followup_mode],
249
  [chatbot, user_input, step, language, questions, followup_mode])
250
 
251
- demo.queue().launch()
 
11
 
12
  # === Normal Question Bank ===
13
  NORMAL_SET_I = [
14
+ "When was the last time you walked for more than an hour? Please describe what you saw.",
15
  "What is the best gift you have ever received? Why?",
16
  "If you had to leave your current city, where would you move to? What do you miss most about your current city?",
17
+ "How did you celebrate the New Year last year?",
18
+ "Do you read newspapers often? Why?",
19
  "What is the ideal number of students to share a house with? Why?",
20
  "If you could invent a new flavor of ice cream, what flavor would you create?",
21
  "What is the best restaurant you have been to in the past month? Please describe it.",
22
+ "Please describe the pet you last got.",
23
  "What is your favorite holiday? Why?",
24
+ "Can you tell me something funny that happened when you were spending time with a child?"
25
  ]
26
  NORMAL_SET_II = [
27
  "What gift did you get on your last birthday?",
28
+ "Please tell me something about your last trip to the zoo.",
29
  "Is there someone in your family you feel especially close to? What makes your bond strong?",
30
+ "Imagine we were both stranded on a tropical island. What would be the first thing you would do?",
31
  "Do you like to wake up early or stay up late? Has anything interesting ever happened to you because of this?",
32
+ "If you could choose to live anywhere, what city or environment would you choose?",
33
  "What is your favorite subject in school and why?",
34
  "What is your plan for the next holiday?",
35
+ "What gifts did you receive for Christmas or New Year last year?",
36
  "Who is your favorite actor or actress? ",
37
  "What was your impression of your university when you first came to it?",
38
+ "What is the best TV show you have seen in the past month? Please describe it a little bit.",
39
  "What is your favorite holiday? Why?"
40
  ]
41
  NORMAL_SET_III = [
42
+ "What do you remember most about your time in high school?",
43
+ "What is the best book you have read in the past three months? ",
44
  "What country would you most like to visit? What attracts you to it?",
45
  "Do you prefer digital or analog watches/clocks? Why?",
46
  "Can you think of someone your mother or father is really close to? What do you think makes their friendship special?"
47
  "What are the pros and cons of artificial Christmas trees?",
48
+ "How often do you get your hair cut? Have you ever had a bad haircut?",
49
  "Did you have a class pet in elementary school? Do you remember its name?",
50
  "Do you think left-handed people are more creative than right-handed people?",
51
+ "What was the last concert you attended? How was the concert?",
52
  "What magazines do you subscribe to? What have you subscribed to in the past?",
53
+ "Have you ever participated in a school play? What role did you play? Did anything funny happen during the play?"
54
  ]
55
 
 
56
  def generate_question_set():
57
+ return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
58
 
59
  async def gpt_translate(text, target_lang):
60
  if target_lang == "English":
 
69
 
70
  async def respond(user_input, history, step, language, questions, followup_mode):
71
  history.append({"role": "user", "content": user_input})
72
+
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": ""})
79
+ for c in f"{welcome}\n\n1. {translated_questions[0]}":
80
+ history[-1]["content"] += c
81
+ await asyncio.sleep(0.03)
82
  return history, "", 1, user_language, translated_questions, False
83
+
84
  if step >= len(questions):
85
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
86
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
87
+ for msg in [thank_you, end_note]:
88
+ history.append({"role": "assistant", "content": ""})
89
+ for c in msg:
90
+ history[-1]["content"] += c
91
+ await asyncio.sleep(0.03)
92
  return history, "", step, language, questions, False
93
+
94
  if followup_mode:
95
  comment_prompt = [
96
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
 
103
  temperature=0.7
104
  )
105
  comment = comment_response.choices[0].message.content.strip()
106
+
107
+ rand = random.random()
108
+ if rand < 0.1:
109
+ followup_prompt = [
110
+ {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's last answer."},
111
+ history[-2],
112
+ history[-1]
113
+ ]
114
+ followup_response = client.chat.completions.create(
115
+ model="gpt-3.5-turbo",
116
+ messages=followup_prompt,
117
+ temperature=0.7
118
+ )
119
+ followup_question = followup_response.choices[0].message.content.strip()
120
+
121
+ history.append({"role": "assistant", "content": ""})
122
+ for c in comment:
123
+ history[-1]["content"] += c
124
+ await asyncio.sleep(0.03)
125
+
126
+ history.append({"role": "assistant", "content": ""})
127
+ for c in followup_question:
128
+ history[-1]["content"] += c
129
+ await asyncio.sleep(0.03)
130
+
131
+ return history, "", step, language, questions, True
132
+ else:
133
+ history.append({"role": "assistant", "content": ""})
134
+ for c in comment:
135
+ history[-1]["content"] += c
136
+ await asyncio.sleep(0.03)
137
+
138
+ next_question = f"{step+1}. {questions[step]}"
139
+ history.append({"role": "assistant", "content": ""})
140
+ for c in next_question:
141
+ history[-1]["content"] += c
142
+ await asyncio.sleep(0.03)
143
+
144
+ return history, "", step + 1, language, questions, False
145
 
146
  comment_prompt = [
147
+ {"role": "system", "content": f"Write a short, personalized comment in {language} responding to the user's answer. Add a light emoji."},
148
  history[-2],
149
  history[-1]
150
  ]
151
  comment_response = client.chat.completions.create(
152
  model="gpt-3.5-turbo",
153
  messages=comment_prompt,
154
+ temperature=0.7
155
  )
156
+ comment = comment_response.choices[0].message.content.strip()
157
 
 
158
  rand = random.random()
159
+ if rand < 0.4:
 
 
 
 
 
160
  followup_prompt = [
161
+ {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's answer."},
162
  history[-2],
163
  history[-1]
164
  ]
 
167
  messages=followup_prompt,
168
  temperature=0.7
169
  )
170
+ followup_question = followup_response.choices[0].message.content.strip()
171
+
172
+ history.append({"role": "assistant", "content": ""})
173
+ for c in comment:
174
+ history[-1]["content"] += c
175
+ await asyncio.sleep(0.03)
176
+
177
  history.append({"role": "assistant", "content": ""})
178
+ for c in followup_question:
179
  history[-1]["content"] += c
180
  await asyncio.sleep(0.03)
181
+
182
  return history, "", step, language, questions, True
183
+ else:
184
+ history.append({"role": "assistant", "content": ""})
185
+ for c in comment:
186
+ history[-1]["content"] += c
187
+ await asyncio.sleep(0.03)
188
+
189
+ next_question = f"{step+1}. {questions[step]}"
190
+ history.append({"role": "assistant", "content": ""})
191
+ for c in next_question:
192
+ history[-1]["content"] += c
193
+ await asyncio.sleep(0.03)
194
 
195
+ return history, "", step + 1, language, questions, False
 
 
 
 
 
 
 
 
 
196
 
197
  def init():
198
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
199
 
200
+
201
  with gr.Blocks(css="""
202
  .send-btn {
203
  background-color: #25D366 !important;
 
227
  }
228
  #chatbox .message.user { background-color: #DCF8C6 !important; }
229
  #chatbox .message.assistant { background-color: #ffffff !important; }
 
230
  footer { display: none !important; }
231
  .svelte-1ipelgc { display: none !important; }
 
232
  .icon-button-wrapper {
233
  display: none !important;
234
  }
 
235
  label.svelte-1to105q {
236
  display: none !important;
237
  }
 
 
238
  """) as demo:
239
  gr.HTML("""
240
  <script>
 
284
  send_btn.click(respond, [user_input, state, step, language, questions, followup_mode],
285
  [chatbot, user_input, step, language, questions, followup_mode])
286
 
287
+ demo.queue().launch()