Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from transformers import(
|
| 4 |
+
EncoderDecoderModel,
|
| 5 |
+
PreTrainedTokenizerFast,
|
| 6 |
+
BertJapaneseTokenizer,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
encoder_model_name = "cl-tohoku/bert-base-japanese-v2"
|
| 10 |
+
decoder_model_name = "skt/kogpt2-base-v2"
|
| 11 |
+
src_tokenizer = BertJapaneseTokenizer.from_pretrained(encoder_model_name)
|
| 12 |
+
trg_tokenizer = PreTrainedTokenizerFast.from_pretrained(decoder_model_name)
|
| 13 |
+
model = EncoderDecoderModel.from_pretrained("sappho192/ffxiv-ja-ko-translator")
|
| 14 |
+
|
| 15 |
+
def translate(text_src):
|
| 16 |
+
embeddings = src_tokenizer(text_src, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')
|
| 17 |
+
embeddings = {k: v for k, v in embeddings.items()}
|
| 18 |
+
output = model.generate(**embeddings, max_length=300)[0, 1:-1]
|
| 19 |
+
text_trg = trg_tokenizer.decode(output.cpu())
|
| 20 |
+
return text_trg
|
| 21 |
+
|
| 22 |
+
def endpoint(sentence):
|
| 23 |
+
return translate(sentence)
|
| 24 |
+
|
| 25 |
+
# demo = gr.Interface(fn=endpoint, inputs="text", outputs="text")
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
input = gr.Textbox(label="Sentence")
|
| 28 |
+
output = gr.Textbox(label="Result")
|
| 29 |
+
btn = gr.Button(value="Submit")
|
| 30 |
+
btn.click(endpoint, inputs=[input], outputs=[output])
|
| 31 |
+
|
| 32 |
+
gr.Markdown("## Examples")
|
| 33 |
+
gr.Markdown(
|
| 34 |
+
"""
|
| 35 |
+
For now, the model can translate the words included in Auto-Translate dictionary.
|
| 36 |
+
현재 버전의 번역기는 상용구에 포함된 단어나 표현이 들어간 문장을 대부분 번역할 수 있습니다.
|
| 37 |
+
""")
|
| 38 |
+
gr.Examples(
|
| 39 |
+
[["絶アルテマウェポン破壊作戦をクリアし事ありますか?"],
|
| 40 |
+
["ギルガメッシュ討伐戦に行ってきます。一緒に行きましょうか?"],
|
| 41 |
+
["美容師の呼び鈴を使って髪方を変えますよ。"]],
|
| 42 |
+
[input],
|
| 43 |
+
output,
|
| 44 |
+
endpoint,
|
| 45 |
+
cache_examples=False
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|