adamelliotfields commited on
Commit
eb8fc69
1 Parent(s): 60849d7

Image-to-image fixes

Browse files
Files changed (4) hide show
  1. README.md +5 -7
  2. app.py +8 -6
  3. lib/inference.py +18 -1
  4. lib/loader.py +2 -2
README.md CHANGED
@@ -49,14 +49,13 @@ preload_from_hub:
49
 
50
  Gradio app for Stable Diffusion 1.5 including:
51
  * txt2img and img2img pipelines
52
- * curated models and TI embeddings
53
- * multiple samplers with Karras schedule
54
- * Compel prompting
55
  * 100+ styles from sdxl_prompt_styler
56
- * FreeU and Clip Skip for quality
57
- * DeepCache and ToMe for speed
 
58
  * Real-ESRGAN upscaling
59
- * optional TAESD
60
 
61
  ## Usage
62
 
@@ -84,5 +83,4 @@ python cli.py 'an astronaut riding a horse on mars'
84
 
85
  ## TODO
86
 
87
- - [ ] Metadata embed and display
88
  - [ ] IP-Adapter and T2I-Adapter
 
49
 
50
  Gradio app for Stable Diffusion 1.5 including:
51
  * txt2img and img2img pipelines
52
+ * Curated models and TI embeddings
 
 
53
  * 100+ styles from sdxl_prompt_styler
54
+ * Compel prompt weighting
55
+ * Multiple samplers with Karras scheduling
56
+ * DeepCache, ToMe, FreeU, and Clip Skip available
57
  * Real-ESRGAN upscaling
58
+ * Optional tiny autoencoder
59
 
60
  ## Usage
61
 
 
83
 
84
  ## TODO
85
 
 
86
  - [ ] IP-Adapter and T2I-Adapter
app.py CHANGED
@@ -48,11 +48,10 @@ def random_fn():
48
  def gallery_fn(images, image):
49
  if image is not None:
50
  return gr.Dropdown(
51
- choices=[("🔒", -1)],
52
  interactive=False,
53
- value=-1,
54
  )
55
-
56
  return gr.Dropdown(
57
  choices=[("None", -1)]
58
  + [(str(i + 1), i) for i, _ in enumerate(images if images is not None else [])],
@@ -67,9 +66,12 @@ def image_prompt_fn(images):
67
 
68
  # can't use image input in JS
69
  def image_select_fn(images, image, i):
70
- if image is not None and i == -1:
71
- return gr.Image(value=image)
72
- return gr.Image(value=images[i][0]) if i > -1 else None
 
 
 
73
 
74
 
75
  def generate_fn(*args):
 
48
  def gallery_fn(images, image):
49
  if image is not None:
50
  return gr.Dropdown(
51
+ choices=[("🔒", -2)],
52
  interactive=False,
53
+ value=-2,
54
  )
 
55
  return gr.Dropdown(
56
  choices=[("None", -1)]
57
  + [(str(i + 1), i) for i, _ in enumerate(images if images is not None else [])],
 
66
 
67
  # can't use image input in JS
68
  def image_select_fn(images, image, i):
69
+ # -2 is the lock icon, -1 is None
70
+ if i == -2:
71
+ return gr.Image(image)
72
+ if i == -1:
73
+ return gr.Image(None)
74
+ return gr.Image(images[i][0]) if i > -1 else None
75
 
76
 
77
  def generate_fn(*args):
lib/inference.py CHANGED
@@ -7,12 +7,14 @@ from datetime import datetime
7
  from itertools import product
8
  from typing import Callable
9
 
 
10
  import spaces
11
  import tomesd
12
  import torch
13
  from compel import Compel, DiffusersTextualInversionManager, ReturnedEmbeddingsType
14
  from compel.prompt_parser import PromptParser
15
  from huggingface_hub.utils import HFValidationError, RepositoryNotFoundError
 
16
 
17
  from .loader import Loader
18
 
@@ -66,6 +68,21 @@ def apply_style(prompt, style_id, negative=False):
66
  return prompt
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  @spaces.GPU(duration=40)
70
  def generate(
71
  positive_prompt,
@@ -196,8 +213,8 @@ def generate(
196
  }
197
 
198
  if KIND == "img2img":
199
- kwargs["image"] = image_prompt
200
  kwargs["strength"] = denoising_strength
 
201
 
202
  with token_merging(pipe, tome_ratio=tome_ratio):
203
  try:
 
7
  from itertools import product
8
  from typing import Callable
9
 
10
+ import numpy as np
11
  import spaces
12
  import tomesd
13
  import torch
14
  from compel import Compel, DiffusersTextualInversionManager, ReturnedEmbeddingsType
15
  from compel.prompt_parser import PromptParser
16
  from huggingface_hub.utils import HFValidationError, RepositoryNotFoundError
17
+ from PIL import Image
18
 
19
  from .loader import Loader
20
 
 
68
  return prompt
69
 
70
 
71
+ def prepare_image(input, size=(512, 512)):
72
+ image = None
73
+ if isinstance(input, Image.Image):
74
+ image = input
75
+ if isinstance(input, np.ndarray):
76
+ image = Image.fromarray(input)
77
+ if isinstance(input, str):
78
+ if os.path.isfile(input):
79
+ image = Image.open(input)
80
+ if image is not None:
81
+ return image.convert("RGB").resize(size, Image.Resampling.LANCZOS)
82
+ else:
83
+ raise ValueError("Invalid image prompt")
84
+
85
+
86
  @spaces.GPU(duration=40)
87
  def generate(
88
  positive_prompt,
 
213
  }
214
 
215
  if KIND == "img2img":
 
216
  kwargs["strength"] = denoising_strength
217
+ kwargs["image"] = prepare_image(image_prompt, (width, height))
218
 
219
  with token_merging(pipe, tome_ratio=tome_ratio):
220
  try:
lib/loader.py CHANGED
@@ -72,7 +72,7 @@ class Loader:
72
  print("Switching to Tiny VAE...")
73
  self.pipe.vae = AutoencoderTiny.from_pretrained(
74
  pretrained_model_name_or_path="madebyollin/taesd",
75
- ).to(self.pipe.device)
76
  return
77
 
78
  if is_tiny and not taesd:
@@ -81,7 +81,7 @@ class Loader:
81
  pretrained_model_name_or_path=model_name,
82
  subfolder="vae",
83
  variant=variant,
84
- ).to(self.pipe.device)
85
  self.pipe.vae = torch.compile(
86
  mode="reduce-overhead",
87
  fullgraph=True,
 
72
  print("Switching to Tiny VAE...")
73
  self.pipe.vae = AutoencoderTiny.from_pretrained(
74
  pretrained_model_name_or_path="madebyollin/taesd",
75
+ ).to(self.pipe.device, self.pipe.dtype)
76
  return
77
 
78
  if is_tiny and not taesd:
 
81
  pretrained_model_name_or_path=model_name,
82
  subfolder="vae",
83
  variant=variant,
84
+ ).to(self.pipe.device, self.pipe.dtype)
85
  self.pipe.vae = torch.compile(
86
  mode="reduce-overhead",
87
  fullgraph=True,