File size: 15,901 Bytes
c19ca42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
# pylint: disable=no-member
import os
import re
import json
import time
import logging
import importlib
import asyncio
import argparse
from pathlib import Path
from util import Map, log
from sdapi import get, post, close
from generate import generate # pylint: disable=import-error
grid = importlib.import_module('image-grid').grid


options = Map({
    # used by extra networks
    'prompt': 'photo of <keyword> <embedding>, photograph, posing, pose, high detailed, intricate, elegant, sharp focus, skin texture, looking forward, facing camera, 135mm, shot on dslr, canon 5d, 4k, modelshoot style, cinematic lighting',
    # used by models
    'prompts': [
        ('photo citiscape', 'cityscape during night, photorealistic, high detailed, sharp focus, depth of field, 4k'),
        ('photo car', 'photo of a sports car, high detailed, sharp focus, dslr, cinematic lighting, realistic'),
        ('photo woman', 'portrait photo of beautiful woman, high detailed, dslr, 35mm'),
        ('photo naked', 'full body photo of beautiful sexy naked woman, high detailed, dslr, 35mm'),

        ('photo taylor', 'portrait photo of beautiful woman taylor swift, high detailed, sharp focus, depth of field, dslr, 35mm <lora:taylor-swift:1>'),
        ('photo ti-mia', 'portrait photo of beautiful woman "ti-mia", naked, high detailed, dslr, 35mm'),
        ('photo ti-vlado', 'portrait photo of man "ti-vlado", high detailed, dslr, 35mm'),
        ('photo lora-vlado', 'portrait photo of man vlado, high detailed, dslr, 35mm <lora:vlado-original:1>'),

        ('wlop', 'a stunning portrait of sexy teen girl in a wet t-shirt, vivid color palette, digital painting, octane render, highly detailed, particles, light effect, volumetric lighting, art by wlop'),
        ('greg rutkowski', 'beautiful woman, high detailed, sharp focus, depth of field, 4k, art by greg rutkowski'),
        ('carne griffiths', 'beautiful woman taylor swift, high detailed, sharp focus, depth of field, art by carne griffiths <lora:taylor-swift:1>'),
        ('carne griffiths', 'man vlado, high detailed, sharp focus, depth of field, art by carne griffiths <lora:vlado-full:1>'),
    ],
    # save format
    'format': '.jpg',
    # used by generate script
    'paths': {
        "root": "/mnt/c/Users/mandi/OneDrive/Generative/Generate",
        "generate": "image",
        "upscale": "upscale",
        "grid": "grid",
    },
    # generate params
    'generate': {
        'restore_faces': True,
        'prompt': '',
        'negative_prompt': 'foggy, blurry, blurred, duplicate, ugly, mutilated, mutation, mutated, out of frame, bad anatomy, disfigured, deformed, censored, low res, low resolution, watermark, text, poorly drawn face, poorly drawn hands, signature',
        'steps': 20,
        'batch_size': 2,
        'n_iter': 1,
        'seed': -1,
        'sampler_name': 'UniPC',
        'cfg_scale': 6,
        'width': 512,
        'height': 512,
    },
    'lora': {
        'strength': 1.0,
    },
    'hypernetwork': {
        'keyword': '',
        'strength': 1.0,
    },
})


def preview_exists(folder, model):
    model = os.path.splitext(model)[0]
    for suffix in ['', '.preview']:
        for ext in ['.jpg', '.png', '.webp']:
            fn = os.path.join(folder, f'{model}{suffix}{ext}')
            if os.path.exists(fn):
                return True
    return False


async def preview_models(params):
    data = await get('/sdapi/v1/sd-models')
    allmodels = [m['title'] for m in data]
    models = []
    excluded = []
    for m in allmodels: # loop through all registered models
        ok = True
        for e in params.exclude: # check if model is excluded
            if e in m:
                excluded.append(m)
                ok = False
                break
        if ok:
            short = m.split(' [')[0]
            short = short.replace('.ckpt', '').replace('.safetensors', '')
            models.append(short)
    if len(params.input) > 0: # check if model is included in cmd line
        filtered = []
        for m in params.input:
            if m in models:
                filtered.append(m)
            else:
                log.error({ 'model not found': m })
                return
        models = filtered
    log.info({ 'models preview' })
    log.info({ 'models': len(models), 'excluded': len(excluded) })
    opt = await get('/sdapi/v1/options')
    log.info({ 'total jobs': len(models) * options.generate.batch_size, 'per-model': options.generate.batch_size })
    log.info(json.dumps(options, indent=2))
    for model in models:
        if preview_exists(opt['ckpt_dir'], model) and len(params.input) == 0: # if model preview exists and not manually included
            log.info({ 'model preview exists': model })
            continue
        fn = os.path.join(opt['ckpt_dir'], os.path.splitext(model)[0] + options.format)
        log.info({ 'model load': model })

        opt['sd_model_checkpoint'] = model
        del opt['sd_lora']
        del opt['sd_lyco']
        await post('/sdapi/v1/options', opt)
        opt = await get('/sdapi/v1/options')
        images = []
        labels = []
        t0 = time.time()
        for label, p in options.prompts:
            options.generate.prompt = p
            log.info({ 'model generating': model, 'label': label, 'prompt': options.generate.prompt })
            data = await generate(options = options, quiet=True)
            if 'image' in data:
                for img in data['image']:
                    images.append(img)
                    labels.append(label)
            else:
                log.error({ 'model': model, 'error': data })
        t1 = time.time()
        if len(images) == 0:
            log.error({ 'model': model, 'error': 'no images generated' })
            continue
        image = grid(images = images, labels = labels, border = 8)
        log.info({ 'saving preview': fn, 'images': len(images), 'size': [image.width, image.height] })
        image.save(fn)
        t = t1 - t0
        its = 1.0 * options.generate.steps * len(images) / t
        log.info({ 'model preview created': model, 'image': fn, 'images': len(images), 'grid': [image.width, image.height], 'time': round(t, 2), 'its': round(its, 2) })

    opt = await get('/sdapi/v1/options')
    if opt['sd_model_checkpoint'] != params.model:
        log.info({ 'model set default': params.model })
        opt['sd_model_checkpoint'] = params.model
        del opt['sd_lora']
        del opt['sd_lyco']
        await post('/sdapi/v1/options', opt)


async def lora(params):
    opt = await get('/sdapi/v1/options')
    folder = opt['lora_dir']
    if not os.path.exists(folder):
        log.error({ 'lora directory not found': folder })
        return
    models1 = list(Path(folder).glob('**/*.safetensors'))
    models2 = list(Path(folder).glob('**/*.ckpt'))
    models = [os.path.splitext(f)[0] for f in models1 + models2]
    log.info({ 'loras': len(models) })
    for model in models:
        if preview_exists('', model) and len(params.input) == 0: # if model preview exists and not manually included
            log.info({ 'lora preview exists': model })
            continue
        fn = model + options.format
        model = os.path.basename(model)
        images = []
        labels = []
        t0 = time.time()
        keywords = re.sub(r'\d', '', model)
        keywords = keywords.replace('-v', ' ').replace('-', ' ').strip().split(' ')
        keyword = '\"' + '\" \"'.join(keywords) + '\"'
        options.generate.prompt = options.prompt.replace('<keyword>', keyword)
        options.generate.prompt = options.generate.prompt.replace('<embedding>', '')
        options.generate.prompt += f' <lora:{model}:{options.lora.strength}>'
        log.info({ 'lora generating': model, 'keyword': keyword, 'prompt': options.generate.prompt })
        data = await generate(options = options, quiet=True)
        if 'image' in data:
            for img in data['image']:
                images.append(img)
                labels.append(keyword)
        else:
            log.error({ 'lora': model, 'keyword': keyword, 'error': data })
        t1 = time.time()
        if len(images) == 0:
            log.error({ 'model': model, 'error': 'no images generated' })
            continue
        image = grid(images = images, labels = labels, border = 8)
        log.info({ 'saving preview': fn, 'images': len(images), 'size': [image.width, image.height] })
        image.save(fn)
        t = t1 - t0
        its = 1.0 * options.generate.steps * len(images) / t
        log.info({ 'lora preview created': model, 'image': fn, 'images': len(images), 'grid': [image.width, image.height], 'time': round(t, 2), 'its': round(its, 2) })


async def lyco(params):
    opt = await get('/sdapi/v1/options')
    folder = opt['lyco_dir']
    if not os.path.exists(folder):
        log.error({ 'lyco directory not found': folder })
        return
    models1 = list(Path(folder).glob('**/*.safetensors'))
    models2 = list(Path(folder).glob('**/*.ckpt'))
    models = [os.path.splitext(f)[0] for f in models1 + models2]
    log.info({ 'lycos': len(models) })
    for model in models:
        if preview_exists('', model) and len(params.input) == 0: # if model preview exists and not manually included
            log.info({ 'lyco preview exists': model })
            continue
        fn = model + options.format
        model = os.path.basename(model)
        images = []
        labels = []
        t0 = time.time()
        keywords = re.sub(r'\d', '', model)
        keywords = keywords.replace('-v', ' ').replace('-', ' ').strip().split(' ')
        keyword = '\"' + '\" \"'.join(keywords) + '\"'
        options.generate.prompt = options.prompt.replace('<keyword>', keyword)
        options.generate.prompt = options.generate.prompt.replace('<embedding>', '')
        options.generate.prompt += f' <lyco:{model}:{options.lora.strength}>'
        log.info({ 'lyco generating': model, 'keyword': keyword, 'prompt': options.generate.prompt })
        data = await generate(options = options, quiet=True)
        if 'image' in data:
            for img in data['image']:
                images.append(img)
                labels.append(keyword)
        else:
            log.error({ 'lyco': model, 'keyword': keyword, 'error': data })
        t1 = time.time()
        if len(images) == 0:
            log.error({ 'model': model, 'error': 'no images generated' })
            continue
        image = grid(images = images, labels = labels, border = 8)
        log.info({ 'saving preview': fn, 'images': len(images), 'size': [image.width, image.height] })
        image.save(fn)
        t = t1 - t0
        its = 1.0 * options.generate.steps * len(images) / t
        log.info({ 'lyco preview created': model, 'image': fn, 'images': len(images), 'grid': [image.width, image.height], 'time': round(t, 2), 'its': round(its, 2) })


async def hypernetwork(params):
    opt = await get('/sdapi/v1/options')
    folder = opt['hypernetwork_dir']
    if not os.path.exists(folder):
        log.error({ 'hypernetwork directory not found': folder })
        return
    models = [os.path.splitext(f)[0] for f in Path(folder).glob('**/*.pt')]
    log.info({ 'hypernetworks': len(models) })
    for model in models:
        if preview_exists(folder, model) and len(params.input) == 0: # if model preview exists and not manually included
            log.info({ 'hypernetwork preview exists': model })
            continue
        fn = os.path.join(folder, model + options.format)
        images = []
        labels = []
        t0 = time.time()
        keyword = options.hypernetwork.keyword
        options.generate.prompt = options.prompt.replace('<keyword>', options.hypernetwork.keyword)
        options.generate.prompt = options.generate.prompt.replace('<embedding>', '')
        options.generate.prompt = f' <hypernet:{model}:{options.hypernetwork.strength}> ' + options.generate.prompt
        log.info({ 'hypernetwork generating': model, 'keyword': keyword, 'prompt': options.generate.prompt })
        data = await generate(options = options, quiet=True)
        if 'image' in data:
            for img in data['image']:
                images.append(img)
                labels.append(keyword)
        else:
            log.error({ 'hypernetwork': model, 'keyword': keyword, 'error': data })
        t1 = time.time()
        if len(images) == 0:
            log.error({ 'model': model, 'error': 'no images generated' })
            continue
        image = grid(images = images, labels = labels, border = 8)
        log.info({ 'saving preview': fn, 'images': len(images), 'size': [image.width, image.height] })
        image.save(fn)
        t = t1 - t0
        its = 1.0 * options.generate.steps * len(images) / t
        log.info({ 'hypernetwork preview created': model, 'image': fn, 'images': len(images), 'grid': [image.width, image.height], 'time': round(t, 2), 'its': round(its, 2) })


async def embedding(params):
    opt = await get('/sdapi/v1/options')
    folder = opt['embeddings_dir']
    if not os.path.exists(folder):
        log.error({ 'embeddings directory not found': folder })
        return
    models = [os.path.splitext(f)[0] for f in Path(folder).glob('**/*.pt')]
    log.info({ 'embeddings': len(models) })
    for model in models:
        if preview_exists(folder, model) and len(params.input) == 0: # if model preview exists and not manually included
            log.info({ 'embedding preview exists': model })
            continue
        fn = os.path.join(folder, model + '.preview' + options.format)
        images = []
        labels = []
        t0 = time.time()
        keyword = '\"' + re.sub(r'\d', '', model) + '\"'
        options.generate.batch_size = 4
        options.generate.prompt = options.prompt.replace('<keyword>', keyword)
        options.generate.prompt = options.generate.prompt.replace('<embedding>', '')
        log.info({ 'embedding generating': model, 'keyword': keyword, 'prompt': options.generate.prompt })
        data = await generate(options = options, quiet=True)
        if 'image' in data:
            for img in data['image']:
                images.append(img)
                labels.append(keyword)
        else:
            log.error({ 'embeding': model, 'keyword': keyword, 'error': data })
        t1 = time.time()
        if len(images) == 0:
            log.error({ 'model': model, 'error': 'no images generated' })
            continue
        image = grid(images = images, labels = labels, border = 8)
        log.info({ 'saving preview': fn, 'images': len(images), 'size': [image.width, image.height] })
        image.save(fn)
        t = t1 - t0
        its = 1.0 * options.generate.steps * len(images) / t
        log.info({ 'embeding preview created': model, 'image': fn, 'images': len(images), 'grid': [image.width, image.height], 'time': round(t, 2), 'its': round(its, 2) })


async def create_previews(params):
    await preview_models(params)
    await lora(params)
    await lyco(params)
    await hypernetwork(params)
    await embedding(params)
    await close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = 'generate model previews')
    parser.add_argument('--model', default='best/icbinp-icantbelieveIts-final.safetensors [73f48afbdc]', help="model used to create extra network previews")
    parser.add_argument('--exclude', default=['sd-v20', 'sd-v21', 'inpainting', 'pix2pix'], help="exclude models with keywords")
    parser.add_argument('--debug', default = False, action='store_true', help = 'print extra debug information')
    parser.add_argument('input', type = str, nargs = '*')
    args = parser.parse_args()
    if args.debug:
        log.setLevel(logging.DEBUG)
        log.debug({ 'debug': True })
    log.debug({ 'args': args.__dict__ })
    asyncio.run(create_previews(args))