hysts HF staff commited on
Commit
93c9d70
1 Parent(s): c039d86
Files changed (8) hide show
  1. .pre-commit-config.yaml +36 -0
  2. .style.yapf +5 -0
  3. .vscode/settings.json +18 -0
  4. LICENSE +21 -0
  5. README.md +3 -1
  6. app.py +211 -0
  7. requirements.txt +6 -0
  8. style.css +16 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: double-quote-string-fixer
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ args: ['--fix=lf']
15
+ - id: requirements-txt-fixer
16
+ - id: trailing-whitespace
17
+ - repo: https://github.com/myint/docformatter
18
+ rev: v1.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ additional_dependencies: ['types-python-slugify']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
.vscode/settings.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "python.linting.enabled": true,
3
+ "python.linting.flake8Enabled": true,
4
+ "python.linting.pylintEnabled": false,
5
+ "python.linting.lintOnSave": true,
6
+ "python.formatting.provider": "yapf",
7
+ "python.formatting.yapfArgs": [
8
+ "--style={based_on_style: pep8, indent_width: 4, blank_line_before_nested_class_or_def: false, spaces_before_comment: 2, split_before_logical_operator: true}"
9
+ ],
10
+ "[python]": {
11
+ "editor.formatOnType": true,
12
+ "editor.codeActionsOnSave": {
13
+ "source.organizeImports": true
14
+ }
15
+ },
16
+ "editor.formatOnSave": true,
17
+ "files.insertFinalNewline": true
18
+ }
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 hysts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Kandinsky 2 1
3
  emoji: 📚
4
  colorFrom: purple
5
  colorTo: green
@@ -7,6 +7,8 @@ sdk: gradio
7
  sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Kandinsky 2.1
3
  emoji: 📚
4
  colorFrom: purple
5
  colorTo: green
 
7
  sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
+ suggested_hardware: t4-small
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import random
7
+
8
+ import gradio as gr
9
+ import numpy as np
10
+ import PIL.Image
11
+ import torch
12
+ from diffusers import DDPMScheduler, DiffusionPipeline
13
+
14
+ DESCRIPTION = '# Kandinsky 2.1'
15
+ if not torch.cuda.is_available():
16
+ DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'
17
+
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv(
20
+ 'CACHE_EXAMPLES') == '1'
21
+ MAX_IMAGE_SIZE = int(os.getenv('MAX_IMAGE_SIZE', '768'))
22
+ USE_TORCH_COMPILE = os.getenv('USE_TORCH_COMPILE') == '1'
23
+
24
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
25
+ if torch.cuda.is_available():
26
+ pipe_prior = DiffusionPipeline.from_pretrained(
27
+ 'kandinsky-community/kandinsky-2-1-prior', torch_dtype=torch.float16)
28
+ pipe_prior.to(device)
29
+
30
+ scheduler = DDPMScheduler.from_pretrained(
31
+ 'kandinsky-community/kandinsky-2-1', subfolder='ddpm_scheduler')
32
+ pipe = DiffusionPipeline.from_pretrained(
33
+ 'kandinsky-community/kandinsky-2-1',
34
+ scheduler=scheduler,
35
+ torch_dtype=torch.float16)
36
+ pipe.to(device)
37
+ if USE_TORCH_COMPILE:
38
+ pipe.unet.to(memory_format=torch.channels_last)
39
+ pipe.unet = torch.compile(pipe.unet,
40
+ mode='reduce-overhead',
41
+ fullgraph=True)
42
+ else:
43
+ pipe_prior = None
44
+ pipe = None
45
+
46
+
47
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
48
+ if randomize_seed:
49
+ seed = random.randint(0, MAX_SEED)
50
+ return seed
51
+
52
+
53
+ def generate(prompt: str,
54
+ negative_prompt: str = 'low quality, bad quality',
55
+ seed: int = 0,
56
+ width: int = 768,
57
+ height: int = 768,
58
+ guidance_scale_prior: float = 1.0,
59
+ guidance_scale: float = 4.0,
60
+ num_inference_steps_prior: int = 50,
61
+ num_inference_steps: int = 100) -> PIL.Image.Image:
62
+ generator = torch.Generator().manual_seed(seed)
63
+ image_embeds, negative_image_embeds = pipe_prior(
64
+ prompt,
65
+ negative_prompt,
66
+ generator=generator,
67
+ guidance_scale=guidance_scale_prior,
68
+ num_inference_steps=num_inference_steps_prior,
69
+ ).to_tuple()
70
+ image = pipe(
71
+ prompt=prompt,
72
+ image_embeds=image_embeds,
73
+ negative_image_embeds=negative_image_embeds,
74
+ height=height,
75
+ width=width,
76
+ generator=generator,
77
+ guidance_scale=guidance_scale,
78
+ num_inference_steps=num_inference_steps,
79
+ ).images[0]
80
+ return image
81
+
82
+
83
+ examples = [
84
+ 'An astronaut riding a horse',
85
+ 'portrait of a young woman, blue eyes, cinematic',
86
+ 'A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting',
87
+ 'bird eye view shot of a full body woman with cyan light orange magenta makeup, digital art, long braided hair her face separated by makeup in the style of yin Yang surrealism, symmetrical face, real image, contrasting tone, pastel gradient background',
88
+ 'A car exploding into colorful dust',
89
+ 'editorial photography of an organic, almost liquid smoke style armchair',
90
+ 'birds eye view of a quilted paper style alien planet landscape, vibrant colours, Cinematic lighting',
91
+ ]
92
+
93
+ with gr.Blocks(css='style.css') as demo:
94
+ gr.Markdown(DESCRIPTION)
95
+ gr.DuplicateButton(value='Duplicate Space for private use',
96
+ elem_id='duplicate-button',
97
+ visible=os.getenv('SHOW_DUPLICATE_BUTTON') == '1')
98
+ with gr.Box():
99
+ with gr.Row():
100
+ prompt = gr.Text(
101
+ label='Prompt',
102
+ show_label=False,
103
+ max_lines=1,
104
+ placeholder='Enter your prompt',
105
+ container=False,
106
+ )
107
+ run_button = gr.Button('Run', scale=0)
108
+ result = gr.Image(label='Result', show_label=False)
109
+ with gr.Accordion('Advanced options', open=False):
110
+ negative_prompt = gr.Text(
111
+ label='Negative prompt',
112
+ value='low quality, bad quality',
113
+ max_lines=1,
114
+ placeholder='Enter a negative prompt',
115
+ )
116
+ seed = gr.Slider(label='Seed',
117
+ minimum=0,
118
+ maximum=MAX_SEED,
119
+ step=1,
120
+ value=0)
121
+ randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
122
+ width = gr.Slider(
123
+ label='Width',
124
+ minimum=256,
125
+ maximum=MAX_IMAGE_SIZE,
126
+ step=32,
127
+ value=768,
128
+ )
129
+ height = gr.Slider(
130
+ label='Height',
131
+ minimum=256,
132
+ maximum=MAX_IMAGE_SIZE,
133
+ step=32,
134
+ value=768,
135
+ )
136
+ guidance_scale_prior = gr.Slider(label='Guidance scale for prior',
137
+ minimum=1,
138
+ maximum=20,
139
+ step=0.1,
140
+ value=4.0)
141
+ guidance_scale = gr.Slider(label='Guidance scale',
142
+ minimum=1,
143
+ maximum=20,
144
+ step=0.1,
145
+ value=4.0)
146
+ num_inference_steps_prior = gr.Slider(
147
+ label='Number of inference steps for prior',
148
+ minimum=10,
149
+ maximum=100,
150
+ step=1,
151
+ value=50)
152
+ num_inference_steps = gr.Slider(label='Number of inference steps',
153
+ minimum=10,
154
+ maximum=150,
155
+ step=1,
156
+ value=100)
157
+
158
+ gr.Examples(examples=examples,
159
+ inputs=prompt,
160
+ outputs=result,
161
+ fn=generate,
162
+ cache_examples=CACHE_EXAMPLES)
163
+
164
+ inputs = [
165
+ prompt,
166
+ negative_prompt,
167
+ seed,
168
+ width,
169
+ height,
170
+ guidance_scale_prior,
171
+ guidance_scale,
172
+ num_inference_steps_prior,
173
+ num_inference_steps,
174
+ ]
175
+ prompt.submit(
176
+ fn=randomize_seed_fn,
177
+ inputs=[seed, randomize_seed],
178
+ outputs=seed,
179
+ queue=False,
180
+ api_name=False,
181
+ ).then(
182
+ fn=generate,
183
+ inputs=inputs,
184
+ outputs=result,
185
+ api_name='run',
186
+ )
187
+ negative_prompt.submit(
188
+ fn=randomize_seed_fn,
189
+ inputs=[seed, randomize_seed],
190
+ outputs=seed,
191
+ queue=False,
192
+ api_name=False,
193
+ ).then(
194
+ fn=generate,
195
+ inputs=inputs,
196
+ outputs=result,
197
+ api_name=False,
198
+ )
199
+ run_button.click(
200
+ fn=randomize_seed_fn,
201
+ inputs=[seed, randomize_seed],
202
+ outputs=seed,
203
+ queue=False,
204
+ api_name=False,
205
+ ).then(
206
+ fn=generate,
207
+ inputs=inputs,
208
+ outputs=result,
209
+ api_name=False,
210
+ )
211
+ demo.queue(max_size=20).launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ accelerate==0.20.3
2
+ diffusers==0.18.2
3
+ gradio==3.36.1
4
+ torch==2.0.1
5
+ torchvision==0.15.2
6
+ transformers==4.30.2
style.css ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #component-0 {
6
+ max-width: 730px;
7
+ margin: auto;
8
+ padding-top: 1.5rem;
9
+ }
10
+
11
+ #duplicate-button {
12
+ margin: auto;
13
+ color: white;
14
+ background: #1565c0;
15
+ border-radius: 100vh;
16
+ }