First commit
Browse files- app.py +40 -0
- requirements.txt +5 -0
- utils.py +6 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import T5Tokenizer, AutoModelForCausalLM
|
4 |
+
from utils import translate_from_jp_to_en
|
5 |
+
|
6 |
+
tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-gpt-1b")
|
7 |
+
model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt-1b")
|
8 |
+
|
9 |
+
|
10 |
+
def generate(text, max_length=128):
|
11 |
+
|
12 |
+
token_ids = tokenizer.encode(
|
13 |
+
text, add_special_tokens=False, return_tensors="pt")
|
14 |
+
|
15 |
+
with torch.no_grad():
|
16 |
+
output_ids = model.generate(
|
17 |
+
token_ids,
|
18 |
+
max_length=max_length,
|
19 |
+
do_sample=True,
|
20 |
+
top_k=500,
|
21 |
+
top_p=0.95,
|
22 |
+
pad_token_id=tokenizer.pad_token_id,
|
23 |
+
bos_token_id=tokenizer.bos_token_id,
|
24 |
+
eos_token_id=tokenizer.eos_token_id,
|
25 |
+
bad_word_ids=[[tokenizer.unk_token_id]]
|
26 |
+
)
|
27 |
+
|
28 |
+
output = tokenizer.decode(output_ids.tolist()[0])
|
29 |
+
return output, translate_from_jp_to_en(output)
|
30 |
+
|
31 |
+
|
32 |
+
title = "JP GPT Demo"
|
33 |
+
description = "Demo for generating text in Japanase using a GPT model"
|
34 |
+
examples = [['日本のeスポーツ障害者がステレオタイプを撃ち落とす', 128]]
|
35 |
+
gr.Interface(fn=generate, inputs=[gr.inputs.Textbox(lines=4, label="Prompt"),
|
36 |
+
gr.inputs.Slider(minimum=8, maximum=1024, step=8, default=64, label="Numbers of tokens")],
|
37 |
+
outputs=["text", "text"],
|
38 |
+
title=title, description=description,
|
39 |
+
# article= article,
|
40 |
+
examples=examples).launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
streamlit
|
4 |
+
sentencepiece
|
5 |
+
translate
|
utils.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from translate import Translator
|
2 |
+
|
3 |
+
|
4 |
+
def translate_from_jp_to_en(text):
|
5 |
+
translator = Translator(from_lang='ja', to_lang='en')
|
6 |
+
return translator.translate(text)
|