Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +79 -0
- bot_avatar.png +0 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import random
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
QUESTIONS = [
|
| 9 |
+
"请分享您最近一次难忘的购物经历",
|
| 10 |
+
"购物中您最看重哪些因素?(比如价格、品质、服务等)",
|
| 11 |
+
"您如何看待促销活动对您购买行为的影响?",
|
| 12 |
+
"您是否曾因为社交媒体上的推荐而下单?",
|
| 13 |
+
"您喜欢在线购物还是实体店购物?为什么?",
|
| 14 |
+
"购物过程中,您有没有使用过AI购物助手?觉得有帮助吗?",
|
| 15 |
+
"在未来,您希望购物体验有哪些改进?",
|
| 16 |
+
"您是否遇到过购物陷阱或不愉快的消费体验?",
|
| 17 |
+
"请用三个词形容理想的购物体验"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
EMOJIS = ["👍", "❤️", "👏", "😄", "😢", "😂", "🥺", "💡", "🔥"]
|
| 21 |
+
BOT_COMMENTS = [
|
| 22 |
+
"我也有类似的经历,真的很有共鸣!",
|
| 23 |
+
"听起来很有意思,我可能也会这么选。",
|
| 24 |
+
"我曾经也遇到过类似的情况呢~",
|
| 25 |
+
"太真实了,我懂你的感觉!"
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
def respond(user_input, chat_history, step):
|
| 29 |
+
if step >= len(QUESTIONS):
|
| 30 |
+
return chat_history + [
|
| 31 |
+
{"role": "user", "content": user_input},
|
| 32 |
+
{"role": "assistant", "content": random.choice(EMOJIS) + " 感谢您的精彩回答!"},
|
| 33 |
+
{"role": "assistant", "content": "我们的问题已全部结束,感谢您的分享与陪伴!📝\n👉 请点击页面右下角的蓝色箭头继续填写问卷!"}
|
| 34 |
+
], "", step, chat_history
|
| 35 |
+
|
| 36 |
+
messages = [
|
| 37 |
+
{"role": "system", "content": "你是一位友善的调查助手,请用一句积极话语评价用户刚才的回答,并附带一个轻松的表情符号,不要提出新问题。"},
|
| 38 |
+
{"role": "user", "content": user_input}
|
| 39 |
+
]
|
| 40 |
+
response = openai.ChatCompletion.create(
|
| 41 |
+
model="gpt-3.5-turbo",
|
| 42 |
+
messages=messages,
|
| 43 |
+
temperature=0.7
|
| 44 |
+
)
|
| 45 |
+
comment = response.choices[0].message["content"]
|
| 46 |
+
bot_thought = ""
|
| 47 |
+
if random.random() < 0.4:
|
| 48 |
+
bot_thought = "\n🤖 小秘书:" + random.choice(BOT_COMMENTS)
|
| 49 |
+
next_q = QUESTIONS[step]
|
| 50 |
+
|
| 51 |
+
updated_history = chat_history + [
|
| 52 |
+
{"role": "user", "content": user_input},
|
| 53 |
+
{"role": "assistant", "content": comment + bot_thought},
|
| 54 |
+
{"role": "assistant", "content": f"{step+1}. {next_q}"}
|
| 55 |
+
]
|
| 56 |
+
return updated_history, "", step + 1, updated_history
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(title="SwansGPTBot") as demo:
|
| 59 |
+
gr.Markdown("### 🛍️ 调查小秘书")
|
| 60 |
+
chatbot = gr.Chatbot(
|
| 61 |
+
avatar_images=[None, "bot_avatar.png"],
|
| 62 |
+
show_label=False,
|
| 63 |
+
height=500,
|
| 64 |
+
layout="bubble",
|
| 65 |
+
type="messages"
|
| 66 |
+
)
|
| 67 |
+
user_input = gr.Textbox(placeholder="请输入内容后点击发送...", show_label=False)
|
| 68 |
+
state = gr.State([])
|
| 69 |
+
step = gr.State(0)
|
| 70 |
+
|
| 71 |
+
def init_chat():
|
| 72 |
+
return [{"role": "assistant", "content": f"你好!我是小秘书 🤖 很高兴见到你!我们开始吧~\n\n1. {QUESTIONS[0]}"}], 1
|
| 73 |
+
|
| 74 |
+
demo.load(fn=init_chat, outputs=[chatbot, step])
|
| 75 |
+
user_input.submit(respond, [user_input, state, step], [chatbot, user_input, step, state])
|
| 76 |
+
user_input.change(lambda _: "", None, user_input)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.queue().launch(debug=True)
|
bot_avatar.png
ADDED
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[oauth,mcp]==5.29.1
|
| 2 |
+
openai
|