File size: 2,110 Bytes
577164e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
import torch
from utils.simple_bleu import simple_score

model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", torch_dtype=torch.bfloat16, device_map="auto")
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")


def translate_ko2en(text):
    tokenizer.src_lang = "ko_KR"
    input_ids = tokenizer(text, return_tensors="pt").input_ids.to(model.device)
    outputs = model.generate(input_ids=input_ids, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])

    outputs = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return outputs


def translate_en2ko(text):
    tokenizer.src_lang = "en_XX"
    input_ids = tokenizer(text, return_tensors="pt").input_ids.to(model.device)
    outputs = model.generate(input_ids=input_ids, forced_bos_token_id=tokenizer.lang_code_to_id["ko_KR"], max_new_tokens=2048)

    outputs = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return outputs


def main():
    while True:
        text = input('>')
        en_text = translate_ko2en(text)
        ko_text = translate_en2ko(en_text)
        print('en_text', en_text)
        print('ko_text', ko_text)
        print('score', simple_score(text, ko_text))
    """ 
    >>? 3์ฒœ๋งŒ ๊ฐœ๊ฐ€ ๋„˜๋Š” ํŒŒ์ผ๊ณผ 250์–ต ๊ฐœ์˜ ํ† ํฐ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Phi1.5์˜ ๋ฐ์ดํ„ฐ ์„ธํŠธ ๊ตฌ์„ฑ์— ์ ‘๊ทผํ•˜์ง€๋งŒ ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์ธ Mixtral 8x7B๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  Apache2.0 ๋ผ์ด์„ ์Šค์— ๋”ฐ๋ผ ๋ผ์ด์„ ์Šค๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. 
en_text It has over 30 million files and 2.5 billion tokens, accesses the data set configuration of Phi1.5, but uses an open-source model, Mixtral 8x7B, and is licensed under the Apache 2.0 license.
ko_text 30๋งŒ๊ฐœ์˜ ํŒŒ์ผ๊ณผ 2.5์–ต๊ฐœ์˜ ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , Phi1.5์˜ ๋ฐ์ดํ„ฐ ์„ธํŠธ configuration์— ์ ‘๊ทผํ•˜์ง€๋งŒ, ์˜คํ”ˆ์†Œ์Šค ๋ชจ๋ธ์ธ Mixtral 8x7B๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Apache 2.0 ๋ผ์ด์„ผ์Šค ์•„๋ž˜ licenc๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
score 0.14724623770949022
    """

if __name__ == "__main__":
    main()