Garrett Goon commited on
Commit
e3f97f3
1 Parent(s): 4811b12

changed to stability ai code

Browse files
Files changed (2) hide show
  1. app.py +278 -50
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  import pathlib
2
  import os
3
  from PIL import Image
@@ -10,6 +15,7 @@ import utils
10
 
11
  use_auth_token = os.environ["HF_AUTH_TOKEN"]
12
  NSFW_IMAGE = Image.open("nsfw.png")
 
13
 
14
  # Instantiate the pipeline.
15
  device, revision, torch_dtype = (
@@ -53,66 +59,288 @@ def replace_concept_tokens(text: str):
53
  text = text.replace(concept_token, dummy_tokens)
54
  return text
55
 
56
- all_generated_images = []
57
- def inference(
58
- prompt: str, guidance_scale: int, num_inference_steps: int, seed: int
59
- ):
 
60
  prompt = replace_concept_tokens(prompt)
61
  generator = torch.Generator(device=device).manual_seed(seed)
62
  output = pipeline(
63
- prompt=[prompt] * 2,
64
  num_inference_steps=num_inference_steps,
65
  guidance_scale=guidance_scale,
66
  generator=generator,
67
  )
68
  img_list, nsfw_list = output.images, output.nsfw_content_detected
69
- for img, nsfw in zip(img_list, nsfw_list):
70
- if nsfw:
71
- all_generated_images.append(NSFW_IMAGE)
72
- else:
73
- all_generated_images.append(img)
74
- return all_generated_images
75
-
76
- DEFAULT_PROMPT = (
77
- "A watercolor painting on textured paper of a <det-logo> using soft strokes,"
78
- " pastel colors, incredible composition, masterpiece"
79
- )
80
 
81
- with gr.Blocks() as demo:
82
- prompt = gr.Textbox(
83
- label="Prompt including the token '<det-logo>'",
84
- placeholder=DEFAULT_PROMPT,
85
- interactive=True,
86
- )
87
- guidance_scale = gr.Slider(
88
- minimum=1.0, maximum=20.0, value=3.0, label="Guidance Scale", interactive=True
89
- )
90
- num_inference_steps = gr.Slider(
91
- minimum=25,
92
- maximum=75,
93
- value=40,
94
- label="Num Inference Steps",
95
- interactive=True,
96
- step=1,
97
- )
98
- seed = gr.Slider(
99
- minimum=2147483147,
100
- maximum=2147483647,
101
- label="Seed",
102
- interactive=True,
103
- randomize=True
104
- )
105
 
106
- generate_btn = gr.Button(value="Generate")
107
- gallery = gr.Gallery(
108
- label="Generated Images",
109
- value=[],
110
- ).style(height="auto")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
- generate_btn.click(
113
- inference,
114
- inputs=[prompt, guidance_scale, num_inference_steps, seed],
115
- outputs=gallery,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- demo.launch()
 
1
+ """The model used in this Space alters the underlying Stable Diffusion model available at
2
+ https://huggingface.co/CompVis/stable-diffusion-v1-4 through the addition of new embedding vectors
3
+ in order to capture the likeness of the Determined AI logo. These alternations are fully captured
4
+ in the learned_embeddings_dict.pt pickle file in the root of the repository."""
5
+
6
  import pathlib
7
  import os
8
  from PIL import Image
 
15
 
16
  use_auth_token = os.environ["HF_AUTH_TOKEN"]
17
  NSFW_IMAGE = Image.open("nsfw.png")
18
+ BATCH_SIZE = 2
19
 
20
  # Instantiate the pipeline.
21
  device, revision, torch_dtype = (
 
59
  text = text.replace(concept_token, dummy_tokens)
60
  return text
61
 
62
+
63
+ all_imgs = []
64
+
65
+
66
+ def inference(prompt: str, guidance_scale: int, num_inference_steps: int, seed: int):
67
  prompt = replace_concept_tokens(prompt)
68
  generator = torch.Generator(device=device).manual_seed(seed)
69
  output = pipeline(
70
+ prompt=[prompt] * BATCH_SIZE,
71
  num_inference_steps=num_inference_steps,
72
  guidance_scale=guidance_scale,
73
  generator=generator,
74
  )
75
  img_list, nsfw_list = output.images, output.nsfw_content_detected
76
+ filtered_imgs = [
77
+ img if not nsfw else NSFW_IMAGE for img, nsfw in zip(img_list, nsfw_list)
78
+ ]
79
+ all_imgs.extend(filtered_imgs)
80
+ return all_imgs
 
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ css = """
84
+ .gradio-container {
85
+ font-family: 'IBM Plex Sans', sans-serif;
86
+ }
87
+ .gr-button {
88
+ color: white;
89
+ border-color: black;
90
+ background: black;
91
+ }
92
+ input[type='range'] {
93
+ accent-color: black;
94
+ }
95
+ .dark input[type='range'] {
96
+ accent-color: #dfdfdf;
97
+ }
98
+ .container {
99
+ max-width: 730px;
100
+ margin: auto;
101
+ padding-top: 1.5rem;
102
+ }
103
+ #gallery {
104
+ min-height: 22rem;
105
+ margin-bottom: 15px;
106
+ margin-left: auto;
107
+ margin-right: auto;
108
+ border-bottom-right-radius: .5rem !important;
109
+ border-bottom-left-radius: .5rem !important;
110
+ }
111
+ #gallery>div>.h-full {
112
+ min-height: 20rem;
113
+ }
114
+ .details:hover {
115
+ text-decoration: underline;
116
+ }
117
+ .gr-button {
118
+ white-space: nowrap;
119
+ }
120
+ .gr-button:focus {
121
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
122
+ outline: none;
123
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
124
+ --tw-border-opacity: 1;
125
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
126
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
127
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
128
+ --tw-ring-opacity: .5;
129
+ }
130
+ #advanced-btn {
131
+ font-size: .7rem !important;
132
+ line-height: 19px;
133
+ margin-top: 12px;
134
+ margin-bottom: 12px;
135
+ padding: 2px 8px;
136
+ border-radius: 14px !important;
137
+ }
138
+ #advanced-options {
139
+ display: none;
140
+ margin-bottom: 20px;
141
+ }
142
+ .footer {
143
+ margin-bottom: 45px;
144
+ margin-top: 35px;
145
+ text-align: center;
146
+ border-bottom: 1px solid #e5e5e5;
147
+ }
148
+ .footer>p {
149
+ font-size: .8rem;
150
+ display: inline-block;
151
+ padding: 0 10px;
152
+ transform: translateY(10px);
153
+ background: white;
154
+ }
155
+ .dark .footer {
156
+ border-color: #303030;
157
+ }
158
+ .dark .footer>p {
159
+ background: #0b0f19;
160
+ }
161
+ .acknowledgments h4{
162
+ margin: 1.25em 0 .25em 0;
163
+ font-weight: bold;
164
+ font-size: 115%;
165
+ }
166
+ #container-advanced-btns{
167
+ display: flex;
168
+ flex-wrap: wrap;
169
+ justify-content: space-between;
170
+ align-items: center;
171
+ }
172
+ .animate-spin {
173
+ animation: spin 1s linear infinite;
174
+ }
175
+ @keyframes spin {
176
+ from {
177
+ transform: rotate(0deg);
178
+ }
179
+ to {
180
+ transform: rotate(360deg);
181
+ }
182
+ }
183
+ #share-btn-container {
184
+ 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;
185
+ }
186
+ #share-btn {
187
+ 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;
188
+ }
189
+ #share-btn * {
190
+ all: unset;
191
+ }
192
+ .gr-form{
193
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
194
+ }
195
+ #prompt-container{
196
+ gap: 0;
197
+ }
198
+ """
199
 
200
+ block = gr.Blocks(css=css)
201
+
202
+ examples = [
203
+ [
204
+ "a Van Gogh painting of a <det-logo> with thick strokes, masterful composition",
205
+ # 4,
206
+ # 45,
207
+ # 7.5,
208
+ # 1024,
209
+ ],
210
+ [
211
+ "Futuristic <det-logo> in a desert, painting, octane render, 4 k, anime sky, warm colors",
212
+ # 4,
213
+ # 45,
214
+ # 7,
215
+ # 1024,
216
+ ],
217
+ [
218
+ "cell shaded cartoon of a <det-logo>, subtle colors, post grunge, concept art by josan gonzales and wlop, by james jean, victo ngai, david rubin, mike mignola, deviantart, art by artgem",
219
+ # 4,
220
+ # 45,
221
+ # 7,
222
+ # 1024,
223
+ ],
224
+ [
225
+ "a <det-logo> cloudy sky background lush landscape illustration concept art anime key visual trending pixiv fanbox by wlop and greg rutkowski and makoto shinkai and studio ghibli",
226
+ # 4,
227
+ # 45,
228
+ # 7,
229
+ # 1024,
230
+ ],
231
+ [
232
+ "Immense <det-logo> advanced technology scifi architectural structure desert planet alien wardrobe tim hildebrandt, wayne barlowe, bruce pennington, donato giancola, larry elmore, oil on canvas, masterpiece, trending on artstation, featured on pixiv, cinematic composition, dramatic, beautiful lighting",
233
+ # 4,
234
+ # 45,
235
+ # 7,
236
+ # 1024,
237
+ ],
238
+ ]
239
+
240
+
241
+ with block:
242
+ gr.HTML(
243
+ """
244
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
245
+ <div
246
+ style="
247
+ display: inline-flex;
248
+ align-items: center;
249
+ gap: 0.8rem;
250
+ font-size: 1.75rem;
251
+ "
252
+ >
253
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
254
+ Determined AI Textual Inversion Demo
255
+ </h1>
256
+ </div>
257
+ </div>
258
+ """
259
  )
260
+ with gr.Group():
261
+ with gr.Box():
262
+ with gr.Row(elem_id="prompt-container").style(equal_height=True):
263
+ prompt = gr.Textbox(
264
+ label="Enter a prompt including '<det-logo>'",
265
+ show_label=False,
266
+ max_lines=1,
267
+ placeholder="Enter a prompt including '<det-logo>'",
268
+ elem_id="prompt-text-input",
269
+ ).style(
270
+ container=False,
271
+ )
272
+ btn = gr.Button("Generate image").style(
273
+ full_width=False,
274
+ )
275
+
276
+ gallery = gr.Gallery(
277
+ label="Generated images", show_label=False, elem_id="gallery"
278
+ ).style(grid=[BATCH_SIZE], height="auto")
279
+
280
+ with gr.Group(elem_id="container-advanced-btns"):
281
+ advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
282
+
283
+ with gr.Row(elem_id="advanced-options"):
284
+ num_inference_steps = gr.Slider(
285
+ label="Steps", minimum=20, maximum=60, value=40, step=1
286
+ )
287
+ guidance_scale = gr.Slider(
288
+ label="Guidance Scale", minimum=1.0, maximum=30.0, value=4.0, step=0.1
289
+ )
290
+ seed = gr.Slider(
291
+ label="Seed",
292
+ minimum=0,
293
+ maximum=2147483647,
294
+ step=1,
295
+ randomize=True,
296
+ )
297
+
298
+ ex = gr.Examples(
299
+ examples=examples,
300
+ fn=inference,
301
+ inputs=[prompt, guidance_scale, num_inference_steps, seed],
302
+ outputs=[gallery],
303
+ cache_examples=False,
304
+ )
305
+ ex.dataset.headers = [""]
306
+
307
+ prompt.submit(
308
+ inference,
309
+ inputs=[prompt, guidance_scale, num_inference_steps, seed],
310
+ outputs=[gallery],
311
+ )
312
+ btn.click(
313
+ inference,
314
+ inputs=[prompt, guidance_scale, num_inference_steps, seed],
315
+ outputs=[gallery],
316
+ )
317
+ advanced_button.click(
318
+ None,
319
+ [],
320
+ prompt,
321
+ _js="""
322
+ () => {
323
+ var appDom = document.querySelector("body > gradio-app");
324
+ var options = appDom.querySelector("#advanced-options")
325
+ if (options == null) {options = appDom.shadowRoot.querySelector("#advanced-options")}
326
+ options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
327
+ }""",
328
+ )
329
+ gr.HTML(
330
+ """
331
+ <div class="footer">
332
+ <p>Underlying model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a> - Gradio code based on the <a href="https://huggingface.co/spaces/stabilityai/stable-diffusion" style="text-decoration: underline;" target="_blank">Stability AI Demo</a>
333
+ </p>
334
+ </div>
335
+ <div class="acknowledgments">
336
+ <p><h4>LICENSE</h4>
337
+ 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>
338
+ <p><h4>Model Changes</h4>
339
+ The model used in this Space alters the underlying <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">stable-diffusion-v1-4</a> model through the addition of new embedding vectors in order to capture the likeness of the <a href="https://www.determined.ai" style="text-decoration: underline;" target="_blank">Determined AI</a> logo.</p>
340
+ <p><h4>Biases and content acknowledgment</h4>
341
+ 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>
342
+ </div>
343
+ """
344
+ )
345
 
346
+ block.queue(max_size=10).launch(share=True, enable_queue=True)
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  diffusers==0.5.1
2
  transformers==4.21.3
3
  ftfy==6.1.1
4
- torch
 
 
1
  diffusers==0.5.1
2
  transformers==4.21.3
3
  ftfy==6.1.1
4
+ torch
5
+ pillow