Wanswan commited on
Commit
5d97117
·
verified ·
1 Parent(s): 2c35a84

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -10,15 +10,15 @@ DEFAULT_LANGUAGE = "English"
10
  WELCOME_MESSAGE_EN = "Hi! I'm your friendly assistant 🤖 Let's begin!\n\nPlease tell me which language you'd like to use."
11
 
12
  QUESTION_TEMPLATES = [
13
- "Given the choice of anyone in the world, whom would you want as a dinner guest.",
14
- "Would you like to be famous? In what way?",
15
- "Before making a telephone call, do you ever rehearse what you are going to say? Why?",
16
- "If a crystal ball could tell you the truth about yourself, your life, the future or anything else, what would you want to know?",
17
- "What do you value most in a friendship?",
18
- "What is your most terrible memory?",
19
  "What improvements would you like to see in future shopping experiences?",
20
- "If you were going to become a close friend with your partner, please share what would be important for him or her to know.",
21
- "When did you last cry in front of another person? By yourself?"
22
  ]
23
 
24
  BOT_COMMENTS_EN = [
@@ -28,6 +28,12 @@ BOT_COMMENTS_EN = [
28
  "That’s so real—I get what you mean!"
29
  ]
30
 
 
 
 
 
 
 
31
  async def gpt_translate(text, target_lang):
32
  if target_lang == "English":
33
  return text
@@ -39,29 +45,30 @@ async def gpt_translate(text, target_lang):
39
  )
40
  return response.choices[0].message.content.strip()
41
 
42
- async def respond(user_input, chat_history, step, language, questions):
43
  if language == "":
44
  user_language = user_input.strip().capitalize()
45
  translated_questions = [await gpt_translate(q, user_language) for q in QUESTION_TEMPLATES]
46
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
47
- chat_history.append((user_input, None))
48
- chat_history.append((None, f"{welcome}\n\n1. {translated_questions[0]}"))
49
- return chat_history, "", 1, user_language, translated_questions
50
 
51
  if step >= len(questions):
 
52
  thank_you = await gpt_translate("Thank you for your response!", language)
53
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
54
- chat_history.append((user_input, None))
55
- chat_history.append((None, thank_you))
56
- chat_history.append((None, end_note))
57
- return chat_history, "", step, language, questions
58
 
59
- chat_history.append((user_input, None))
60
 
61
- # GPT 正式回应(+ 强制分气泡)
62
  messages = [
63
  {"role": "system", "content": f"You are a friendly assistant. Provide a short, positive comment on the user's answer in {language}, and include a light emoji. Do not ask a new question."},
64
- {"role": "user", "content": user_input}
 
65
  ]
66
  response = client.chat.completions.create(
67
  model="gpt-3.5-turbo",
@@ -69,25 +76,23 @@ async def respond(user_input, chat_history, step, language, questions):
69
  temperature=0.7
70
  )
71
  bot_reply = response.choices[0].message.content.strip()
72
- await asyncio.sleep(0.3)
73
- chat_history.append((None, bot_reply))
74
- chat_history.append((" ", None)) # 用空白用户消息打断气泡
 
75
 
76
  if random.random() < 0.4:
77
  extra_comment = await gpt_translate(random.choice(BOT_COMMENTS_EN), language)
78
- await asyncio.sleep(0.3)
79
- chat_history.append((None, f"🤖 {extra_comment}"))
80
- chat_history.append((" ", None)) # 再插入打断
81
-
82
- await asyncio.sleep(0.3)
83
- next_question = f"{step+1}. {questions[step]}"
84
- chat_history.append((None, next_question))
85
 
 
 
86
  step += 1
87
- return chat_history, "", step, language, questions
88
 
89
  def init():
90
- return [], 0, "", []
91
 
92
  with gr.Blocks(css="""
93
  #chatbox .message.user {
@@ -147,7 +152,7 @@ with gr.Blocks(css="""
147
  bubble_full_width=False,
148
  height=500,
149
  show_copy_button=False,
150
- type="tuples"
151
  )
152
 
153
  with gr.Row():
 
10
  WELCOME_MESSAGE_EN = "Hi! I'm your friendly assistant 🤖 Let's begin!\n\nPlease tell me which language you'd like to use."
11
 
12
  QUESTION_TEMPLATES = [
13
+ "Please share a memorable shopping experience you had recently.",
14
+ "What factors do you value most when shopping? (e.g., price, quality, service)",
15
+ "How do promotions influence your buying behavior?",
16
+ "Have you ever purchased something because of a recommendation on social media?",
17
+ "Do you prefer online or in-store shopping? Why?",
18
+ "Have you used an AI shopping assistant before? Was it helpful?",
19
  "What improvements would you like to see in future shopping experiences?",
20
+ "Have you ever encountered shopping traps or unpleasant experiences?",
21
+ "Describe your ideal shopping experience in three words."
22
  ]
23
 
24
  BOT_COMMENTS_EN = [
 
28
  "That’s so real—I get what you mean!"
29
  ]
30
 
31
+ BOT_NAME_PREFIX = {
32
+ "English": "🤖 Assistant:",
33
+ "中文": "🤖 小秘书:",
34
+ "Deutsch": "🤖 Assistent:"
35
+ }
36
+
37
  async def gpt_translate(text, target_lang):
38
  if target_lang == "English":
39
  return text
 
45
  )
46
  return response.choices[0].message.content.strip()
47
 
48
+ async def respond(user_input, history, step, language, questions):
49
  if language == "":
50
  user_language = user_input.strip().capitalize()
51
  translated_questions = [await gpt_translate(q, user_language) for q in QUESTION_TEMPLATES]
52
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
53
+ history.append({"role": "user", "content": user_input})
54
+ history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"} )
55
+ return history, "", 1, user_language, translated_questions
56
 
57
  if step >= len(questions):
58
+ history.append({"role": "user", "content": user_input})
59
  thank_you = await gpt_translate("Thank you for your response!", language)
60
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
61
+ history.append({"role": "assistant", "content": thank_you})
62
+ history.append({"role": "assistant", "content": end_note})
63
+ return history, "", step, language, questions
 
64
 
65
+ history.append({"role": "user", "content": user_input})
66
 
67
+ # GPT 主回答
68
  messages = [
69
  {"role": "system", "content": f"You are a friendly assistant. Provide a short, positive comment on the user's answer in {language}, and include a light emoji. Do not ask a new question."},
70
+ history[-2],
71
+ history[-1]
72
  ]
73
  response = client.chat.completions.create(
74
  model="gpt-3.5-turbo",
 
76
  temperature=0.7
77
  )
78
  bot_reply = response.choices[0].message.content.strip()
79
+ history.append({"role": "assistant", "content": bot_reply})
80
+
81
+ # ✅ 插入空白用户消息用于“断开”气泡
82
+ history.append({"role": "user", "content": " "})
83
 
84
  if random.random() < 0.4:
85
  extra_comment = await gpt_translate(random.choice(BOT_COMMENTS_EN), language)
86
+ history.append({"role": "assistant", "content": f"{BOT_NAME_PREFIX.get(language, '🤖')}{extra_comment}"})
87
+ history.append({"role": "user", "content": " "})
 
 
 
 
 
88
 
89
+ # 下一题
90
+ history.append({"role": "assistant", "content": f"{step+1}. {questions[step]}"})
91
  step += 1
92
+ return history, "", step, language, questions
93
 
94
  def init():
95
+ return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", []
96
 
97
  with gr.Blocks(css="""
98
  #chatbox .message.user {
 
152
  bubble_full_width=False,
153
  height=500,
154
  show_copy_button=False,
155
+ type="messages"
156
  )
157
 
158
  with gr.Row():