我如何自动使我的代码以这样一种方式改写输出的段落,即有不止一个可能的输出



我的代码从教科书中获取某些段落。我希望代码能自动使用某种转述工具吐出这段话的几个版本。代码主要是用Python编写的,但在某些方面也使用Js(和Ts(。

我尝试实现了几个完整的模型,包括:;(python(Pytorch、鹦鹉、scpn等,但它很快就变得太复杂了。有人能帮我写一个更好的方法吗,或者帮我使用一个已经制作好的模型吗?谢谢<3

释义不是一件容易的事。你需要检查哪些内容可以从段落中删除,并且仍然有意义。你需要一个懂英语的算法来去除不必要的东西。

考虑到你提到你在python上,你可以随时尝试鹦鹉

您可能想看看https://huggingface.co/tuner007/pegasus_paraphrase

Huggingface拥有大量的ML模型和相关库(在本例中为"transformers"python库(,这些库可以自动从网站下载模型并以非常精简的方式运行。

pegasusrepreset就是其中一个模型。您也可以在上面的链接中阅读模型卡中的代码。

import torch
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
model_name = 'tuner007/pegasus_paraphrase'
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = PegasusTokenizer.from_pretrained(model_name)
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
def get_response(input_text,num_return_sequences,num_beams):
batch = tokenizer([input_text],truncation=True,padding='longest',max_length=60, return_tensors="pt").to(torch_device)
translated = model.generate(**batch,max_length=60,num_beams=num_beams, num_return_sequences=num_return_sequences, temperature=1.5)
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
return tgt_text

样本结果

num_beams = 10
num_return_sequences = 10
context = "The ultimate test of your knowledge is your capacity to convey it to another."
get_response(context,num_return_sequences,num_beams)
# output:
['The test of your knowledge is your ability to convey it.',
'The ability to convey your knowledge is the ultimate test of your knowledge.',
'The ability to convey your knowledge is the most important test of your knowledge.',
'Your capacity to convey your knowledge is the ultimate test of it.',
'The test of your knowledge is your ability to communicate it.',
'Your capacity to convey your knowledge is the ultimate test of your knowledge.',
'Your capacity to convey your knowledge to another is the ultimate test of your knowledge.',
'Your capacity to convey your knowledge is the most important test of your knowledge.',
'The test of your knowledge is how well you can convey it.',
'Your capacity to convey your knowledge is the ultimate test.']

最新更新