hysts HF staff commited on
Commit
3494400
1 Parent(s): 5df7af3

pre-commit

Browse files
Files changed (4) hide show
  1. app_base.py +10 -7
  2. app_sketch.py +9 -6
  3. model.py +5 -2
  4. utils.py +6 -4
app_base.py CHANGED
@@ -4,10 +4,13 @@ import gradio as gr
4
  import PIL.Image
5
 
6
  from model import ADAPTER_NAMES, Model
7
- from utils import MAX_SEED, randomize_seed_fn, styles, style_names, apply_style
8
-
9
-
10
- default_style_name = "Photographic"
 
 
 
11
 
12
 
13
  def create_demo(model: Model) -> gr.Blocks:
@@ -16,7 +19,7 @@ def create_demo(model: Model) -> gr.Blocks:
16
  prompt: str,
17
  negative_prompt: str,
18
  adapter_name: str,
19
- style_name: str = default_style_name,
20
  num_inference_steps: int = 30,
21
  guidance_scale: float = 5.0,
22
  adapter_conditioning_scale: float = 1.0,
@@ -26,7 +29,7 @@ def create_demo(model: Model) -> gr.Blocks:
26
  progress=gr.Progress(track_tqdm=True),
27
  ) -> list[PIL.Image.Image]:
28
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
29
-
30
  return model.run(
31
  image=image,
32
  prompt=prompt,
@@ -54,7 +57,7 @@ def create_demo(model: Model) -> gr.Blocks:
54
  label="Negative prompt",
55
  value="",
56
  )
57
- style = gr.Dropdown(choices=style_names, value=default_style_name, label="Style")
58
  num_inference_steps = gr.Slider(
59
  label="Number of steps",
60
  minimum=1,
 
4
  import PIL.Image
5
 
6
  from model import ADAPTER_NAMES, Model
7
+ from utils import (
8
+ DEFAULT_STYLE_NAME,
9
+ MAX_SEED,
10
+ STYLE_NAMES,
11
+ apply_style,
12
+ randomize_seed_fn,
13
+ )
14
 
15
 
16
  def create_demo(model: Model) -> gr.Blocks:
 
19
  prompt: str,
20
  negative_prompt: str,
21
  adapter_name: str,
22
+ style_name: str = DEFAULT_STYLE_NAME,
23
  num_inference_steps: int = 30,
24
  guidance_scale: float = 5.0,
25
  adapter_conditioning_scale: float = 1.0,
 
29
  progress=gr.Progress(track_tqdm=True),
30
  ) -> list[PIL.Image.Image]:
31
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
32
+
33
  return model.run(
34
  image=image,
35
  prompt=prompt,
 
57
  label="Negative prompt",
58
  value="",
59
  )
60
+ style = gr.Dropdown(choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME, label="Style")
61
  num_inference_steps = gr.Slider(
62
  label="Number of steps",
63
  minimum=1,
app_sketch.py CHANGED
@@ -6,20 +6,23 @@ import torch
6
  import torchvision.transforms.functional as TF
7
 
8
  from model import Model
9
- from utils import MAX_SEED, randomize_seed_fn, styles, style_names, apply_style
10
-
 
 
 
 
 
11
 
12
  SKETCH_ADAPTER_NAME = "TencentARC/t2i-adapter-sketch-sdxl-1.0"
13
 
14
- default_style_name = "Photographic"
15
-
16
 
17
  def create_demo(model: Model) -> gr.Blocks:
18
  def run(
19
  image: PIL.Image.Image,
20
  prompt: str,
21
  negative_prompt: str,
22
- style_name: str = default_style_name,
23
  num_steps: int = 25,
24
  guidance_scale: float = 5,
25
  adapter_conditioning_scale: float = 0.8,
@@ -63,7 +66,7 @@ def create_demo(model: Model) -> gr.Blocks:
63
  prompt = gr.Textbox(label="Prompt")
64
  run_button = gr.Button("Run")
65
  with gr.Accordion("Advanced options", open=False):
66
- style = gr.Dropdown(choices=style_names, value=default_style_name, label="Style")
67
  negative_prompt = gr.Textbox(label="Negative prompt")
68
  num_steps = gr.Slider(
69
  label="Number of steps",
 
6
  import torchvision.transforms.functional as TF
7
 
8
  from model import Model
9
+ from utils import (
10
+ DEFAULT_STYLE_NAME,
11
+ MAX_SEED,
12
+ STYLE_NAMES,
13
+ apply_style,
14
+ randomize_seed_fn,
15
+ )
16
 
17
  SKETCH_ADAPTER_NAME = "TencentARC/t2i-adapter-sketch-sdxl-1.0"
18
 
 
 
19
 
20
  def create_demo(model: Model) -> gr.Blocks:
21
  def run(
22
  image: PIL.Image.Image,
23
  prompt: str,
24
  negative_prompt: str,
25
+ style_name: str = DEFAULT_STYLE_NAME,
26
  num_steps: int = 25,
27
  guidance_scale: float = 5,
28
  adapter_conditioning_scale: float = 0.8,
 
66
  prompt = gr.Textbox(label="Prompt")
67
  run_button = gr.Button("Run")
68
  with gr.Accordion("Advanced options", open=False):
69
+ style = gr.Dropdown(choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME, label="Style")
70
  negative_prompt = gr.Textbox(label="Negative prompt")
71
  num_steps = gr.Slider(
72
  label="Number of steps",
model.py CHANGED
@@ -47,10 +47,11 @@ SD_XL_BASE_RATIOS = {
47
  "3.0": (1728, 576),
48
  }
49
 
 
50
  def find_closest_aspect_ratio(target_width, target_height):
51
  target_ratio = target_width / target_height
52
  closest_ratio = None
53
- min_difference = float('inf')
54
 
55
  for ratio_str, (width, height) in SD_XL_BASE_RATIOS.items():
56
  ratio = width / height
@@ -260,7 +261,9 @@ class Model:
260
  variant="fp16",
261
  ).to(self.device)
262
  self.pipe.enable_xformers_memory_efficient_attention()
263
- self.pipe.load_lora_weights("stabilityai/stable-diffusion-xl-base-1.0", weight_name="sd_xl_offset_example-lora_1.0.safetensors")
 
 
264
  self.pipe.fuse_lora(lora_scale=0.4)
265
  else:
266
  self.preprocessor = None # type: ignore
 
47
  "3.0": (1728, 576),
48
  }
49
 
50
+
51
  def find_closest_aspect_ratio(target_width, target_height):
52
  target_ratio = target_width / target_height
53
  closest_ratio = None
54
+ min_difference = float("inf")
55
 
56
  for ratio_str, (width, height) in SD_XL_BASE_RATIOS.items():
57
  ratio = width / height
 
261
  variant="fp16",
262
  ).to(self.device)
263
  self.pipe.enable_xformers_memory_efficient_attention()
264
+ self.pipe.load_lora_weights(
265
+ "stabilityai/stable-diffusion-xl-base-1.0", weight_name="sd_xl_offset_example-lora_1.0.safetensors"
266
+ )
267
  self.pipe.fuse_lora(lora_scale=0.4)
268
  else:
269
  self.preprocessor = None # type: ignore
utils.py CHANGED
@@ -44,20 +44,22 @@ style_list = [
44
  {
45
  "name": "Neonpunk",
46
  "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",
47
- "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured"
48
  },
49
  {
50
  "name": "Manga",
51
  "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
52
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style"
53
  },
54
  ]
55
 
56
  styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
57
- style_names = list(styles.keys())
 
 
58
 
59
  def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
60
- p, n = styles.get(style_name, "Photographic")
61
  return p.replace("{prompt}", positive), n + negative
62
 
63
 
 
44
  {
45
  "name": "Neonpunk",
46
  "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",
47
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
48
  },
49
  {
50
  "name": "Manga",
51
  "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
52
+ "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
53
  },
54
  ]
55
 
56
  styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
57
+ STYLE_NAMES = list(styles.keys())
58
+ DEFAULT_STYLE_NAME = "Photographic"
59
+
60
 
61
  def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
62
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
63
  return p.replace("{prompt}", positive), n + negative
64
 
65