【AI】实现中文文章摘要的AI模型

2025-04-18T15:18:38+08:00 | 2分钟阅读 | 更新于 2025-04-18T15:18:38+08:00

Macro Zhao

【AI】实现中文文章摘要的AI模型

推荐超级课程:

@TOC

简介

最近逛github查看AI文章摘要的模型,发现了这个模型:

@misc{alpaca,
  author={Ziang Leng, Qiyuan Chen and Cheng Li},
  title = {Luotuo: An Instruction-following Chinese Language model, LoRA tuning on LLaMA},
  year = {2023},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/LC1332/Luotuo-Chinese-LLM}},
}

虽然是2年前(2023年)的项目,但是,运行试了下,文章摘要效果还是不错的。

下面记述一下测试步骤。

更详细内容可以参考源项目地址: https://github.com/LC1332/Luotuo-Silk-Road

安装库

!git clone https://github.com/LC1332/luotuo-silk-road.git ./luotuo_silk_road
!wget https://github.com/LC1332/Luotuo-Chinese-LLM/raw/main/notebook/utils.py
!cd luotuo_silk_road/TuoLing
!pip install bitsandbytes transformers==4.27.1 peft==0.4.0 sentencepiece cpm_kernels mdtex2html protobuf torch

注意: 这里由于本身项目的requirement.txt里的peft的版本用的当时的比较老的版本,因此这里我指定到了0.4.0。

加载模型

import os
import torch
from utils import DeviceMap
from transformers import AutoModel, AutoTokenizer

torch.set_default_tensor_type(torch.cuda.HalfTensor)

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)

model = AutoModel.from_pretrained(
    "THUDM/chatglm-6b", 
    trust_remote_code=True, 
    device_map=DeviceMap("ChatGLM").get()
)

获取 PEFT 模型

from peft import get_peft_model, LoraConfig, TaskType

peft_path = "./luotuo_silk_road/TuoLing/output/luotuoC.pt"

peft_config = LoraConfig(
    task_type=TaskType.CAUSAL_LM,
    inference_mode=True,
    r=8,
    lora_alpha=32,
    lora_dropout=0.1,
)

model = get_peft_model(model, peft_config)

model.load_state_dict(torch.load(peft_path), strict=False)

torch.set_default_tensor_type(torch.cuda.FloatTensor)
    

测试模型


from luotuo_silk_road.TuoLing.cover_alpaca2jsonl import format_example


def evaluate(instruction, input=None):
    with torch.no_grad():
        feature = format_example(
            {"instruction": "请用20个字以内帮我总结以下内容:", "output": "", "input": f"{instruction}"}
        )
        input_text = feature["context"]
        input_ids = tokenizer.encode(input_text, return_tensors="pt")
        out = model.generate(input_ids=input_ids, max_length=2048, temperature=0)
        answer = tokenizer.decode(out[0])
        print(answer)

evaluate(input("""请输入要进行总结的文章内容(长度为2048以内):"""))

© 2011 - 2025 Macro Zhao的分享站

关于我

如遇到加载502错误,请尝试刷新😄

Hi,欢迎访问 Macro Zhao 的博客。Macro Zhao(或 Macro)是我在互联网上经常使用的名字。

我是一个热衷于技术探索和分享的IT工程师,在这里我会记录分享一些关于技术、工作和生活上的事情。

我的CSDN博客:
https://macro-zhao.blog.csdn.net/

欢迎你通过评论或者邮件与我交流。
Mail Me

推荐好玩(You'll Like)
  • AI 动·画
    • 这是一款有趣·免费的能让您画的画中的角色动起来的AI工具。
    • 支持几十种动作生成。
我的项目(My Projects)
  • 爱学习网

  • 小乙日语App

    • 这是一个帮助日语学习者学习日语的App。
      (当然初衷也是为了自用😄)
    • 界面干净,简洁,漂亮!
    • 其中包含 N1 + N2 的全部单词和语法。
    • 不需注册,更不需要订阅!完全免费!
  • 小乙日文阅读器

    • 词汇不够?照样能读日语名著!
    • 越读积累越多,积跬步致千里!
    • 哪里不会点哪里!妈妈再也不担心我读不了原版读物了!
赞助我(Sponsor Me)

如果你喜欢我的作品或者发现它们对你有所帮助,可以考虑给我买一杯咖啡 ☕️。这将激励我在未来创作和分享更多的项目和技术。🦾

👉 请我喝一杯咖啡

If you like my works or find them helpful, please consider buying me a cup of coffee ☕️. It inspires me to create and share more projects in the future. 🦾

👉 Buy me a coffee