t-montes commited on
Commit
dc423bd
·
verified ·
1 Parent(s): 0c8bd52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -38
app.py CHANGED
@@ -3,11 +3,12 @@ import random
3
  import warnings
4
  import os
5
  import gradio as gr
6
- import spaces
7
  import numpy as np
 
8
  import torch
9
  from diffusers import FluxControlNetModel
10
  from diffusers.pipelines import FluxControlNetPipeline
 
11
  from PIL import Image
12
  from huggingface_hub import snapshot_download
13
 
@@ -18,7 +19,6 @@ css = """
18
  }
19
  """
20
 
21
- # Check for GPU availability
22
  if torch.cuda.is_available():
23
  power_device = "GPU"
24
  device = "cuda"
@@ -26,11 +26,11 @@ else:
26
  power_device = "CPU"
27
  device = "cpu"
28
 
29
- # Load HuggingFace model
30
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
 
31
  model_path = snapshot_download(
32
- repo_id="black-forest-labs/FLUX.1-dev",
33
- repo_type="model",
34
  ignore_patterns=["*.md", "*..gitattributes"],
35
  local_dir="FLUX.1-dev",
36
  token=huggingface_token,
@@ -48,7 +48,7 @@ pipe.to(device)
48
  MAX_SEED = 1000000
49
  MAX_PIXEL_BUDGET = 1024 * 1024
50
 
51
- def process_input(input_image, upscale_factor):
52
  w, h = input_image.size
53
  w_original, h_original = w, h
54
  aspect_ratio = w / h
@@ -58,6 +58,9 @@ def process_input(input_image, upscale_factor):
58
  warnings.warn(
59
  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."
60
  )
 
 
 
61
  input_image = input_image.resize(
62
  (
63
  int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
@@ -66,32 +69,35 @@ def process_input(input_image, upscale_factor):
66
  )
67
  was_resized = True
68
 
69
- # Resize to multiple of 8
70
  w, h = input_image.size
71
  w = w - w % 8
72
  h = h - h % 8
 
73
  return input_image.resize((w, h)), w_original, h_original, was_resized
74
 
75
  @spaces.GPU
76
  def infer(
77
- seed, randomize_seed, input_image_path, num_inference_steps, upscale_factor, controlnet_conditioning_scale
 
 
 
 
 
 
78
  ):
79
- # Load image
80
- input_image = Image.open(input_image_path)
81
-
82
- # Handle random seed if specified
83
  if randomize_seed:
84
  seed = random.randint(0, MAX_SEED)
85
-
86
  true_input_image = input_image
87
- input_image, w_original, h_original, was_resized = process_input(input_image, upscale_factor)
88
-
89
- # Rescale with upscale factor
 
90
  w, h = input_image.size
91
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
 
92
  generator = torch.Generator().manual_seed(seed)
93
 
94
- # Upscale
95
  image = pipe(
96
  prompt="",
97
  control_image=control_image,
@@ -103,37 +109,103 @@ def infer(
103
  generator=generator,
104
  ).images[0]
105
 
106
- # Resize output if initially resized
107
  if was_resized:
108
- image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
 
 
 
 
109
  image.save("output.jpg")
110
-
111
- return true_input_image, image, seed
112
 
113
- # Gradio setup without ImageSlider
 
114
  with gr.Blocks(css=css) as demo:
115
  gr.Markdown(
116
  f"""
117
  # ⚡ Flux.1-dev Upscaler ControlNet ⚡
118
- This is an interactive demo of [Flux.1-dev Upscaler ControlNet](https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler).
 
 
 
119
  """
120
  )
121
 
122
- run_button = gr.Button(value="Run")
123
- input_im = gr.Image(label="Input Image", type="filepath")
124
- num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=8, maximum=50, step=1, value=28)
125
- upscale_factor = gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, value=4)
126
- controlnet_conditioning_scale = gr.Slider(label="Controlnet Conditioning Scale", minimum=0.1, maximum=1.5, step=0.1, value=0.6)
127
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
128
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
129
-
130
- input_image_display = gr.Image(label="Input Image Display")
131
- output_image_display = gr.Image(label="Upscaled Image Display")
132
-
133
- run_button.click(
134
- infer,
135
- inputs=[seed, randomize_seed, input_im, num_inference_steps, upscale_factor, controlnet_conditioning_scale],
136
- outputs=[input_image_display, output_image_display, gr.Textbox(label="Used Seed")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  )
138
 
139
  demo.queue().launch(share=False, show_api=True, show_error=True)
 
3
  import warnings
4
  import os
5
  import gradio as gr
 
6
  import numpy as np
7
+ import spaces
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
  from huggingface_hub import snapshot_download
14
 
 
19
  }
20
  """
21
 
 
22
  if torch.cuda.is_available():
23
  power_device = "GPU"
24
  device = "cuda"
 
26
  power_device = "CPU"
27
  device = "cpu"
28
 
 
29
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
30
+
31
  model_path = snapshot_download(
32
+ repo_id="black-forest-labs/FLUX.1-dev",
33
+ repo_type="model",
34
  ignore_patterns=["*.md", "*..gitattributes"],
35
  local_dir="FLUX.1-dev",
36
  token=huggingface_token,
 
48
  MAX_SEED = 1000000
49
  MAX_PIXEL_BUDGET = 1024 * 1024
50
 
51
+ def process_input(input_image, upscale_factor, **kwargs):
52
  w, h = input_image.size
53
  w_original, h_original = w, h
54
  aspect_ratio = w / h
 
58
  warnings.warn(
59
  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."
60
  )
61
+ gr.Info(
62
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing input to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels budget."
63
+ )
64
  input_image = input_image.resize(
65
  (
66
  int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
 
69
  )
70
  was_resized = True
71
 
 
72
  w, h = input_image.size
73
  w = w - w % 8
74
  h = h - h % 8
75
+
76
  return input_image.resize((w, h)), w_original, h_original, was_resized
77
 
78
  @spaces.GPU
79
  def infer(
80
+ seed,
81
+ randomize_seed,
82
+ input_image,
83
+ num_inference_steps,
84
+ upscale_factor,
85
+ controlnet_conditioning_scale,
86
+ progress=gr.Progress(track_tqdm=True),
87
  ):
 
 
 
 
88
  if randomize_seed:
89
  seed = random.randint(0, MAX_SEED)
 
90
  true_input_image = input_image
91
+ input_image, w_original, h_original, was_resized = process_input(
92
+ input_image, upscale_factor
93
+ )
94
+
95
  w, h = input_image.size
96
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
97
+
98
  generator = torch.Generator().manual_seed(seed)
99
 
100
+ gr.Info("Upscaling image...")
101
  image = pipe(
102
  prompt="",
103
  control_image=control_image,
 
109
  generator=generator,
110
  ).images[0]
111
 
 
112
  if was_resized:
113
+ gr.Info(
114
+ f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
115
+ )
116
+
117
+ image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
118
  image.save("output.jpg")
 
 
119
 
120
+ return [true_input_image, image, seed]
121
+
122
  with gr.Blocks(css=css) as demo:
123
  gr.Markdown(
124
  f"""
125
  # ⚡ Flux.1-dev Upscaler ControlNet ⚡
126
+ This is an interactive demo of [Flux.1-dev Upscaler ControlNet](https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler) taking as input a low resolution image to generate a high resolution image.
127
+ Currently running on {power_device}.
128
+
129
+ *Note*: Even though the model can handle higher resolution images, due to GPU memory constraints, this demo was limited to a generated output not exceeding a pixel budget of 1024x1024. If the requested size exceeds that limit, the input will be first resized keeping the aspect ratio such that the output of the controlNet model does not exceed the allocated pixel budget. The output is then resized to the targeted shape using a simple resizing. This may explain some artifacts for high resolution input. To adress this, run the demo locally or consider implementing a tiling strategy. Happy upscaling! ���
130
  """
131
  )
132
 
133
+ with gr.Row():
134
+ run_button = gr.Button(value="Run")
135
+
136
+ with gr.Row():
137
+ with gr.Column(scale=4):
138
+ input_im = gr.Image(label="Input Image", type="pil")
139
+ with gr.Column(scale=1):
140
+ num_inference_steps = gr.Slider(
141
+ label="Number of Inference Steps",
142
+ minimum=8,
143
+ maximum=50,
144
+ step=1,
145
+ value=28,
146
+ )
147
+ upscale_factor = gr.Slider(
148
+ label="Upscale Factor",
149
+ minimum=1,
150
+ maximum=4,
151
+ step=1,
152
+ value=4,
153
+ )
154
+ controlnet_conditioning_scale = gr.Slider(
155
+ label="Controlnet Conditioning Scale",
156
+ minimum=0.1,
157
+ maximum=1.5,
158
+ step=0.1,
159
+ value=0.6,
160
+ )
161
+ seed = gr.Slider(
162
+ label="Seed",
163
+ minimum=0,
164
+ maximum=MAX_SEED,
165
+ step=1,
166
+ value=42,
167
+ )
168
+
169
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
170
+
171
+ with gr.Row():
172
+ result = ImageSlider(label="Input / Output", type="pil", interactive=True)
173
+
174
+ examples = gr.Examples(
175
+ examples=[
176
+ [42, False, "examples/image_2.jpg", 28, 4, 0.6],
177
+ [42, False, "examples/image_4.jpg", 28, 4, 0.6],
178
+ ],
179
+ inputs=[
180
+ seed,
181
+ randomize_seed,
182
+ input_im,
183
+ num_inference_steps,
184
+ upscale_factor,
185
+ controlnet_conditioning_scale,
186
+ ],
187
+ fn=infer,
188
+ outputs=result,
189
+ cache_examples="lazy",
190
+ )
191
+
192
+ gr.Markdown("**Disclaimer:**")
193
+ gr.Markdown(
194
+ "This demo is only for research purpose. Jasper cannot be held responsible for the generation of NSFW (Not Safe For Work) content through the use of this demo. Users are solely responsible for any content they create, and it is their obligation to ensure that it adheres to appropriate and ethical standards. Jasper provides the tools, but the responsibility for their use lies with the individual user."
195
+ )
196
+ gr.on(
197
+ [run_button.click],
198
+ fn=infer,
199
+ inputs=[
200
+ seed,
201
+ randomize_seed,
202
+ input_im,
203
+ num_inference_steps,
204
+ upscale_factor,
205
+ controlnet_conditioning_scale,
206
+ ],
207
+ outputs=result,
208
+ show_api=True,
209
  )
210
 
211
  demo.queue().launch(share=False, show_api=True, show_error=True)