traintogpb/aihub-flores-koen-integrated-prime-base-300k
Viewer • Updated • 299k • 22
How to use traintogpb/llama-3-enko-translator-8b-qlora-bf16-upscaled with Transformers:
# Use a pipeline as a high-level helper
# Warning: Pipeline type "translation" is no longer supported in transformers v5.
# You must load the model directly (see below) or downgrade to v4.x with:
# pip install "transformers<5.0.0"
from transformers import pipeline
pipe = pipeline("translation", model="traintogpb/llama-3-enko-translator-8b-qlora-bf16-upscaled") # pip install -U transformers accelerate
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("traintogpb/llama-3-enko-translator-8b-qlora-bf16-upscaled")
model = AutoModelForCausalLM.from_pretrained("traintogpb/llama-3-enko-translator-8b-qlora-bf16-upscaled", device_map="auto") prompt = f"Translate this from {src_lang} to {tgt_lang}\n### {src_lang}: {src_text}\n### {tgt_lang}:"
>>> # src_lang can be 'English', '한국어'
>>> # tgt_lang can be '한국어', 'English'
Mind that there is no "space (_)" at the end of the prompt (unpredictable first token will be popped up).<|end_of_text|>, id=128001) at the end of the prompt. # MODEL
model_name = 'traintogpb/llama-3-enko-translator-8b-qlora-bf16-upscaled'
model = AutoModelForCausalLM.from_pretrained(
model_name,
max_length=768,
attn_implementation='flash_attention_2',
torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(adapter_name)
tokenizer.pad_token_id = 128002 # eos_token_id and pad_token_id should be different
# tokenizer.add_eos_token = False # There is no 'add_eos_token' option in llama3
text = "Someday, QWER will be the greatest girl band in the world."
input_prompt = f"Translate this from English to 한국어.\n### English: {text}\n### 한국어:"
inputs = tokenizer(input_prompt, max_length=768, truncation=True, return_tensors='pt')
if inputs['input_ids'][0][-1] == tokenizer.eos_token_id:
inputs['input_ids'] = inputs['input_ids'][0][:-1].unsqueeze(dim=0)
inputs['attention_mask'] = inputs['attention_mask'][0][:-1].unsqueeze(dim=0)
outputs = model.generate(**inputs, max_length=768, eos_token_id=tokenizer.eos_token_id)
input_len = len(inputs['input_ids'].squeeze())
translation = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True)
print(translation)