Spaces:
Paused
Paused
Update app.py
Browse files15 unmodified lines1617181920212223242526272829303132333435366 unmodified lines434445464748497 unmodified lines5758596061626364656615 unmodified linesif model is None:model = AutoModelForCausalLM.from_pretrained(MODEL_ID,dtype=torch.float16, # 修复弃用警告:torch_dtype -> dtypetrust_remote_code=True,).to("cuda")# 更加鲁棒的 history 处理逻辑messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]for entry in history:# 自动兼容 字典格式 {'role': '...', 'content': '...'}if isinstance(entry, dict):messages.append(entry)# 自动兼容 元组/列表格式 [user, assistant]elif isinstance(entry, (list, tuple)) and len(entry) == 2:messages.append({"role": "user", "content": entry[0]})messages.append({"role": "assistant", "content": entry[1]})messages.append({"role": "user", "content": message})6 unmodified linesgenerate_kwargs = dict(input_ids=input_ids,streamer=streamer,max_new_tokens=1024,temperature=0.5,top_p=0.9,do_sample=True,7 unmodified linesresponse += tokenyield response# 强制指定 type="tuples" 来适配上面的 history 处理,避免 unpacked 错误demo = gr.ChatInterface(fn=respond,type="tuples", # 显式声明,确保 entry 是 [user, bot] 结构title="Spartacus Chat",description="Chat with NoesisLab/Spartacus-1B-Instruct",)15 unmodified lines16171819202122232425262728293031326 unmodified lines394041424344457 unmodified lines53545556575859606115 unmodified linesif model is None:model = AutoModelForCausalLM.from_pretrained(MODEL_ID,dtype=torch.float16,trust_remote_code=True,).to("cuda")# 针对旧版 Gradio 结构处理:history 是 [[q1, a1], [q2, a2]]messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]if history:for user_msg, assistant_msg in history:messages.append({"role": "user", "content": user_msg})messages.append({"role": "assistant", "content": assistant_msg})messages.append({"role": "user", "content": message})6 unmodified linesgenerate_kwargs = dict(input_ids=input_ids,streamer=streamer,max_new_tokens=2048,temperature=0.5,top_p=0.9,do_sample=True,7 unmodified linesresponse += tokenyield response# 彻底删掉 type 参数,只保留最基础的配置demo = gr.ChatInterface(fn=respond,title="Spartacus Chat",description="Chat with NoesisLab/Spartacus-1B-Instruct",)
app.py+8-13