Wanswan commited on
Commit
282bef1
·
verified ·
1 Parent(s): fbd43ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -230
app.py CHANGED
@@ -56,7 +56,7 @@ SET_III = [
56
  ]
57
 
58
  def generate_question_set():
59
- return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
60
 
61
  async def gpt_translate(text, target_lang):
62
  if target_lang == "English":
@@ -71,7 +71,6 @@ async def gpt_translate(text, target_lang):
71
 
72
  async def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
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()
@@ -88,244 +87,54 @@ async def respond(user_input, history, step, language, questions, followup_mode,
88
  return history, "", step, language, questions, False, 0
89
 
90
  if followup_mode:
91
- # bot 回复用户的追问回答
92
  comment_prompt = [
93
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
94
  history[-2],
95
  history[-1]
96
  ]
97
- comment_response = client.chat.completions.create(
98
- model="gpt-3.5-turbo",
99
- messages=comment_prompt,
100
- temperature=0.7
101
- )
102
  comment = comment_response.choices[0].message.content.strip()
103
-
104
- history.append({"role": "assistant", "content": ""})
105
- for c in comment:
106
- history[-1]["content"] += c
107
- await asyncio.sleep(0.03)
108
-
109
- if followup_stage == 1:
110
- # 可能还有第2次追问
111
- if random.random() < 0.1:
112
- followup_prompt = [
113
- {"role": "system", "content": f"Ask 1 more follow-up question based on the user's last answer in {language}. Do NOT number it."},
114
- history[-2],
115
- history[-1]
116
- ]
117
- followup_response = client.chat.completions.create(
118
- model="gpt-3.5-turbo",
119
- messages=followup_prompt,
120
- temperature=0.7
121
- )
122
- followup_q = followup_response.choices[0].message.content.strip()
123
- history.append({"role": "assistant", "content": ""})
124
- for c in followup_q:
125
- history[-1]["content"] += c
126
- await asyncio.sleep(0.03)
127
- return history, "", step, language, questions, True, 2
128
- else:
129
- # 无第二次追问,进入下一题
130
- next_question = f"{step+1}. {questions[step]}"
131
- history.append({"role": "assistant", "content": ""})
132
- for c in next_question:
133
- history[-1]["content"] += c
134
- await asyncio.sleep(0.03)
135
- return history, "", step + 1, language, questions, False, 0
136
-
137
- elif followup_stage == 2:
138
- # 回答完第二次追问,直接进入下一主问题
139
  next_question = f"{step+1}. {questions[step]}"
140
- history.append({"role": "assistant", "content": ""})
141
- for c in next_question:
142
- history[-1]["content"] += c
143
- await asyncio.sleep(0.03)
144
  return history, "", step + 1, language, questions, False, 0
145
 
146
- # 正常主问题回答逻辑
147
  comment_prompt = [
148
- {"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. Just write them as natural, standalone questions. Write a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
149
  history[-2],
150
  history[-1]
151
  ]
152
- comment_response = client.chat.completions.create(
153
- model="gpt-3.5-turbo",
154
- messages=comment_prompt,
155
- temperature=0.8
156
- )
157
  bot_reply = comment_response.choices[0].message.content.strip()
158
 
159
- followup_count = 0
160
- rand = random.random()
161
- if rand < 0.1:
162
- followup_count = 2
163
- elif rand < 0.5:
164
- followup_count = 1
165
-
166
  if followup_count > 0:
167
- followup_prompt = [
168
- {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}. Do NOT number them."},
169
- history[-2],
170
- history[-1]
171
- ]
172
- followup_response = client.chat.completions.create(
173
- model="gpt-3.5-turbo",
174
- messages=followup_prompt,
175
- temperature=0.7
176
- )
177
  bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
178
-
179
- history.append({"role": "assistant", "content": ""})
180
- for c in bot_reply:
181
- history[-1]["content"] += c
182
- await asyncio.sleep(0.03)
183
  return history, "", step, language, questions, True, 1
184
 
185
- # 无追问,直接进入下一题
186
- history.append({"role": "assistant", "content": ""})
187
- for c in bot_reply:
188
- history[-1]["content"] += c
189
- await asyncio.sleep(0.03)
190
-
191
  next_question = f"{step+1}. {questions[step]}"
192
- history.append({"role": "assistant", "content": ""})
193
- for c in next_question:
194
- history[-1]["content"] += c
195
- await asyncio.sleep(0.03)
196
  return history, "", step + 1, language, questions, False, 0
197
 
198
- # === 正常主问题回答流程 ===
199
- comment_prompt = [
200
- {"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. Just write them as natural, standalone questions. Write a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
201
- history[-2],
202
- history[-1]
203
- ]
204
- comment_response = client.chat.completions.create(
205
- model="gpt-3.5-turbo",
206
- messages=comment_prompt,
207
- temperature=0.8
208
- )
209
- bot_reply = comment_response.choices[0].message.content.strip()
210
-
211
- followup_count = 0
212
- rand = random.random()
213
- if rand < 0.1:
214
- followup_count = 2
215
- elif rand < 0.5:
216
- followup_count = 1
217
-
218
- if followup_count > 0:
219
- followup_prompt = [
220
- {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}. Do NOT number them."},
221
- history[-2],
222
- history[-1]
223
- ]
224
- followup_response = client.chat.completions.create(
225
- model="gpt-3.5-turbo",
226
- messages=followup_prompt,
227
- temperature=0.7
228
- )
229
- bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
230
- history.append({"role": "assistant", "content": ""})
231
- for c in bot_reply:
232
- history[-1]["content"] += c
233
- await asyncio.sleep(0.03)
234
- return history, "", step, language, questions, True
235
-
236
- history.append({"role": "assistant", "content": ""})
237
- for c in bot_reply:
238
- history[-1]["content"] += c
239
- await asyncio.sleep(0.03)
240
-
241
- next_question = f"{step+1}. {questions[step]}"
242
- history.append({"role": "assistant", "content": ""})
243
- for c in next_question:
244
- history[-1]["content"] += c
245
- await asyncio.sleep(0.03)
246
- return history, "", step + 1, language, questions, False
247
-
248
-
249
-
250
  def init():
251
- return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
252
-
253
- with gr.Blocks(css="""
254
- .send-btn {
255
- background-color: #25D366 !important;
256
- color: white !important;
257
- border: none;
258
- border-radius: 24px;
259
- font-size: 20px;
260
- padding: 8px 20px;
261
- height: 48px;
262
- width: 60px;
263
- margin-left: 8px;
264
- cursor: pointer;
265
- }
266
- #chatbox .avatar-container {
267
- width: 64px !important;
268
- height: 64px !important;
269
- min-width: 64px !important;
270
- min-height: 64px !important;
271
- background-size: 100% 100% !important;
272
- background-position: center center !important;
273
- border-radius: 50% !important;
274
- padding: 0 !important;
275
- margin: 0 !important;
276
- border: none !important;
277
- box-shadow: none !important;
278
- background-color: transparent !important;
279
- }
280
- #chatbox .message.user { background-color: #DCF8C6 !important; }
281
- #chatbox .message.assistant { background-color: #ffffff !important; }
282
- footer { display: none !important; }
283
- .svelte-1ipelgc { display: none !important; }
284
-
285
- .icon-button-wrapper {
286
- display: none !important;
287
- }
288
-
289
- label.svelte-1to105q {
290
- display: none !important;
291
- }
292
-
293
-
294
- """) as demo:
295
- gr.HTML("""
296
- <script>
297
- const waitForChatbox = () => {
298
- const chatbox = document.getElementById("chatbox");
299
- if (chatbox) {
300
- const observer = new MutationObserver(() => {
301
- chatbox.scrollTop = chatbox.scrollHeight;
302
- });
303
- observer.observe(chatbox, { childList: true, subtree: true });
304
- } else {
305
- setTimeout(waitForChatbox, 500);
306
- }
307
- };
308
- waitForChatbox();
309
- </script>
310
- """)
311
-
312
- chatbot = gr.Chatbot(
313
- elem_id="chatbox",
314
- label="",
315
- avatar_images=["user_avatar.jpg", "humanlike_avatar.png"],
316
- bubble_full_width=False,
317
- height=500,
318
- show_copy_button=False,
319
- type="messages"
320
- )
321
 
 
 
322
  with gr.Row():
323
- user_input = gr.Textbox(
324
- show_label=False,
325
- placeholder="Type your message here and press send...",
326
- container=True,
327
- scale=10
328
- )
329
  send_btn = gr.Button(value="➤", elem_classes="send-btn")
330
 
331
  state = gr.State([])
@@ -335,19 +144,9 @@ label.svelte-1to105q {
335
  followup_mode = gr.State(False)
336
  followup_stage = gr.State(0)
337
 
338
-
339
- demo.load(fn=init, outputs=[chatbot, step, language, questions, followup_mode, followup_stage])
340
  demo.load(fn=init, outputs=[chatbot, step, language, questions, followup_mode, followup_stage])
341
- user_input.submit(
342
- respond,
343
- [user_input, state, step, language, questions, followup_mode, followup_stage],
344
- [chatbot, user_input, step, language, questions, followup_mode, followup_stage]
345
- )
346
-
347
- send_btn.click(
348
- respond,
349
- [user_input, state, step, language, questions, followup_mode, followup_stage],
350
- [chatbot, user_input, step, language, questions, followup_mode, followup_stage]
351
- )
352
-
353
- demo.queue().launch()
 
56
  ]
57
 
58
  def generate_question_set():
59
+ return random.sample(SET_I, 1) + random.sample(SET_II, 1) + random.sample(SET_III, 1)
60
 
61
  async def gpt_translate(text, target_lang):
62
  if target_lang == "English":
 
71
 
72
  async def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
73
  history.append({"role": "user", "content": user_input})
 
74
  if language == "":
75
  user_language = user_input.strip().capitalize()
76
  questions_en = generate_question_set()
 
87
  return history, "", step, language, questions, False, 0
88
 
89
  if followup_mode:
 
90
  comment_prompt = [
91
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
92
  history[-2],
93
  history[-1]
94
  ]
95
+ comment_response = client.chat.completions.create(model="gpt-3.5-turbo", messages=comment_prompt, temperature=0.7)
 
 
 
 
96
  comment = comment_response.choices[0].message.content.strip()
97
+ history.append({"role": "assistant", "content": comment})
98
+
99
+ if followup_stage == 1 and random.random() < 0.1:
100
+ followup_prompt = [{"role": "system", "content": f"Ask 1 more follow-up question in {language}."}, history[-2], history[-1]]
101
+ followup_response = client.chat.completions.create(model="gpt-3.5-turbo", messages=followup_prompt, temperature=0.7)
102
+ followup_q = followup_response.choices[0].message.content.strip()
103
+ history.append({"role": "assistant", "content": followup_q})
104
+ return history, "", step, language, questions, True, 2
105
+ else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  next_question = f"{step+1}. {questions[step]}"
107
+ history.append({"role": "assistant", "content": next_question})
 
 
 
108
  return history, "", step + 1, language, questions, False, 0
109
 
 
110
  comment_prompt = [
111
+ {"role": "system", "content": f"You are Robin, a warm and engaging conversational AI. Write 1–2 follow-up questions. Also comment briefly in {language}."},
112
  history[-2],
113
  history[-1]
114
  ]
115
+ comment_response = client.chat.completions.create(model="gpt-3.5-turbo", messages=comment_prompt, temperature=0.8)
 
 
 
 
116
  bot_reply = comment_response.choices[0].message.content.strip()
117
 
118
+ followup_count = 2 if random.random() < 0.1 else 1 if random.random() < 0.5 else 0
 
 
 
 
 
 
119
  if followup_count > 0:
120
+ followup_prompt = [{"role": "system", "content": f"Ask {followup_count} follow-up question(s) in {language}."}, history[-2], history[-1]]
121
+ followup_response = client.chat.completions.create(model="gpt-3.5-turbo", messages=followup_prompt, temperature=0.7)
 
 
 
 
 
 
 
 
122
  bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
123
+ history.append({"role": "assistant", "content": bot_reply})
 
 
 
 
124
  return history, "", step, language, questions, True, 1
125
 
126
+ history.append({"role": "assistant", "content": bot_reply})
 
 
 
 
 
127
  next_question = f"{step+1}. {questions[step]}"
128
+ history.append({"role": "assistant", "content": next_question})
 
 
 
129
  return history, "", step + 1, language, questions, False, 0
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  def init():
132
+ return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False, 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ with gr.Blocks() as demo:
135
+ chatbot = gr.Chatbot(label="", avatar_images=["user_avatar.jpg", "humanlike_avatar.png"], bubble_full_width=False, height=500)
136
  with gr.Row():
137
+ user_input = gr.Textbox(show_label=False, placeholder="Type your message here...", container=True, scale=10)
 
 
 
 
 
138
  send_btn = gr.Button(value="➤", elem_classes="send-btn")
139
 
140
  state = gr.State([])
 
144
  followup_mode = gr.State(False)
145
  followup_stage = gr.State(0)
146
 
 
 
147
  demo.load(fn=init, outputs=[chatbot, step, language, questions, followup_mode, followup_stage])
148
+ user_input.submit(respond, [user_input, state, step, language, questions, followup_mode, followup_stage],
149
+ [chatbot, user_input, step, language, questions, followup_mode, followup_stage])
150
+ send_btn.click(respond, [user_input, state, step, language, questions, followup_mode, followup_stage],
151
+ [chatbot, user_input, step, language, questions, followup_mode, followup_stage])
152
+ demo.queue().launch()