Update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,7 @@ NORMAL_SET_III = [
|
|
| 54 |
]
|
| 55 |
|
| 56 |
def generate_question_set():
|
| 57 |
-
return random.sample(
|
| 58 |
|
| 59 |
async def gpt_translate(text, target_lang):
|
| 60 |
if target_lang == "English":
|
|
@@ -69,19 +69,28 @@ async def gpt_translate(text, target_lang):
|
|
| 69 |
|
| 70 |
async def respond(user_input, history, step, language, questions, followup_mode):
|
| 71 |
history.append({"role": "user", "content": user_input})
|
|
|
|
| 72 |
if language == "":
|
| 73 |
user_language = user_input.strip().capitalize()
|
| 74 |
questions_en = generate_question_set()
|
| 75 |
translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
|
| 76 |
welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
|
| 77 |
-
history.append({"role": "assistant", "content":
|
|
|
|
|
|
|
|
|
|
| 78 |
return history, "", 1, user_language, translated_questions, False
|
|
|
|
| 79 |
if step >= len(questions):
|
| 80 |
thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
|
| 81 |
end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
return history, "", step, language, questions, False
|
|
|
|
| 85 |
if followup_mode:
|
| 86 |
comment_prompt = [
|
| 87 |
{"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
|
|
@@ -94,39 +103,62 @@ async def respond(user_input, history, step, language, questions, followup_mode)
|
|
| 94 |
temperature=0.7
|
| 95 |
)
|
| 96 |
comment = comment_response.choices[0].message.content.strip()
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
comment_prompt = [
|
| 109 |
-
{"role": "system", "content": f"
|
| 110 |
history[-2],
|
| 111 |
history[-1]
|
| 112 |
]
|
| 113 |
comment_response = client.chat.completions.create(
|
| 114 |
model="gpt-3.5-turbo",
|
| 115 |
messages=comment_prompt,
|
| 116 |
-
temperature=0.
|
| 117 |
)
|
| 118 |
-
|
| 119 |
|
| 120 |
-
followup_count = 0
|
| 121 |
rand = random.random()
|
| 122 |
-
if rand < 0.
|
| 123 |
-
followup_count = 2
|
| 124 |
-
elif rand < 0.5:
|
| 125 |
-
followup_count = 1
|
| 126 |
-
|
| 127 |
-
if followup_count > 0:
|
| 128 |
followup_prompt = [
|
| 129 |
-
{"role": "system", "content": f"Ask
|
| 130 |
history[-2],
|
| 131 |
history[-1]
|
| 132 |
]
|
|
@@ -135,27 +167,37 @@ async def respond(user_input, history, step, language, questions, followup_mode)
|
|
| 135 |
messages=followup_prompt,
|
| 136 |
temperature=0.7
|
| 137 |
)
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
history.append({"role": "assistant", "content": ""})
|
| 140 |
-
for c in
|
| 141 |
history[-1]["content"] += c
|
| 142 |
await asyncio.sleep(0.03)
|
|
|
|
| 143 |
return history, "", step, language, questions, True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
history[-1]["content"] += c
|
| 153 |
-
await asyncio.sleep(0.03)
|
| 154 |
-
return history, "", step + 1, language, questions, False
|
| 155 |
|
| 156 |
def init():
|
| 157 |
return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
|
| 158 |
|
|
|
|
| 159 |
with gr.Blocks(css="""
|
| 160 |
.send-btn {
|
| 161 |
background-color: #25D366 !important;
|
|
@@ -187,15 +229,12 @@ with gr.Blocks(css="""
|
|
| 187 |
#chatbox .message.assistant { background-color: #ffffff !important; }
|
| 188 |
footer { display: none !important; }
|
| 189 |
.svelte-1ipelgc { display: none !important; }
|
| 190 |
-
|
| 191 |
.icon-button-wrapper {
|
| 192 |
display: none !important;
|
| 193 |
}
|
| 194 |
-
|
| 195 |
label.svelte-1to105q {
|
| 196 |
display: none !important;
|
| 197 |
}
|
| 198 |
-
|
| 199 |
""") as demo:
|
| 200 |
gr.HTML("""
|
| 201 |
<script>
|
|
@@ -245,4 +284,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()
|
|
|
|
| 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;
|
|
|
|
| 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()
|