Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import transformers
|
| 3 |
+
|
| 4 |
+
encoder_model_name = "cl-tohoku/bert-base-japanese-v2"
|
| 5 |
+
decoder_model_name = "skt/kogpt2-base-v2"
|
| 6 |
+
src_tokenizer = transformers.BertJapaneseTokenizer.from_pretrained(encoder_model_name)
|
| 7 |
+
trg_tokenizer = transformers.PreTrainedTokenizerFast.from_pretrained(decoder_model_name)
|
| 8 |
+
model = transformers.EncoderDecoderModel.from_pretrained("sappho192/aihub-ja-ko-translator")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def translate(text_src):
|
| 12 |
+
embeddings = src_tokenizer(text_src, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')
|
| 13 |
+
embeddings = {k: v for k, v in embeddings.items()}
|
| 14 |
+
# using default generation method: GreedySearch, No LogitsProcessor
|
| 15 |
+
output = model.generate(**embeddings)[0, 1:-1]
|
| 16 |
+
text_trg = trg_tokenizer.decode(output.cpu())
|
| 17 |
+
return text_trg
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def endpoint(sentence):
|
| 21 |
+
return translate(sentence)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# demo = gr.Interface(fn=endpoint, inputs="text", outputs="text")
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
input = gr.Textbox(label="Sentence")
|
| 27 |
+
output = gr.Textbox(label="Result")
|
| 28 |
+
btn = gr.Button(value="Submit")
|
| 29 |
+
btn.click(endpoint, inputs=[input], outputs=[output])
|
| 30 |
+
|
| 31 |
+
gr.Markdown("## Examples")
|
| 32 |
+
gr.Markdown(
|
| 33 |
+
"""
|
| 34 |
+
Translated result can be wrong or containg misleading content.
|
| 35 |
+
번역된 결과는 정확하지 않을 수 있으며, 적절치 않은 표현을 포함할 수 있습니다.
|
| 36 |
+
""")
|
| 37 |
+
gr.Examples(
|
| 38 |
+
[["試験前に緊張したあまり、熱がでてしまった。"],
|
| 39 |
+
["山田は英語にかけてはクラスの誰にも負けない。"],
|
| 40 |
+
["この本によれば、最初の人工橋梁は新石器時代にさかのぼるという。"]],
|
| 41 |
+
[input],
|
| 42 |
+
output,
|
| 43 |
+
endpoint,
|
| 44 |
+
cache_examples=False
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch(share=True)
|