Spaces:
Running
Running
conan1024hao
commited on
Commit
·
f5c7afe
1
Parent(s):
9d7b7c9
add application file
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
|
4 |
+
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("./mt5")
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("./mt5")
|
7 |
+
model.resize_token_embeddings(len(tokenizer))
|
8 |
+
model.eval()
|
9 |
+
|
10 |
+
def translate(hakubun):
|
11 |
+
input_ids = tokenizer.encode(hakubun, return_tensors="pt", max_length=20, truncation=True)
|
12 |
+
output = model.generate(input_ids)
|
13 |
+
kakikudashi = tokenizer.decode(output[0], skip_special_tokens=True)
|
14 |
+
return kakikudashi
|
15 |
+
|
16 |
+
title = "Kanbun-LM"
|
17 |
+
description = "Gradio Demo for Kanbun-LM. Upload a hakubun then you can earn get its kanbun. Texts other than Tang poetry may not be translated correctly.<br>" \
|
18 |
+
"書き下し文生成のデモです。白文を入力し、翻訳された書き下し文を得ることができます。唐詩以外の漢文は正しく翻訳されない可能性があります。"
|
19 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2305.12759' target='_blank'>arXiv</a></p> " \
|
20 |
+
"<p style='text-align: center'><a href='https://github.com/nlp-waseda/Kanbun-LM' target='_blank'>Github Repo</a></p>"
|
21 |
+
examples = [['春眠不覚暁'],
|
22 |
+
['処処聞啼鳥'],
|
23 |
+
['洛陽親友如相問'],
|
24 |
+
['一片氷心在玉壺']]
|
25 |
+
demo = gr.Interface(fn=translate, inputs="text", outputs="text",
|
26 |
+
title=title, description=description, article=article, examples=examples, allow_flagging=False)
|
27 |
+
demo.launch()
|