Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,9 +68,11 @@ async def gpt_translate(text, target_lang):
|
|
| 68 |
)
|
| 69 |
return response.choices[0].message.content.strip()
|
| 70 |
|
| 71 |
-
async def respond(user_input, history, step, language, questions,
|
|
|
|
| 72 |
history.append({"role": "user", "content": user_input})
|
| 73 |
|
|
|
|
| 74 |
if language == "":
|
| 75 |
user_language = user_input.strip()
|
| 76 |
questions_en = generate_question_set()
|
|
@@ -79,6 +81,7 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 79 |
history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"} )
|
| 80 |
return history, "", 1, user_language, translated_questions, False, [], 0
|
| 81 |
|
|
|
|
| 82 |
if step >= len(questions):
|
| 83 |
thank_you = await gpt_translate("Thank you for your response!", language)
|
| 84 |
end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
|
|
@@ -86,16 +89,16 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 86 |
history.append({"role": "assistant", "content": end_note})
|
| 87 |
return history, "", step, language, questions, False, [], 0
|
| 88 |
|
| 89 |
-
# ----------
|
| 90 |
comment_prompt = [
|
| 91 |
-
{"role": "system",
|
| 92 |
-
"content": f"""
|
| 93 |
You are a helpful, attentive assistant.
|
| 94 |
Always respond in {language}. Use natural, respectful, and logically relevant tone.
|
| 95 |
Avoid starting with generic responses like 'Yes' or 'No'.
|
| 96 |
Keep your replies short and meaningful. Show interest in the user's input.
|
| 97 |
Do not use emojis or overly emotional phrases.
|
| 98 |
-
"""},
|
|
|
|
| 99 |
]
|
| 100 |
comment_response = client.chat.completions.create(
|
| 101 |
model="gpt-3.5-turbo",
|
|
@@ -106,10 +109,10 @@ Do not use emojis or overly emotional phrases.
|
|
| 106 |
history.append({"role": "assistant", "content": comment})
|
| 107 |
await asyncio.sleep(0.03)
|
| 108 |
|
| 109 |
-
# ----------
|
| 110 |
if followup_mode:
|
| 111 |
-
# 如果刚完成第1次追问,有10%概率再追问一次
|
| 112 |
if followup_total_count == 1 and random.random() < 0.1:
|
|
|
|
| 113 |
followup_prompt = [
|
| 114 |
{"role": "system", "content": f"Ask 1 more open-ended follow-up question in {language}, unnumbered."},
|
| 115 |
history[-4], history[-3], history[-2], history[-1]
|
|
@@ -123,11 +126,12 @@ Do not use emojis or overly emotional phrases.
|
|
| 123 |
history.append({"role": "assistant", "content": second_followup})
|
| 124 |
return history, "", step, language, questions, True, [], 2
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
return history, "", step, language, questions, False, [],
|
| 128 |
|
| 129 |
-
# ----------
|
| 130 |
if random.random() < 0.4:
|
|
|
|
| 131 |
followup_prompt = [
|
| 132 |
{"role": "system", "content": f"Ask 1 open-ended follow-up question in {language}, based on user's response."},
|
| 133 |
history[-2], history[-1]
|
|
@@ -141,11 +145,12 @@ Do not use emojis or overly emotional phrases.
|
|
| 141 |
history.append({"role": "assistant", "content": first_followup})
|
| 142 |
return history, "", step, language, questions, True, [], 1
|
| 143 |
|
| 144 |
-
# ----------
|
| 145 |
next_question = f"{step+1}. {questions[step]}"
|
| 146 |
history.append({"role": "assistant", "content": next_question})
|
| 147 |
return history, "", step + 1, language, questions, False, [], 0
|
| 148 |
|
|
|
|
| 149 |
def init():
|
| 150 |
return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False, [], 0
|
| 151 |
|
|
|
|
| 68 |
)
|
| 69 |
return response.choices[0].message.content.strip()
|
| 70 |
|
| 71 |
+
async def respond(user_input, history, step, language, questions,
|
| 72 |
+
followup_mode, followup_queue, followup_total_count):
|
| 73 |
history.append({"role": "user", "content": user_input})
|
| 74 |
|
| 75 |
+
# ---------- 初始化语言 ----------
|
| 76 |
if language == "":
|
| 77 |
user_language = user_input.strip()
|
| 78 |
questions_en = generate_question_set()
|
|
|
|
| 81 |
history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"} )
|
| 82 |
return history, "", 1, user_language, translated_questions, False, [], 0
|
| 83 |
|
| 84 |
+
# ---------- 所有问题已结束 ----------
|
| 85 |
if step >= len(questions):
|
| 86 |
thank_you = await gpt_translate("Thank you for your response!", language)
|
| 87 |
end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
|
|
|
|
| 89 |
history.append({"role": "assistant", "content": end_note})
|
| 90 |
return history, "", step, language, questions, False, [], 0
|
| 91 |
|
| 92 |
+
# ---------- 生成对用户的评论 ----------
|
| 93 |
comment_prompt = [
|
| 94 |
+
{"role": "system", "content": f"""
|
|
|
|
| 95 |
You are a helpful, attentive assistant.
|
| 96 |
Always respond in {language}. Use natural, respectful, and logically relevant tone.
|
| 97 |
Avoid starting with generic responses like 'Yes' or 'No'.
|
| 98 |
Keep your replies short and meaningful. Show interest in the user's input.
|
| 99 |
Do not use emojis or overly emotional phrases.
|
| 100 |
+
"""},
|
| 101 |
+
history[-2], history[-1]
|
| 102 |
]
|
| 103 |
comment_response = client.chat.completions.create(
|
| 104 |
model="gpt-3.5-turbo",
|
|
|
|
| 109 |
history.append({"role": "assistant", "content": comment})
|
| 110 |
await asyncio.sleep(0.03)
|
| 111 |
|
| 112 |
+
# ---------- 追问模式下 ----------
|
| 113 |
if followup_mode:
|
|
|
|
| 114 |
if followup_total_count == 1 and random.random() < 0.1:
|
| 115 |
+
# 第二次追问触发
|
| 116 |
followup_prompt = [
|
| 117 |
{"role": "system", "content": f"Ask 1 more open-ended follow-up question in {language}, unnumbered."},
|
| 118 |
history[-4], history[-3], history[-2], history[-1]
|
|
|
|
| 126 |
history.append({"role": "assistant", "content": second_followup})
|
| 127 |
return history, "", step, language, questions, True, [], 2
|
| 128 |
|
| 129 |
+
# 第二次追问完成或未触发:追问模式结束(不立刻进入下一题,等待用户再输入)
|
| 130 |
+
return history, "", step, language, questions, False, [], 0
|
| 131 |
|
| 132 |
+
# ---------- 主问题流程中:判断是否追问 ----------
|
| 133 |
if random.random() < 0.4:
|
| 134 |
+
# 第一次追问触发
|
| 135 |
followup_prompt = [
|
| 136 |
{"role": "system", "content": f"Ask 1 open-ended follow-up question in {language}, based on user's response."},
|
| 137 |
history[-2], history[-1]
|
|
|
|
| 145 |
history.append({"role": "assistant", "content": first_followup})
|
| 146 |
return history, "", step, language, questions, True, [], 1
|
| 147 |
|
| 148 |
+
# ---------- 没有追问:直接进入下一主问题 ----------
|
| 149 |
next_question = f"{step+1}. {questions[step]}"
|
| 150 |
history.append({"role": "assistant", "content": next_question})
|
| 151 |
return history, "", step + 1, language, questions, False, [], 0
|
| 152 |
|
| 153 |
+
|
| 154 |
def init():
|
| 155 |
return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False, [], 0
|
| 156 |
|