clementchadebec commited on
Commit
bc47113
1 Parent(s): 3ded1a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -8,7 +8,6 @@ import spaces
8
  import torch
9
  from diffusers import FluxControlNetModel
10
  from diffusers.pipelines import FluxControlNetPipeline
11
- from diffusers.utils import load_image
12
  from gradio_imageslider import ImageSlider
13
  from PIL import Image
14
 
@@ -46,12 +45,12 @@ def process_input(input_image, upscale_factor, **kwargs):
46
 
47
  if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
48
  warnings.warn(
49
- f"Input image is too large ({w}x{h}). Resizing to {MAX_PIXEL_BUDGET} pixels."
50
  )
51
  input_image = input_image.resize(
52
  (
53
- int(aspect_ratio * MAX_PIXEL_BUDGET // upscale_factor),
54
- int(MAX_PIXEL_BUDGET // aspect_ratio // upscale_factor),
55
  )
56
  )
57
 
@@ -63,23 +62,21 @@ def process_input(input_image, upscale_factor, **kwargs):
63
  return input_image.resize((w, h)), w_original, h_original
64
 
65
 
66
- # @spaces.GPU
67
  def infer(
68
  seed,
69
  randomize_seed,
70
  input_image,
71
  num_inference_steps,
72
  upscale_factor,
 
73
  progress=gr.Progress(track_tqdm=True),
74
  ):
75
- print(input_image)
76
  if randomize_seed:
77
  seed = random.randint(0, MAX_SEED)
78
-
79
  input_image, w_original, h_original = process_input(input_image, upscale_factor)
80
 
81
- print(input_image.size, w_original, h_original)
82
-
83
  # rescale with upscale factor
84
  w, h = input_image.size
85
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
@@ -89,7 +86,7 @@ def infer(
89
  image = pipe(
90
  prompt="",
91
  control_image=control_image,
92
- controlnet_conditioning_scale=0.6,
93
  num_inference_steps=num_inference_steps,
94
  guidance_scale=3.5,
95
  height=control_image.size[1],
@@ -101,7 +98,7 @@ def infer(
101
  image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
102
  image.save("output.jpg")
103
  # convert to numpy
104
- return [input_image, image]
105
 
106
 
107
  with gr.Blocks(css=css) as demo:
@@ -135,6 +132,13 @@ with gr.Blocks(css=css) as demo:
135
  step=1,
136
  value=4,
137
  )
 
 
 
 
 
 
 
138
  seed = gr.Slider(
139
  label="Seed",
140
  minimum=0,
@@ -166,10 +170,17 @@ with gr.Blocks(css=css) as demo:
166
  gr.on(
167
  [run_button.click],
168
  fn=infer,
169
- inputs=[seed, randomize_seed, input_im, num_inference_steps, upscale_factor],
 
 
 
 
 
 
 
170
  outputs=result,
171
  show_api=False,
172
  # show_progress="minimal",
173
  )
174
 
175
- demo.queue().launch()
 
8
  import torch
9
  from diffusers import FluxControlNetModel
10
  from diffusers.pipelines import FluxControlNetPipeline
 
11
  from gradio_imageslider import ImageSlider
12
  from PIL import Image
13
 
 
45
 
46
  if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
47
  warnings.warn(
48
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels."
49
  )
50
  input_image = input_image.resize(
51
  (
52
+ int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
53
+ int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
54
  )
55
  )
56
 
 
62
  return input_image.resize((w, h)), w_original, h_original
63
 
64
 
65
+ @spaces.GPU
66
  def infer(
67
  seed,
68
  randomize_seed,
69
  input_image,
70
  num_inference_steps,
71
  upscale_factor,
72
+ controlnet_conditioning_scale,
73
  progress=gr.Progress(track_tqdm=True),
74
  ):
 
75
  if randomize_seed:
76
  seed = random.randint(0, MAX_SEED)
77
+ true_input_image = input_image
78
  input_image, w_original, h_original = process_input(input_image, upscale_factor)
79
 
 
 
80
  # rescale with upscale factor
81
  w, h = input_image.size
82
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
 
86
  image = pipe(
87
  prompt="",
88
  control_image=control_image,
89
+ controlnet_conditioning_scale=controlnet_conditioning_scale,
90
  num_inference_steps=num_inference_steps,
91
  guidance_scale=3.5,
92
  height=control_image.size[1],
 
98
  image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
99
  image.save("output.jpg")
100
  # convert to numpy
101
+ return [true_input_image, image, seed]
102
 
103
 
104
  with gr.Blocks(css=css) as demo:
 
132
  step=1,
133
  value=4,
134
  )
135
+ controlnet_conditioning_scale = gr.Slider(
136
+ label="Controlnet Conditioning Scale",
137
+ minimum=0.1,
138
+ maximum=1.5,
139
+ step=0.1,
140
+ value=0.6,
141
+ )
142
  seed = gr.Slider(
143
  label="Seed",
144
  minimum=0,
 
170
  gr.on(
171
  [run_button.click],
172
  fn=infer,
173
+ inputs=[
174
+ seed,
175
+ randomize_seed,
176
+ input_im,
177
+ num_inference_steps,
178
+ upscale_factor,
179
+ controlnet_conditioning_scale,
180
+ ],
181
  outputs=result,
182
  show_api=False,
183
  # show_progress="minimal",
184
  )
185
 
186
+ demo.queue().launch(share=True)