File size: 2,230 Bytes
80b8ba6
 
 
78a2900
80b8ba6
78a2900
80b8ba6
 
 
78a2900
80b8ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78a2900
80b8ba6
 
 
 
78a2900
80b8ba6
78a2900
80b8ba6
 
 
78a2900
80b8ba6
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from semdiffusers import SemanticEditPipeline

device='cuda'

pipe = SemanticEditPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
).to(device)

def infer(prompt, seed):
    gen = torch.Generator(device=device)
    gen.manual_seed(seed)
    
    out = pipe(
        prompt=prompt, 
        generator=gen, 
        num_images_per_prompt=1, 
        guidance_scale=7
    )
    
    images = out.images[0]
    
    out_edit = pipe(
        prompt=prompt, 
        generator=gen, 
        num_images_per_prompt=1, 
        guidance_scale=7,
        editing_prompt=['male person', 'female person'],     # Concepts to apply 
        reverse_editing_direction=[True, False],             # Direction of guidance i.e. decrease the first and increase the second concept
        edit_warmup_steps=[10, 10],                         # Warmup period for each concept
        edit_guidance_scale=[4, 4],                         # Guidance scale for each concept
        edit_threshold=[0.95, 0.95],                       # Threshold for each concept. Threshold equals the percentile of the latent space that will be discarded. I.e. threshold=0.99 uses 1% of the latent dimensions
        edit_momentum_scale=0.3,                             # Momentum scale that will be added to the latent guidance
        edit_mom_beta=0.6,                                   # Momentum beta
        edit_weights=[1, 1]                                 # Weights of the individual concepts against each other
    )
    
    images_edited = out_edit.images[0]
    
    return [
        (images, 'Stable Diffusion'), 
        (images_edited, 'Fair Diffusion')
    ]

inputs = [
    gr.inputs.Textbox(label='Prompt'),
    gr.inputs.Number(label='Seed', default=0, step=1)
]

outputs = gr.outputs.Image(label='Images', type='numpy', number=2)

title = 'Semantic Edit Pipeline'
description = 'Semantic Edit Pipeline implementation using SemDiffusers.'
article = "<h3 style='text-align: center'><a href='https://github.com/crowsonkb/semdiffusers'>SemDiffusers</a></h3>"

gr.Interface(
    infer,
    inputs,
    outputs,
    title=title,
    description=description,
    article=article,
    theme='compact'
).launch();