niggathug commited on
Commit
62d6ed7
1 Parent(s): 6f66fbc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +266 -0
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()