File size: 2,091 Bytes
52a48f1
4966749
85d9872
 
 
1773b0a
 
 
85d9872
4966749
85d9872
 
 
4281155
85d9872
 
 
4966749
85d9872
 
 
4966749
85d9872
 
 
 
 
 
 
 
 
 
 
38ea4cd
 
85d9872
 
16eb9f2
 
85d9872
 
 
 
 
 
 
 
4966749
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
47
48
49
import gradio as gr
import transformers

encoder_model_name = "cl-tohoku/bert-base-japanese-v2"
decoder_model_name = "skt/kogpt2-base-v2"
src_tokenizer = transformers.BertJapaneseTokenizer.from_pretrained(encoder_model_name)
trg_tokenizer = transformers.PreTrainedTokenizerFast.from_pretrained(decoder_model_name)
model = transformers.EncoderDecoderModel.from_pretrained("sappho192/ffxiv-ja-ko-translator")


def translate(text_src):
    embeddings = src_tokenizer(text_src, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')
    embeddings = {k: v for k, v in embeddings.items()}
    output = model.generate(**embeddings, max_length=500)[0, 1:-1]
    text_trg = trg_tokenizer.decode(output.cpu())
    return text_trg


def endpoint(sentence):
    return translate(sentence)


# demo = gr.Interface(fn=endpoint, inputs="text", outputs="text")
with gr.Blocks() as demo:
    input = gr.Textbox(label="Sentence")
    output = gr.Textbox(label="Result")
    btn = gr.Button(value="Submit")
    btn.click(endpoint, inputs=[input], outputs=[output])
    
    gr.Markdown("## Examples")
    gr.Markdown(
    """
    For now, the model can translate the words included in Auto-Translate dictionary.
    ํ˜„์žฌ ๋ฒ„์ „์˜ ๋ฒˆ์—ญ๊ธฐ๋Š” ์ƒ์šฉ๊ตฌ์— ํฌํ•จ๋œ ๋‹จ์–ด๋‚˜ ํ‘œํ˜„์ด ๋“ค์–ด๊ฐ„ ๋ฌธ์žฅ์„ ์–ด๋Š์ •๋„ ๋ฒˆ์—ญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ํ•™์Šต์— ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ ๋ฐ ๋ฒˆ์—ญ ๋ฐ์ดํ„ฐ๋Š” ๊ธ€๋กœ๋ฒŒ ์„œ๋ฒ„์˜ ์ผ๋ณธ์–ด ํด๋ผ์ด์–ธํŠธ์—์„œ๋งŒ ์ทจ๋“ํ•˜์˜€์œผ๋ฉฐ, ํ•œ๊ตญ ์„œ๋น„์Šค์—์„œ ์ œ๊ณต๋˜๋Š” ๋ฒˆ์—ญ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฉ์–ด์˜ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ๋Œ€๋ถ€๋ถ„ ์ง์—ญ์˜ ํ˜•ํƒœ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.
    """)
    gr.Examples(
        [["ใ‚ฎใƒซใ‚ฌใƒกใƒƒใ‚ทใƒฅ่จŽไผๆˆฆใซ่กŒใฃใฆใใพใ™ใ€‚ไธ€็ท’ใซ่กŒใใพใ—ใ‚‡ใ†ใ‹๏ผŸ"], 
         ["็ตถใ‚ขใƒซใƒ†ใƒžใ‚ฆใ‚งใƒใƒณ็ ดๅฃŠไฝœๆˆฆใ‚’ใ‚ฏใƒชใ‚ขใ—ไบ‹ใ‚ใ‚Šใพใ™ใ‹๏ผŸ"],
         ["็พŽๅฎนๅธซใฎๅ‘ผใณ้ˆดใ‚’ไฝฟใฃใฆ้ซชๆ–นใ‚’ๅค‰ใˆใพใ™ใ‚ˆใ€‚"]],
        [input],
        output,
        endpoint,
        cache_examples=False
    )
    
if __name__ == "__main__":
    demo.launch()