Wanswan commited on
Commit
24590e9
·
verified ·
1 Parent(s): 20aedc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -87
app.py CHANGED
@@ -55,16 +55,6 @@ SET_III = [
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
56
  ]
57
 
58
- BASE_TIRE_KEYWORDS = [
59
- "you're asking too much",
60
- "stop asking",
61
- "i don't want to answer",
62
- "enough",
63
- "annoying",
64
- "leave me alone",
65
- "i'm tired of this"
66
- ]
67
-
68
  def generate_question_set():
69
  return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
70
 
@@ -79,52 +69,29 @@ async def gpt_translate(text, target_lang):
79
  )
80
  return response.choices[0].message.content.strip()
81
 
82
- async def get_tired_keywords_in_lang(language: str):
83
- if language == "English":
84
- return [kw.lower() for kw in BASE_TIRE_KEYWORDS]
85
- prompt = f"Translate the following expressions into {language}, separated by semicolons:\n" + "; ".join(BASE_TIRE_KEYWORDS)
86
- response = client.chat.completions.create(
87
- model="gpt-3.5-turbo",
88
- messages=[{"role": "user", "content": prompt}],
89
- temperature=0.3
90
- )
91
- translated_text = response.choices[0].message.content.strip()
92
- return [kw.strip().lower() for kw in translated_text.split(";")]
93
-
94
- async def user_is_tired(user_input: str, language: str) -> bool:
95
- keywords = await get_tired_keywords_in_lang(language)
96
- user_input_lower = user_input.lower()
97
- return any(kw in user_input_lower for kw in keywords)
98
-
99
- async def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
100
  history.append({"role": "user", "content": user_input})
101
-
102
  if language == "":
103
  user_language = user_input.strip().capitalize()
104
  questions_en = generate_question_set()
105
  translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
106
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
107
- history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"} )
108
- return history, "", 1, user_language, translated_questions, False, 0
109
 
110
  if step >= len(questions):
111
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
112
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
113
  history.append({"role": "assistant", "content": thank_you})
114
  history.append({"role": "assistant", "content": end_note})
115
- return history, "", step, language, questions, False, 0
116
 
117
  if followup_mode:
118
- if await user_is_tired(user_input, language):
119
- skip_comment = await gpt_translate("了解了,我们来继续聊下一个问题:", language)
120
- next_question = f"{step+1}. {questions[step]}"
121
- combined = f"{skip_comment}\n\n{next_question}"
122
- history.append({"role": "assistant", "content": combined})
123
- return history, "", step + 1, language, questions, False, 0
124
-
125
  comment_prompt = [
126
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
127
- history[-2], history[-1]
 
128
  ]
129
  comment_response = client.chat.completions.create(
130
  model="gpt-3.5-turbo",
@@ -132,35 +99,39 @@ async def respond(user_input, history, step, language, questions, followup_mode,
132
  temperature=0.7
133
  )
134
  comment = comment_response.choices[0].message.content.strip()
135
- history.append({"role": "assistant", "content": comment})
136
-
137
- if followup_stage == 1:
138
- if random.random() < 0.1:
139
- followup_prompt = [
140
- {"role": "system", "content": f"Ask 1 follow-up question in {language}. Do NOT number it."},
141
- history[-2], history[-1]
142
- ]
143
- followup_response = client.chat.completions.create(
144
- model="gpt-3.5-turbo",
145
- messages=followup_prompt,
146
- temperature=0.7
147
- )
148
- followup_q = followup_response.choices[0].message.content.strip()
149
- history.append({"role": "assistant", "content": followup_q})
150
- return history, "", step, language, questions, True, 2
151
- else:
152
- next_question = f"{step+1}. {questions[step]}"
153
- history.append({"role": "assistant", "content": next_question})
154
- return history, "", step + 1, language, questions, False, 0
155
 
156
- elif followup_stage == 2:
157
- next_question = f"{step+1}. {questions[step]}"
158
- history.append({"role": "assistant", "content": next_question})
159
- return history, "", step + 1, language, questions, False, 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  comment_prompt = [
162
- {"role": "system", "content": f"Write a short, warm comment in {language} responding to the user's last answer."},
163
- history[-2], history[-1]
 
164
  ]
165
  comment_response = client.chat.completions.create(
166
  model="gpt-3.5-turbo",
@@ -169,39 +140,38 @@ async def respond(user_input, history, step, language, questions, followup_mode,
169
  )
170
  comment = comment_response.choices[0].message.content.strip()
171
 
172
- followup_count = 0
173
  rand = random.random()
174
- if rand < 0.1:
175
- followup_count = 2
176
- elif rand < 0.5:
177
- followup_count = 1
178
-
179
- if await user_is_tired(user_input, language):
180
- followup_count = 0
181
-
182
- if followup_count > 0:
183
  followup_prompt = [
184
- {"role": "system", "content": f"Ask 1 follow-up question in {language}. Do NOT number it."},
185
- history[-2], history[-1]
 
186
  ]
187
  followup_response = client.chat.completions.create(
188
  model="gpt-3.5-turbo",
189
  messages=followup_prompt,
190
  temperature=0.7
191
  )
192
- followup_q = followup_response.choices[0].message.content.strip()
193
- history.append({"role": "assistant", "content": comment})
194
- history.append({"role": "assistant", "content": followup_q})
195
- return history, "", step, language, questions, True, 1
196
-
197
- history.append({"role": "assistant", "content": comment})
198
- next_question = f"{step+1}. {questions[step]}"
199
- history.append({"role": "assistant", "content": next_question})
200
- return history, "", step + 1, language, questions, False, 0
 
 
 
 
 
 
201
 
202
  def init():
203
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
204
 
 
205
  with gr.Blocks(css="""
206
  .send-btn {
207
  background-color: #25D366 !important;
 
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
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
 
 
69
  )
70
  return response.choices[0].message.content.strip()
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."},
93
+ history[-2],
94
+ history[-1]
95
  ]
96
  comment_response = client.chat.completions.create(
97
  model="gpt-3.5-turbo",
 
99
  temperature=0.7
100
  )
101
  comment = comment_response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]
110
+ followup_response = client.chat.completions.create(
111
+ model="gpt-3.5-turbo",
112
+ messages=followup_prompt,
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
  ]
136
  comment_response = client.chat.completions.create(
137
  model="gpt-3.5-turbo",
 
140
  )
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],
148
+ history[-1]
149
  ]
150
  followup_response = client.chat.completions.create(
151
  model="gpt-3.5-turbo",
152
  messages=followup_prompt,
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
168
+ await asyncio.sleep(0.03)
169
+ return history, "", step + 1, language, questions, False
170
 
171
  def init():
172
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
173
 
174
+
175
  with gr.Blocks(css="""
176
  .send-btn {
177
  background-color: #25D366 !important;