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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from torch import autocast
|
5 |
+
from diffusers import StableDiffusionPipeline
|
6 |
+
from datasets import load_dataset
|
7 |
+
from PIL import Image
|
8 |
+
import re
|
9 |
+
|
10 |
+
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained("kingery/zrn-07-512-sd15-2e-6-800-woman-ddim", torch_dtype=torch.float32)
|
12 |
+
pipe = pipe.to("cpu")
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
disable_safety = False
|
17 |
+
|
18 |
+
if disable_safety:
|
19 |
+
def null_safety(images, **kwargs):
|
20 |
+
return images, False
|
21 |
+
pipe.safety_checker = null_safety
|
22 |
+
|
23 |
+
|
24 |
+
def infer(prompt, samples,scale):
|
25 |
+
prompt = prompt
|
26 |
+
scale = scale
|
27 |
+
n_samples = samples
|
28 |
+
|
29 |
+
|
30 |
+
images = pipe(n_samples*[prompt], guidance_scale=scale).images
|
31 |
+
|
32 |
+
return images
|
33 |
+
|
34 |
+
css = """
|
35 |
+
.gradio-container {
|
36 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
37 |
+
}
|
38 |
+
.gr-button {
|
39 |
+
color: white;
|
40 |
+
border-color: black;
|
41 |
+
background: black;
|
42 |
+
}
|
43 |
+
input[type='range'] {
|
44 |
+
accent-color: black;
|
45 |
+
}
|
46 |
+
.dark input[type='range'] {
|
47 |
+
accent-color: #dfdfdf;
|
48 |
+
}
|
49 |
+
.container {
|
50 |
+
max-width: 730px;
|
51 |
+
margin: auto;
|
52 |
+
padding-top: 1.5rem;
|
53 |
+
}
|
54 |
+
#gallery {
|
55 |
+
min-height: 22rem;
|
56 |
+
margin-bottom: 15px;
|
57 |
+
margin-left: auto;
|
58 |
+
margin-right: auto;
|
59 |
+
border-bottom-right-radius: .5rem !important;
|
60 |
+
border-bottom-left-radius: .5rem !important;
|
61 |
+
}
|
62 |
+
#gallery>div>.h-full {
|
63 |
+
min-height: 20rem;
|
64 |
+
}
|
65 |
+
.details:hover {
|
66 |
+
text-decoration: underline;
|
67 |
+
}
|
68 |
+
.gr-button {
|
69 |
+
white-space: nowrap;
|
70 |
+
}
|
71 |
+
.gr-button:focus {
|
72 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
73 |
+
outline: none;
|
74 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
75 |
+
--tw-border-opacity: 1;
|
76 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
77 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
78 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
79 |
+
--tw-ring-opacity: .5;
|
80 |
+
}
|
81 |
+
#advanced-btn {
|
82 |
+
font-size: .7rem !important;
|
83 |
+
line-height: 19px;
|
84 |
+
margin-top: 12px;
|
85 |
+
margin-bottom: 12px;
|
86 |
+
padding: 2px 8px;
|
87 |
+
border-radius: 14px !important;
|
88 |
+
}
|
89 |
+
#advanced-options {
|
90 |
+
display: none;
|
91 |
+
margin-bottom: 20px;
|
92 |
+
}
|
93 |
+
.footer {
|
94 |
+
margin-bottom: 45px;
|
95 |
+
margin-top: 35px;
|
96 |
+
text-align: center;
|
97 |
+
border-bottom: 1px solid #e5e5e5;
|
98 |
+
}
|
99 |
+
.footer>p {
|
100 |
+
font-size: .8rem;
|
101 |
+
display: inline-block;
|
102 |
+
padding: 0 10px;
|
103 |
+
transform: translateY(10px);
|
104 |
+
background: white;
|
105 |
+
}
|
106 |
+
.dark .footer {
|
107 |
+
border-color: #303030;
|
108 |
+
}
|
109 |
+
.dark .footer>p {
|
110 |
+
background: #0b0f19;
|
111 |
+
}
|
112 |
+
.acknowledgments h4{
|
113 |
+
margin: 1.25em 0 .25em 0;
|
114 |
+
font-weight: bold;
|
115 |
+
font-size: 115%;
|
116 |
+
}
|
117 |
+
"""
|
118 |
+
|
119 |
+
block = gr.Blocks(css=css)
|
120 |
+
|
121 |
+
examples = [
|
122 |
+
[
|
123 |
+
'Abraham Lincoln',
|
124 |
+
2,
|
125 |
+
7.5,
|
126 |
+
],
|
127 |
+
[
|
128 |
+
'George Washington',
|
129 |
+
2,
|
130 |
+
7,
|
131 |
+
],
|
132 |
+
]
|
133 |
+
|
134 |
+
with block:
|
135 |
+
gr.HTML(
|
136 |
+
"""
|
137 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
138 |
+
<div
|
139 |
+
style="
|
140 |
+
display: inline-flex;
|
141 |
+
align-items: center;
|
142 |
+
gap: 0.8rem;
|
143 |
+
font-size: 1.75rem;
|
144 |
+
"
|
145 |
+
>
|
146 |
+
<svg
|
147 |
+
width="0.65em"
|
148 |
+
height="0.65em"
|
149 |
+
viewBox="0 0 115 115"
|
150 |
+
fill="none"
|
151 |
+
xmlns="http://www.w3.org/2000/svg"
|
152 |
+
>
|
153 |
+
<rect width="23" height="23" fill="white"></rect>
|
154 |
+
<rect y="69" width="23" height="23" fill="white"></rect>
|
155 |
+
<rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
156 |
+
<rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
157 |
+
<rect x="46" width="23" height="23" fill="white"></rect>
|
158 |
+
<rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
159 |
+
<rect x="69" width="23" height="23" fill="black"></rect>
|
160 |
+
<rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
161 |
+
<rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
162 |
+
<rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
163 |
+
<rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
164 |
+
<rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
165 |
+
<rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
166 |
+
<rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
167 |
+
<rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
168 |
+
<rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
169 |
+
<rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
170 |
+
<rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
171 |
+
<rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
172 |
+
<rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
173 |
+
<rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
174 |
+
<rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
175 |
+
<rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
176 |
+
<rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
177 |
+
<rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
178 |
+
</svg>
|
179 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
180 |
+
Pokémon text to image
|
181 |
+
</h1>
|
182 |
+
</div>
|
183 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
184 |
+
Demo demonstrates inference using Stable Diffusion fine tuned on Pokémon to generate new Pokémon form text prompts
|
185 |
+
</p>
|
186 |
+
</div>
|
187 |
+
"""
|
188 |
+
)
|
189 |
+
with gr.Group():
|
190 |
+
with gr.Box():
|
191 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
192 |
+
text = gr.Textbox(
|
193 |
+
label="Enter your prompt",
|
194 |
+
show_label=False,
|
195 |
+
max_lines=1,
|
196 |
+
placeholder="Enter your prompt",
|
197 |
+
).style(
|
198 |
+
border=(True, False, True, True),
|
199 |
+
rounded=(True, False, False, True),
|
200 |
+
container=False,
|
201 |
+
)
|
202 |
+
btn = gr.Button("Generate image").style(
|
203 |
+
margin=False,
|
204 |
+
rounded=(False, True, True, False),
|
205 |
+
)
|
206 |
+
|
207 |
+
gallery = gr.Gallery(
|
208 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
209 |
+
).style(grid=[2], height="auto")
|
210 |
+
|
211 |
+
advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
|
212 |
+
|
213 |
+
with gr.Row(elem_id="advanced-options"):
|
214 |
+
samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
|
215 |
+
scale = gr.Slider(
|
216 |
+
label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
|
217 |
+
)
|
218 |
+
|
219 |
+
|
220 |
+
ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, scale], outputs=gallery, cache_examples=False)
|
221 |
+
ex.dataset.headers = [""]
|
222 |
+
|
223 |
+
|
224 |
+
text.submit(infer, inputs=[text, samples, scale], outputs=gallery)
|
225 |
+
btn.click(infer, inputs=[text, samples, scale], outputs=gallery)
|
226 |
+
advanced_button.click(
|
227 |
+
None,
|
228 |
+
[],
|
229 |
+
text,
|
230 |
+
_js="""
|
231 |
+
() => {
|
232 |
+
const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
|
233 |
+
options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
|
234 |
+
}""",
|
235 |
+
)
|
236 |
+
gr.HTML(
|
237 |
+
"""
|
238 |
+
<div class="footer">
|
239 |
+
<p> Gradio Demo by 🤗 Hugging Face
|
240 |
+
</p>
|
241 |
+
</div>
|
242 |
+
<div class="acknowledgments">
|
243 |
+
<p><h4>LICENSE</h4>
|
244 |
+
The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
|
245 |
+
<p><h4>Biases and content acknowledgment</h4>
|
246 |
+
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
|
247 |
+
</div>
|
248 |
+
"""
|
249 |
+
)
|
250 |
+
|
251 |
+
block.launch(debug=True)
|