Spaces:
Running
on
Zero
Running
on
Zero
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -246,40 +246,65 @@ def _format_user_input_for_chatbot(text, files):
|
|
| 246 |
|
| 247 |
@gpu_decorator
|
| 248 |
def respond(user_msg, history, temp, tokens):
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
files = user_msg.get("files", [])
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
#
|
| 256 |
-
history.append([user_display, None])
|
| 257 |
-
# 此时先 yield 一次,让用户看到自己的输入
|
| 258 |
yield history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 259 |
|
|
|
|
| 260 |
try:
|
| 261 |
-
# 将旧格式的 history 转换为新格式的 messages,用于 predict 函数
|
| 262 |
-
previous_history = _convert_history_to_messages_format(history[:-1])
|
| 263 |
-
|
| 264 |
-
# 在这里调用 handler,此时我们在 @gpu_decorator 的保护下,可以访问 GPU
|
| 265 |
handler = get_model_handler()
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
yield history, gr.MultimodalTextbox(interactive=False)
|
| 275 |
|
| 276 |
except Exception as e:
|
| 277 |
import traceback
|
| 278 |
traceback.print_exc()
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
yield history, gr.MultimodalTextbox(interactive=True)
|
| 281 |
|
| 282 |
-
|
|
|
|
| 283 |
|
| 284 |
def create_chat_ui():
|
| 285 |
custom_css = """
|
|
@@ -298,7 +323,8 @@ def create_chat_ui():
|
|
| 298 |
elem_id="chatbot",
|
| 299 |
label="Chat",
|
| 300 |
avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
|
| 301 |
-
height=650
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
chat_input = gr.MultimodalTextbox(
|
|
|
|
| 246 |
|
| 247 |
@gpu_decorator
|
| 248 |
def respond(user_msg, history, temp, tokens):
|
| 249 |
+
"""
|
| 250 |
+
user_msg: Gradio MultimodalTextbox 返回的字典 {'text': '...', 'files': ['...']}
|
| 251 |
+
history: Gradio Chatbot (type='messages') 维护的列表 [{'role': 'user', 'content': ...}, ...]
|
| 252 |
+
"""
|
| 253 |
+
|
| 254 |
+
# 1. 解析用户输入,构建符合 OpenAI 格式的 User Message
|
| 255 |
+
user_content = []
|
| 256 |
+
|
| 257 |
+
# 处理图片文件
|
| 258 |
files = user_msg.get("files", [])
|
| 259 |
+
for file_path in files:
|
| 260 |
+
user_content.append({"type": "image", "image": file_path})
|
| 261 |
|
| 262 |
+
# 处理文本
|
| 263 |
+
text = user_msg.get("text", "")
|
| 264 |
+
if text:
|
| 265 |
+
user_content.append({"type": "text", "text": text})
|
| 266 |
+
|
| 267 |
+
if not user_content:
|
| 268 |
+
yield history, gr.MultimodalTextbox(interactive=True)
|
| 269 |
+
return
|
| 270 |
+
|
| 271 |
+
# 2. 将当前用户消息加入历史记录
|
| 272 |
+
new_message = {"role": "user", "content": user_content}
|
| 273 |
+
history.append(new_message)
|
| 274 |
|
| 275 |
+
# 3. 立即 yield 更新 UI(显示用户消息),同时清空输入框
|
|
|
|
|
|
|
| 276 |
yield history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 277 |
|
| 278 |
+
# 4. 准备调用模型
|
| 279 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
handler = get_model_handler()
|
| 281 |
|
| 282 |
+
# 预先加入一个空的 Assistant 消息用于流式填充
|
| 283 |
+
history.append({"role": "assistant", "content": ""})
|
| 284 |
+
|
| 285 |
+
# 调用 predict (注意:这里直接传 history 即可,因为 history 已经是完整的上下文)
|
| 286 |
+
# 我们传入 history 的深拷贝以防修改影响 UI,或者直接传引用如果 predict 内部做了处理
|
| 287 |
+
# 这里为了安全,我们只把 history 传进去,predict 内部会处理 sys_prompt 的追加逻辑
|
| 288 |
+
import copy
|
| 289 |
+
messages_payload = copy.deepcopy(history[:-1]) # 去掉刚才加的空 assistant
|
| 290 |
+
|
| 291 |
+
for chunk in handler.predict(messages_payload, temp, tokens):
|
| 292 |
+
# 实时更新最后一条消息的内容
|
| 293 |
+
history[-1]["content"] = chunk
|
| 294 |
yield history, gr.MultimodalTextbox(interactive=False)
|
| 295 |
|
| 296 |
except Exception as e:
|
| 297 |
import traceback
|
| 298 |
traceback.print_exc()
|
| 299 |
+
# 发生错误时,追加错误信息
|
| 300 |
+
if history and history[-1]["role"] == "assistant":
|
| 301 |
+
history[-1]["content"] += f"\n❌ Error: {str(e)}"
|
| 302 |
+
else:
|
| 303 |
+
history.append({"role": "assistant", "content": f"❌ Error: {str(e)}"})
|
| 304 |
yield history, gr.MultimodalTextbox(interactive=True)
|
| 305 |
|
| 306 |
+
# 最后恢复输入框可交互
|
| 307 |
+
yield history, gr.MultimodalTextbox(interactive=True)
|
| 308 |
|
| 309 |
def create_chat_ui():
|
| 310 |
custom_css = """
|
|
|
|
| 323 |
elem_id="chatbot",
|
| 324 |
label="Chat",
|
| 325 |
avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
|
| 326 |
+
height=650,
|
| 327 |
+
type="messages"
|
| 328 |
)
|
| 329 |
|
| 330 |
chat_input = gr.MultimodalTextbox(
|