Spaces:
Runtime error
Runtime error
File size: 3,556 Bytes
617aba5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import torch
from RetNet.retnet.modeling_retnet import RetNetForCausalLM
from transformers import AutoTokenizer
import gradio as gr
MODEL_NAME = "p1atdev/LightNovel-Intro-RetNet-400M"
DEFAULT_INPUT_TEXT = "目が覚めると、"
EXAMPLE_INPUTS = [
DEFAULT_INPUT_TEXT,
"冒険者ギルドには",
"真っ白い部屋の中、そこには",
"20XX年、",
"「なんだって!?」",
"どうやらトラックにはねられ、俺は",
]
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = RetNetForCausalLM.from_pretrained(MODEL_NAME)
model.eval()
@torch.no_grad()
def generate(
input_text,
max_new_tokens=128,
do_sample=True,
temperature=1.0,
top_p=0.95,
top_k=20,
):
if input_text.strip() == "":
return ""
inputs = tokenizer(input_text, return_tensors="pt", add_special_tokens=False)
generated = model.custom_generate(
**inputs,
parallel_compute_prompt=True,
max_new_tokens=max_new_tokens,
do_sample=do_sample,
temperature=temperature,
top_p=top_p,
top_k=top_k,
)
return tokenizer.batch_decode(generated)[0]
def continue_generate(
input_text,
*args,
):
return input_text, generate(input_text, *args)
with gr.Blocks() as demo:
gr.Markdown(
"""\
# LightNovel-Intro-RetNet-400M-Demo
ライトノベルの冒頭だけを学習した 400M パラメータの RetNet モデルのデモです。
### 参考:
- https://github.com/syncdoth/RetNet
"""
)
input_text = gr.Textbox(
value=DEFAULT_INPUT_TEXT,
placeholder="私の名前は...",
lines=2,
)
output_text = gr.Textbox(
value="",
placeholder="ここに出力が表示されます...",
lines=8,
interactive=False,
)
with gr.Row():
generate_btn = gr.Button("Generate ✒️", variant="primary")
continue_btn = gr.Button("Continue ➡️", variant="secondary")
clear_btn = gr.ClearButton(
value="Clear 🧹",
components=[input_text, output_text],
)
with gr.Accordion("Advanced settings", open=False):
max_tokens = gr.Slider(
label="Max tokens",
minimum=8,
maximum=512,
value=128,
step=4,
)
do_sample = gr.Checkbox(
label="Do sample",
value=True,
)
temperature = gr.Slider(
label="Temperature",
minimum=0,
maximum=2,
value=1,
step=0.05,
)
top_p = gr.Slider(
label="Top p",
minimum=0,
maximum=1,
value=0.95,
step=0.05,
)
top_k = gr.Slider(
label="Top k",
minimum=0,
maximum=100,
value=20,
step=1,
)
gr.Examples(
examples=EXAMPLE_INPUTS,
inputs=input_text,
)
generate_btn.click(
fn=generate,
inputs=[
input_text,
max_tokens,
do_sample,
temperature,
top_p,
top_k,
],
outputs=output_text,
queue=False,
)
continue_btn.click(
fn=continue_generate,
inputs=[
input_text,
max_tokens,
do_sample,
temperature,
top_p,
top_k,
],
outputs=[input_text, output_text],
queue=False,
)
demo.launch()
|