File size: 37,529 Bytes
6b5dfe6
bfdbdf6
 
 
 
 
182990e
b3d3b2f
666a605
 
ded3b8b
efd358c
c947e95
b7f76d9
0e68d2d
25db69d
f87de06
25db69d
c947e95
 
188d396
 
 
 
e9eed9e
c947e95
6b5dfe6
 
13e5571
6b5dfe6
f1d9efe
6b5dfe6
bfdbdf6
0e68d2d
8d49336
1b5780b
0688e12
a001849
 
0688e12
 
8d49336
88a47b3
 
efd358c
e89b310
a001849
843cf9f
bfdbdf6
 
88a47b3
4ce0eba
bfdbdf6
 
88a47b3
a001849
084e0c6
4b0b44b
b113e87
 
 
868b6a5
bfdbdf6
c4b99ca
bfdbdf6
6409531
8d49336
 
1b5780b
0688e12
 
 
a001849
0688e12
 
 
bfdbdf6
ac586a8
 
666a605
ac586a8
 
666a605
 
 
 
ac586a8
 
31e1648
e89b310
ac586a8
 
 
0efd930
b113e87
 
c947e95
5798e9e
e89b310
 
 
a001849
e89b310
 
 
a001849
e89b310
 
31e1648
e89b310
c947e95
 
 
 
c24dac7
8d49336
 
 
 
 
 
 
e2138ad
 
 
 
 
 
 
 
 
 
 
 
 
f87de06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666a605
c947e95
 
b17ef3e
 
f87de06
 
 
 
 
 
0e68d2d
 
8d49336
0e68d2d
8d49336
0e68d2d
c947e95
666a605
 
c947e95
c9de947
0fd53b3
bfdbdf6
31024de
a001849
bfdbdf6
 
 
 
 
 
666a605
 
b3d3b2f
 
e2138ad
31024de
b3d3b2f
ded3b8b
22d5d2c
bfdbdf6
 
9255bd7
bfdbdf6
d1c3953
8d49336
31024de
bfdbdf6
 
 
9465fd2
666a605
 
88a47b3
666a605
 
88a47b3
8d49336
084e0c6
88a47b3
0efd930
b113e87
 
666a605
6a75a80
1235e6e
8d49336
 
 
 
 
 
 
 
 
 
 
 
1235e6e
8d49336
 
 
 
 
 
 
 
1235e6e
 
8d49336
 
 
 
 
 
88a47b3
 
 
 
 
 
8d49336
 
 
 
 
 
 
1235e6e
8d49336
 
 
 
 
 
 
 
1235e6e
 
 
8d49336
 
 
 
88a47b3
0e68d2d
126bc9f
8d49336
 
 
 
 
c947e95
b5965be
c947e95
 
8d49336
b5965be
 
 
c947e95
b5965be
c947e95
b5965be
 
 
 
 
 
 
3164600
8d49336
0fd53b3
 
 
 
c9de947
8d49336
 
0e68d2d
c9de947
8d49336
 
 
 
 
 
 
 
c9de947
 
8d49336
f87de06
666a605
 
 
 
 
010c9e3
 
 
666a605
 
 
 
efd358c
f87de06
 
666a605
 
 
 
 
 
 
 
 
 
 
 
c08bad3
b7f76d9
666a605
 
 
 
6b49c52
 
666a605
8d49336
666a605
0e68d2d
666a605
0e68d2d
666a605
 
 
0e68d2d
666a605
 
 
 
 
 
214d6ac
 
 
24452d6
 
 
666a605
 
0e68d2d
666a605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c947e95
 
 
 
 
 
f87de06
c947e95
d1c3953
ac586a8
9110980
 
 
 
 
cb024a4
c947e95
efd358c
cab8b2c
0fd53b3
c947e95
 
 
 
4ce0eba
c947e95
 
 
 
 
 
 
 
 
0fd53b3
 
cab8b2c
0fd53b3
 
 
 
 
 
2cd9a44
cab8b2c
 
c947e95
 
9b78871
 
0fd53b3
 
 
 
9b78871
0fd53b3
 
 
 
3164600
0fd53b3
6b5dfe6
 
c947e95
0fd53b3
c4b99ca
 
4ce0eba
c4b99ca
 
 
 
c947e95
e9eed9e
 
 
 
 
 
 
 
 
 
 
4ce0eba
e9eed9e
 
ac586a8
0fd53b3
c947e95
 
8d49336
c947e95
 
8d49336
76c4247
0fd53b3
 
bfdbdf6
a001849
8d49336
0fd53b3
 
6b5dfe6
8d49336
88a47b3
bfdbdf6
c4b99ca
8d49336
bfdbdf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b5dfe6
0e68d2d
bfdbdf6
0e68d2d
bfdbdf6
 
 
 
 
 
 
 
 
 
 
 
4a499a4
bfdbdf6
4a499a4
bfdbdf6
 
 
 
 
49a200f
bfdbdf6
0e68d2d
666a605
 
88a47b3
 
bfdbdf6
8d49336
0fd53b3
3608299
8d49336
0a646c7
 
 
 
 
8d49336
bfdbdf6
d1442c6
 
b17ef3e
 
d1442c6
 
0fd53b3
 
 
 
0e68d2d
 
 
 
9255bd7
666a605
8d49336
0e68d2d
 
 
 
 
 
 
0fd53b3
3164600
0e68d2d
 
ac586a8
666a605
ac586a8
0fd53b3
 
e89b310
0fd53b3
8d49336
b71d32f
8d49336
 
0fd53b3
 
88a47b3
e89b310
c35f883
31e1648
c35f883
e89b310
31e1648
0fd53b3
 
1a3a15c
 
0fd53b3
cab8b2c
0fd53b3
 
8d49336
0fd53b3
 
8d49336
0fd53b3
8d49336
0fd53b3
0eddda3
b764093
 
c8f1310
0fd53b3
148af15
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import gradio as gr
import os
from pathlib import Path
import argparse
import shutil
from train_dreambooth import run_training
from convertosd import convert
from PIL import Image
from slugify import slugify
import requests
import torch
import zipfile
import tarfile
import urllib.parse
import gc
from diffusers import StableDiffusionPipeline
from huggingface_hub import snapshot_download, update_repo_visibility, HfApi


is_spaces = True if "SPACE_ID" in os.environ else False
if(is_spaces):
    is_shared_ui = True if "multimodalart/dreambooth-training" in os.environ['SPACE_ID'] else False
else:
    is_shared_ui = False
is_gpu_associated = torch.cuda.is_available()

css = '''
    .instruction{position: absolute; top: 0;right: 0;margin-top: 0px !important}
    .arrow{position: absolute;top: 0;right: -110px;margin-top: -8px !important}
    #component-4, #component-3, #component-10{min-height: 0}
    .duplicate-button img{margin: 0}
'''
maximum_concepts = 3

#Pre download the files
if(is_gpu_associated):
    model_v1 = snapshot_download(repo_id="multimodalart/sd-fine-tunable")
    model_v2 = snapshot_download(repo_id="stabilityai/stable-diffusion-2-1")
    model_v2_512 = snapshot_download(repo_id="stabilityai/stable-diffusion-2-1-base")
    safety_checker = snapshot_download(repo_id="multimodalart/sd-sc")
    model_to_load = model_v1

with zipfile.ZipFile("mix.zip", 'r') as zip_ref:
    zip_ref.extractall(".")

def swap_text(option, base):
    resize_width = 768 if base == "v2-1-768" else 512
    mandatory_liability = "You must have the right to do so and you are liable for the images you use, example:"
    if(option == "object"):
        instance_prompt_example = "cttoy"
        freeze_for = 30
        return [f"You are going to train `object`(s), upload 5-10 images of each object you are planning on training on from different angles/perspectives. You can use services like <a style='text-decoration: underline' target='_blank' href='https://www.birme.net/?target_width={resize_width}&target_height={resize_width}'>birme</a> for smart cropping. {mandatory_liability}:", '''<img src="file/cat-toy.png" />''', f"You should name your concept with a unique made up word that has low chance of the model already knowing it (e.g.: `{instance_prompt_example}` here). Images will be automatically cropped to {resize_width}x{resize_width}.", freeze_for, gr.update(visible=False)]
    elif(option == "person"):
       instance_prompt_example = "julcto"
       freeze_for = 70
       #show_prior_preservation = True if base != "v2-1-768" else False
       show_prior_preservation=False
       if(show_prior_preservation):
           prior_preservation_box_update = gr.update(visible=show_prior_preservation)
       else: 
           prior_preservation_box_update = gr.update(visible=show_prior_preservation, value=False)
       return [f"You are going to train a `person`(s), upload 10-20 images of each person you are planning on training on from different angles/perspectives. You can use services like <a style='text-decoration: underline' target='_blank' href='https://www.birme.net/?target_width={resize_width}&target_height={resize_width}'>birme</a> for smart cropping. {mandatory_liability}:", '''<img src="file/person.png" />''', f"You should name your concept with a unique made up word that has low chance of the model already knowing it (e.g.: `{instance_prompt_example}` here). Images will be automatically cropped to {resize_width}x{resize_width}.", freeze_for, prior_preservation_box_update]
    elif(option == "style"):
        instance_prompt_example = "trsldamrl"
        freeze_for = 10
        return [f"You are going to train a `style`, upload 10-20 images of the style you are planning on training on. You can use services like <a style='text-decoration: underline' target='_blank' href='https://www.birme.net/?target_width={resize_width}&target_height={resize_width}'>birme</a> for smart cropping. {mandatory_liability}:", '''<img src="file/trsl_style.png" />''', f"You should name your concept with a unique made up word that has low chance of the model already knowing it (e.g.: `{instance_prompt_example}` here). Images will be automatically cropped to {resize_width}x{resize_width}", freeze_for, gr.update(visible=False)]

def swap_base_model(selected_model):
    if(is_gpu_associated):
        global model_to_load
        if(selected_model == "v1-5"):
            model_to_load = model_v1
        elif(selected_model == "v2-1-768"):
            model_to_load = model_v2
        else:
            model_to_load = model_v2_512

def count_files(*inputs):
    file_counter = 0
    concept_counter = 0
    for i, input in enumerate(inputs):
        if(i < maximum_concepts-1):
            files = inputs[i]
            if(files):
                concept_counter+=1
                file_counter+=len(files)
    uses_custom = inputs[-1] 
    type_of_thing = inputs[-4]
    selected_model = inputs[-5]
    experimental_faces = inputs[-6]
    if(uses_custom):
        Training_Steps = int(inputs[-3])
    else:
        Training_Steps = file_counter*150
        if(type_of_thing == "person" and Training_Steps > 2400):
            Training_Steps = 2400 #Avoid overfitting on person faces
    if(is_spaces):
        if(selected_model == "v1-5"):
            its = 1.1
            if(experimental_faces):
                its = 1
        elif(selected_model == "v2-1-512"):
            its = 0.8
            if(experimental_faces):
                its = 0.7
        elif(selected_model == "v2-1-768"):
            its = 0.5
        summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps. The training should take around {round(Training_Steps/its, 2)} seconds, or {round((Training_Steps/its)/60, 2)} minutes.
            The setup, compression and uploading the model can take up to 20 minutes.<br>As the T4-Small GPU costs US$0.60 for 1h, <span style="font-size: 120%"><b>the estimated cost for this training is below US${round((((Training_Steps/its)/3600)+0.3+0.1)*0.60, 2)}.</b></span><br><br>
            If you check the box below the GPU attribution will automatically removed after training is done and the model is uploaded. If not, don't forget to come back here and swap the hardware back to CPU.<br><br>'''
    else:
        summary_sentence = f'''You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps.<br><br>'''
        
    return([gr.update(visible=True), gr.update(visible=True, value=summary_sentence)])

def update_steps(*files_list):
    file_counter = 0
    for i, files in enumerate(files_list):
        if(files):
            file_counter+=len(files)
    return(gr.update(value=file_counter*200))

def pad_image(image):
    w, h = image.size
    if w == h:
        return image
    elif w > h:
        new_image = Image.new(image.mode, (w, w), (0, 0, 0))
        new_image.paste(image, (0, (w - h) // 2))
        return new_image
    else:
        new_image = Image.new(image.mode, (h, h), (0, 0, 0))
        new_image.paste(image, ((h - w) // 2, 0))
        return new_image

def validate_model_upload(hf_token, model_name):
    if(hf_token != ''):
        api = HfApi()
        try:
            _ = api.whoami(hf_token)
        except:
            raise gr.Error("You have inserted an invalid Hugging Face token")
        try:
            update_repo_visibility(repo_id=os.environ['SPACE_ID'], private=True, token=hf_token, repo_type="space")
        except:
            raise gr.Error("Oops, you created a Hugging Face token with read permissions only. You need one with write permissions")
    else:
        raise gr.Error("Please insert a Hugging Face Token (make sure to create it with write permissions)")        
    if(model_name == ""):
        raise gr.Error("Please fill in your model's name")

def train(*inputs):
    if is_shared_ui:
        raise gr.Error("This Space only works in duplicated instances")
    if not is_gpu_associated:
        raise gr.Error("Please associate a T4 GPU for this Space")
    hf_token = inputs[-5]
    model_name = inputs[-7]
    remove_attribution_after = inputs[-6]
    if(remove_attribution_after):
        validate_model_upload(hf_token, model_name)
    
    torch.cuda.empty_cache()
    if 'pipe' in globals():
        global pipe, pipe_is_set
        del pipe
        pipe_is_set = False
        gc.collect()
        
    if os.path.exists("output_model"): shutil.rmtree('output_model')
    if os.path.exists("instance_images"): shutil.rmtree('instance_images')
    if os.path.exists("diffusers_model.tar"): os.remove("diffusers_model.tar")
    if os.path.exists("model.ckpt"): os.remove("model.ckpt")
    if os.path.exists("hastrained.success"): os.remove("hastrained.success")
    file_counter = 0
    which_model = inputs[-10]
    resolution = 512 if which_model != "v2-1-768" else 768
    for i, input in enumerate(inputs):
        if(i < maximum_concepts-1):
            if(input):
                os.makedirs('instance_images',exist_ok=True)
                files = inputs[i+(maximum_concepts*2)]
                prompt = inputs[i+maximum_concepts]
                if(prompt == "" or prompt == None):
                    raise gr.Error("You forgot to define your concept prompt")
                for j, file_temp in enumerate(files):
                    file = Image.open(file_temp.name)
                    image = pad_image(file)
                    image = image.resize((resolution, resolution))
                    extension = file_temp.name.split(".")[1]
                    image = image.convert('RGB')
                    image.save(f'instance_images/{prompt}_({j+1}).jpg', format="JPEG", quality = 100)
                    file_counter += 1
    
    os.makedirs('output_model',exist_ok=True)
    uses_custom = inputs[-1] 
    type_of_thing = inputs[-4]
    experimental_face_improvement = inputs[-9]
    
    if(uses_custom):
        Training_Steps = int(inputs[-3])
        Train_text_encoder_for = int(inputs[-2])
    else:
        if(type_of_thing == "object"):
            Train_text_encoder_for=30
            
        elif(type_of_thing == "style"):
            Train_text_encoder_for=15
            
        elif(type_of_thing == "person"):
            Train_text_encoder_for=70
        
        Training_Steps = file_counter*150
        if(type_of_thing == "person" and Training_Steps > 2600):
            Training_Steps = 2600 #Avoid overfitting on people's faces
    stptxt = int((Training_Steps*Train_text_encoder_for)/100)
    gradient_checkpointing = True if (experimental_face_improvement or which_model != "v1-5") else False 
    cache_latents = True if which_model != "v1-5" else False
    if (type_of_thing == "object" or type_of_thing == "style" or (type_of_thing == "person" and not experimental_face_improvement)):
        args_general = argparse.Namespace(
            image_captions_filename = True,
            train_text_encoder = True if stptxt > 0 else False,
            stop_text_encoder_training = stptxt,
            save_n_steps = 0,
            pretrained_model_name_or_path = model_to_load,
            instance_data_dir="instance_images",
            class_data_dir=None,
            output_dir="output_model",
            instance_prompt="",
            seed=42,
            resolution=resolution,
            mixed_precision="fp16",
            train_batch_size=1,
            gradient_accumulation_steps=1,
            use_8bit_adam=True,
            learning_rate=2e-6,
            lr_scheduler="polynomial",
            lr_warmup_steps = 0,
            max_train_steps=Training_Steps,     
            gradient_checkpointing=gradient_checkpointing,
            cache_latents=cache_latents,
        )
        print("Starting single training...")
        lock_file = open("intraining.lock", "w")
        lock_file.close()
        run_training(args_general)
    else:
        args_general = argparse.Namespace(
            image_captions_filename = True,
            train_text_encoder = True if stptxt > 0 else False,
            stop_text_encoder_training = stptxt,
            save_n_steps = 0,
            pretrained_model_name_or_path = model_to_load,
            instance_data_dir="instance_images",
            class_data_dir="Mix",
            output_dir="output_model",
            with_prior_preservation=True,
            prior_loss_weight=1.0,
            instance_prompt="",
            seed=42,
            resolution=resolution,
            mixed_precision="fp16",
            train_batch_size=1,
            gradient_accumulation_steps=1,
            use_8bit_adam=True,
            learning_rate=2e-6,
            lr_scheduler="polynomial",
            lr_warmup_steps = 0,
            max_train_steps=Training_Steps,
            num_class_images=200,     
            gradient_checkpointing=gradient_checkpointing,
            cache_latents=cache_latents,
        )
        print("Starting multi-training...")
        lock_file = open("intraining.lock", "w")
        lock_file.close()
        run_training(args_general)
    gc.collect()
    torch.cuda.empty_cache()
    if(which_model == "v1-5"):
        print("Adding Safety Checker to the model...")
        shutil.copytree(f"{safety_checker}/feature_extractor", "output_model/feature_extractor")
        shutil.copytree(f"{safety_checker}/safety_checker", "output_model/safety_checker")
        shutil.copy(f"model_index.json", "output_model/model_index.json")
    
    if(not remove_attribution_after):
        print("Archiving model file...")
        with tarfile.open("diffusers_model.tar", "w") as tar:
            tar.add("output_model", arcname=os.path.basename("output_model"))
        if os.path.exists("intraining.lock"): os.remove("intraining.lock")
        trained_file = open("hastrained.success", "w")
        trained_file.close()
        print("Training completed!")
        return [
            gr.update(visible=True, value=["diffusers_model.tar"]), #result
            gr.update(visible=True), #try_your_model
            gr.update(visible=True), #push_to_hub
            gr.update(visible=True), #convert_button
            gr.update(visible=False), #training_ongoing
            gr.update(visible=True) #completed_training
        ]
    else:
        where_to_upload = inputs[-8]
        push(model_name, where_to_upload, hf_token, which_model, True)
        hardware_url = f"https://huggingface.co/spaces/{os.environ['SPACE_ID']}/hardware"
        headers = { "authorization" : f"Bearer {hf_token}"}
        body = {'flavor': 'cpu-basic'}
        requests.post(hardware_url, json = body, headers=headers)

pipe_is_set = False
def generate(prompt, steps):
    torch.cuda.empty_cache()
    from diffusers import StableDiffusionPipeline
    global pipe_is_set
    if(not pipe_is_set):
        global pipe
        pipe = StableDiffusionPipeline.from_pretrained("./output_model", torch_dtype=torch.float16)
        pipe = pipe.to("cuda")
        pipe_is_set = True
        
    image = pipe(prompt, num_inference_steps=steps).images[0]  
    return(image)
    
def push(model_name, where_to_upload, hf_token, which_model, comes_from_automated=False):
    validate_model_upload(hf_token, model_name)
    if(not os.path.exists("model.ckpt")):
        convert("output_model", "model.ckpt")
    from huggingface_hub import HfApi, HfFolder, CommitOperationAdd
    from huggingface_hub import create_repo
    model_name_slug = slugify(model_name)
    api = HfApi()
    your_username = api.whoami(token=hf_token)["name"]
    if(where_to_upload == "My personal profile"):    
        model_id = f"{your_username}/{model_name_slug}"
    else:
        model_id = f"sd-dreambooth-library/{model_name_slug}"
        headers = {"Authorization" : f"Bearer: {hf_token}", "Content-Type": "application/json"}
        response = requests.post("https://huggingface.co/organizations/sd-dreambooth-library/share/SSeOwppVCscfTEzFGQaqpfcjukVeNrKNHX", headers=headers)

    print(f"Starting to upload the model {model_id}...")
    images_upload = os.listdir("instance_images")
    image_string = ""
    instance_prompt_list = []
    previous_instance_prompt = ''
    for i, image in enumerate(images_upload):
        instance_prompt = image.split("_")[0]
        if(instance_prompt != previous_instance_prompt):
            title_instance_prompt_string = instance_prompt
            instance_prompt_list.append(instance_prompt)
        else:
            title_instance_prompt_string = ''
        previous_instance_prompt = instance_prompt
        image_string = f'''{title_instance_prompt_string} {"(use that on your prompt)" if title_instance_prompt_string != "" else ""} 
{image_string}![{instance_prompt} {i}](https://huggingface.co/{model_id}/resolve/main/concept_images/{urllib.parse.quote(image)})'''
    readme_text = f'''---
license: creativeml-openrail-m
tags:
- text-to-image
widget:
- text: {instance_prompt_list[0]}
---
### {model_name} Dreambooth model trained by {api.whoami(token=hf_token)["name"]} with [Hugging Face Dreambooth Training Space](https://huggingface.co/spaces/multimodalart/dreambooth-training) with the {which_model} base model

You run your new concept via `diffusers` [Colab Notebook for Inference](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_dreambooth_inference.ipynb). Don't forget to use the concept prompts! 

Sample pictures of:
{image_string}
'''
    #Save the readme to a file
    readme_file = open("model.README.md", "w")
    readme_file.write(readme_text)
    readme_file.close()
    #Save the token identifier to a file
    text_file = open("token_identifier.txt", "w")
    text_file.write(', '.join(instance_prompt_list))
    text_file.close()
    try:
        create_repo(model_id,private=True, token=hf_token)
    except:
        import time
        epoch_time = str(int(time.time()))
        create_repo(f"{model_id}-{epoch_time}", private=True,token=hf_token)
    operations = [
        CommitOperationAdd(path_in_repo="token_identifier.txt", path_or_fileobj="token_identifier.txt"),
        CommitOperationAdd(path_in_repo="README.md", path_or_fileobj="model.README.md"),
        CommitOperationAdd(path_in_repo=f"model.ckpt",path_or_fileobj="model.ckpt")
    ]
    api.create_commit(
    repo_id=model_id,
    operations=operations,
    commit_message=f"Upload the model {model_name}",
    token=hf_token
    )
    api.upload_folder(
    folder_path="output_model",
    repo_id=model_id,
    token=hf_token
    )
    api.upload_folder(
    folder_path="instance_images",
    path_in_repo="concept_images",
    repo_id=model_id,
    token=hf_token
    )
    if is_spaces:
        if(not comes_from_automated):
            extra_message = "Don't forget to remove the GPU attribution after you play with it."
        else:
            extra_message = "The GPU has been removed automatically as requested, and you can try the model via the model page"
        api.create_discussion(repo_id=os.environ['SPACE_ID'], title=f"Your model {model_name} has finished trained from the Dreambooth Train Spaces!", description=f"Your model has been successfully uploaded to: https://huggingface.co/{model_id}. {extra_message}",repo_type="space", token=hf_token)
    print("Model uploaded successfully!")
    return [gr.update(visible=True, value=f"Successfully uploaded your model. Access it [here](https://huggingface.co/{model_id})"), gr.update(visible=True, value=["diffusers_model.tar", "model.ckpt"])]

def convert_to_ckpt():
    if 'pipe' in globals():
        global pipe, pipe_is_set
        del pipe
        pipe_is_set = False
        gc.collect()
    convert("output_model", "model.ckpt")
    return gr.update(visible=True, value=["diffusers_model.tar", "model.ckpt"])

def check_status(top_description):
    if os.path.exists("hastrained.success"):
        if is_spaces:
            update_top_tag = gr.update(value=f'''
            <div class="gr-prose" style="max-width: 80%">
                <h2>Your model has finished training ✅</h2>
                <p>Yay, congratulations on training your model. Scroll down to play with with it, save it (either downloading it or on the Hugging Face Hub). Once you are done, your model is safe, and you don't want to train a new one, go to the <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}" target="_blank">settings page</a> and downgrade your Space to a CPU Basic</p> 
            </div>
            ''')
        else:
            update_top_tag = gr.update(value=f'''
            <div class="gr-prose" style="max-width: 80%">
                <h2>Your model has finished training ✅</h2>
                <p>Yay, congratulations on training your model. Scroll down to play with with it, save it (either downloading it or on the Hugging Face Hub).</p> 
            </div>
            ''')
        show_outputs = True
    elif os.path.exists("intraining.lock"):
        update_top_tag = gr.update(value='''
        <div class="gr-prose" style="max-width: 80%">
            <h2>Don't worry, your model is still training! ⌛</h2>
            <p>You closed the tab while your model was training, but it's all good! It is still training right now. You can click the "Open logs" button above here to check the training status. Once training is done, reload this tab to interact with your model</p> 
        </div>
        ''')
        show_outputs = False
    else:
        update_top_tag = gr.update(value=top_description)
        show_outputs = False
    if os.path.exists("diffusers_model.tar"):
       update_files_tag = gr.update(visible=show_outputs, value=["diffusers_model.tar"])
    else:
       update_files_tag = gr.update(visible=show_outputs)
    return [
        update_top_tag, #top_description
        gr.update(visible=show_outputs), #try_your_model
        gr.update(visible=show_outputs), #push_to_hub
        update_files_tag, #result
        gr.update(visible=show_outputs), #convert_button
    ]

def checkbox_swap(checkbox):
    return [gr.update(visible=checkbox), gr.update(visible=checkbox), gr.update(visible=checkbox), gr.update(visible=checkbox)]

with gr.Blocks(css=css) as demo:
    with gr.Box():
        if is_shared_ui:
            top_description = gr.HTML(f'''
                <div class="gr-prose" style="max-width: 80%">
                <h2>Attention - This Space doesn't work in this shared UI</h2>
                <p>For it to work, you can either run locally or duplicate the Space and run it on your own profile using a (paid) private T4 GPU for training. As each T4 costs US$0.60/h, it should cost < US$1 to train most models using default settings!&nbsp;&nbsp;<a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>
                <img class="instruction" src="file/duplicate.png"> 
                <img class="arrow" src="file/arrow.png" />
                </div>
            ''')
        elif(is_spaces):
            if(is_gpu_associated):
                top_description = gr.HTML(f'''
                        <div class="gr-prose" style="max-width: 80%">
                        <h2>You have successfully associated a GPU to the Dreambooth Training Space 🎉</h2>
                        <p>Certify that you got a T4. You can now train your model! You will be billed by the minute from when you activated the GPU until when it is turned it off.</p> 
                        </div>
                ''')
            else:
                top_description = gr.HTML(f'''
                        <div class="gr-prose" style="max-width: 80%">
                        <h2>You have successfully duplicated the Dreambooth Training Space 🎉</h2>
                        <p>There's only one step left before you can train your model: <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}/settings" style="text-decoration: underline" target="_blank">attribute a <b>T4 GPU</b> to it (via the Settings tab)</a> and run the training below. Other GPUs are not compatible for now. You will be billed by the minute from when you activate the GPU until when it is turned it off.</p> 
                        </div>
                ''')
        else:
            top_description = gr.HTML(f'''
                    <div class="gr-prose" style="max-width: 80%">
                    <h2>You have successfully cloned the Dreambooth Training Space locally 🎉</h2>
                    <p>Do a <code>pip install requirements-local.txt</code></p> 
                    </div>
                ''')
    gr.Markdown("# Dreambooth Training UI 💭")
    gr.Markdown("Customize Stable Diffusion v1 or v2 (ⁿᵉʷ!) by giving it a few examples of a concept. Based on the [🧨 diffusers](https://github.com/huggingface/diffusers) implementation, additional techniques from [TheLastBen](https://github.com/TheLastBen/diffusers) and [ShivamShrirao](https://github.com/ShivamShrirao/diffusers)")
    
    with gr.Row() as what_are_you_training:
        type_of_thing = gr.Dropdown(label="What would you like to train?", choices=["object", "person", "style"], value="object", interactive=True)
        base_model_to_use = gr.Dropdown(label="Which base model would you like to use?", choices=["v1-5", "v2-1-512", "v2-1-768"], value="v1-5", interactive=True)
    
    #Very hacky approach to emulate dynamically created Gradio components   
    with gr.Row() as upload_your_concept:
        with gr.Column():
            thing_description = gr.Markdown("You are going to train an `object`, please upload 5-10 images of the object you are planning on training on from different angles/perspectives. You must have the right to do so and you are liable for the images you use, example")
            thing_experimental = gr.Checkbox(label="Improve faces (prior preservation) - can take longer training but can improve faces", visible=False, value=False)
            thing_image_example = gr.HTML('''<img src="file/cat-toy.png" />''')
            things_naming = gr.Markdown("You should name your concept with a unique made up word that has low chance of the model already knowing it (e.g.: `cttoy` here). Images will be automatically cropped to 512x512.")
            
        with gr.Column():
            file_collection = []
            concept_collection = []
            buttons_collection = []
            delete_collection = []
            is_visible = []

            row = [None] * maximum_concepts
            for x in range(maximum_concepts):
                ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[(n // 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
                if(x == 0):
                    visible = True
                    is_visible.append(gr.State(value=True))
                else:
                    visible = False
                    is_visible.append(gr.State(value=False))

                file_collection.append(gr.File(label=f'''Upload the images for your {ordinal(x+1) if (x>0) else ""} concept''', file_count="multiple", interactive=True, visible=visible))
                with gr.Column(visible=visible) as row[x]:
                    concept_collection.append(gr.Textbox(label=f'''{ordinal(x+1) if (x>0) else ""} concept prompt - use a unique, made up word to avoid collisions'''))  
                    with gr.Row():
                        if(x < maximum_concepts-1):
                            buttons_collection.append(gr.Button(value="Add +1 concept", visible=visible))
                        if(x > 0):
                            delete_collection.append(gr.Button(value=f"Delete {ordinal(x+1)} concept"))
            
            counter_add = 1
            for button in buttons_collection:
                if(counter_add < len(buttons_collection)):
                    button.click(lambda:
                    [gr.update(visible=True),gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), True, None],
                    None, 
                    [row[counter_add], file_collection[counter_add], buttons_collection[counter_add-1], buttons_collection[counter_add], is_visible[counter_add], file_collection[counter_add]], queue=False)
                else:
                    button.click(lambda:[gr.update(visible=True),gr.update(visible=True), gr.update(visible=False), True], None, [row[counter_add], file_collection[counter_add], buttons_collection[counter_add-1], is_visible[counter_add]], queue=False)
                counter_add += 1
            
            counter_delete = 1
            for delete_button in delete_collection:
                if(counter_delete < len(delete_collection)+1):
                    delete_button.click(lambda:[gr.update(visible=False),gr.update(visible=False), gr.update(visible=True), False], None, [file_collection[counter_delete], row[counter_delete], buttons_collection[counter_delete-1], is_visible[counter_delete]], queue=False)
                counter_delete += 1
                  
    with gr.Accordion("Custom Settings", open=False):
        swap_auto_calculated = gr.Checkbox(label="Use custom settings")
        gr.Markdown("If not checked, the % of frozen encoder will be tuned automatically to whether you are training an `object`, `person` or `style`. The text-encoder is frozen after 10% of the steps for a style, 30% of the steps for an object and 75% trained for persons. The number of steps varies between 1400 and 2400 depending on how many images uploaded. If you see too many artifacts in your output, it means it may have overfit and you need less steps. If your results aren't really what you wanted, it may be underfitting and you need more steps.")
        steps = gr.Number(label="How many steps", value=2400)
        perc_txt_encoder = gr.Number(label="Percentage of the training steps the text-encoder should be trained as well", value=30)
        
    with gr.Box(visible=False) as training_summary:
        training_summary_text = gr.HTML("", visible=True, label="Training Summary")
        is_advanced_visible = True if is_spaces else False
        training_summary_checkbox = gr.Checkbox(label="Automatically remove paid GPU attribution and upload model to the Hugging Face Hub after training", value=True, visible=is_advanced_visible)
        training_summary_model_name = gr.Textbox(label="Name of your model", visible=True)
        training_summary_where_to_upload = gr.Dropdown(["My personal profile", "Public Library"], value="My personal profile", label="Upload to", visible=True)
        training_summary_token_message = gr.Markdown("[A Hugging Face write access token](https://huggingface.co/settings/tokens), go to \"New token\" -> Role : Write. A regular read token won't work here.", visible=True)            
        training_summary_token = gr.Textbox(label="Hugging Face Write Token", type="password", visible=True)
        
    train_btn = gr.Button("Start Training")
    if(is_shared_ui):
        training_ongoing = gr.Markdown("## This Space only works in duplicated instances. Please duplicate it and try again!", visible=False)
    elif(not is_gpu_associated):
        training_ongoing = gr.Markdown("## Oops, you haven't associated your T4 GPU to this Space. Visit the Settings tab, associate and try again.", visible=False)
    else:
        training_ongoing = gr.Markdown("## Training is ongoing ⌛... You can close this tab if you like or just wait. If you did not check the `Remove GPU After training`, you can come back here to try your model and upload it after training. Don't forget to remove the GPU attribution after you are done. ", visible=False)
    
    #Post-training UI
    completed_training = gr.Markdown('''# ✅ Training completed. 
    ### Don't forget to remove the GPU attribution after you are done trying and uploading your model''', visible=False)
    
    with gr.Row():
        with gr.Box(visible=False) as try_your_model:
            gr.Markdown("## Try your model")
            prompt = gr.Textbox(label="Type your prompt")
            result_image = gr.Image()
            inference_steps = gr.Slider(minimum=1, maximum=150, value=50, step=1)
            generate_button = gr.Button("Generate Image")
        
        with gr.Box(visible=False) as push_to_hub:
            gr.Markdown("## Push to Hugging Face Hub")
            model_name = gr.Textbox(label="Name of your model", placeholder="Tarsila do Amaral Style")
            where_to_upload = gr.Dropdown(["My personal profile", "Public Library"], label="Upload to")
            gr.Markdown("[A Hugging Face write access token](https://huggingface.co/settings/tokens), go to \"New token\" -> Role : Write. A regular read token won't work here.")
            hf_token = gr.Textbox(label="Hugging Face Write Token", type="password")
            
            push_button = gr.Button("Push to the Hub")
    
    result = gr.File(label="Download the uploaded models in the diffusers format", visible=True)
    success_message_upload = gr.Markdown(visible=False)
    convert_button = gr.Button("Convert to CKPT", visible=False)
    
    #Swap the examples and the % of text encoder trained depending if it is an object, person or style
    type_of_thing.change(fn=swap_text, inputs=[type_of_thing, base_model_to_use], outputs=[thing_description, thing_image_example, things_naming, perc_txt_encoder, thing_experimental], queue=False, show_progress=False)
    
    #Swap the base model
    base_model_to_use.change(fn=swap_text, inputs=[type_of_thing, base_model_to_use], outputs=[thing_description, thing_image_example, things_naming, perc_txt_encoder, thing_experimental], queue=False, show_progress=False)
    base_model_to_use.change(fn=swap_base_model, inputs=base_model_to_use, outputs=[])

    #Update the summary box below the UI according to how many images are uploaded and whether users are using custom settings or not 
    for file in file_collection:
        #file.change(fn=update_steps,inputs=file_collection, outputs=steps)
        file.change(fn=count_files, inputs=file_collection+[thing_experimental]+[base_model_to_use]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[training_summary, training_summary_text], queue=False)
    
    thing_experimental.change(fn=count_files, inputs=file_collection+[thing_experimental]+[base_model_to_use]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[training_summary, training_summary_text], queue=False)
    base_model_to_use.change(fn=count_files, inputs=file_collection+[thing_experimental]+[base_model_to_use]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[training_summary, training_summary_text], queue=False)
    steps.change(fn=count_files, inputs=file_collection+[thing_experimental]+[base_model_to_use]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[training_summary, training_summary_text], queue=False)
    perc_txt_encoder.change(fn=count_files, inputs=file_collection+[thing_experimental]+[base_model_to_use]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[training_summary, training_summary_text], queue=False)
    
    #Give more options if the user wants to finish everything after training
    if(is_spaces):
        training_summary_checkbox.change(fn=checkbox_swap, inputs=training_summary_checkbox, outputs=[training_summary_token_message, training_summary_token, training_summary_model_name, training_summary_where_to_upload],queue=False, show_progress=False)
    #Add a message for while it is in training
    train_btn.click(lambda:gr.update(visible=True), inputs=None, outputs=training_ongoing)
    
    #The main train function
    train_btn.click(fn=train, inputs=is_visible+concept_collection+file_collection+[base_model_to_use]+[thing_experimental]+[training_summary_where_to_upload]+[training_summary_model_name]+[training_summary_checkbox]+[training_summary_token]+[type_of_thing]+[steps]+[perc_txt_encoder]+[swap_auto_calculated], outputs=[result, try_your_model, push_to_hub, convert_button, training_ongoing, completed_training], queue=False)
    
    #Button to generate an image from your trained model after training
    generate_button.click(fn=generate, inputs=[prompt, inference_steps], outputs=result_image, queue=False)
    #Button to push the model to the Hugging Face Hub
    push_button.click(fn=push, inputs=[model_name, where_to_upload, hf_token, base_model_to_use], outputs=[success_message_upload, result], queue=False)
    #Button to convert the model to ckpt format 
    convert_button.click(fn=convert_to_ckpt, inputs=[], outputs=result, queue=False)
    
    #Checks if the training is running
    demo.load(fn=check_status, inputs=top_description, outputs=[top_description, try_your_model, push_to_hub, result, convert_button], queue=False, show_progress=False)

demo.queue(default_enabled=False).launch(debug=True)