Wanswan commited on
Commit
4bb1e77
·
verified ·
1 Parent(s): 24590e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -71,22 +71,29 @@ async def gpt_translate(text, target_lang):
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": f"{welcome}\n\n1. {translated_questions[0]}"})
 
 
 
81
  return history, "", 1, user_language, translated_questions, False
82
 
83
  if step >= len(questions):
84
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
85
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
86
- history.append({"role": "assistant", "content": thank_you})
87
- history.append({"role": "assistant", "content": end_note})
 
 
 
88
  return history, "", step, language, questions, False
89
 
 
90
  if followup_mode:
91
  comment_prompt = [
92
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
@@ -102,8 +109,9 @@ async def respond(user_input, history, step, language, questions, followup_mode)
102
 
103
  rand = random.random()
104
  if rand < 0.1:
 
105
  followup_prompt = [
106
- {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's previous answer."},
107
  history[-2],
108
  history[-1]
109
  ]
@@ -113,23 +121,32 @@ async def respond(user_input, history, step, language, questions, followup_mode)
113
  temperature=0.7
114
  )
115
  followup_question = followup_response.choices[0].message.content.strip()
116
- content = comment + "\n\n" + followup_question
 
117
  history.append({"role": "assistant", "content": ""})
118
- for c in content:
119
  history[-1]["content"] += c
120
  await asyncio.sleep(0.03)
 
 
 
 
 
 
121
  return history, "", step, language, questions, True
122
  else:
123
- next_q = f"{step+1}. {questions[step]}"
124
- content = comment + "\n\n" + next_q
 
125
  history.append({"role": "assistant", "content": ""})
126
  for c in content:
127
  history[-1]["content"] += c
128
  await asyncio.sleep(0.03)
129
  return history, "", step + 1, language, questions, False
130
 
 
131
  comment_prompt = [
132
- {"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 a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
133
  history[-2],
134
  history[-1]
135
  ]
@@ -141,7 +158,8 @@ async def respond(user_input, history, step, language, questions, followup_mode)
141
  comment = comment_response.choices[0].message.content.strip()
142
 
143
  rand = random.random()
144
- if rand < 0.5:
 
145
  followup_prompt = [
146
  {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's answer."},
147
  history[-2],
@@ -153,15 +171,23 @@ async def respond(user_input, history, step, language, questions, followup_mode)
153
  temperature=0.7
154
  )
155
  followup_question = followup_response.choices[0].message.content.strip()
156
- content = comment + "\n\n" + followup_question
 
157
  history.append({"role": "assistant", "content": ""})
158
- for c in content:
159
  history[-1]["content"] += c
160
  await asyncio.sleep(0.03)
 
 
 
 
 
 
161
  return history, "", step, language, questions, True
162
  else:
163
- next_q = f"{step+1}. {questions[step]}"
164
- content = comment + "\n\n" + next_q
 
165
  history.append({"role": "assistant", "content": ""})
166
  for c in content:
167
  history[-1]["content"] += c
 
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
+ # Handle follow-up answer
97
  if followup_mode:
98
  comment_prompt = [
99
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
 
109
 
110
  rand = random.random()
111
  if rand < 0.1:
112
+ # Trigger second follow-up
113
  followup_prompt = [
114
+ {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's last answer."},
115
  history[-2],
116
  history[-1]
117
  ]
 
121
  temperature=0.7
122
  )
123
  followup_question = followup_response.choices[0].message.content.strip()
124
+
125
+ # Comment bubble
126
  history.append({"role": "assistant", "content": ""})
127
+ for c in comment:
128
  history[-1]["content"] += c
129
  await asyncio.sleep(0.03)
130
+ # Follow-up question bubble
131
+ history.append({"role": "assistant", "content": ""})
132
+ for c in followup_question:
133
+ history[-1]["content"] += c
134
+ await asyncio.sleep(0.03)
135
+
136
  return history, "", step, language, questions, True
137
  else:
138
+ # No more follow-ups, move to next main question
139
+ next_question = f"{step+1}. {questions[step]}"
140
+ content = comment + "\n\n" + next_question
141
  history.append({"role": "assistant", "content": ""})
142
  for c in content:
143
  history[-1]["content"] += c
144
  await asyncio.sleep(0.03)
145
  return history, "", step + 1, language, questions, False
146
 
147
+ # Handle main question answer
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
  ]
 
158
  comment = comment_response.choices[0].message.content.strip()
159
 
160
  rand = random.random()
161
+ if rand < 0.4:
162
+ # Trigger first follow-up
163
  followup_prompt = [
164
  {"role": "system", "content": f"Ask one open-ended follow-up question in {language} based on the user's answer."},
165
  history[-2],
 
171
  temperature=0.7
172
  )
173
  followup_question = followup_response.choices[0].message.content.strip()
174
+
175
+ # Comment bubble
176
  history.append({"role": "assistant", "content": ""})
177
+ for c in comment:
178
  history[-1]["content"] += c
179
  await asyncio.sleep(0.03)
180
+ # Follow-up question bubble
181
+ history.append({"role": "assistant", "content": ""})
182
+ for c in followup_question:
183
+ history[-1]["content"] += c
184
+ await asyncio.sleep(0.03)
185
+
186
  return history, "", step, language, questions, True
187
  else:
188
+ # No follow-up, move to next question
189
+ next_question = f"{step+1}. {questions[step]}"
190
+ content = comment + "\n\n" + next_question
191
  history.append({"role": "assistant", "content": ""})
192
  for c in content:
193
  history[-1]["content"] += c