Jiaqi-hkust commited on
Commit
38eab31
·
verified ·
1 Parent(s): 78ed009

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +58 -22
app.py CHANGED
@@ -196,49 +196,86 @@ def get_model_handler():
196
  model_handler = ModelHandler(MODEL_PATH)
197
  return model_handler
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  @gpu_decorator
200
  async def respond(user_msg, history, temp, tokens):
201
  text = user_msg.get("text", "").strip()
202
  files = user_msg.get("files", [])
203
 
204
- # ### <<< 修改点 3:构建正确的多模态消息格式
205
- # 不能直接 append 路径字符串,要用字典 {"type": "image", "image": path}
206
- user_content = []
207
- for file_path in files:
208
- user_content.append({"type": "image", "image": file_path})
209
-
210
- if text:
211
- user_content.append({"type": "text", "text": text})
212
-
213
- # 构建符合 type="messages" 的用户消息
214
- user_message = {"role": "user", "content": user_content}
215
 
216
- history.append(user_message)
 
217
  # 此时先 yield 一次,让用户看到自己的输入
218
  yield history, gr.MultimodalTextbox(value=None, interactive=False)
219
-
220
- history.append({"role": "assistant", "content": ""})
221
 
222
  try:
223
- # 截取历史记录(只取之前的对话,不包含当前这一轮,避免重复)
224
- previous_history = history[:-2] if len(history) >= 2 else []
225
 
226
  # 在这里调用 handler,此时我们在 @gpu_decorator 的保护下,可以访问 GPU
227
  handler = get_model_handler()
228
 
229
  generated_text = ""
230
- # 传递原始的 user_msg 字典给 predict,或者根据需要调整 predict 的输入
231
- # 注意:你的 predict 函数解析逻辑需要适配
232
  for chunk in handler.predict(user_msg, previous_history, temp, tokens):
233
  generated_text = chunk
234
  safe_text = generated_text.replace("<", "&lt;").replace(">", "&gt;")
235
- history[-1]["content"] = safe_text
 
236
  yield history, gr.MultimodalTextbox(interactive=False)
237
 
238
  except Exception as e:
239
  import traceback
240
  traceback.print_exc()
241
- history[-1]["content"] = f"❌ Error: {str(e)}"
242
  yield history, gr.MultimodalTextbox(interactive=True)
243
 
244
  yield history, gr.MultimodalTextbox(value=None, interactive=True)
@@ -260,8 +297,7 @@ def create_chat_ui():
260
  elem_id="chatbot",
261
  label="Chat",
262
  avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
263
- height=650,
264
- type="messages"
265
  )
266
 
267
  chat_input = gr.MultimodalTextbox(
 
196
  model_handler = ModelHandler(MODEL_PATH)
197
  return model_handler
198
 
199
+ def _convert_history_to_messages_format(history):
200
+ """将旧格式的 Chatbot history 转换为新格式的 messages"""
201
+ messages = []
202
+ for pair in history:
203
+ if isinstance(pair, list) and len(pair) >= 2:
204
+ user_msg = pair[0]
205
+ assistant_msg = pair[1] if len(pair) > 1 else ""
206
+
207
+ # 处理用户消息
208
+ user_content = []
209
+ if isinstance(user_msg, str):
210
+ user_content.append({"type": "text", "text": user_msg})
211
+ elif isinstance(user_msg, tuple):
212
+ # 旧格式可能是 (text, image) 或 (image, text)
213
+ for item in user_msg:
214
+ if isinstance(item, str):
215
+ if os.path.exists(item) or any(item.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
216
+ user_content.append({"type": "image", "image": item})
217
+ else:
218
+ user_content.append({"type": "text", "text": item})
219
+ elif isinstance(user_msg, list):
220
+ # 可能是新格式的内容列表
221
+ user_content = user_msg
222
+
223
+ if user_content:
224
+ messages.append({"role": "user", "content": user_content})
225
+
226
+ # 处理助手消息
227
+ if assistant_msg and isinstance(assistant_msg, str):
228
+ messages.append({"role": "assistant", "content": assistant_msg})
229
+ elif isinstance(pair, dict):
230
+ # 如果已经是新格式,直接使用
231
+ messages.append(pair)
232
+ return messages
233
+
234
+ def _format_user_input_for_chatbot(text, files):
235
+ """格式化用户输入为 Chatbot 可显示的格式"""
236
+ if files and text:
237
+ # 有图片和文本,返回元组格式
238
+ return (text, *files)
239
+ elif files:
240
+ # 只有图片
241
+ return files[0] if len(files) == 1 else tuple(files)
242
+ else:
243
+ # 只有文本
244
+ return text
245
+
246
  @gpu_decorator
247
  async def respond(user_msg, history, temp, tokens):
248
  text = user_msg.get("text", "").strip()
249
  files = user_msg.get("files", [])
250
 
251
+ # 格式化用户输入用于 Chatbot 显示(旧格式)
252
+ user_display = _format_user_input_for_chatbot(text, files)
 
 
 
 
 
 
 
 
 
253
 
254
+ # 添加用户消息到 history(旧格式:列表的列表)
255
+ history.append([user_display, None])
256
  # 此时先 yield 一次,让用户看到自己的输入
257
  yield history, gr.MultimodalTextbox(value=None, interactive=False)
 
 
258
 
259
  try:
260
+ # 将旧格式的 history 转换为新格式的 messages,用于 predict 函数
261
+ previous_history = _convert_history_to_messages_format(history[:-1])
262
 
263
  # 在这里调用 handler,此时我们在 @gpu_decorator 的保护下,可以访问 GPU
264
  handler = get_model_handler()
265
 
266
  generated_text = ""
267
+ # 传递原始的 user_msg 字典给 predict
 
268
  for chunk in handler.predict(user_msg, previous_history, temp, tokens):
269
  generated_text = chunk
270
  safe_text = generated_text.replace("<", "&lt;").replace(">", "&gt;")
271
+ # 更新最后一条助手消息(旧格式)
272
+ history[-1][1] = safe_text
273
  yield history, gr.MultimodalTextbox(interactive=False)
274
 
275
  except Exception as e:
276
  import traceback
277
  traceback.print_exc()
278
+ history[-1][1] = f"❌ Error: {str(e)}"
279
  yield history, gr.MultimodalTextbox(interactive=True)
280
 
281
  yield history, gr.MultimodalTextbox(value=None, interactive=True)
 
297
  elem_id="chatbot",
298
  label="Chat",
299
  avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=Qwen"),
300
+ height=650
 
301
  )
302
 
303
  chat_input = gr.MultimodalTextbox(