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(message, history):
|
| 7 |
+
"""处理用户消息并生成AI响应"""
|
| 8 |
+
|
| 9 |
+
# 预设的智能回复模板
|
| 10 |
+
greetings = ["你好", "您好", "嗨", "hi", "hello", "早上好", "下午好", "晚上好"]
|
| 11 |
+
farewells = ["再见", "拜拜", "bye", "goodbye", "晚安"]
|
| 12 |
+
thanks = ["谢谢", "感谢", "多谢", "thanks", "thank you"]
|
| 13 |
+
|
| 14 |
+
message_lower = message.lower().strip()
|
| 15 |
+
|
| 16 |
+
# 问候语回复
|
| 17 |
+
if any(g in message_lower for g in greetings):
|
| 18 |
+
responses = [
|
| 19 |
+
"你好!很高兴见到你!有什么我可以帮助你的吗?😊",
|
| 20 |
+
"您好!欢迎来到人机互动平台!请问有什么需要?",
|
| 21 |
+
"嗨!今天过得怎么样?有什么想聊的吗?",
|
| 22 |
+
"你好呀!我是你的AI助手,随时为你服务!"
|
| 23 |
+
]
|
| 24 |
+
response = random.choice(responses)
|
| 25 |
+
|
| 26 |
+
# 告别语回复
|
| 27 |
+
elif any(f in message_lower for f in farewells):
|
| 28 |
+
responses = [
|
| 29 |
+
"再见!期待下次与你交流!👋",
|
| 30 |
+
"拜拜!祝你有美好的一天!",
|
| 31 |
+
"再会!有问题随时来找我哦!",
|
| 32 |
+
"晚安!好梦!🌙"
|
| 33 |
+
]
|
| 34 |
+
response = random.choice(responses)
|
| 35 |
+
|
| 36 |
+
# 感谢回复
|
| 37 |
+
elif any(t in message_lower for t in thanks):
|
| 38 |
+
responses = [
|
| 39 |
+
"不客气!能帮到你我很开心!😄",
|
| 40 |
+
"这是我应该做的!还有其他问题吗?",
|
| 41 |
+
"别客气!随时为你效劳!",
|
| 42 |
+
"很高兴能帮到你!"
|
| 43 |
+
]
|
| 44 |
+
response = random.choice(responses)
|
| 45 |
+
|
| 46 |
+
# 询问身份
|
| 47 |
+
elif "你是谁" in message_lower or "你叫什么" in message_lower:
|
| 48 |
+
response = "我是一个AI智能助手,专门为你提供帮助和陪伴。你可以问我问题、和我聊天,或者让我帮你完成一些任务!🤖"
|
| 49 |
+
|
| 50 |
+
# 询问能力
|
| 51 |
+
elif "你能做什么" in message_lower or "你会什么" in message_lower:
|
| 52 |
+
response = """我可以做很多事情呢!比如:
|
| 53 |
+
|
| 54 |
+
🔹 回答你的问题
|
| 55 |
+
🔹 和你进行日常对话
|
| 56 |
+
🔹 提供建议和想法
|
| 57 |
+
🔹 陪你聊天解闷
|
| 58 |
+
🔹 帮你思考问题
|
| 59 |
+
|
| 60 |
+
有什么想让我帮忙的吗?"""
|
| 61 |
+
|
| 62 |
+
# 询问时间
|
| 63 |
+
elif "几点" in message_lower or "时间" in message_lower:
|
| 64 |
+
current_time = time.strftime("%Y年%m月%d日 %H:%M:%S", time.localtime())
|
| 65 |
+
response = f"现在的时间是:{current_time} ⏰"
|
| 66 |
+
|
| 67 |
+
# 天气相关
|
| 68 |
+
elif "天气" in message_lower:
|
| 69 |
+
response = "抱歉,我目前无法获取实时天气信息。建议你查看手机上的天气应用或访问天气网站获取准确信息!🌤️"
|
| 70 |
+
|
| 71 |
+
# 心情相关
|
| 72 |
+
elif "开心" in message_lower or "高兴" in message_lower:
|
| 73 |
+
response = "太棒了!看到你开心我也很高兴!希望你每天都这么快乐!🎉"
|
| 74 |
+
|
| 75 |
+
elif "难过" in message_lower or "伤心" in message_lower or "不开心" in message_lower:
|
| 76 |
+
response = "别难过,一切都会好起来的!如果你愿意,可以和我聊聊发生了什么,我会一直陪着你。💝"
|
| 77 |
+
|
| 78 |
+
# 笑话
|
| 79 |
+
elif "笑话" in message_lower or "搞笑" in message_lower:
|
| 80 |
+
jokes = [
|
| 81 |
+
"为什么程序员总是分不清万圣节和圣诞节?因为 Oct 31 = Dec 25(八进制的31等于十进制的25)😄",
|
| 82 |
+
"一只蜗牛爬上了苹果树,树上的毛毛虫问:你来干嘛?蜗牛说:等苹果熟了我就可以吃到新鲜的了!🐌",
|
| 83 |
+
"老师问小明:1+1等于几?小明说:不知道。老师说:回家问你爸爸。小明回家问爸爸,爸爸正在打麻将,说:二筒!😂"
|
| 84 |
+
]
|
| 85 |
+
response = random.choice(jokes)
|
| 86 |
+
|
| 87 |
+
# 默认回复
|
| 88 |
+
else:
|
| 89 |
+
default_responses = [
|
| 90 |
+
f"你说的'{message}'很有意思!能详细说说吗?🤔",
|
| 91 |
+
f"我理解你说的'{message}',让我想想怎么回应...",
|
| 92 |
+
"这是个好问题!让我来思考一下...",
|
| 93 |
+
"嗯,我在认真听你说呢,请继续!",
|
| 94 |
+
"有趣的观点!你是怎么想到的?",
|
| 95 |
+
"我很喜欢和你交流!还有什么想说的吗?"
|
| 96 |
+
]
|
| 97 |
+
response = random.choice(default_responses)
|
| 98 |
+
|
| 99 |
+
# 模拟打字效果
|
| 100 |
+
for i in range(len(response)):
|
| 101 |
+
time.sleep(0.02)
|
| 102 |
+
yield response[:i+1]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def quick_reply(choice):
|
| 106 |
+
"""快捷回复功能"""
|
| 107 |
+
quick_messages = {
|
| 108 |
+
"👋 打个招呼": "你好!",
|
| 109 |
+
"🤔 你是谁": "你是谁?能介绍一下自己吗?",
|
| 110 |
+
"💡 你能做什么": "你能做什么?有哪些功能?",
|
| 111 |
+
"😄 讲个笑话": "给我讲个笑话吧!",
|
| 112 |
+
"⏰ 现在几点": "现在几点了?",
|
| 113 |
+
"👋 再见": "再见!"
|
| 114 |
+
}
|
| 115 |
+
return quick_messages.get(choice, "")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def clear_history():
|
| 119 |
+
"""清空聊天记录"""
|
| 120 |
+
return [], ""
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def get_stats(history):
|
| 124 |
+
"""获��对话统计"""
|
| 125 |
+
if not history:
|
| 126 |
+
return "📊 暂无对话记录"
|
| 127 |
+
|
| 128 |
+
total_messages = len(history)
|
| 129 |
+
user_messages = sum(1 for msg in history if msg.get("role") == "user")
|
| 130 |
+
ai_messages = sum(1 for msg in history if msg.get("role") == "assistant")
|
| 131 |
+
|
| 132 |
+
return f"""📊 对话统计
|
| 133 |
+
|
| 134 |
+
💬 总消息数: {total_messages}
|
| 135 |
+
👤 用户消息: {user_messages}
|
| 136 |
+
🤖 AI回复: {ai_messages}"""
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# 创建Gradio界面
|
| 140 |
+
with gr.Blocks(
|
| 141 |
+
title="人机互动平台",
|
| 142 |
+
theme=gr.themes.Soft(),
|
| 143 |
+
fill_height=True,
|
| 144 |
+
footer_links=[
|
| 145 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 146 |
+
]
|
| 147 |
+
) as demo:
|
| 148 |
+
|
| 149 |
+
# 头部
|
| 150 |
+
gr.Markdown("""
|
| 151 |
+
# 🤖 人机互动平台
|
| 152 |
+
|
| 153 |
+
欢迎来到智能对话系统!在这里你可以与AI进行自然、流畅的对话交流。
|
| 154 |
+
|
| 155 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #6366f1;">Built with anycoder</a>
|
| 156 |
+
""")
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
# 左侧:聊天区域
|
| 160 |
+
with gr.Column(scale=3):
|
| 161 |
+
chatbot = gr.Chatbot(
|
| 162 |
+
label="💬 对话窗口",
|
| 163 |
+
height=500,
|
| 164 |
+
placeholder="开始和AI聊天吧!输入任何内容即可开始对话...",
|
| 165 |
+
avatar_images=(None, "https://em-content.zobj.net/source/twitter/376/robot_1f916.png"),
|
| 166 |
+
show_copy_button=True,
|
| 167 |
+
render_markdown=True,
|
| 168 |
+
layout="bubble"
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
msg = gr.Textbox(
|
| 173 |
+
label="输入消息",
|
| 174 |
+
placeholder="在这里输入你想说的话...",
|
| 175 |
+
lines=2,
|
| 176 |
+
scale=4,
|
| 177 |
+
submit_btn=True
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
with gr.Row():
|
| 181 |
+
clear_btn = gr.Button("🗑️ 清空对话", variant="secondary", size="sm")
|
| 182 |
+
|
| 183 |
+
# 右侧:功能面板
|
| 184 |
+
with gr.Column(scale=1):
|
| 185 |
+
gr.Markdown("### ⚡ 快捷操作")
|
| 186 |
+
|
| 187 |
+
quick_buttons = gr.Radio(
|
| 188 |
+
choices=[
|
| 189 |
+
"👋 打个招呼",
|
| 190 |
+
"🤔 你是谁",
|
| 191 |
+
"💡 你能做什么",
|
| 192 |
+
"😄 讲个笑话",
|
| 193 |
+
"⏰ 现在几点",
|
| 194 |
+
"👋 再见"
|
| 195 |
+
],
|
| 196 |
+
label="选择快捷消息",
|
| 197 |
+
interactive=True
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
send_quick_btn = gr.Button("📤 发送快捷消息", variant="primary", size="sm")
|
| 201 |
+
|
| 202 |
+
gr.Markdown("---")
|
| 203 |
+
|
| 204 |
+
gr.Markdown("### 📊 对话统计")
|
| 205 |
+
stats_display = gr.Textbox(
|
| 206 |
+
label="",
|
| 207 |
+
value="📊 暂无对话记录",
|
| 208 |
+
lines=5,
|
| 209 |
+
interactive=False
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
refresh_stats_btn = gr.Button("🔄 刷新统计", size="sm")
|
| 213 |
+
|
| 214 |
+
gr.Markdown("---")
|
| 215 |
+
|
| 216 |
+
gr.Markdown("""
|
| 217 |
+
### 💡 使用提示
|
| 218 |
+
|
| 219 |
+
- 💬 直接输入文字开始对话
|
| 220 |
+
- ⚡ 使用快捷按钮快速发送
|
| 221 |
+
- 🗑️ 随时清空重新开始
|
| 222 |
+
- 📊 查看对话统计信息
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
### 🎯 试试这些话题
|
| 227 |
+
|
| 228 |
+
- 问我今天的时间
|
| 229 |
+
- 让我讲个笑话
|
| 230 |
+
- 和我分享你的心情
|
| 231 |
+
- 询问我能做什么
|
| 232 |
+
""")
|
| 233 |
+
|
| 234 |
+
# 底部说明
|
| 235 |
+
gr.Markdown("""
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
<center>
|
| 239 |
+
|
| 240 |
+
🌟 **人机互动平台** - 让AI成为你的智能伙伴
|
| 241 |
+
|
| 242 |
+
支持自然语言对话 | 实时响应 | 友好交互
|
| 243 |
+
|
| 244 |
+
</center>
|
| 245 |
+
""")
|
| 246 |
+
|
| 247 |
+
# 事件绑定
|
| 248 |
+
|
| 249 |
+
# 发送消息
|
| 250 |
+
msg.submit(
|
| 251 |
+
fn=chat_response,
|
| 252 |
+
inputs=[msg, chatbot],
|
| 253 |
+
outputs=chatbot,
|
| 254 |
+
api_visibility="public"
|
| 255 |
+
).then(
|
| 256 |
+
fn=lambda: "",
|
| 257 |
+
outputs=msg
|
| 258 |
+
).then(
|
| 259 |
+
fn=get_stats,
|
| 260 |
+
inputs=chatbot,
|
| 261 |
+
outputs=stats_display
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
# 快捷消息选择
|
| 265 |
+
def send_quick_message(choice, history):
|
| 266 |
+
if not choice:
|
| 267 |
+
return history, ""
|
| 268 |
+
message = quick_reply(choice)
|
| 269 |
+
return history, message
|
| 270 |
+
|
| 271 |
+
send_quick_btn.click(
|
| 272 |
+
fn=send_quick_message,
|
| 273 |
+
inputs=[quick_buttons, chatbot],
|
| 274 |
+
outputs=[chatbot, msg],
|
| 275 |
+
api_visibility="private"
|
| 276 |
+
).then(
|
| 277 |
+
fn=chat_response,
|
| 278 |
+
inputs=[msg, chatbot],
|
| 279 |
+
outputs=chatbot
|
| 280 |
+
).then(
|
| 281 |
+
fn=lambda: ("", None),
|
| 282 |
+
outputs=[msg, quick_buttons]
|
| 283 |
+
).then(
|
| 284 |
+
fn=get_stats,
|
| 285 |
+
inputs=chatbot,
|
| 286 |
+
outputs=stats_display
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
# 清空对话
|
| 290 |
+
clear_btn.click(
|
| 291 |
+
fn=clear_history,
|
| 292 |
+
outputs=[chatbot, msg],
|
| 293 |
+
api_visibility="private"
|
| 294 |
+
).then(
|
| 295 |
+
fn=get_stats,
|
| 296 |
+
inputs=chatbot,
|
| 297 |
+
outputs=stats_display
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
# 刷新统计
|
| 301 |
+
refresh_stats_btn.click(
|
| 302 |
+
fn=get_stats,
|
| 303 |
+
inputs=chatbot,
|
| 304 |
+
outputs=stats_display,
|
| 305 |
+
api_visibility="private"
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
# 启动应用
|
| 310 |
+
if __name__ == "__main__":
|
| 311 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|
| 4 |
+
numpy
|
| 5 |
+
pandas
|
| 6 |
+
matplotlib
|
| 7 |
+
plotly
|
| 8 |
+
fastapi
|
| 9 |
+
uvicorn
|
| 10 |
+
pydantic
|