module.py 13.8 KB
Newer Older
C
chenjian 已提交
1 2
import argparse
import ast
3
import base64
C
chenjian 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import os
import re
import sys
import time
from functools import partial
from io import BytesIO
from typing import List
from typing import Optional

import requests
from PIL import Image
from tqdm.auto import tqdm

import paddlehub as hub
from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving


@moduleinfo(name="ernie_vilg",
C
chenjian 已提交
24
            version="1.1.0",
C
chenjian 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            type="image/text_to_image",
            summary="",
            author="baidu-nlp",
            author_email="paddle-dev@baidu.com")
class ErnieVilG:

    def __init__(self, ak=None, sk=None):
        """
      :param ak: ak for applying token to request wenxin api.
      :param sk: sk for applying token to request wenxin api.
      """
        if ak is None or sk is None:
            self.ak = 'G26BfAOLpGIRBN5XrOV2eyPA25CE01lE'
            self.sk = 'txLZOWIjEqXYMU3lSm05ViW4p9DWGOWs'
        else:
            self.ak = ak
            self.sk = sk
        self.token_host = 'https://wenxin.baidu.com/younger/portal/api/oauth/token'
        self.token = self._apply_token(self.ak, self.sk)

    def _apply_token(self, ak, sk):
        if ak is None or sk is None:
            ak = self.ak
            sk = self.sk
        response = requests.get(self.token_host,
                                params={
                                    'grant_type': 'client_credentials',
                                    'client_id': ak,
                                    'client_secret': sk
                                })
        if response:
            res = response.json()
            if res['code'] != 0:
                print('Request access token error.')
                raise RuntimeError("Request access token error.")
        else:
            print('Request access token error.')
            raise RuntimeError("Request access token error.")
        return res['data']

    def generate_image(self,
                       text_prompts,
C
chenjian 已提交
67 68
                       style: Optional[str] = "探索无限",
                       resolution: Optional[str] = "1024*1024",
C
chenjian 已提交
69
                       topk: Optional[int] = 6,
70
                       visualization: Optional[bool] = True,
C
chenjian 已提交
71 72 73 74 75
                       output_dir: Optional[str] = 'ernievilg_output'):
        """
        Create image by text prompts using ErnieVilG model.

        :param text_prompts: Phrase, sentence, or string of words and phrases describing what the image should look like.
C
chenjian 已提交
76 77 78
        :param style: Image stype, currently supported 古风、油画、水彩、卡通、二次元、浮世绘、蒸汽波艺术、
        low poly、像素风格、概念艺术、未来主义、赛博朋克、写实风格、洛丽塔风格、巴洛克风格、超现实主义、探索无限。
        :param resolution: Resolution of images, currently supported "1024*1024", "1024*1536", "1536*1024".
C
chenjian 已提交
79
        :param topk: Top k images to save.
80
        :param visualization: Whether to save images or not.
C
chenjian 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        :output_dir: Output directory
        """
        if not os.path.exists(output_dir):
            os.makedirs(output_dir, exist_ok=True)
        token = self.token
        create_url = 'https://wenxin.baidu.com/younger/portal/api/rest/1.0/ernievilg/v1/txt2img?from=paddlehub'
        get_url = 'https://wenxin.baidu.com/younger/portal/api/rest/1.0/ernievilg/v1/getImg?from=paddlehub'
        if isinstance(text_prompts, str):
            text_prompts = [text_prompts]
        taskids = []
        for text_prompt in text_prompts:
            res = requests.post(create_url,
                                headers={'Content-Type': 'application/x-www-form-urlencoded'},
                                data={
                                    'access_token': token,
                                    "text": text_prompt,
C
chenjian 已提交
97 98
                                    "style": style,
                                    "resolution": resolution
C
chenjian 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
                                })
            res = res.json()
            if res['code'] == 4001:
                print('请求参数错误')
                raise RuntimeError("请求参数错误")
            elif res['code'] == 4002:
                print('请求参数格式错误,请检查必传参数是否齐全,参数类型等')
                raise RuntimeError("请求参数格式错误,请检查必传参数是否齐全,参数类型等")
            elif res['code'] == 4003:
                print('请求参数中,图片风格不在可选范围内')
                raise RuntimeError("请求参数中,图片风格不在可选范围内")
            elif res['code'] == 4004:
                print('API服务内部错误,可能引起原因有请求超时、模型推理错误等')
                raise RuntimeError("API服务内部错误,可能引起原因有请求超时、模型推理错误等")
            elif res['code'] == 100 or res['code'] == 110 or res['code'] == 111:
                token = self._apply_token(self.ak, self.sk)
                res = requests.post(create_url,
                                    headers={'Content-Type': 'application/x-www-form-urlencoded'},
                                    data={
                                        'access_token': token,
                                        "text": text_prompt,
C
chenjian 已提交
120 121
                                        "style": style,
                                        "resolution": resolution
C
chenjian 已提交
122 123 124 125 126
                                    })
                res = res.json()
                if res['code'] != 0:
                    print("Token失效重新请求后依然发生错误,请检查输入的参数")
                    raise RuntimeError("Token失效重新请求后依然发生错误,请检查输入的参数")
C
chenjian 已提交
127 128 129 130 131
            if res['msg'] == 'success':
                taskids.append(res['data']["taskId"])
            else:
                print(res['msg'])
                raise RuntimeError(res['msg'])
C
chenjian 已提交
132 133 134 135

        start_time = time.time()
        process_bar = tqdm(total=100, unit='%')
        results = {}
C
chenjian 已提交
136
        total_time = 60 * len(taskids)
C
chenjian 已提交
137
        while True:
C
chenjian 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            end_time = time.time()
            duration = end_time - start_time
            progress_rate = int((duration) / total_time * 100)
            if not taskids:
                progress_rate = 100
            if progress_rate > process_bar.n:
                if progress_rate >= 100:
                    if not taskids:
                        increase_rate = 100 - process_bar.n
                    else:
                        increase_rate = 0
                else:
                    increase_rate = progress_rate - process_bar.n
            else:
                increase_rate = 0
            process_bar.update(increase_rate)
            if duration < 30:
                time.sleep(5)
                continue
            else:
                time.sleep(6)
C
chenjian 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            if not taskids:
                break
            has_done = []
            for taskid in taskids:
                res = requests.post(get_url,
                                    headers={'Content-Type': 'application/x-www-form-urlencoded'},
                                    data={
                                        'access_token': token,
                                        'taskId': {taskid}
                                    })
                res = res.json()
                if res['code'] == 4001:
                    print('请求参数错误')
                    raise RuntimeError("请求参数错误")
                elif res['code'] == 4002:
                    print('请求参数格式错误,请检查必传参数是否齐全,参数类型等')
                    raise RuntimeError("请求参数格式错误,请检查必传参数是否齐全,参数类型等")
                elif res['code'] == 4003:
                    print('请求参数中,图片风格不在可选范围内')
                    raise RuntimeError("请求参数中,图片风格不在可选范围内")
                elif res['code'] == 4004:
                    print('API服务内部错误,可能引起原因有请求超时、模型推理错误等')
                    raise RuntimeError("API服务内部错误,可能引起原因有请求超时、模型推理错误等")
                elif res['code'] == 100 or res['code'] == 110 or res['code'] == 111:
                    token = self._apply_token(self.ak, self.sk)
                    res = requests.post(get_url,
                                        headers={'Content-Type': 'application/x-www-form-urlencoded'},
                                        data={
                                            'access_token': token,
                                            'taskId': {taskid}
                                        })
                    res = res.json()
                    if res['code'] != 0:
                        print("Token失效重新请求后依然发生错误,请检查输入的参数")
                        raise RuntimeError("Token失效重新请求后依然发生错误,请检查输入的参数")
C
chenjian 已提交
194 195 196 197 198 199 200 201 202 203 204
                if res['msg'] == 'success':
                    if res['data']['status'] == 1:
                        has_done.append(res['data']['taskId'])
                    results[res['data']['text']] = {
                        'imgUrls': res['data']['imgUrls'],
                        'waiting': res['data']['waiting'],
                        'taskId': res['data']['taskId']
                    }
                else:
                    print(res['msg'])
                    raise RuntimeError(res['msg'])
C
chenjian 已提交
205 206 207 208 209 210
            for taskid in has_done:
                taskids.remove(taskid)
        print('Saving Images...')
        result_images = []
        for text, data in results.items():
            for idx, imgdata in enumerate(data['imgUrls']):
C
chenjian 已提交
211 212 213 214 215 216 217 218
                try:
                    image = Image.open(BytesIO(requests.get(imgdata['image']).content))
                except Exception as e:
                    print('Download generated images error, retry one time')
                    try:
                        image = Image.open(BytesIO(requests.get(imgdata['image']).content))
                    except Exception:
                        raise RuntimeError('Download generated images failed.')
219 220
                if visualization:
                    image.save(os.path.join(output_dir, '{}_{}.png'.format(text, idx)))
C
chenjian 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
                result_images.append(image)
                if idx + 1 >= topk:
                    break
        print('Done')
        return result_images

    @runnable
    def run_cmd(self, argvs):
        """
        Run as a command.
        """
        self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
                                              prog='hub run {}'.format(self.name),
                                              usage='%(prog)s',
                                              add_help=True)
        self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
        self.add_module_input_arg()
        args = self.parser.parse_args(argvs)
        if args.ak is not None and args.sk is not None:
            self.ak = args.ak
            self.sk = args.sk
            self.token = self._apply_token(self.ak, self.sk)
        results = self.generate_image(text_prompts=args.text_prompts,
                                      style=args.style,
C
chenjian 已提交
245
                                      resolution=args.resolution,
C
chenjian 已提交
246
                                      topk=args.topk,
247
                                      visualization=args.visualization,
C
chenjian 已提交
248 249 250
                                      output_dir=args.output_dir)
        return results

251 252 253 254 255 256 257 258 259 260 261 262 263 264
    @serving
    def serving_method(self, text_prompts, **kwargs):
        """
        Run as a service.
        """
        results_base64encoded = []
        results = self.generate_image(text_prompts=text_prompts, **kwargs)
        for result in results:
            buffered = BytesIO()
            result.save(buffered, format="png")
            img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
            results_base64encoded.append(img_str)
        return results_base64encoded

C
chenjian 已提交
265 266 267 268 269 270 271
    def add_module_input_arg(self):
        """
        Add the command input options.
        """
        self.arg_input_group.add_argument('--text_prompts', type=str)
        self.arg_input_group.add_argument('--style',
                                          type=str,
C
chenjian 已提交
272 273 274 275 276
                                          default='探索无限',
                                          choices=[
                                              '古风', '油画', '水彩', '卡通', '二次元', '浮世绘', '蒸汽波艺术', 'low poly', '像素风格', '概念艺术',
                                              '未来主义', '赛博朋克', '写实风格', '洛丽塔风格', '巴洛克风格', '超现实主义', '探索无限'
                                          ],
C
chenjian 已提交
277
                                          help="绘画风格")
C
chenjian 已提交
278 279 280 281 282
        self.arg_input_group.add_argument('--resolution',
                                          type=str,
                                          default='1024*1024',
                                          choices=['1024*1024', '1024*1536', '1536*1024'],
                                          help="图像分辨率")
C
chenjian 已提交
283
        self.arg_input_group.add_argument('--topk', type=int, default=6, help="选取保存前多少张图,最多10张")
C
chenjian 已提交
284 285
        self.arg_input_group.add_argument('--ak', type=str, default=None, help="申请文心api使用token的ak")
        self.arg_input_group.add_argument('--sk', type=str, default=None, help="申请文心api使用token的sk")
286
        self.arg_input_group.add_argument('--visualization', type=bool, default=True, help="是否保存生成的图片")
C
chenjian 已提交
287
        self.arg_input_group.add_argument('--output_dir', type=str, default='ernievilg_output')