You can perform inference using the transformers library. SeqGPT supports two primary task types: classification (分类) and extraction (抽取).
Important Note: The model relies on a specific prompt template. Changing the instruction format can harm performance. The expected format is:
输入: {sent} {task}: {labels} 输出: [GEN]
Where [GEN] is the special token [GEN] used to trigger generation.
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
import torch
model_name_or_path = 'DAMO-NLP/SeqGPT-560M'
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path)
tokenizer.padding_side = 'left'
tokenizer.truncation_side = 'left'
if torch.cuda.is_available():
model = model.half().cuda()
model.eval()
GEN_TOK = '[GEN]'
# Example usage logic
sent = 'Your input text here'
task = '分类' # or '抽取'
labels = 'labelA,LabelB'
p = '输入: {}\n{}: {}\n输出: {}'.format(sent, task, labels, GEN_TOK)
input_ids = tokenizer(p, return_tensors="pt", padding=True, truncation=True, max_length=1024)
input_ids = input_ids.to(model.device)
outputs = model.generate(**input_ids, num_beams=4, do_sample=False, max_new_tokens=256)
# Extracting only the generated part
input_ids_tensor = input_ids.get('input_ids', input_ids)
outputs = outputs[0][len(input_ids_tensor[0]):]
response = tokenizer.decode(outputs, skip_special_tokens=True)
print(response)