hysts HF staff commited on
Commit
cecb702
1 Parent(s): bd2a246
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 +207 -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 2
3
  emoji: 😻
4
  colorFrom: gray
5
  colorTo: purple
@@ -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.2
3
  emoji: 😻
4
  colorFrom: gray
5
  colorTo: purple
 
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,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 KandinskyV22Pipeline, KandinskyV22PriorPipeline
13
+
14
+ DESCRIPTION = '# Kandinsky 2.2'
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', '1024'))
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 = KandinskyV22PriorPipeline.from_pretrained(
27
+ 'kandinsky-community/kandinsky-2-2-prior', torch_dtype=torch.float16)
28
+ pipe_prior.to(device)
29
+ pipe = KandinskyV22Pipeline.from_pretrained(
30
+ 'kandinsky-community/kandinsky-2-2-decoder', torch_dtype=torch.float16)
31
+ pipe.to(device)
32
+ if USE_TORCH_COMPILE:
33
+ pipe.unet.to(memory_format=torch.channels_last)
34
+ pipe.unet = torch.compile(pipe.unet,
35
+ mode='reduce-overhead',
36
+ fullgraph=True)
37
+ else:
38
+ pipe_prior = None
39
+ pipe = None
40
+
41
+
42
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
43
+ if randomize_seed:
44
+ seed = random.randint(0, MAX_SEED)
45
+ return seed
46
+
47
+
48
+ def generate(prompt: str,
49
+ negative_prompt: str = 'low quality, bad quality',
50
+ seed: int = 0,
51
+ width: int = 768,
52
+ height: int = 768,
53
+ guidance_scale_prior: float = 1.0,
54
+ guidance_scale: float = 4.0,
55
+ num_inference_steps_prior: int = 50,
56
+ num_inference_steps: int = 100) -> PIL.Image.Image:
57
+ generator = torch.Generator().manual_seed(seed)
58
+ image_embeds, negative_image_embeds = pipe_prior(
59
+ prompt,
60
+ negative_prompt,
61
+ generator=generator,
62
+ guidance_scale=guidance_scale_prior,
63
+ num_inference_steps=num_inference_steps_prior,
64
+ ).to_tuple()
65
+ image = pipe(
66
+ image_embeds=image_embeds,
67
+ negative_image_embeds=negative_image_embeds,
68
+ height=height,
69
+ width=width,
70
+ generator=generator,
71
+ guidance_scale=guidance_scale,
72
+ num_inference_steps=num_inference_steps,
73
+ ).images[0]
74
+ return image
75
+
76
+
77
+ examples = [
78
+ 'An astronaut riding a horse',
79
+ 'portrait of a young woman, blue eyes, cinematic',
80
+ 'A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting',
81
+ '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',
82
+ 'A car exploding into colorful dust',
83
+ 'editorial photography of an organic, almost liquid smoke style armchair',
84
+ 'birds eye view of a quilted paper style alien planet landscape, vibrant colours, Cinematic lighting',
85
+ 'Toy smiling cute octopus in a black hat, sticker',
86
+ 'Red sport car, sticker',
87
+ ]
88
+
89
+ with gr.Blocks(css='style.css') as demo:
90
+ gr.Markdown(DESCRIPTION)
91
+ gr.DuplicateButton(value='Duplicate Space for private use',
92
+ elem_id='duplicate-button',
93
+ visible=os.getenv('SHOW_DUPLICATE_BUTTON') == '1')
94
+ with gr.Box():
95
+ with gr.Row():
96
+ prompt = gr.Text(
97
+ label='Prompt',
98
+ show_label=False,
99
+ max_lines=1,
100
+ placeholder='Enter your prompt',
101
+ container=False,
102
+ )
103
+ run_button = gr.Button('Run', scale=0)
104
+ result = gr.Image(label='Result', show_label=False)
105
+ with gr.Accordion('Advanced options', open=False):
106
+ negative_prompt = gr.Text(
107
+ label='Negative prompt',
108
+ value='low quality, bad quality',
109
+ max_lines=1,
110
+ placeholder='Enter a negative prompt',
111
+ )
112
+ seed = gr.Slider(label='Seed',
113
+ minimum=0,
114
+ maximum=MAX_SEED,
115
+ step=1,
116
+ value=0)
117
+ randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
118
+ width = gr.Slider(
119
+ label='Width',
120
+ minimum=256,
121
+ maximum=MAX_IMAGE_SIZE,
122
+ step=32,
123
+ value=768,
124
+ )
125
+ height = gr.Slider(
126
+ label='Height',
127
+ minimum=256,
128
+ maximum=MAX_IMAGE_SIZE,
129
+ step=32,
130
+ value=768,
131
+ )
132
+ guidance_scale_prior = gr.Slider(label='Guidance scale for prior',
133
+ minimum=1,
134
+ maximum=20,
135
+ step=0.1,
136
+ value=1.0)
137
+ guidance_scale = gr.Slider(label='Guidance scale',
138
+ minimum=1,
139
+ maximum=20,
140
+ step=0.1,
141
+ value=4.0)
142
+ num_inference_steps_prior = gr.Slider(
143
+ label='Number of inference steps for prior',
144
+ minimum=10,
145
+ maximum=100,
146
+ step=1,
147
+ value=50)
148
+ num_inference_steps = gr.Slider(label='Number of inference steps',
149
+ minimum=10,
150
+ maximum=150,
151
+ step=1,
152
+ value=100)
153
+
154
+ gr.Examples(examples=examples,
155
+ inputs=prompt,
156
+ outputs=result,
157
+ fn=generate,
158
+ cache_examples=CACHE_EXAMPLES)
159
+
160
+ inputs = [
161
+ prompt,
162
+ negative_prompt,
163
+ seed,
164
+ width,
165
+ height,
166
+ guidance_scale_prior,
167
+ guidance_scale,
168
+ num_inference_steps_prior,
169
+ num_inference_steps,
170
+ ]
171
+ prompt.submit(
172
+ fn=randomize_seed_fn,
173
+ inputs=[seed, randomize_seed],
174
+ outputs=seed,
175
+ queue=False,
176
+ api_name=False,
177
+ ).then(
178
+ fn=generate,
179
+ inputs=inputs,
180
+ outputs=result,
181
+ api_name='run',
182
+ )
183
+ negative_prompt.submit(
184
+ fn=randomize_seed_fn,
185
+ inputs=[seed, randomize_seed],
186
+ outputs=seed,
187
+ queue=False,
188
+ api_name=False,
189
+ ).then(
190
+ fn=generate,
191
+ inputs=inputs,
192
+ outputs=result,
193
+ api_name=False,
194
+ )
195
+ run_button.click(
196
+ fn=randomize_seed_fn,
197
+ inputs=[seed, randomize_seed],
198
+ outputs=seed,
199
+ queue=False,
200
+ api_name=False,
201
+ ).then(
202
+ fn=generate,
203
+ inputs=inputs,
204
+ outputs=result,
205
+ api_name=False,
206
+ )
207
+ 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
+ }