File size: 2,439 Bytes
5c2dae5
 
6d954e4
 
 
ec0497c
 
48b0464
5c2dae5
ec0497c
 
 
5c2dae5
ec0497c
 
59a1177
 
ec0497c
 
e5a4373
 
ec0497c
48b0464
4dc002b
3a34f41
4dc002b
3a34f41
 
59a1177
 
 
 
ef210ec
 
 
 
 
 
 
 
 
 
cb2ce74
48b0464
bfd7dfd
3a34f41
671920a
 
3a34f41
ef210ec
bfd7dfd
 
48b0464
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
import gradio as gr
import transformers
# import tokenizers
import torch
from transformers import pipeline, set_seed
from transformers import GPT2Model, GPT2Config, GPT2LMHeadModel, AutoModel
from transformers import BertTokenizerFast, BertTokenizer
# https://huggingface.co/docs/hub/spaces-sdks-gradio

# tokenizer_bert = BertTokenizer.from_pretrained('bert-base-chinese',
#     additional_special_tokens=["<s>","<pad>","</s>","<unk>","<mask>"],
#     pad_token='<pad>', max_len=512)

# configuration = GPT2Config(vocab_size=25000, n_layer=8)
# model = GPT2LMHeadModel(config=configuration)
# path2pytorch_model = "/home/binxuwang/Datasets/ancChn_L8_LB_cont_output/checkpoint-100000/pytorch_model.bin"
# model.load_state_dict(torch.load(path2pytorch_model))
# model.from_pretrained("binxu/Ziyue-GPT2")
#%%
# model = GPT2LMHeadModel.from_pretrained("binxu/Ziyue-GPT2")
model = GPT2LMHeadModel.from_pretrained("binxu/Ziyue-GPT2-deep")
generator = pipeline('text-generation', model=model, tokenizer='bert-base-chinese')

def generate(prompt, num_beams, max_length, repetition_penalty, ):

    torch.manual_seed(42)

    outputs = generator(prompt, max_length=max_length, num_return_sequences=5, num_beams=num_beams, repetition_penalty=repetition_penalty)
    output_texts = [output['generated_text'] for output in outputs]
    output_all = "\n\n".join(output_texts)
    return output_all

examples = [["子曰", 10, 50, 1.5, ],
           ["子墨子曰", 10, 50, 1.5, ],
           ["孟子", 10, 50, 1.5, ],
            ["秦王", 10, 50, 1.5, ],
            ["子路问仁", 10, 50, 1.5, ],
            ["孙行者笑道", 10, 50, 1.5, ],
            ["牛魔王与红孩儿", 10, 50, 1.5, ],
            ["鲲鹏", 10, 50, 1.5, ],
            ["宝玉道", 10, 50, 1.5, ],
            ["黛玉行至贾母处", 10, 50, 1.5, ],]


iface = gr.Interface(fn=generate,
                     inputs=[gr.inputs.Textbox(lines=2, label="Prompt"),
                             gr.inputs.Slider(minimum=1, maximum=20, default=10, step=1, label="Number of beams"),
                              gr.inputs.Slider(minimum=10, maximum=100, default=50, step=1, label="Max length"),
                              gr.inputs.Slider(minimum=1, maximum=5, default=1.5, label="Repetition penalty"),
                              ],
                     outputs=gr.outputs.Textbox(label="Generated Text"),
                     examples=examples)
iface.launch()