Deadmon commited on
Commit
763126f
·
verified ·
1 Parent(s): 23cd94b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import numpy as np
2
  from PIL import Image
3
  from einops import rearrange
@@ -6,7 +9,6 @@ import spaces
6
  from huggingface_hub import login
7
  from gradio_imageslider import ImageSlider # Import ImageSlider
8
 
9
-
10
  from image_datasets.canny_dataset import canny_processor, c_crop
11
  from src.flux.sampling import denoise_controlnet, get_noise, get_schedule, prepare, unpack
12
  from src.flux.util import load_ae, load_clip, load_t5, load_flow_model, load_controlnet, load_safetensors
@@ -46,12 +48,24 @@ def preprocess_image(image, target_width, target_height, crop=True):
46
  original_width, original_height = image.size
47
 
48
  # Resize to match the target size without stretching
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def preprocess_canny_image(image, target_width, target_height, crop=True):
51
  image = preprocess_image(image, target_width, target_height, crop=crop)
52
  image = canny_processor(image)
53
-
54
-
55
  return image
56
 
57
  @spaces.GPU(duration=120)
@@ -106,10 +120,12 @@ interface = gr.Interface(
106
  gr.Slider(minimum=128, maximum=2048, step=128, value=1024, label="Height"),
107
  gr.Number(value=42, label="Seed"),
108
  gr.Checkbox(label="Random Seed")
109
-
110
  ],
111
  outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
112
  title="FLUX.1 Controlnet Canny",
 
 
113
 
114
  if __name__ == "__main__":
115
- interface.launch()
 
 
1
+ import os
2
+ import torch
3
+ import gradio as gr
4
  import numpy as np
5
  from PIL import Image
6
  from einops import rearrange
 
9
  from huggingface_hub import login
10
  from gradio_imageslider import ImageSlider # Import ImageSlider
11
 
 
12
  from image_datasets.canny_dataset import canny_processor, c_crop
13
  from src.flux.sampling import denoise_controlnet, get_noise, get_schedule, prepare, unpack
14
  from src.flux.util import load_ae, load_clip, load_t5, load_flow_model, load_controlnet, load_safetensors
 
48
  original_width, original_height = image.size
49
 
50
  # Resize to match the target size without stretching
51
+ scale = max(target_width / original_width, target_height / original_height)
52
+ resized_width = int(scale * original_width)
53
+ resized_height = int(scale * original_height)
54
+
55
+ image = image.resize((resized_width, resized_height), Image.LANCZOS)
56
+
57
+ # Center crop to match the target dimensions
58
+ left = (resized_width - target_width) // 2
59
+ top = (resized_height - target_height) // 2
60
+ image = image.crop((left, top, left + target_width, top + target_height))
61
+ else:
62
+ image = image.resize((target_width, target_height), Image.LANCZOS)
63
+
64
+ return image
65
 
66
  def preprocess_canny_image(image, target_width, target_height, crop=True):
67
  image = preprocess_image(image, target_width, target_height, crop=crop)
68
  image = canny_processor(image)
 
 
69
  return image
70
 
71
  @spaces.GPU(duration=120)
 
120
  gr.Slider(minimum=128, maximum=2048, step=128, value=1024, label="Height"),
121
  gr.Number(value=42, label="Seed"),
122
  gr.Checkbox(label="Random Seed")
 
123
  ],
124
  outputs=ImageSlider(label="Before / After"), # Use ImageSlider as the output
125
  title="FLUX.1 Controlnet Canny",
126
+ description="Generate images using ControlNet and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]"
127
+ )
128
 
129
  if __name__ == "__main__":
130
+ interface.launch()
131
+