模型:
Salesforce/mixqg-base
MixQG是一个新的问题生成模型,它在一系列包含不同类型答案的QA数据集上进行了预训练。该模型在论文 MixQG: Neural Question Generation with Mixed Answer Types 中进行了介绍,相关代码已经在 this 的存储库中发布。
使用Huggingface的管道抽象:
from transformers import pipeline nlp = pipeline("text2text-generation", model='Salesforce/mixqg-base', tokenizer='Salesforce/mixqg-base') CONTEXT = "In the late 17th century, Robert Boyle proved that air is necessary for combustion." ANSWER = "Robert Boyle" def format_inputs(context: str, answer: str): return f"{answer} \\n {context}" text = format_inputs(CONTEXT, ANSWER) nlp(text) # should output [{'generated_text': 'Who proved that air is necessary for combustion?'}]
直接使用预训练模型:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained('Salesforce/mixqg-base') model = AutoModelForSeq2SeqLM.from_pretrained('Salesforce/mixqg-base') CONTEXT = "In the late 17th century, Robert Boyle proved that air is necessary for combustion." ANSWER = "Robert Boyle" def format_inputs(context: str, answer: str): return f"{answer} \\n {context}" text = format_inputs(CONTEXT, ANSWER) input_ids = tokenizer(text, return_tensors="pt").input_ids generated_ids = model.generate(input_ids, max_length=32, num_beams=4) output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) print(output) # should output "Who proved that air is necessary for combustion?"
@misc{murakhovska2021mixqg, title={MixQG: Neural Question Generation with Mixed Answer Types}, author={Lidiya Murakhovs'ka and Chien-Sheng Wu and Tong Niu and Wenhao Liu and Caiming Xiong}, year={2021}, eprint={2110.08175}, archivePrefix={arXiv}, primaryClass={cs.CL} }