Views
No views yet
1
2from transformers import GPT2LMHeadModel, AutoTokenizer
3
4model_name_or_path = "isaachong127/gpt2_chinese_with_personal_qqchat_data"#"checkpoint-16000"
5tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
6
7# add the EOS token as PAD token to avoid warnings
8model = GPT2LMHeadModel.from_pretrained(model_name_or_path, pad_token_id=tokenizer.eos_token_id)1txt = """\
2今天
3"""
4# encode context the generation is conditioned on
5input_ids = tokenizer.encode(txt, return_tensors='pt')
6# set no_repeat_ngram_size to 2
7beam_output = model.generate(
8 input_ids,
9 max_length=100,
10 num_beams=5,
11 no_repeat_ngram_size=2,
12 early_stopping=True
13)
14
15print("Output:\n" + 50 * '-')
16print(tokenizer.decode(beam_output[0], skip_special_tokens=True))
171Output:
2----------------------------------------------------------------------------------------------------
3今天 已 经 是 你 的 第 667 次 签 到 啦 ~ 纱 雾 酱 对 乃 的 好 感 度 [ + 10 ] 2021 年 , 要 加 油 哦 ~ ','签 到 ','@ \ u202e