Spaces:
Paused
Paused
Update app.py
Browse files5 unmodified lines6789101112131415161718192021222324252627282930313233343536372 unmodified lines40414243444546474849502 unmodified lines53545556575859606162635 unmodified linesMODEL_ID = "NoesisLab/Spartacus-1B-Instruct"# 静态加载 Tokenizertokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = None@spaces.GPUdef respond(message, history):global modelif 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})input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to("cuda")streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)2 unmodified linesinput_ids=input_ids,streamer=streamer,max_new_tokens=2048,temperature=0.5,top_p=0.9,do_sample=True,)thread = Thread(target=model.generate, kwargs=generate_kwargs)thread.start()2 unmodified linesresponse += tokenyield response# 彻底删掉 type 参数,只保留最基础的配置demo = gr.ChatInterface(fn=respond,title="Spartacus Chat",description="Chat with NoesisLab/Spartacus-1B-Instruct",)if __name__ == "__main__":5 unmodified lines67891011121314151617181920212223242526272829303132333435362 unmodified lines3940414243444546474849502 unmodified lines535455565758596061626364655 unmodified linesMODEL_ID = "NoesisLab/Spartacus-1B-Instruct"# 静态加载 Tokenizer (不占 GPU)tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = None@spaces.GPUdef respond(message, history):global model# ZeroGPU 核心逻辑:在装饰器函数内初始化并移动到 CUDAif model is None:model = AutoModelForCausalLM.from_pretrained(MODEL_ID,torch_dtype=torch.float16,trust_remote_code=True,).to("cuda")# Gradio 5.x 的 history 已经是 [{'role': 'user', 'content': '...'}, ...] 格式# 直接拼接到 messages 即可messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]messages.extend(history)messages.append({"role": "user", "content": message})input_ids = tokenizer.apply_chat_template(messages,add_generation_prompt=True,return_tensors="pt").to("cuda")streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)2 unmodified linesinput_ids=input_ids,streamer=streamer,max_new_tokens=2048,temperature=0.6,top_p=0.95,do_sample=True,)# 启动异步生成线程thread = Thread(target=model.generate, kwargs=generate_kwargs)thread.start()2 unmodified linesresponse += tokenyield response# 使用最新版的配置参数demo = gr.ChatInterface(fn=respond,type="messages", # 这需要 gradio>=5.0.0title="Spartacus Chat",description="Chat with NoesisLab/Spartacus-1B-Instruct",examples=["Who are you?", "Explain the concept of Noesis."],)if __name__ == "__main__":
app.py+15-13