Spaces:
Paused
Paused
Update app.py
Browse files5 unmodified lines678991011121314151617181919202122232324252625262728293027282930343132333435362 unmodified lines39404143444243444546474849502 unmodified lines53545556565758596061626364655 unmodified lines
MODEL_ID = "NoesisLab/Spartacus-1B-Instruct"
# 静态加载 Tokenizer# 静态加载 Tokenizer (不占 GPU)tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = None
@spaces.GPUdef respond(message, history): global model # ZeroGPU 核心逻辑:在装饰器函数内初始化并移动到 CUDA if model is None: model = AutoModelForCausalLM.from_pretrained( MODEL_ID, dtype=torch.float16, torch_dtype=torch.float16, trust_remote_code=True, ).to("cuda")
# 针对旧版 Gradio 结构处理:history 是 [[q1, a1], [q2, a2]] # Gradio 5.x 的 history 已经是 [{'role': 'user', 'content': '...'}, ...] 格式 # 直接拼接到 messages 即可 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.extend(history) messages.append({"role": "user", "content": message})
input_ids = tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt" messages, add_generation_prompt=True, return_tensors="pt" ).to("cuda")
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)2 unmodified lines input_ids=input_ids, streamer=streamer, max_new_tokens=2048, temperature=0.5, top_p=0.9, temperature=0.6, top_p=0.95, do_sample=True, )
# 启动异步生成线程 thread = Thread(target=model.generate, kwargs=generate_kwargs) thread.start()
2 unmodified lines response += token yield response
# 彻底删掉 type 参数,只保留最基础的配置# 使用最新版的配置参数demo = gr.ChatInterface( fn=respond, type="messages", # 这需要 gradio>=5.0.0 title="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