用文心大模型生成剪纸风格的城市插画

1、 简介

  • 文心大模型能够根据描述生成图片,对中文支持非常好
  • 这个项目利用文心大模型,尝试生成剪纸风格的城市插画
  • 把文字描述通过API交给模型,然后由模型生成图片
  • 文字描述中带有“黑白”即可生成黑白图片
  • 文字描述中带有“剪纸”模型就会尽可能的生成剪纸风格
  • 经过试验,发现把“黑白”和“剪纸”同时使用,并且使用“粉笔画”类型,生成的效果比较好
  • 这个实验工程使用了一个免费的天气预报接口,为的是随机获取一个城市和当前的天气,并以此生成图片,
    如果你复制了我这个工程,需要把天气预报接口换成你自己的,然后才能运行
    或者,直接交给模型一个城市名称
  • 这个实验工程中用到了文心大模型的开放API,因此需要配置ak和sk

放两张效果图

2、 使用paddlehub 调用文心大模型生成城市风景剪纸

  • 增加一个使用paddlehub调用文心大模型来生成图片的处理办法
  • 参考 PaddleHub:使用文心大模型-ERNIE VILG 进行高质量图文生成
  • 调用的接口函数有个保存图片的动作,图片的文件名就是传入的提示词prompts,
    然而它没有检查提示词是否符合操作系统文件名的规则,所以要特别注意,
    传入的提示词里面不能包含冒号等不能出现在文件名里面的字符
  • 加入提示词“红白”来限制颜色,效果更接近真实的剪纸

2.1 第一步,安装paddlehub

!pip install --upgrade paddlehub
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: paddlehub in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (2.0.4)
Collecting paddlehub
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c3/47/21f5502fe7b03a9b6c25ceec716e2d10027e70dc7676dedce5f024d77d60/paddlehub-2.3.0-py3-none-any.whl (212 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m212.2/212.2 kB[0m [31m3.5 MB/s[0m eta [36m0:00:00[0ma [36m0:00:01[0m
[?25hRequirement already satisfied: filelock in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (3.0.12)
Requirement already satisfied: gunicorn>=19.10.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (20.0.4)
Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (1.19.5)
Requirement already satisfied: pyyaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (5.1.2)
Requirement already satisfied: paddlenlp>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (2.1.1)
Requirement already satisfied: colorama in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (0.4.4)
Requirement already satisfied: easydict in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (1.9)
Requirement already satisfied: Pillow in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (8.2.0)
Requirement already satisfied: pyzmq in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (23.2.0)
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (4.27.0)
Requirement already satisfied: flask>=1.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (1.1.1)
Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (4.1.0)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (4.1.1.26)
Requirement already satisfied: matplotlib in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (2.2.3)
Collecting paddle2onnx>=0.5.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/be/62/bd17eeccd7cfd3601ae8607371673046158d67f48d095c3055edac2e5250/paddle2onnx-0.9.8-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.9 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m2.9/2.9 MB[0m [31m5.2 MB/s[0m eta [36m0:00:00[0m00:01[0m00:01[0m
[?25hRequirement already satisfied: visualdl>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (2.3.0)
Requirement already satisfied: rarfile in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (3.1)
Requirement already satisfied: packaging in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub) (21.3)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub) (7.0)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub) (1.1.0)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub) (0.16.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub) (3.0.0)
Requirement already satisfied: setuptools>=3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from gunicorn>=19.10.0->paddlehub) (41.4.0)
Requirement already satisfied: multiprocess in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp>=2.0.0->paddlehub) (0.70.11.1)
Requirement already satisfied: h5py in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp>=2.0.0->paddlehub) (2.9.0)
Requirement already satisfied: paddlefsl==1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp>=2.0.0->paddlehub) (1.0.0)
Requirement already satisfied: seqeval in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp>=2.0.0->paddlehub) (1.2.2)
Requirement already satisfied: jieba in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp>=2.0.0->paddlehub) (0.42.1)
Requirement already satisfied: requests~=2.24.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlefsl==1.0.0->paddlenlp>=2.0.0->paddlehub) (2.24.0)
Requirement already satisfied: pandas in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddlehub) (1.1.5)
Requirement already satisfied: six>=1.14.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddlehub) (1.16.0)
Requirement already satisfied: protobuf>=3.11.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddlehub) (3.20.0)
Requirement already satisfied: Flask-Babel>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddlehub) (1.0.0)
Requirement already satisfied: bce-python-sdk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0->paddlehub) (0.8.53)
Requirement already satisfied: pytz in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->paddlehub) (2019.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->paddlehub) (1.1.0)
Requirement already satisfied: cycler>=0.10 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->paddlehub) (0.10.0)
Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->paddlehub) (2.8.2)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from matplotlib->paddlehub) (3.0.9)
Requirement already satisfied: Babel>=2.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0->paddlehub) (2.8.0)
Requirement already satisfied: MarkupSafe>=2.0.0rc2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.0->paddlehub) (2.0.1)
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.24.0->paddlefsl==1.0.0->paddlenlp>=2.0.0->paddlehub) (1.25.6)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests~=2.24.0->paddlefsl==1.0.0->paddlenlp>=2.0.0->paddlehub) (2019.9.11)
Requirement already satisfied: idna<3,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests~=2.24.0->paddlefsl==1.0.0->paddlenlp>=2.0.0->paddlehub) (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.24.0->paddlefsl==1.0.0->paddlenlp>=2.0.0->paddlehub) (3.0.4)
Requirement already satisfied: pycryptodome>=3.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0->paddlehub) (3.9.9)
Requirement already satisfied: future>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0->paddlehub) (0.18.0)
Requirement already satisfied: dill>=0.3.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from multiprocess->paddlenlp>=2.0.0->paddlehub) (0.3.3)
Requirement already satisfied: scikit-learn>=0.21.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from seqeval->paddlenlp>=2.0.0->paddlehub) (0.22.1)
Requirement already satisfied: joblib>=0.11 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn>=0.21.3->seqeval->paddlenlp>=2.0.0->paddlehub) (0.14.1)
Requirement already satisfied: scipy>=0.17.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn>=0.21.3->seqeval->paddlenlp>=2.0.0->paddlehub) (1.3.0)
Installing collected packages: paddle2onnx, paddlehub
  Attempting uninstall: paddlehub
    Found existing installation: paddlehub 2.0.4
    Uninstalling paddlehub-2.0.4:
      Successfully uninstalled paddlehub-2.0.4
Successfully installed paddle2onnx-0.9.8 paddlehub-2.3.0

[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.2.2[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m

2.2 第二步:导入需要的包

import json
import PIL.Image as Image
import random
#import numpy as np
from io import BytesIO
#import base64
import requests
import matplotlib.pyplot as plt
#import cv2
#import wenxin_api # 可以通过"pip install wenxin-api"命令安装
#from wenxin_api.tasks.text_to_image import TextToImage

2.3 第三步:配置天气预报接口

  • 我这里使用了一个免费的天气预报接口,有访问次数限制
# 天气api接口配置
tianqi_apicfg = "yiketianqi.com"    # api 配置文件
tianqi_appid,tianqi_appsecret = (None,None)
with open(tianqi_apicfg) as f:
    i = 0    # 行计数,只读取2行
    for line in f:
        i += 1
        if i>2: break
        lbl,val = line.strip().split("=")
        lbl = lbl.strip()
        val = val.strip()
        if lbl == "appid":
            tianqi_appid = val
        elif lbl == "appsecret":
            tianqi_appsecret = val
        # print(line)
#print(f"appid:{tianqi_appid}\nappsecret:{tianqi_appsecret}")

2.4 第四步:随机获取一个城市代码

  • 城市代码放在了 city.json 这个文件,这是我使用的这个天气预报接口提供的,但基本上通用的
# 读取城市列表
city_json = "city.json"
citys = None
with open(city_json) as f:
    citys = json.load(f)
idx = random.randint(0,len(citys)-1)    # 随机选取一个城市
#print(citys[idx])
#city_id = citys[0]["id"]
city_id = citys[idx]["id"]    # 获取城市id
print(city_id)
print(citys[idx]["cityZh"])
101140310
桑日

2.5 第五步:获取天气预报

  • 请使用你自己的天气预报接口,或者直接传入城市名和天气描述
# 读取天气数据

#tianqi_url = f"https://v0.yiketianqi.com/api?unescape=1&version=v61&appid={tianqi_appid}&appsecret={tianqi_appsecret}"
tianqi_url = f"https://www.yiketianqi.com/free/day?appid={tianqi_appid}&appsecret={tianqi_appsecret}&unescape=1&cityid={city_id}"
tianqi_resp = requests.get(tianqi_url)
tianqi_resp = tianqi_resp.json()
print(tianqi_resp)
{'nums': 2, 'cityid': '101140310', 'city': '桑日', 'date': '2022-08-25', 'week': '星期四', 'update_time': '19:51', 'wea': '多云', 'wea_img': 'yun', 'tem': '23', 'tem_day': '25', 'tem_night': '11', 'win': '北风', 'win_speed': '2级', 'win_meter': '9km/h', 'air': '44', 'pressure': '659', 'humidity': '35%'}

2.6 第六步:提取城市名称和天气状况

# 提取天气字段
txt_tianqi = tianqi_resp["wea"]
txt_city = tianqi_resp["city"]
print(f"{txt_city}:{txt_tianqi}")
桑日:多云

2.7 第七步,通过paddlehub调用文心大模型接口

# 通过paddlehub调用文心大模型
import paddlehub as hub    # 导入paddlehub
ernie_vilg_module = hub.Module(name='ernie_vilg')    # 导入模型

input_txt = f"红白,剪纸,城市:{txt_city},{txt_tianqi}"
results = ernie_vilg_module.generate_image(
    text_prompts=input_txt,    # 提示词
    style="粉笔画",    # 图片类型
    topk = 10,    # 图片数量,最大10
    output_dir='./imgs')    # 图片保存目录
# 返回的results 就是图片列表,PIL的Image格式
Download https://bj.bcebos.com/paddlehub/paddlehub_dev/ernie_vilg.tar.gz
[##################################################] 100.00%
Decompress /home/aistudio/.paddlehub/tmp/tmpck579ycq/ernie_vilg.tar.gz
[##################################################] 100.00%


[2022-08-25 20:52:25,869] [    INFO] - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: | - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: / - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: - - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: | - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: / - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: - - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: | - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: / - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: - - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: | - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: / - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: - - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: | - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: / - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: - - Installing dependent packages from /home/aistudio/.paddlehub/tmp/tmphvvdi0lv/ernie_vilg/requirements.txt: \ - Successfully installed dependent packages.
[2022-08-25 20:52:26,030] [    INFO] - Successfully installed ernie_vilg-1.0.0



HBox(children=(IntProgress(value=0), HTML(value='')))


Saving Images...
Done

2.8 显示图片

# 将结果显示出来

# 定义一个用于显示图片的函数
def show_pic(pic):
    plt.figure(dpi = 144)
    plt.axis('off')
    plt.imshow(pic)

for pic in results:
    show_pic(pic)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3、 使用文心大模型开放API生成城市风景剪纸

  • 这里使用了“黑白”来限制颜色,比“红白”保险,但不如“红白”效果好

3.1 第一步:安装文心API

  • 因为是直接调用API,因此不需要GPU环境,但是需要安装文心API包
# 安装文心api
!pip install --upgrade wenxin-api

3.2 第二步:准备一个图片显示函数

# 图片显示函数
def show_img(img_path):
    response = requests.get(img_path) #图片地址
    response = response.content
    BytesIOObj = BytesIO()
    BytesIOObj.write(response)
    img = Image.open(BytesIOObj)
    #img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    #img = img.resize((120,120))
    #plt.figure(dpi = 300)
    plt.figure(dpi = 144)
    plt.axis('off')
    plt.imshow(img) 

3.3 第三步:配置文心API

  • 先要去文心官网 https://wenxin.baidu.com/ 申请自己的ak和sk
# 模型api接口配置
import wenxin_api # 可以通过"pip install wenxin-api"命令安装
from wenxin_api.tasks.text_to_image import TextToImage

# 我这里把ak和sk放到了一个文件里,请使用自己申请到的ak和sk
# 文件内只有两行,形如    ak = XXXXX
wenxin_apicfg = "wenxin.baidu.com"    # api 配置文件
with open(wenxin_apicfg) as f:
    i = 0    # 行计数,只读取2行
    for line in f:
        i += 1
        if i>2: break
        lbl,val = line.strip().split("=")
        lbl = lbl.strip()
        val = val.strip()
        if lbl == "ak":
            wenxin_api.ak = val
        elif lbl == "sk":
            wenxin_api.sk = val
        # print(line)
#print(f"wenxin_ak:{wenxin_api.ak}\nwenxin_sk:{wenxin_api.sk}")

3.4 第四步:调用模型生成图片,并显示出来

  • “黑白”效果比较保险,但“红白”更接近真实剪纸效果
# 调用模型
# -*- coding: utf-8 -*
#import wenxin_api # 可以通过"pip install wenxin-api"命令安装
#from wenxin_api.tasks.text_to_image import TextToImage
#wenxin_api.ak = ""
#wenxin_api.sk = ""

input_txt = f"黑白,剪纸,城市:{txt_city},{txt_tianqi}"
input_dict = {
    "text": input_txt,
    "style": "粉笔画"
}
rst = TextToImage.create(**input_dict)    # 转换成关键字参数传递给接口
# rst dict类型
#print(rst)

# 显示图片
imgUrls = rst["imgUrls"]    # 提取图片地址,list格式

for imurl in imgUrls:
    #print(imurl)
    show_img(imurl)
2022-08-25 12:45:36,668 - model is painting now!, taskId: 1055201, waiting: 30s
2022-08-25 12:45:56,849 - model is painting now!, taskId: 1055201, waiting: 30s

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4、 调整一下输入的提示词,还可以生成其他风格的画面

4.1 苏绣风格
# 通过paddlehub调用文心大模型
import paddlehub as hub    # 导入paddlehub
ernie_vilg_module = hub.Module(name='ernie_vilg')    # 导入模型

input_txt = f"苏绣,城市:{txt_city},{txt_tianqi}"
results = ernie_vilg_module.generate_image(
    text_prompts=input_txt,    # 提示词
    style="粉笔画",    # 图片类型
    topk = 10,    # 图片数量,最大10
    output_dir='./imgs')    # 图片保存目录
# 返回的results 就是图片列表,PIL的Image格式

# 将结果显示出来

# 定义一个用于显示图片的函数
def show_pic(pic):
    plt.figure(dpi = 144)
    plt.axis('off')
    plt.imshow(pic)

for pic in results:
    show_pic(pic)
HBox(children=(IntProgress(value=0), HTML(value='')))


Saving Images...
Done

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.2 刺绣风格
#  “刺绣”
input_txt = f"刺绣,城市:{txt_city},{txt_tianqi}"
results = ernie_vilg_module.generate_image(
    text_prompts=input_txt,    # 提示词
    style="粉笔画",    # 图片类型
    topk = 10,    # 图片数量,最大10
    output_dir='./imgs')    # 图片保存目录
# 返回的results 就是图片列表,PIL的Image格式

# 将结果显示出来
for pic in results:
# 将结果显示出来
for pic in results:
    show_pic(pic)
HBox(children=(IntProgress(value=0), HTML(value='')))


Saving Images...
Done

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

5、 总结

  • 两种方式可以调用文心大模型:使用开放API,或者通过Paddlehub
  • 使用开放API需要申请ak和sk,通过paddlehub调用不需要
  • 使用paddlehub时,要注意保存的文件名问题,否则无法保存图片
  • 就 剪纸 效果来看,目前以“粉笔画”生成的效果最好
  • 可以传入颜色描述,比如“红白”来控制图片的颜色

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

Please click here for more detailed instructions.

此文章为搬运
原项目链接

Logo

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

更多推荐