#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from transformers import BertTokenizer, BartForConditionalGeneration, Text2TextGenerationPipeline
USERNAME = os.getenv("USERNAME")

# 模型来自：https://huggingface.co/fnlp/bart-base-chinese/tree/main
pretrained_model_name_or_path = rf"D:\Users\{USERNAME}\data\bart-base-chinese"

tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path)
model = BartForConditionalGeneration.from_pretrained(pretrained_model_name_or_path)
text2text_generator = Text2TextGenerationPipeline(model, tokenizer)
result = text2text_generator("北京是[MASK]的首都", max_length=50, do_sample=True)
print(result)

# 上面仅输出一个结果，若要多个结果，可参考：bert/用bert进行mask预测.py

def main():
    pass


if __name__ == '__main__':
    main()

6d4f41ebcd34e0ef62a46a38cb5480ded784db9a
<mask token>


def main():
    pass


<mask token>

<mask token>
print(result)


def main():
    pass


if __name__ == '__main__':
    main()

<mask token>
USERNAME = os.getenv('USERNAME')
pretrained_model_name_or_path = (
    f'D:\\Users\\{USERNAME}\\data\\bart-base-chinese')
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path)
model = BartForConditionalGeneration.from_pretrained(
    pretrained_model_name_or_path)
text2text_generator = Text2TextGenerationPipeline(model, tokenizer)
result = text2text_generator('北京是[MASK]的首都', max_length=50, do_sample=True)
print(result)


def main():
    pass


if __name__ == '__main__':
    main()

import os
from transformers import BertTokenizer, BartForConditionalGeneration, Text2TextGenerationPipeline
USERNAME = os.getenv('USERNAME')
pretrained_model_name_or_path = (
    f'D:\\Users\\{USERNAME}\\data\\bart-base-chinese')
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path)
model = BartForConditionalGeneration.from_pretrained(
    pretrained_model_name_or_path)
text2text_generator = Text2TextGenerationPipeline(model, tokenizer)
result = text2text_generator('北京是[MASK]的首都', max_length=50, do_sample=True)
print(result)


def main():
    pass


if __name__ == '__main__':
    main()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
from transformers import BertTokenizer, BartForConditionalGeneration, Text2TextGenerationPipeline
USERNAME = os.getenv("USERNAME")

# 模型来自：https://huggingface.co/fnlp/bart-base-chinese/tree/main
pretrained_model_name_or_path = rf"D:\Users\{USERNAME}\data\bart-base-chinese"

tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path)
model = BartForConditionalGeneration.from_pretrained(pretrained_model_name_or_path)
text2text_generator = Text2TextGenerationPipeline(model, tokenizer)
result = text2text_generator("北京是[MASK]的首都", max_length=50, do_sample=True)
print(result)

# 上面仅输出一个结果，若要多个结果，可参考：bert/用bert进行mask预测.py

def main():
    pass


if __name__ == '__main__':
    main()
