File size: 9,243 Bytes
69a6cef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import glob
import io
import json
import logging
import os
import shutil
from dataclasses import dataclass
from textwrap import dedent
from typing import List, Union, Optional

import yaml
from PIL.PngImagePlugin import PngInfo
from imgutils.detect import detect_censors

try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper
from PIL import Image
from hbutils.system import TemporaryDirectory
from hcpdiff import Visualizer
from hcpdiff.utils import load_config_with_cli

from ..utils import data_to_cli_args

_DEFAULT_INFER_CFG_FILE = 'cfgs/infer/text2img_anime_lora.yaml'
_DEFAULT_INFER_MODEL = 'LittleApple-fp16/SpiritForeseerMix'


def sample_method_to_config(method):
    if method == 'DPM++ SDE Karras':
        return {
            '_target_': 'diffusers.DPMSolverSDEScheduler',
            'beta_start': 0.00085,
            'beta_end': 0.012,
            'beta_schedule': 'scaled_linear',
            'use_karras_sigmas': True,
        }
    elif method == 'DPM++ 2M Karras':
        return {
            '_target_': 'diffusers.DPMSolverMultistepScheduler',
            'beta_start': 0.00085,
            'beta_end': 0.012,
            'algorithm_type': 'dpmsolver++',
            'beta_schedule': 'scaled_linear',
            'use_karras_sigmas': True
        }
    elif method == 'Euler a':
        return {
            '_target_': 'diffusers.EulerAncestralDiscreteScheduler',
            'beta_start': 0.00085,
            'beta_end': 0.012,
            'beta_schedule': 'scaled_linear',
        }
    else:
        raise ValueError(f'Unknown sample method - {method!r}.')


def draw_images(
        workdir: str, prompts: Union[str, List[str]], neg_prompts: Union[str, List[str]] = None,
        seeds: Union[int, List[str]] = None, emb_name: str = None, save_cfg: bool = True,
        model_steps: int = 1000, n_repeats: int = 2, pretrained_model: str = _DEFAULT_INFER_MODEL,
        width: int = 512, height: int = 768, gscale: float = 8, infer_steps: int = 30,
        lora_alpha: float = 0.85, output_dir: str = 'output', cfg_file: str = _DEFAULT_INFER_CFG_FILE,
        clip_skip: int = 2, sample_method: str = 'DPM++ 2M Karras',
):
    emb_name = emb_name or os.path.basename(workdir)
    with TemporaryDirectory() as emb_dir:
        src_pt_files = glob.glob(os.path.join(workdir, 'ckpts', f'*-{model_steps}.pt'))
        if not src_pt_files:
            raise FileNotFoundError(f'Embedding not found for step {model_steps}.')

        src_pt_file = src_pt_files[0]
        shutil.copyfile(src_pt_file, os.path.join(emb_dir, f'{emb_name}.pt'))

        cli_args = data_to_cli_args({
            'pretrained_model': pretrained_model,
            'N_repeats': n_repeats,

            'vae_optimize': {
                'tiling': False,
            },

            'clip_skip': clip_skip - 1,

            'bs': 1,
            'num': 1,

            'infer_args': {
                'width': width,
                'height': height,
                'guidance_scale': gscale,
                'num_inference_steps': infer_steps,
            },

            'exp_dir': workdir,
            'model_steps': model_steps,
            'emb_dir': emb_dir,
            'output_dir': output_dir,

            'merge': {
                'alpha': lora_alpha,
            },

            'new_components': {
                'scheduler': sample_method_to_config(sample_method),
                'vae': {
                    '_target_': 'diffusers.AutoencoderKL.from_pretrained',
                    'pretrained_model_name_or_path': 'deepghs/animefull-latest',  # path to vae model
                    'subfolder': 'vae',
                }
            }
        })
        logging.info(f'Infer based on {cfg_file!r}, with {cli_args!r}')
        cfgs = load_config_with_cli(cfg_file, args_list=cli_args)  # skip --cfg

        N = None
        if isinstance(prompts, list):
            N = len(prompts)
        if isinstance(neg_prompts, list):
            if N is not None and len(neg_prompts) != N:
                raise ValueError(f'Number of prompts ({len(prompts)}) and neg_prompts ({len(neg_prompts)}) not match.')
            N = len(neg_prompts)
        if isinstance(seeds, list):
            if N is not None and len(seeds) != N:
                raise ValueError(f'Number of both prompts ({N}) and seed ({len(seeds)}) not match.')
            N = len(seeds)

        if N is None:
            N = 1
        if not isinstance(prompts, list):
            prompts = [prompts] * N
        if not isinstance(neg_prompts, list):
            neg_prompts = [neg_prompts] * N
        if not isinstance(seeds, list):
            seeds = [seeds] * N

        viser = Visualizer(cfgs)
        viser.vis_to_dir(prompt=prompts, negative_prompt=neg_prompts, seeds=seeds,
                         save_cfg=save_cfg, **cfgs.infer_args)


@dataclass
class Drawing:
    name: str
    prompt: str
    neg_prompt: str
    seed: int
    sfw: bool
    width: int
    height: int
    gscale: float
    steps: int
    image: Image.Image
    sample_method: str
    clip_skip: int
    model: str
    model_hash: Optional[str] = None

    @property
    def preview_info(self):
        return dedent(f"""
Prompt: {self.prompt}
Neg Prompt: {self.neg_prompt}
Width: {self.width}
Height: {self.height}
Guidance Scale: {self.gscale}
Sample Method: {self.sample_method}
Infer Steps: {self.steps}
Clip Skip: {self.clip_skip}
Seed: {self.seed}
Model: {self.model}
Safe For Work: {"yes" if self.sfw else "no"}
    """).lstrip()

    @property
    def pnginfo_text(self) -> str:
        with io.StringIO() as sf:
            print(self.prompt, file=sf)
            print(f'Negative prompt: {self.neg_prompt}', file=sf)

            if self.model_hash:
                print(f'Steps: {self.steps}, Sampler: {self.sample_method}, '
                      f'CFG scale: {self.gscale}, Seed: {self.seed}, Size: {self.width}x{self.height}, '
                      f'Model hash: {self.model_hash.lower()}, Model: {self.model}, '
                      f'Clip skip: {self.clip_skip}', file=sf)
            else:
                print(f'Steps: {self.steps}, Sampler: {self.sample_method}, '
                      f'CFG scale: {self.gscale}, Seed: {self.seed}, Size: {self.width}x{self.height}, '
                      f'Model: {self.model}, '
                      f'Clip skip: {self.clip_skip}', file=sf)

            return sf.getvalue()

    @property
    def pnginfo(self) -> PngInfo:
        info = PngInfo()
        info.add_text('parameters', self.pnginfo_text)
        return info


_N_MAX_DRAW = 20


def draw_with_workdir(
        workdir: str, emb_name: str = None, save_cfg: bool = True,
        model_steps: int = 1000, n_repeats: int = 2, pretrained_model: str = _DEFAULT_INFER_MODEL,
        width: int = 512, height: int = 768, gscale: float = 8, infer_steps: int = 30,
        lora_alpha: float = 0.85, output_dir: str = None, cfg_file: str = _DEFAULT_INFER_CFG_FILE,
        clip_skip: int = 2, sample_method: str = 'DPM++ 2M Karras', model_hash: Optional[str] = None,
):
    n_pnames, n_prompts, n_neg_prompts, n_seeds, n_sfws = [], [], [], [], []
    for jfile in glob.glob(os.path.join(workdir, 'rtags', '*.json')):
        with open(jfile, 'r', encoding='utf-8') as f:
            data = json.load(f)
            n_pnames.append(data['name'])
            n_prompts.append(data['prompt'])
            n_neg_prompts.append(data['neg_prompt'])
            n_seeds.append(data['seed'])
            n_sfws.append(data['sfw'])

    n_total = len(n_pnames)
    retval = []
    for x in range(0, n_total, _N_MAX_DRAW):
        pnames, prompts, neg_prompts, seeds, sfws = \
            n_pnames[x:x + _N_MAX_DRAW], n_prompts[x:x + _N_MAX_DRAW], n_neg_prompts[x:x + _N_MAX_DRAW], \
                n_seeds[x:x + _N_MAX_DRAW], n_sfws[x:x + _N_MAX_DRAW]

        with TemporaryDirectory() as td:
            _tmp_output_dir = output_dir or td
            draw_images(
                workdir, prompts, neg_prompts, seeds,
                emb_name, save_cfg, model_steps, n_repeats, pretrained_model,
                width, height, gscale, infer_steps, lora_alpha, _tmp_output_dir, cfg_file,
                clip_skip, sample_method,
            )

            for i, (pname, prompt, neg_prompt, seed, sfw) in \
                    enumerate(zip(pnames, prompts, neg_prompts, seeds, sfws), start=1):
                img_file = glob.glob(os.path.join(_tmp_output_dir, f'{i}-*.png'))[0]
                yaml_file = glob.glob(os.path.join(_tmp_output_dir, f'{i}-*.yaml'))[0]
                with open(yaml_file, 'r', encoding='utf-8') as f:
                    seed = yaml.load(f, Loader)['seed']

                img = Image.open(img_file)
                img.load()

                retval.append(Drawing(
                    pname, prompt, neg_prompt, seed,
                    sfw=sfw and len(detect_censors(img, conf_threshold=0.45)) == 0,
                    width=width, height=height, gscale=gscale, steps=infer_steps,
                    image=img, sample_method=sample_method, clip_skip=clip_skip,
                    model=pretrained_model, model_hash=model_hash,
                ))

    return retval