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)