利用T5大模型读一段文章,自动生成一段摘要。
本次测试环境为windows10环境,python版本为3.8.7。
1、安装pytorch
pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/cpu/torch_stable.html
2、安装transformers库
pip install transformers
pip install sentencepiece
1,在notepad++中编写代码t5.py
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
def summarize_text(text, max_length=50, min_length=25):
# 加载预训练的T5模型和分词器
model = T5ForConditionalGeneration.from_pretrained("t5-small")
tokenizer = T5Tokenizer.from_pretrained("t5-small")
# 对输入文本进行编码
input_text = tokenizer.encode("summarize: " + text, return_tensors="pt")
# 生成摘要
with torch.no_grad():
summary_ids = model.generate(
input_text,
max_length=max_length,
min_length=min_length,
num_return_sequences=1,
no_repeat_ngram_size=3
)
# 将摘要ID解码为文本
summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary_text
if __name__ == "__main__":
text = "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower. Constructed from 1887 to 1889 as the entrance to the 1889 World's Fair, it was initially criticized by some of France's leading artists and intellectuals for its design, but it has become a global cultural icon of France and one of the most recognizable structures in the world."
summary = summarize_text(text)
print(f"生成的摘要:{summary}")

2、运行程序
D:\>python t5.py
3、查看结果
生成的摘要:the tower was designed by the engineer Gustave Eiffel. it was initially criticized by some of France's leading artists and intellectuals.
1、T5大模型
2、T5大模型和gpt3大模型的区别
Chatbot GPT(例如 GPT-3)和 T5 都是基于 Transformer 架构的大型自然语言处理模型。它们在许多自然语言处理任务上都表现出色。然而,它们之间存在一些关键区别:
3、T5大模型和gpt3大模型的相同点
code/s?__biz=MzI1ODEyNzM2Nw==&mid=2247484374&idx=1&sn=ff72231d913ab8c1da6accc039e5440c&chksm=ea0da0bedd7a29a8a35553375ced4396e368589ec8a428d565669fb390b28053db98e245d90b#rd