Spaces:
Paused
Paused
Update app.py
Browse files5 unmodified lines67891011123 unmodified lines16171819202122232425262728293018 unmodified lines49505152535455565758595 unmodified linesMODEL_ID = "NoesisLab/Spartacus-1B-Instruct"# Tokenizer 静态加载tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = None3 unmodified linesif model is None:model = AutoModelForCausalLM.from_pretrained(MODEL_ID,torch_dtype=torch.float16,trust_remote_code=True,).to("cuda")# 适配旧版 Gradio 的 history 格式: [[user, assistant], [user, assistant]]messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]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})18 unmodified linesresponse = ""for token in streamer:response += tokenyield response # 旧版 ChatInterface 需要 yield 完整的累计字符串# 移除 type="messages" 参数demo = gr.ChatInterface(fn=respond,title="Spartacus Chat",description="Chat with NoesisLab/Spartacus-1B-Instruct",)5 unmodified lines67891011123 unmodified lines16171819202122232425262728293031323334353618 unmodified lines5556575859606162636465665 unmodified linesMODEL_ID = "NoesisLab/Spartacus-1B-Instruct"# 静态加载 Tokenizertokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = None3 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})18 unmodified linesresponse = ""for token in streamer:response += 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",)
app.py+15-8