from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
# 加载tokenizer
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-32B-Instruct", trust_remote_code=True)
# 加载基础模型
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-32B-Instruct", trust_remote_code=True)
# 加载LoRA适配器
model = PeftModel.from_pretrained(base_model, "aigc-x/Qwen2.5-32B-novel-writting").merge_and_unload()
# 模型推理
prompt = "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=4096
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
- Downloads last month
- 2
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
🙋
Ask for provider support