Spaces:
Build error
Build error
andrewburns
commited on
Commit
•
c0faf6a
1
Parent(s):
5b42b76
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from contextlib import nullcontext
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from torch import autocast
|
5 |
+
from diffusers import StableDiffusionPipeline
|
6 |
+
|
7 |
+
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
context = autocast if device == "cuda" else nullcontext
|
10 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
11 |
+
|
12 |
+
pipe = StableDiffusionPipeline.from_pretrained("andrewburns/sd-icon-test-diffusers", torch_dtype=dtype)
|
13 |
+
pipe = pipe.to(device)
|
14 |
+
|
15 |
+
|
16 |
+
# Sometimes the nsfw checker is confused by the Pokémon images, you can disable
|
17 |
+
# it at your own risk here
|
18 |
+
disable_safety = True
|
19 |
+
|
20 |
+
if disable_safety:
|
21 |
+
def null_safety(images, **kwargs):
|
22 |
+
return images, False
|
23 |
+
pipe.safety_checker = null_safety
|
24 |
+
|
25 |
+
|
26 |
+
def infer(prompt, n_samples, steps, scale):
|
27 |
+
|
28 |
+
with context("cuda"):
|
29 |
+
images = pipe(n_samples*[prompt], guidance_scale=scale, num_inference_steps=steps).images
|
30 |
+
|
31 |
+
return images
|
32 |
+
|
33 |
+
css = """
|
34 |
+
a {
|
35 |
+
color: inherit;
|
36 |
+
text-decoration: underline;
|
37 |
+
}
|
38 |
+
.gradio-container {
|
39 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
40 |
+
}
|
41 |
+
.gr-button {
|
42 |
+
color: white;
|
43 |
+
border-color: #9d66e5;
|
44 |
+
background: #9d66e5;
|
45 |
+
}
|
46 |
+
input[type='range'] {
|
47 |
+
accent-color: #9d66e5;
|
48 |
+
}
|
49 |
+
.dark input[type='range'] {
|
50 |
+
accent-color: #dfdfdf;
|
51 |
+
}
|
52 |
+
.container {
|
53 |
+
max-width: 730px;
|
54 |
+
margin: auto;
|
55 |
+
padding-top: 1.5rem;
|
56 |
+
}
|
57 |
+
#gallery {
|
58 |
+
min-height: 22rem;
|
59 |
+
margin-bottom: 15px;
|
60 |
+
margin-left: auto;
|
61 |
+
margin-right: auto;
|
62 |
+
border-bottom-right-radius: .5rem !important;
|
63 |
+
border-bottom-left-radius: .5rem !important;
|
64 |
+
}
|
65 |
+
#gallery>div>.h-full {
|
66 |
+
min-height: 20rem;
|
67 |
+
}
|
68 |
+
.details:hover {
|
69 |
+
text-decoration: underline;
|
70 |
+
}
|
71 |
+
.gr-button {
|
72 |
+
white-space: nowrap;
|
73 |
+
}
|
74 |
+
.gr-button:focus {
|
75 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
76 |
+
outline: none;
|
77 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
78 |
+
--tw-border-opacity: 1;
|
79 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
80 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
81 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
82 |
+
--tw-ring-opacity: .5;
|
83 |
+
}
|
84 |
+
#advanced-options {
|
85 |
+
margin-bottom: 20px;
|
86 |
+
}
|
87 |
+
.footer {
|
88 |
+
margin-bottom: 45px;
|
89 |
+
margin-top: 35px;
|
90 |
+
text-align: center;
|
91 |
+
border-bottom: 1px solid #e5e5e5;
|
92 |
+
}
|
93 |
+
.footer>p {
|
94 |
+
font-size: .8rem;
|
95 |
+
display: inline-block;
|
96 |
+
padding: 0 10px;
|
97 |
+
transform: translateY(10px);
|
98 |
+
background: white;
|
99 |
+
}
|
100 |
+
.dark .logo{ filter: invert(1); }
|
101 |
+
.dark .footer {
|
102 |
+
border-color: #303030;
|
103 |
+
}
|
104 |
+
.dark .footer>p {
|
105 |
+
background: #0b0f19;
|
106 |
+
}
|
107 |
+
.acknowledgments h4{
|
108 |
+
margin: 1.25em 0 .25em 0;
|
109 |
+
font-weight: bold;
|
110 |
+
font-size: 115%;
|
111 |
+
}
|
112 |
+
"""
|
113 |
+
|
114 |
+
block = gr.Blocks(css=css)
|
115 |
+
|
116 |
+
examples = [
|
117 |
+
[
|
118 |
+
'Yoda',
|
119 |
+
2,
|
120 |
+
7.5,
|
121 |
+
],
|
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 |
+
<img class="logo" src="https://lambdalabs.com/hubfs/logos/lambda-logo.svg" alt="Lambda Logo"
|
140 |
+
style="margin: auto; max-width: 7rem;">
|
141 |
+
<h1 style="font-weight: 900; font-size: 3rem;">
|
142 |
+
Pokémon text to image
|
143 |
+
</h1>
|
144 |
+
</div>
|
145 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
146 |
+
Generate new Pokémon from a text description,
|
147 |
+
<a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">created by Lambda Labs</a>.
|
148 |
+
</p>
|
149 |
+
</div>
|
150 |
+
"""
|
151 |
+
)
|
152 |
+
with gr.Group():
|
153 |
+
with gr.Box():
|
154 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
155 |
+
text = gr.Textbox(
|
156 |
+
label="Enter your prompt",
|
157 |
+
show_label=False,
|
158 |
+
max_lines=1,
|
159 |
+
placeholder="Enter your prompt",
|
160 |
+
).style(
|
161 |
+
border=(True, False, True, True),
|
162 |
+
rounded=(True, False, False, True),
|
163 |
+
container=False,
|
164 |
+
)
|
165 |
+
btn = gr.Button("Generate image").style(
|
166 |
+
margin=False,
|
167 |
+
rounded=(False, True, True, False),
|
168 |
+
)
|
169 |
+
|
170 |
+
gallery = gr.Gallery(
|
171 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
172 |
+
).style(grid=[2], height="auto")
|
173 |
+
|
174 |
+
|
175 |
+
with gr.Row(elem_id="advanced-options"):
|
176 |
+
samples = gr.Slider(label="Images", minimum=1, maximum=4, value=2, step=1)
|
177 |
+
steps = gr.Slider(label="Steps", minimum=5, maximum=50, value=25, step=5)
|
178 |
+
scale = gr.Slider(
|
179 |
+
label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
|
180 |
+
)
|
181 |
+
|
182 |
+
|
183 |
+
ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, scale], outputs=gallery, cache_examples=False)
|
184 |
+
ex.dataset.headers = [""]
|
185 |
+
|
186 |
+
|
187 |
+
text.submit(infer, inputs=[text, samples, steps, scale], outputs=gallery)
|
188 |
+
btn.click(infer, inputs=[text, samples, steps, scale], outputs=gallery)
|
189 |
+
gr.HTML(
|
190 |
+
"""
|
191 |
+
<div class="footer">
|
192 |
+
<p> Gradio Demo by 🤗 Hugging Face and Lambda Labs
|
193 |
+
</p>
|
194 |
+
</div>
|
195 |
+
<div class="acknowledgments">
|
196 |
+
<p> Put in a text prompt and generate your own Pokémon character, no "prompt engineering" required!
|
197 |
+
<p>If you want to find out how we made this model read about it in <a href="https://lambdalabs.com/blog/how-to-fine-tune-stable-diffusion-how-we-made-the-text-to-pokemon-model-at-lambda/">this blog post</a>.
|
198 |
+
<p>And if you want to train your own Stable Diffusion variants, see our <a href="https://github.com/LambdaLabsML/examples/tree/main/stable-diffusion-finetuning">Examples Repo</a>!
|
199 |
+
<p>Trained by <a href="justinpinkney.com">Justin Pinkney</a> (<a href="https://twitter.com/Buntworthy">@Buntworthy</a>) at <a href="https://lambdalabs.com/">Lambda Labs</a>.</p>
|
200 |
+
</div>
|
201 |
+
"""
|
202 |
+
)
|
203 |
+
|
204 |
+
block.launch()
|