【2022 CCF BDCI 文心大模型创意项目】「乐享词话」——诗词意境辅助记忆工具

唐诗宋词记忆不下来?快来领取你的专属辅助记忆工具!
「乐享词话」允许你将古诗词中的意境提取出来,更直观地感受古诗词中展现的意境,从而实现更好的记忆古诗词!

话不多说,先放创作效果!

春水满四泽,
夏云多奇峰。
秋月扬明晖,
冬岭秀孤松。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这里以陶渊明的四时为例,讲解如何生成专属于你的古诗词意境!

1.首先需要安装文心大模型api,输入

!pip install wenxin-api
import sys 
sys.path.append('/home/aistudio/external-libraries')
!pip install wenxin-api 
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.0)
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: 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)
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: 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.6)

[notice] A new release of pip available: 22.1.2 -> 22.3.1
[notice] To update, run: pip install --upgrade pip

pgrade pip

然后从文心大模型官网获取到你的私钥,放入到模型的配置文件中

2、将你想生成的古诗词放入到项目中

example_2 = [
    春水满四泽,
    夏云多奇峰。
    秋月扬明晖,
    冬岭秀孤松。
]

这里选取陶渊明的四时为样例展示,大家也可以选取自己喜欢的代码哦!

import os
import shutil
import wenxin_api
import requests
import paddlehub as hub
from wenxin_api.tasks.text_to_image import TextToImage


example_1=[
    "彩霞戏翠林","蓝天奔水游","山涛织绢边","画廊百里延"
]
#这里选取陶渊明的四时为样例展示,可以替换哦!
example_2=[
    "春水满四泽","夏云多奇峰","秋月扬明晖","冬岭秀寒松"
    ]


for i, word in enumerate(example_2):
    wenxin_api.ak = "请在https://wenxin.baidu.com/moduleApi/key 中申请AK"
    wenxin_api.sk = "请在https://wenxin.baidu.com/moduleApi/key 中申请SK"
    assert wenxin_api.ak != "请在https://wenxin.baidu.com/moduleApi/key 中申请AK" and wenxin_api.sk != "请在https://wenxin.baidu.com/moduleApi/key 中申请SK", "AK、SK自检不通过!"
    input_dict = {
        "text": word,
        "style": "古风",
        "resolution": "1024*1024"
    }
    rst = TextToImage.create(**input_dict)
    iurl = rst['imgUrls']
    print(iurl)
    for j in range(len(iurl)):
        r = requests.get(iurl[j])
        name = '/home/aistudio/save/'+str(i)+'-'+str(j)+'.png'
        with open(name, 'wb') as f:
            f.write(r.content)
        f.close()

---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

/tmp/ipykernel_163/4207883077.py in <module>
     19     wenxin_api.ak = "请在https://wenxin.baidu.com/moduleApi/key 中申请AK"
     20     wenxin_api.sk = "请在https://wenxin.baidu.com/moduleApi/key 中申请K"
---> 21     assert wenxin_api.ak != "请在https://wenxin.baidu.com/moduleApi/key 中申请AK" and wenxin_api.sk != "请在https://wenxin.baidu.com/moduleApi/key 中申请SK", "AK、SK自检不通过!"
     22     input_dict = {
     23         "text": word,


AssertionError: AK、SK自检不通过!

然后模型会自动进行逐行分割,交由文心大模型进行生成并完成意境图片的输出!

所有图片的输出都可以在save文件夹中查看到。

文心大模型一共可以输出6张图片,你可以根据你所喜欢的图片进行选择哦~

请点击此处查看本环境基本用法.

Please click here for more detailed instructions.

此文章为搬运
原项目链接

Logo

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

更多推荐