sappho192 commited on
Commit
6b1a623
โ€ข
1 Parent(s): 0abbef2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
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)