File size: 1,825 Bytes
c7366c4 |
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 = "openai-community/gpt2"
src_tokenizer = transformers.BertJapaneseTokenizer.from_pretrained(encoder_model_name)
trg_tokenizer = transformers.PreTrainedTokenizerFast.from_pretrained(decoder_model_name)
model = transformers.EncoderDecoderModel.from_pretrained("sappho192/jesc-ja-en-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=512)[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(
"""
Translated result can be wrong or containg misleading content.
๋ฒ์ญ๋ ๊ฒฐ๊ณผ๋ ์ ํํ์ง ์์ ์ ์์ผ๋ฉฐ, ์ ์ ์น ์์ ํํ์ ํฌํจํ ์ ์์ต๋๋ค.
""")
gr.Examples(
[["ๅใใพใใฆ."],
["ๅคใซใชใใพใใ"],
["่ฉฆ้จๅใซ็ทๅผตใใใใพใใ็ฑใใงใฆใใพใฃใใ"],
["ๅฑฑ็ฐใฏ่ฑ่ชใซใใใฆใฏใฏใฉในใฎ่ชฐใซใ่ฒ ใใชใใ"],
["ใใฎๆฌใซใใใฐใๆๅใฎไบบๅทฅๆฉๆขใฏๆฐ็ณๅจๆไปฃใซใใใฎใผใใจใใใ"]],
[input],
output,
endpoint,
cache_examples=False
)
if __name__ == "__main__":
demo.launch(share=True) |