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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from semdiffusers import SemanticEditPipeline
|
4 |
+
device='cuda'
|
5 |
+
|
6 |
+
pipe = SemanticEditPipeline.from_pretrained(
|
7 |
+
"runwayml/stable-diffusion-v1-5",
|
8 |
+
).to(device)
|
9 |
+
|
10 |
+
def infer(prompt):
|
11 |
+
|
12 |
+
gen = torch.Generator(device=device)
|
13 |
+
|
14 |
+
gen.manual_seed(21)
|
15 |
+
out = pipe(prompt=prompt, generator=gen, num_images_per_prompt=1, guidance_scale=7,
|
16 |
+
editing_prompt=['male person', # Concepts to apply
|
17 |
+
'female person'],
|
18 |
+
reverse_editing_direction=[True, False], # Direction of guidance i.e. decrease the first and increase the second concept
|
19 |
+
edit_warmup_steps=[10, 10], # Warmup period for each concept
|
20 |
+
edit_guidance_scale=[4, 4], # Guidance scale for each concept
|
21 |
+
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
|
22 |
+
edit_momentum_scale=0.3, # Momentum scale that will be added to the latent guidance
|
23 |
+
edit_mom_beta=0.6, # Momentum beta
|
24 |
+
edit_weights=[1,1] # Weights of the individual concepts against each other
|
25 |
+
)
|
26 |
+
image = out.images[0]
|
27 |
+
|
28 |
+
return [image]
|
29 |
+
|
30 |
+
|
31 |
+
css = """
|
32 |
+
.gradio-container {
|
33 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
34 |
+
}
|
35 |
+
.gr-button {
|
36 |
+
color: white;
|
37 |
+
border-color: black;
|
38 |
+
background: black;
|
39 |
+
}
|
40 |
+
input[type='range'] {
|
41 |
+
accent-color: black;
|
42 |
+
}
|
43 |
+
.dark input[type='range'] {
|
44 |
+
accent-color: #dfdfdf;
|
45 |
+
}
|
46 |
+
.container {
|
47 |
+
max-width: 730px;
|
48 |
+
margin: auto;
|
49 |
+
padding-top: 1.5rem;
|
50 |
+
}
|
51 |
+
#gallery {
|
52 |
+
min-height: 22rem;
|
53 |
+
margin-bottom: 15px;
|
54 |
+
margin-left: auto;
|
55 |
+
margin-right: auto;
|
56 |
+
border-bottom-right-radius: .5rem !important;
|
57 |
+
border-bottom-left-radius: .5rem !important;
|
58 |
+
}
|
59 |
+
#gallery>div>.h-full {
|
60 |
+
min-height: 20rem;
|
61 |
+
}
|
62 |
+
.details:hover {
|
63 |
+
text-decoration: underline;
|
64 |
+
}
|
65 |
+
.gr-button {
|
66 |
+
white-space: nowrap;
|
67 |
+
}
|
68 |
+
.gr-button:focus {
|
69 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
70 |
+
outline: none;
|
71 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
72 |
+
--tw-border-opacity: 1;
|
73 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
74 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
75 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
76 |
+
--tw-ring-opacity: .5;
|
77 |
+
}
|
78 |
+
#advanced-btn {
|
79 |
+
font-size: .7rem !important;
|
80 |
+
line-height: 19px;
|
81 |
+
margin-top: 12px;
|
82 |
+
margin-bottom: 12px;
|
83 |
+
padding: 2px 8px;
|
84 |
+
border-radius: 14px !important;
|
85 |
+
}
|
86 |
+
#advanced-options {
|
87 |
+
display: none;
|
88 |
+
margin-bottom: 20px;
|
89 |
+
}
|
90 |
+
.footer {
|
91 |
+
margin-bottom: 45px;
|
92 |
+
margin-top: 35px;
|
93 |
+
text-align: center;
|
94 |
+
border-bottom: 1px solid #e5e5e5;
|
95 |
+
}
|
96 |
+
.footer>p {
|
97 |
+
font-size: .8rem;
|
98 |
+
display: inline-block;
|
99 |
+
padding: 0 10px;
|
100 |
+
transform: translateY(10px);
|
101 |
+
background: white;
|
102 |
+
}
|
103 |
+
.dark .footer {
|
104 |
+
border-color: #303030;
|
105 |
+
}
|
106 |
+
.dark .footer>p {
|
107 |
+
background: #0b0f19;
|
108 |
+
}
|
109 |
+
.acknowledgments h4{
|
110 |
+
margin: 1.25em 0 .25em 0;
|
111 |
+
font-weight: bold;
|
112 |
+
font-size: 115%;
|
113 |
+
}
|
114 |
+
.animate-spin {
|
115 |
+
animation: spin 1s linear infinite;
|
116 |
+
}
|
117 |
+
@keyframes spin {
|
118 |
+
from {
|
119 |
+
transform: rotate(0deg);
|
120 |
+
}
|
121 |
+
to {
|
122 |
+
transform: rotate(360deg);
|
123 |
+
}
|
124 |
+
}
|
125 |
+
#share-btn-container {
|
126 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
127 |
+
margin-top: 10px;
|
128 |
+
margin-left: auto;
|
129 |
+
}
|
130 |
+
#share-btn {
|
131 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
|
132 |
+
}
|
133 |
+
#share-btn * {
|
134 |
+
all: unset;
|
135 |
+
}
|
136 |
+
#share-btn-container div:nth-child(-n+2){
|
137 |
+
width: auto !important;
|
138 |
+
min-height: 0px !important;
|
139 |
+
}
|
140 |
+
#share-btn-container .wrap {
|
141 |
+
display: none !important;
|
142 |
+
}
|
143 |
+
|
144 |
+
.gr-form{
|
145 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
146 |
+
}
|
147 |
+
#prompt-container{
|
148 |
+
gap: 0;
|
149 |
+
}
|
150 |
+
#prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
|
151 |
+
#component-16{border-top-width: 1px!important;margin-top: 1em}
|
152 |
+
.image_duplication{position: absolute; width: 100px; left: 50px}
|
153 |
+
"""
|
154 |
+
|
155 |
+
block = gr.Blocks(css=css)
|
156 |
+
|
157 |
+
examples = [
|
158 |
+
[
|
159 |
+
'A photo of dolomites',
|
160 |
+
7.5
|
161 |
+
]
|
162 |
+
|
163 |
+
]
|
164 |
+
|
165 |
+
|
166 |
+
with block:
|
167 |
+
gr.HTML(
|
168 |
+
"""
|
169 |
+
<div style="text-align: center; margin: 0 auto;">
|
170 |
+
<div
|
171 |
+
style="
|
172 |
+
display: inline-flex;
|
173 |
+
align-items: center;
|
174 |
+
gap: 0.8rem;
|
175 |
+
font-size: 1.75rem;
|
176 |
+
"
|
177 |
+
>
|
178 |
+
<svg
|
179 |
+
width="0.65em"
|
180 |
+
height="0.65em"
|
181 |
+
viewBox="0 0 115 115"
|
182 |
+
fill="none"
|
183 |
+
xmlns="http://www.w3.org/2000/svg"
|
184 |
+
>
|
185 |
+
<rect width="23" height="23" fill="white"></rect>
|
186 |
+
<rect y="69" width="23" height="23" fill="white"></rect>
|
187 |
+
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
188 |
+
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
189 |
+
<rect x="46" width="23" height="23" fill="white"></rect>
|
190 |
+
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
191 |
+
<rect x="69" width="23" height="23" fill="black"></rect>
|
192 |
+
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
193 |
+
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
194 |
+
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
195 |
+
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
196 |
+
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
197 |
+
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
198 |
+
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
199 |
+
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
200 |
+
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
201 |
+
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
202 |
+
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
203 |
+
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
204 |
+
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
205 |
+
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
206 |
+
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
207 |
+
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
208 |
+
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
209 |
+
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
210 |
+
</svg>
|
211 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
212 |
+
FairDiffusion Demo
|
213 |
+
</h1>
|
214 |
+
</div>
|
215 |
+
</div>
|
216 |
+
"""
|
217 |
+
)
|
218 |
+
with gr.Group():
|
219 |
+
with gr.Box():
|
220 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
221 |
+
with gr.Column():
|
222 |
+
text = gr.Textbox(
|
223 |
+
label="Enter your prompt",
|
224 |
+
show_label=False,
|
225 |
+
max_lines=1,
|
226 |
+
placeholder="Enter your prompt",
|
227 |
+
elem_id="prompt-text-input",
|
228 |
+
).style(
|
229 |
+
border=(True, False, True, True),
|
230 |
+
rounded=(True, False, False, True),
|
231 |
+
container=False,
|
232 |
+
)
|
233 |
+
|
234 |
+
btn = gr.Button("Generate image").style(
|
235 |
+
margin=False,
|
236 |
+
rounded=(False, True, True, False),
|
237 |
+
full_width=False,
|
238 |
+
)
|
239 |
+
|
240 |
+
gallery = gr.Gallery(
|
241 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
242 |
+
).style(height="auto")
|
243 |
+
|
244 |
+
# with gr.Group(elem_id="container-advanced-btns"):
|
245 |
+
# #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
246 |
+
# with gr.Group(elem_id="share-btn-container"):
|
247 |
+
# community_icon = gr.HTML(community_icon_html)
|
248 |
+
# loading_icon = gr.HTML(loading_icon_html)
|
249 |
+
# share_button = gr.Button("Share to community", elem_id="share-btn")
|
250 |
+
|
251 |
+
# seed = gr.Slider(
|
252 |
+
# label="Seed",
|
253 |
+
# minimum=0,
|
254 |
+
# maximum=2147483647,
|
255 |
+
# step=1,
|
256 |
+
# randomize=True,
|
257 |
+
# )
|
258 |
+
|
259 |
+
|
260 |
+
text.submit(infer, inputs=[text], outputs=[gallery])
|
261 |
+
btn.click(infer, inputs=[text], outputs=[gallery])
|
262 |
+
|
263 |
+
|
264 |
+
|
265 |
+
block.queue().launch()
|