File size: 1,477 Bytes
5c2dae5
 
6d954e4
 
 
 
 
48b0464
5c2dae5
48b0464
 
 
5c2dae5
48b0464
 
 
59a1177
 
 
 
48b0464
 
 
 
59a1177
 
 
 
 
bfd7dfd
48b0464
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
import gradio as gr
import transformers
# import tokenizers
import torch
from transformers import pipeline, set_seed
from transformers import GPT2Model, GPT2Config, GPT2LMHeadModel
from transformers import BertTokenizerFast
# https://huggingface.co/docs/hub/spaces-sdks-gradio

tokenizer_bert = BertTokenizerFast.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 = "pytorch_model.bin"
# 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"))

generator = pipeline('text-generation', model=model, tokenizer=tokenizer_bert)

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

examples = ["子曰", "子墨子曰", "孟子", "秦王", "子路问仁"]

iface = gr.Interface(fn=generate,
                     inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
                     outputs=gr.outputs.Textbox(label="Generated Text"),
                     examples=examples)
iface.launch()