KingNish commited on
Commit
2d30d63
1 Parent(s): 1fd4564

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -17
app.py CHANGED
@@ -1,15 +1,17 @@
1
  from __future__ import annotations
2
- import uuid
3
- import os
4
  import math
5
  import random
6
  import spaces
7
  import gradio as gr
 
8
  import torch
9
  from PIL import Image
10
- from diffusers import StableDiffusionInstructPix2PixPipeline
 
 
11
  from huggingface_hub import InferenceClient
12
 
 
13
  help_text = """
14
  To optimize image editing results:
15
  - Adjust the **Image CFG weight** if the image isn't changing enough or is changing too much. Lower it to allow bigger changes, or raise it to preserve original details.
@@ -20,14 +22,38 @@ To optimize image editing results:
20
  - For better facial details, especially if they're small, **crop the image** to enlarge the face's presence.
21
  """
22
 
23
- model_id = "timbrooks/instruct-pix2pix"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
26
 
27
  if not torch.cuda.is_available():
28
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
29
-
30
- MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
31
 
32
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
33
 
@@ -45,8 +71,6 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
45
  seed = random.randint(0, 999999)
46
  return seed
47
 
48
- pipe2 = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda")
49
-
50
  @spaces.GPU(duration=30, queue=False)
51
  def king(type = "Image Editing",
52
  input_image = None,
@@ -86,14 +110,14 @@ def king(type = "Image Editing",
86
  image_cfg_scale = image_cfg_scale
87
  input_image = input_image
88
 
89
- steps=steps*6
90
  generator = torch.manual_seed(seed)
91
- output_image = pipe2(
92
  instruction, image=input_image,
93
  guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
94
  num_inference_steps=steps, generator=generator).images[0]
95
  return seed, output_image
96
-
97
  def response(instruction, input_image=None):
98
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
99
 
@@ -131,28 +155,28 @@ def get_example():
131
  case = [
132
  [
133
  "Image Generation",
134
- None,
135
  "A Super Car",
 
136
  ],
137
  [
138
  "Image Editing",
139
- "./supercar.png",
140
  "make it red",
 
141
  ],
142
  [
143
  "Image Editing",
144
- "./red_car.png",
145
  "add some snow",
 
146
  ],
147
  [
148
  "Image Generation",
149
- None,
150
  "Ironman flying in front of Ststue of liberty",
 
151
  ],
152
  [
153
  "Image Generation",
154
- None,
155
  "Beautiful Eiffel Tower at Night",
 
156
  ],
157
  ]
158
  return case
@@ -195,7 +219,10 @@ with gr.Blocks(css=css) as demo:
195
 
196
  instruction.change(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
197
 
198
- generate_button.click(
 
 
 
199
  fn=king,
200
  inputs=[type,
201
  input_image,
 
1
  from __future__ import annotations
 
 
2
  import math
3
  import random
4
  import spaces
5
  import gradio as gr
6
+ import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from diffusers import StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL
10
+ from custom_pipeline import CosStableDiffusionXLInstructPix2PixPipeline
11
+ from huggingface_hub import hf_hub_download
12
  from huggingface_hub import InferenceClient
13
 
14
+
15
  help_text = """
16
  To optimize image editing results:
17
  - Adjust the **Image CFG weight** if the image isn't changing enough or is changing too much. Lower it to allow bigger changes, or raise it to preserve original details.
 
22
  - For better facial details, especially if they're small, **crop the image** to enlarge the face's presence.
23
  """
24
 
25
+ def set_timesteps_patched(self, num_inference_steps: int, device = None):
26
+ self.num_inference_steps = num_inference_steps
27
+
28
+ ramp = np.linspace(0, 1, self.num_inference_steps)
29
+ sigmas = torch.linspace(math.log(self.config.sigma_min), math.log(self.config.sigma_max), len(ramp)).exp().flip(0)
30
+
31
+ sigmas = (sigmas).to(dtype=torch.float32, device=device)
32
+ self.timesteps = self.precondition_noise(sigmas)
33
+
34
+ self.sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
35
+ self._step_index = None
36
+ self._begin_index = None
37
+ self.sigmas = self.sigmas.to("cpu")
38
+
39
+
40
+ edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
41
+ normal_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl.safetensors")
42
+
43
+ EDMEulerScheduler.set_timesteps = set_timesteps_patched
44
+
45
+ vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
46
+
47
+ pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
48
+ edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16,
49
+ )
50
+ pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
51
+ pipe_edit.to("cuda")
52
 
53
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
54
 
55
  if not torch.cuda.is_available():
56
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
 
 
57
 
58
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
59
 
 
71
  seed = random.randint(0, 999999)
72
  return seed
73
 
 
 
74
  @spaces.GPU(duration=30, queue=False)
75
  def king(type = "Image Editing",
76
  input_image = None,
 
110
  image_cfg_scale = image_cfg_scale
111
  input_image = input_image
112
 
113
+ steps=steps*3
114
  generator = torch.manual_seed(seed)
115
+ output_image = pipe_edit(
116
  instruction, image=input_image,
117
  guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
118
  num_inference_steps=steps, generator=generator).images[0]
119
  return seed, output_image
120
+
121
  def response(instruction, input_image=None):
122
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
123
 
 
155
  case = [
156
  [
157
  "Image Generation",
 
158
  "A Super Car",
159
+ None,
160
  ],
161
  [
162
  "Image Editing",
 
163
  "make it red",
164
+ "./supercar.png",
165
  ],
166
  [
167
  "Image Editing",
 
168
  "add some snow",
169
+ "./red_car.png",
170
  ],
171
  [
172
  "Image Generation",
 
173
  "Ironman flying in front of Ststue of liberty",
174
+ None,
175
  ],
176
  [
177
  "Image Generation",
 
178
  "Beautiful Eiffel Tower at Night",
179
+ None,
180
  ],
181
  ]
182
  return case
 
219
 
220
  instruction.change(fn=response, inputs=[instruction,input_image], outputs=type, queue=False)
221
 
222
+ gr.on(triggers=[
223
+ generate_button.click,
224
+ instruction.submit
225
+ ],
226
  fn=king,
227
  inputs=[type,
228
  input_image,