Spaces:
Runtime error
Runtime error
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)
|