sappho192's picture
Set max_length=500
4281155 verified
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()