|
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) |
|
|
|
|
|
|
|
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() |
|
|