English_to_Japanese Translator
Collection
LLMを用いた英日翻訳モデル
•
2 items
•
Updated
•
1
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("rinna/bilingual-gpt-neox-4b-instruction-sft", use_fast=False)
model = AutoModelForCausalLM.from_pretrained("ryota39/bilingual-gpt-neox-4b-instruction-sft-en-ja-84k")
if torch.cuda.is_available():
model = model.to("cuda")
prompt = list()
prompt.append("指示: 以下の英語を日本語に翻訳してください。")
prompt.append("ユーザー: Do you deliver on Sundays?")
prompt.append("システム: ")
prompt = '\n'.join(prompt)
print(prompt)
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
with torch.no_grad():
output_ids = model.generate(
token_ids.to(model.device),
max_new_tokens=512,
do_sample=False,
temperature=0.7,
top_p=0.85,
pad_token_id=tokenizer.pad_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id
)
output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1):])
print(output)