Update app.py
Browse files
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?
|
| 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
|
| 18 |
-
"Do you read newspapers often?
|
| 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 |
-
"
|
| 23 |
"What is your favorite holiday? Why?",
|
| 24 |
-
"
|
| 25 |
]
|
| 26 |
NORMAL_SET_II = [
|
| 27 |
"What gift did you get on your last birthday?",
|
| 28 |
-
"
|
| 29 |
"Is there someone in your family you feel especially close to? What makes your bond strong?",
|
| 30 |
-
"
|
| 31 |
"Do you like to wake up early or stay up late? Has anything interesting ever happened to you because of this?",
|
| 32 |
-
"
|
| 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
|
| 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?
|
| 39 |
"What is your favorite holiday? Why?"
|
| 40 |
]
|
| 41 |
NORMAL_SET_III = [
|
| 42 |
-
"
|
| 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?
|
| 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
|
| 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?
|
| 54 |
]
|
| 55 |
|
| 56 |
-
|
| 57 |
def generate_question_set():
|
| 58 |
-
return random.sample(
|
| 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":
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
comment_prompt = [
|
| 110 |
-
{"role": "system", "content": f"
|
| 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.
|
| 118 |
)
|
| 119 |
-
|
| 120 |
|
| 121 |
-
followup_count = 0
|
| 122 |
rand = random.random()
|
| 123 |
-
if rand < 0.
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
history.append({"role": "assistant", "content": ""})
|
| 141 |
-
for c in
|
| 142 |
history[-1]["content"] += c
|
| 143 |
await asyncio.sleep(0.03)
|
|
|
|
| 144 |
return history, "", step, language, questions, True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 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()
|