wei112311 hysts HF staff commited on
Commit
4f48ea2
0 Parent(s):

Duplicate from TencentARC/T2I-Adapter-SDXL-Sketch

Browse files

Co-authored-by: hysts <hysts@users.noreply.huggingface.co>

Files changed (8) hide show
  1. .gitattributes +35 -0
  2. .pre-commit-config.yaml +50 -0
  3. .vscode/settings.json +21 -0
  4. LICENSE +21 -0
  5. README.md +13 -0
  6. app.py +266 -0
  7. requirements.txt +9 -0
  8. style.css +16 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.pre-commit-config.yaml ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.4.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: end-of-file-fixer
12
+ - id: mixed-line-ending
13
+ args: ["--fix=lf"]
14
+ - id: requirements-txt-fixer
15
+ - id: trailing-whitespace
16
+ - repo: https://github.com/myint/docformatter
17
+ rev: v1.7.5
18
+ hooks:
19
+ - id: docformatter
20
+ args: ["--in-place"]
21
+ - repo: https://github.com/pycqa/isort
22
+ rev: 5.12.0
23
+ hooks:
24
+ - id: isort
25
+ args: ["--profile", "black"]
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v1.5.1
28
+ hooks:
29
+ - id: mypy
30
+ args: ["--ignore-missing-imports"]
31
+ additional_dependencies: ["types-python-slugify", "types-requests", "types-PyYAML"]
32
+ - repo: https://github.com/psf/black
33
+ rev: 23.7.0
34
+ hooks:
35
+ - id: black
36
+ language_version: python3.10
37
+ args: ["--line-length", "119"]
38
+ - repo: https://github.com/kynan/nbstripout
39
+ rev: 0.6.1
40
+ hooks:
41
+ - id: nbstripout
42
+ args: ["--extra-keys", "metadata.interpreter metadata.kernelspec cell.metadata.pycharm"]
43
+ - repo: https://github.com/nbQA-dev/nbQA
44
+ rev: 1.7.0
45
+ hooks:
46
+ - id: nbqa-black
47
+ - id: nbqa-pyupgrade
48
+ args: ["--py37-plus"]
49
+ - id: nbqa-isort
50
+ args: ["--float-to-top"]
.vscode/settings.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "[python]": {
3
+ "editor.defaultFormatter": "ms-python.black-formatter",
4
+ "editor.formatOnType": true,
5
+ "editor.codeActionsOnSave": {
6
+ "source.organizeImports": true
7
+ }
8
+ },
9
+ "black-formatter.args": [
10
+ "--line-length=119"
11
+ ],
12
+ "isort.args": ["--profile", "black"],
13
+ "flake8.args": [
14
+ "--max-line-length=119"
15
+ ],
16
+ "ruff.args": [
17
+ "--line-length=119"
18
+ ],
19
+ "editor.formatOnSave": true,
20
+ "files.insertFinalNewline": true
21
+ }
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 ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: T2I Adapter SDXL Sketch
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.43.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: TencentARC/T2I-Adapter-SDXL-Sketch
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import os
4
+ import random
5
+
6
+ import gradio as gr
7
+ import numpy as np
8
+ import PIL.Image
9
+ import torch
10
+ import torchvision.transforms.functional as TF
11
+ from diffusers import (
12
+ AutoencoderKL,
13
+ EulerAncestralDiscreteScheduler,
14
+ StableDiffusionXLAdapterPipeline,
15
+ T2IAdapter,
16
+ )
17
+
18
+ DESCRIPTION = '''# Doodly - T2I-Adapter-SDXL **Sketch**
19
+ To try out all the [6 T2I-Adapter](https://huggingface.co/collections/TencentARC/t2i-adapter-sdxl-64fac9cbf393f30370eeb02f) released for SDXL, [click here](https://huggingface.co/spaces/TencentARC/T2I-Adapter-SDXL)
20
+ '''
21
+
22
+ if not torch.cuda.is_available():
23
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
24
+
25
+ style_list = [
26
+ {
27
+ "name": "(No style)",
28
+ "prompt": "{prompt}",
29
+ "negative_prompt": "",
30
+ },
31
+ {
32
+ "name": "Cinematic",
33
+ "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
34
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
35
+ },
36
+ {
37
+ "name": "3D Model",
38
+ "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
39
+ "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
40
+ },
41
+ {
42
+ "name": "Anime",
43
+ "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
44
+ "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
45
+ },
46
+ {
47
+ "name": "Digital Art",
48
+ "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
49
+ "negative_prompt": "photo, photorealistic, realism, ugly",
50
+ },
51
+ {
52
+ "name": "Photographic",
53
+ "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
54
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
55
+ },
56
+ {
57
+ "name": "Pixel art",
58
+ "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
59
+ "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
60
+ },
61
+ {
62
+ "name": "Fantasy art",
63
+ "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
64
+ "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
65
+ },
66
+ {
67
+ "name": "Neonpunk",
68
+ "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
69
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
70
+ },
71
+ {
72
+ "name": "Manga",
73
+ "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
74
+ "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
75
+ },
76
+ ]
77
+
78
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
79
+ STYLE_NAMES = list(styles.keys())
80
+ DEFAULT_STYLE_NAME = "(No style)"
81
+
82
+
83
+ def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
84
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
85
+ return p.replace("{prompt}", positive), n + negative
86
+
87
+
88
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
89
+ if torch.cuda.is_available():
90
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
91
+ adapter = T2IAdapter.from_pretrained(
92
+ "TencentARC/t2i-adapter-sketch-sdxl-1.0", torch_dtype=torch.float16, variant="fp16"
93
+ )
94
+ scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
95
+ pipe = StableDiffusionXLAdapterPipeline.from_pretrained(
96
+ model_id,
97
+ vae=AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16),
98
+ adapter=adapter,
99
+ scheduler=scheduler,
100
+ torch_dtype=torch.float16,
101
+ variant="fp16",
102
+ )
103
+ pipe.to(device)
104
+ else:
105
+ pipe = None
106
+
107
+ MAX_SEED = np.iinfo(np.int32).max
108
+
109
+
110
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
111
+ if randomize_seed:
112
+ seed = random.randint(0, MAX_SEED)
113
+ return seed
114
+
115
+
116
+ def run(
117
+ image: PIL.Image.Image,
118
+ prompt: str,
119
+ negative_prompt: str,
120
+ style_name: str = DEFAULT_STYLE_NAME,
121
+ num_steps: int = 25,
122
+ guidance_scale: float = 5,
123
+ adapter_conditioning_scale: float = 0.8,
124
+ adapter_conditioning_factor: float = 0.8,
125
+ seed: int = 0,
126
+ progress=gr.Progress(track_tqdm=True),
127
+ ) -> PIL.Image.Image:
128
+ image = image.convert("RGB")
129
+ image = TF.to_tensor(image) > 0.5
130
+ image = TF.to_pil_image(image.to(torch.float32))
131
+
132
+ prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
133
+
134
+ generator = torch.Generator(device=device).manual_seed(seed)
135
+ out = pipe(
136
+ prompt=prompt,
137
+ negative_prompt=negative_prompt,
138
+ image=image,
139
+ num_inference_steps=num_steps,
140
+ generator=generator,
141
+ guidance_scale=guidance_scale,
142
+ adapter_conditioning_scale=adapter_conditioning_scale,
143
+ adapter_conditioning_factor=adapter_conditioning_factor,
144
+ ).images[0]
145
+ return out
146
+
147
+
148
+ with gr.Blocks(css="style.css") as demo:
149
+ gr.Markdown(DESCRIPTION, elem_id="description")
150
+ gr.DuplicateButton(
151
+ value="Duplicate Space for private use",
152
+ elem_id="duplicate-button",
153
+ visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
154
+ )
155
+
156
+ with gr.Row():
157
+ with gr.Column():
158
+ with gr.Group():
159
+ image = gr.Image(
160
+ source="canvas",
161
+ tool="sketch",
162
+ type="pil",
163
+ image_mode="L",
164
+ invert_colors=True,
165
+ shape=(1024, 1024),
166
+ brush_radius=4,
167
+ height=440,
168
+ )
169
+ prompt = gr.Textbox(label="Prompt")
170
+ style = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
171
+ run_button = gr.Button("Run")
172
+ with gr.Accordion("Advanced options", open=False):
173
+ negative_prompt = gr.Textbox(
174
+ label="Negative prompt",
175
+ value=" extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured",
176
+ )
177
+ num_steps = gr.Slider(
178
+ label="Number of steps",
179
+ minimum=1,
180
+ maximum=50,
181
+ step=1,
182
+ value=25,
183
+ )
184
+ guidance_scale = gr.Slider(
185
+ label="Guidance scale",
186
+ minimum=0.1,
187
+ maximum=10.0,
188
+ step=0.1,
189
+ value=5,
190
+ )
191
+ adapter_conditioning_scale = gr.Slider(
192
+ label="Adapter conditioning scale",
193
+ minimum=0.5,
194
+ maximum=1,
195
+ step=0.1,
196
+ value=0.8,
197
+ )
198
+ adapter_conditioning_factor = gr.Slider(
199
+ label="Adapter conditioning factor",
200
+ info="Fraction of timesteps for which adapter should be applied",
201
+ minimum=0.5,
202
+ maximum=1,
203
+ step=0.1,
204
+ value=0.8,
205
+ )
206
+ seed = gr.Slider(
207
+ label="Seed",
208
+ minimum=0,
209
+ maximum=MAX_SEED,
210
+ step=1,
211
+ value=0,
212
+ )
213
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
214
+ with gr.Column():
215
+ result = gr.Image(label="Result", height=400)
216
+
217
+ inputs = [
218
+ image,
219
+ prompt,
220
+ negative_prompt,
221
+ style,
222
+ num_steps,
223
+ guidance_scale,
224
+ adapter_conditioning_scale,
225
+ adapter_conditioning_factor,
226
+ seed,
227
+ ]
228
+ prompt.submit(
229
+ fn=randomize_seed_fn,
230
+ inputs=[seed, randomize_seed],
231
+ outputs=seed,
232
+ queue=False,
233
+ api_name=False,
234
+ ).then(
235
+ fn=run,
236
+ inputs=inputs,
237
+ outputs=result,
238
+ api_name=False,
239
+ )
240
+ negative_prompt.submit(
241
+ fn=randomize_seed_fn,
242
+ inputs=[seed, randomize_seed],
243
+ outputs=seed,
244
+ queue=False,
245
+ api_name=False,
246
+ ).then(
247
+ fn=run,
248
+ inputs=inputs,
249
+ outputs=result,
250
+ api_name=False,
251
+ )
252
+ run_button.click(
253
+ fn=randomize_seed_fn,
254
+ inputs=[seed, randomize_seed],
255
+ outputs=seed,
256
+ queue=False,
257
+ api_name=False,
258
+ ).then(
259
+ fn=run,
260
+ inputs=inputs,
261
+ outputs=result,
262
+ api_name=False,
263
+ )
264
+
265
+ if __name__ == "__main__":
266
+ demo.queue(max_size=20).launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.22.0
2
+ git+https://github.com/huggingface/diffusers@t2i-adapter-load-lora
3
+ gradio==3.43.1
4
+ Pillow==10.0.0
5
+ safetensors==0.3.3
6
+ torch==2.0.1
7
+ torchvision==0.15.2
8
+ transformers==4.33.1
9
+ xformers==0.0.20
style.css ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #component-0{
3
+ max-width: 900px;
4
+ margin: 0 auto;
5
+ }
6
+
7
+ #description, h1 {
8
+ text-align: center;
9
+ }
10
+
11
+ #duplicate-button {
12
+ margin: auto;
13
+ color: #fff;
14
+ background: #1565c0;
15
+ border-radius: 100vh;
16
+ }