Views
No views yet
1 # one of 'src_lang' and 'tgt_lang' should be "한국어"
2 src_lang = "English" # English, 한국어, 日本語, 中文
3 tgt_lang = "한국어" # English, 한국어, 日本語, 中文
4 text = "New era, same empire. T1 is your 2024 Worlds champion!"
5
6 # task part
7 task_xml_dict = {
8 'head': "<task>",
9 'body': f"Translate the source sentence from {src_lang} to {tgt_lang}.\nBe sure to reflect the guidelines below when translating.",
10 'tail': "</task>"
11 }
12 task = f"{task_xml_dict['head']}\n{task_xml_dict['body']}\n{task_xml_dict['tail']}"
13
14 # instruction part
15 instruction_xml_dict = {
16 'head': "<instruction>",
17 'body': ["Translate without any condition."],
18 'tail': "</instruction>"
19 }
20 instruction_xml_body = '\n'.join([f'- {body}' for body in instruction_xml_dict['body']])
21 instruction = f"{instruction_xml_dict['head']}\n{instruction_xml_body}\n{instruction_xml_dict['tail']}"
22
23 # translation part
24 src_xml_dict = {
25 'head': f"<source><{src_lang}>",
26 'body': text.strip(),
27 'tail': f"</{src_lang}></source>"
28 }
29 tgt_xml_dict = {
30 'head': f"<target><{tgt_lang}>",
31 }
32 src = f"{src_xml_dict['head']}\n{src_xml_dict['body']}\n{src_xml_dict['tail']}"
33 tgt = f"{tgt_xml_dict['head']}\n"
34 translation_xml_dict = {
35 'head': "<translation>",
36 'body': f"{src}\n{tgt}",
37 }
38 translation = f"{translation_xml_dict['head']}\n{translation_xml_dict['body']}"
39
40 # final prompt
41 prompt = f"{task}\n\n{instruction}\n\n{translation}"<task>
Translate the source sentence from English to 한국어.
Be sure to reflect the guidelines below when translating.
</task>
<instruction>
- Translate without any condition.
</instruction>
<translation>
<source><English>
New era, same empire. T1 is your 2024 Worlds champion!
</English></source>
<target><한국어>새로운 시대, 여전한 왕조. 티원이 2024 월즈의 챔피언입니다!
</한국어></target>
</translation>1 # MODEL
2 model_name = 'beomi/Llama-3-Open-Ko-8B'
3 adapter_name = 'traintogpb/llama-3-mmt-xml-it-sft-adapter'
4
5 model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 max_length=4096,
8 attn_implementation='flash_attention_2',
9 torch_dtype=torch.bfloat16,
10 )
11 model = PeftModel.from_pretrained(
12 model,
13 adapter_path=adapter_name,
14 torch_dtype=torch.bfloat16,
15 )
16
17 tokenizer = AutoTokenizer.from_pretrained(adapter_name)
18 tokenizer.pad_token_id = 128002 # eos_token_id and pad_token_id should be different
19
20 text = "New era, same empire. T1 is your 2024 Worlds champion!"
21 input_prompt = "<task> ~ <target><{tgt_lang}>" # prompt with the template above
22 inputs = tokenizer(input_prompt, max_length=2000, truncation=True, return_tensors='pt')
23
24 if inputs['input_ids'][0][-1] == tokenizer.eos_token_id:
25 inputs['input_ids'] = inputs['input_ids'][0][:-1].unsqueeze(dim=0)
26 inputs['attention_mask'] = inputs['attention_mask'][0][:-1].unsqueeze(dim=0)
27
28 outputs = model.generate(**inputs, max_length=2000, eos_token_id=tokenizer.eos_token_id)
29
30 input_len = len(inputs['input_ids'].squeeze())
31 translation = tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True)
32 print(translation)