fantos commited on
Commit
8d27edd
·
verified ·
1 Parent(s): 540094e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -5
app.py CHANGED
@@ -17,7 +17,6 @@ from diffusers import EulerDiscreteScheduler
17
  from PIL import Image, ImageDraw, ImageFont
18
  import os
19
 
20
-
21
  device = "cuda"
22
  ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
23
  ckpt_dir_canny = snapshot_download(repo_id="Kwai-Kolors/Kolors-ControlNet-Canny")
@@ -49,9 +48,27 @@ def translate_korean_to_english(text):
49
  return translated
50
  return text
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  @spaces.GPU
53
  def process_canny_condition(image, canny_threods=[100,200]):
54
- np_image = image.copy()
55
  np_image = cv2.Canny(np_image, canny_threods[0], canny_threods[1])
56
  np_image = np_image[:, :, None]
57
  np_image = np.concatenate([np_image, np_image, np_image], axis=2)
@@ -61,6 +78,13 @@ def process_canny_condition(image, canny_threods=[100,200]):
61
  MAX_SEED = np.iinfo(np.int32).max
62
  MAX_IMAGE_SIZE = 1024
63
 
 
 
 
 
 
 
 
64
  def text_to_image(text, size=72, position="middle-center"):
65
  width, height = 1024, 576
66
  image = Image.new("RGB", (width, height), "white")
@@ -112,7 +136,6 @@ def text_to_image(text, size=72, position="middle-center"):
112
 
113
  return image
114
 
115
-
116
  @spaces.GPU
117
  def infer_canny(prompt, text_for_image,
118
  negative_prompt = "nsfw, facial shadows, low resolution, jpeg artifacts, blurry, bad quality, dark face, neon lights",
@@ -136,7 +159,7 @@ def infer_canny(prompt, text_for_image,
136
  init_image = resize_image(init_image, MAX_IMAGE_SIZE)
137
 
138
  pipe = pipe_canny.to("cuda")
139
- condi_img = process_canny_condition(np.array(init_image))
140
  image = pipe(
141
  prompt=prompt,
142
  image=init_image,
@@ -239,4 +262,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as Kolors:
239
  outputs = [result, seed_used]
240
  )
241
 
242
- Kolors.queue().launch(debug=True)
 
17
  from PIL import Image, ImageDraw, ImageFont
18
  import os
19
 
 
20
  device = "cuda"
21
  ckpt_dir = snapshot_download(repo_id="Kwai-Kolors/Kolors")
22
  ckpt_dir_canny = snapshot_download(repo_id="Kwai-Kolors/Kolors-ControlNet-Canny")
 
48
  return translated
49
  return text
50
 
51
+ def HWC3(x):
52
+ assert x.dtype == np.uint8
53
+ if x.ndim == 2:
54
+ x = x[:, :, None]
55
+ assert x.ndim == 3
56
+ H, W, C = x.shape
57
+ assert C == 1 or C == 3 or C == 4
58
+ if C == 3:
59
+ return x
60
+ if C == 1:
61
+ return np.concatenate([x, x, x], axis=2)
62
+ if C == 4:
63
+ color = x[:, :, 0:3].astype(np.float32)
64
+ alpha = x[:, :, 3:4].astype(np.float32) / 255.0
65
+ y = color * alpha + 255.0 * (1.0 - alpha)
66
+ y = y.clip(0, 255).astype(np.uint8)
67
+ return y
68
+
69
  @spaces.GPU
70
  def process_canny_condition(image, canny_threods=[100,200]):
71
+ np_image = np.array(image)
72
  np_image = cv2.Canny(np_image, canny_threods[0], canny_threods[1])
73
  np_image = np_image[:, :, None]
74
  np_image = np.concatenate([np_image, np_image, np_image], axis=2)
 
78
  MAX_SEED = np.iinfo(np.int32).max
79
  MAX_IMAGE_SIZE = 1024
80
 
81
+ def resize_image(image, resolution):
82
+ w, h = image.size
83
+ ratio = resolution / max(w, h)
84
+ new_w = int(w * ratio)
85
+ new_h = int(h * ratio)
86
+ return image.resize((new_w, new_h), Image.LANCZOS)
87
+
88
  def text_to_image(text, size=72, position="middle-center"):
89
  width, height = 1024, 576
90
  image = Image.new("RGB", (width, height), "white")
 
136
 
137
  return image
138
 
 
139
  @spaces.GPU
140
  def infer_canny(prompt, text_for_image,
141
  negative_prompt = "nsfw, facial shadows, low resolution, jpeg artifacts, blurry, bad quality, dark face, neon lights",
 
159
  init_image = resize_image(init_image, MAX_IMAGE_SIZE)
160
 
161
  pipe = pipe_canny.to("cuda")
162
+ condi_img = process_canny_condition(init_image)
163
  image = pipe(
164
  prompt=prompt,
165
  image=init_image,
 
262
  outputs = [result, seed_used]
263
  )
264
 
265
+ Kolors.queue().launch(debug=True, share=True)