★★★ 本文源自AI Studio社区精品项目,【点击此处】查看更多精品内容 >>>


基于文心大模型的剧本杀海报生成器(武侠篇)

一、技术路线

  作为一个娱乐项目,剧本杀在近年来很快流行起来,本项目依托文心大模型,支持低成本剧本杀产品落地、宣发阶段业务需求,低成本快速产图,一键式生成剧本杀海报,根据剧本内容一键提取剧本杀摘要/关键词+根据出现最多的主题词生成图片,完成文配图,生成简易的海报。

  本次主要使用文心大模型结合paddlenlp,如果文章在1000字以内可以使用文心大模型的摘要生成(我觉得这个是可以升级的点,比如支持更多的字数),首先使用PaddleNLP对整个小说进行摘要提取,其次根据摘要提取的关键词生成海报。

二、代码实现

  首先安装wenxin_api,接着使用PaddleNLP的Taskflow功能对文章提取摘要,最后再使用文心大模型的AI作图,生成海报图。

! pip install wenxin_api

import wenxin_api # 可以通过"pip install wenxin-api"命令安装
from wenxin_api.tasks.text_to_image import TextToImage
from paddlenlp import Taskflow
summarizer = Taskflow("text_summarization")

wenxin_api.ak = ""
wenxin_api.sk = ""

test_text = 'duoqingjiankewuqingjian_gulong.txt'
with open(test_text,'r') as f:
    text = f.read()

title = summarizer(text)
print(f'Title: {title[0]}')


input_dict = {
    "text": title[0] + "超高清,超细节,唯美,插画,壁纸,背影",
    "style": "古风", #解锁更多风格后,非必选参数
    "resolution":"1024*1536" , #也可设置为 1024*1536、1536*1024
    "num": "4",    #功能解锁后,可设置的范围为[1,2,3,4,5,6]
}
rst = TextToImage.create(**input_dict)
print(rst)
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: wenxin_api in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (0.1.7)
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from wenxin_api) (4.64.1)
Requirement already satisfied: requests>=2.20 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from wenxin_api) (2.24.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin_api) (2019.9.11)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin_api) (1.25.11)
Requirement already satisfied: idna<3,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin_api) (2.8)
Requirement already satisfied: chardet<4,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin_api) (3.0.4)

[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip available: [0m[31;49m22.1.2[0m[39;49m -> [0m[32;49m22.3.1[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m


[2023-01-06 18:57:54,805] [    INFO] - Downloading tokenizer_config.json from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/tokenizer_config.json
100%|██████████| 2.00/2.00 [00:00<00:00, 1.37kB/s]


We use pattern recognition to recognize the Tokenizer class.


[2023-01-06 18:57:54,896] [    INFO] - We are using <class 'paddlenlp.transformers.pegasus.tokenizer.PegasusChineseTokenizer'> to load 'IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese'.
[2023-01-06 18:57:54,899] [    INFO] - Downloading https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/vocab.txt and saved to /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese
[2023-01-06 18:57:54,902] [    INFO] - Downloading vocab.txt from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/vocab.txt
100%|██████████| 365k/365k [00:00<00:00, 1.40MB/s]
[2023-01-06 18:57:55,319] [    INFO] - Downloading https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/added_tokens.json and saved to /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese
[2023-01-06 18:57:55,321] [    INFO] - Downloading added_tokens.json from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/added_tokens.json
100%|██████████| 2.00/2.00 [00:00<00:00, 1.46kB/s]
[2023-01-06 18:57:55,408] [    INFO] - Downloading https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/special_tokens_map.json and saved to /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese
[2023-01-06 18:57:55,410] [    INFO] - Downloading special_tokens_map.json from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/special_tokens_map.json
100%|██████████| 66.0/66.0 [00:00<00:00, 41.5kB/s]
[2023-01-06 18:57:55,529] [    INFO] - Already cached /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/tokenizer_config.json
[2023-01-06 18:57:55,596] [    INFO] - Downloading model_config.json from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/model_config.json
100%|██████████| 732/732 [00:00<00:00, 480kB/s]
[2023-01-06 18:57:55,714] [    INFO] - We are using <class 'paddlenlp.transformers.pegasus.modeling.PegasusForConditionalGeneration'> to load 'IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese'.
[2023-01-06 18:57:55,716] [    INFO] - Downloading https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/model_state.pdparams and saved to /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese
[2023-01-06 18:57:55,718] [    INFO] - Downloading model_state.pdparams from https://bj.bcebos.com/paddlenlp/models/community/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/model_state.pdparams
100%|██████████| 1.26G/1.26G [01:47<00:00, 12.7MB/s]
[2023-01-06 18:59:42,848] [    INFO] - Already cached /home/aistudio/.paddlenlp/models/IDEA-CCNL/Randeng-Pegasus-523M-Summary-Chinese/model_config.json
Building prefix dict from the default dictionary ...
Dumping model to file cache /tmp/jieba.cache
Loading model cost 0.758 seconds.
Prefix dict has been built successfully.


Title: 李寻欢的寂寞
2023-01-06 19:01:25,149 - model is painting now!, taskId: 12624128, waiting: 2m
2023-01-06 19:01:45,663 - model is painting now!, taskId: 12624128, waiting: 50s
2023-01-06 19:02:06,096 - model is painting now!, taskId: 12624128, waiting: 50s
{'imgUrls': ['https://wenxin.baidu.com/younger/file/ERNIE-ViLG/ec986708ad955c6f42fadf9681a889f15q', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/ec986708ad955c6f42fadf9681a889f130', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/ec986708ad955c6f42fadf9681a889f1ex', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/ec986708ad955c6f42fadf9681a889f1i4']}
Logo

学大模型,用大模型上飞桨星河社区!每天8点V100G算力免费领!免费领取ERNIE 4.0 100w Token >>>

更多推荐