t-montes commited on
Commit
1d511db
·
verified ·
1 Parent(s): 6f65672

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -111
app.py CHANGED
@@ -7,7 +7,6 @@ import numpy as np
7
  import torch
8
  from diffusers import FluxControlNetModel
9
  from diffusers.pipelines import FluxControlNetPipeline
10
- from gradio_imageslider import ImageSlider
11
  from PIL import Image
12
  from huggingface_hub import snapshot_download
13
 
@@ -18,6 +17,7 @@ css = """
18
  }
19
  """
20
 
 
21
  if torch.cuda.is_available():
22
  power_device = "GPU"
23
  device = "cuda"
@@ -25,11 +25,11 @@ else:
25
  power_device = "CPU"
26
  device = "cpu"
27
 
 
28
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
29
-
30
  model_path = snapshot_download(
31
- repo_id="black-forest-labs/FLUX.1-dev",
32
- repo_type="model",
33
  ignore_patterns=["*.md", "*..gitattributes"],
34
  local_dir="FLUX.1-dev",
35
  token=huggingface_token,
@@ -47,7 +47,7 @@ pipe.to(device)
47
  MAX_SEED = 1000000
48
  MAX_PIXEL_BUDGET = 1024 * 1024
49
 
50
- def process_input(input_image, upscale_factor, **kwargs):
51
  w, h = input_image.size
52
  w_original, h_original = w, h
53
  aspect_ratio = w / h
@@ -57,9 +57,6 @@ def process_input(input_image, upscale_factor, **kwargs):
57
  warnings.warn(
58
  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."
59
  )
60
- gr.Info(
61
- 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."
62
- )
63
  input_image = input_image.resize(
64
  (
65
  int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
@@ -68,35 +65,31 @@ def process_input(input_image, upscale_factor, **kwargs):
68
  )
69
  was_resized = True
70
 
 
71
  w, h = input_image.size
72
  w = w - w % 8
73
  h = h - h % 8
74
-
75
  return input_image.resize((w, h)), w_original, h_original, was_resized
76
 
77
- @spaces.GPU
78
  def infer(
79
- seed,
80
- randomize_seed,
81
- input_image,
82
- num_inference_steps,
83
- upscale_factor,
84
- controlnet_conditioning_scale,
85
- progress=gr.Progress(track_tqdm=True),
86
  ):
 
 
 
 
87
  if randomize_seed:
88
  seed = random.randint(0, MAX_SEED)
 
89
  true_input_image = input_image
90
- input_image, w_original, h_original, was_resized = process_input(
91
- input_image, upscale_factor
92
- )
93
-
94
  w, h = input_image.size
95
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
96
-
97
  generator = torch.Generator().manual_seed(seed)
98
 
99
- gr.Info("Upscaling image...")
100
  image = pipe(
101
  prompt="",
102
  control_image=control_image,
@@ -108,103 +101,37 @@ def infer(
108
  generator=generator,
109
  ).images[0]
110
 
 
111
  if was_resized:
112
- gr.Info(
113
- f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
114
- )
115
-
116
- image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
117
  image.save("output.jpg")
 
 
118
 
119
- return [true_input_image, image, seed]
120
-
121
  with gr.Blocks(css=css) as demo:
122
  gr.Markdown(
123
  f"""
124
  # ⚡ Flux.1-dev Upscaler ControlNet ⚡
125
- 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.
126
- Currently running on {power_device}.
127
-
128
- *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! 🚀
129
  """
130
  )
131
 
132
- with gr.Row():
133
- run_button = gr.Button(value="Run")
134
-
135
- with gr.Row():
136
- with gr.Column(scale=4):
137
- input_im = gr.Image(label="Input Image", type="pil")
138
- with gr.Column(scale=1):
139
- num_inference_steps = gr.Slider(
140
- label="Number of Inference Steps",
141
- minimum=8,
142
- maximum=50,
143
- step=1,
144
- value=28,
145
- )
146
- upscale_factor = gr.Slider(
147
- label="Upscale Factor",
148
- minimum=1,
149
- maximum=4,
150
- step=1,
151
- value=4,
152
- )
153
- controlnet_conditioning_scale = gr.Slider(
154
- label="Controlnet Conditioning Scale",
155
- minimum=0.1,
156
- maximum=1.5,
157
- step=0.1,
158
- value=0.6,
159
- )
160
- seed = gr.Slider(
161
- label="Seed",
162
- minimum=0,
163
- maximum=MAX_SEED,
164
- step=1,
165
- value=42,
166
- )
167
-
168
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
169
-
170
- with gr.Row():
171
- result = ImageSlider(label="Input / Output", type="pil", interactive=True)
172
-
173
- examples = gr.Examples(
174
- examples=[
175
- [42, False, "examples/image_2.jpg", 28, 4, 0.6],
176
- [42, False, "examples/image_4.jpg", 28, 4, 0.6],
177
- ],
178
- inputs=[
179
- seed,
180
- randomize_seed,
181
- input_im,
182
- num_inference_steps,
183
- upscale_factor,
184
- controlnet_conditioning_scale,
185
- ],
186
- fn=infer,
187
- outputs=result,
188
- cache_examples="lazy",
189
- )
190
-
191
- gr.Markdown("**Disclaimer:**")
192
- gr.Markdown(
193
- "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."
194
- )
195
- gr.on(
196
- [run_button.click],
197
- fn=infer,
198
- inputs=[
199
- seed,
200
- randomize_seed,
201
- input_im,
202
- num_inference_steps,
203
- upscale_factor,
204
- controlnet_conditioning_scale,
205
- ],
206
- outputs=result,
207
- show_api=True,
208
  )
209
 
210
- demo.queue().launch(share=True, show_api=True, show_error=True)
 
7
  import torch
8
  from diffusers import FluxControlNetModel
9
  from diffusers.pipelines import FluxControlNetPipeline
 
10
  from PIL import Image
11
  from huggingface_hub import snapshot_download
12
 
 
17
  }
18
  """
19
 
20
+ # Check for GPU availability
21
  if torch.cuda.is_available():
22
  power_device = "GPU"
23
  device = "cuda"
 
25
  power_device = "CPU"
26
  device = "cpu"
27
 
28
+ # Load HuggingFace model
29
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
 
30
  model_path = snapshot_download(
31
+ repo_id="black-forest-labs/FLUX.1-dev",
32
+ repo_type="model",
33
  ignore_patterns=["*.md", "*..gitattributes"],
34
  local_dir="FLUX.1-dev",
35
  token=huggingface_token,
 
47
  MAX_SEED = 1000000
48
  MAX_PIXEL_BUDGET = 1024 * 1024
49
 
50
+ def process_input(input_image, upscale_factor):
51
  w, h = input_image.size
52
  w_original, h_original = w, h
53
  aspect_ratio = w / h
 
57
  warnings.warn(
58
  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."
59
  )
 
 
 
60
  input_image = input_image.resize(
61
  (
62
  int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
 
65
  )
66
  was_resized = True
67
 
68
+ # Resize to multiple of 8
69
  w, h = input_image.size
70
  w = w - w % 8
71
  h = h - h % 8
 
72
  return input_image.resize((w, h)), w_original, h_original, was_resized
73
 
 
74
  def infer(
75
+ seed, randomize_seed, input_image_path, num_inference_steps, upscale_factor, controlnet_conditioning_scale
 
 
 
 
 
 
76
  ):
77
+ # Load image
78
+ input_image = Image.open(input_image_path)
79
+
80
+ # Handle random seed if specified
81
  if randomize_seed:
82
  seed = random.randint(0, MAX_SEED)
83
+
84
  true_input_image = input_image
85
+ input_image, w_original, h_original, was_resized = process_input(input_image, upscale_factor)
86
+
87
+ # Rescale with upscale factor
 
88
  w, h = input_image.size
89
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
 
90
  generator = torch.Generator().manual_seed(seed)
91
 
92
+ # Upscale
93
  image = pipe(
94
  prompt="",
95
  control_image=control_image,
 
101
  generator=generator,
102
  ).images[0]
103
 
104
+ # Resize output if initially resized
105
  if was_resized:
106
+ image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
 
 
 
 
107
  image.save("output.jpg")
108
+
109
+ return true_input_image, image, seed
110
 
111
+ # Gradio setup without ImageSlider
 
112
  with gr.Blocks(css=css) as demo:
113
  gr.Markdown(
114
  f"""
115
  # ⚡ Flux.1-dev Upscaler ControlNet ⚡
116
+ This is an interactive demo of [Flux.1-dev Upscaler ControlNet](https://huggingface.co/jasperai/Flux.1-dev-Controlnet-Upscaler).
 
 
 
117
  """
118
  )
119
 
120
+ run_button = gr.Button(value="Run")
121
+ input_im = gr.Image(label="Input Image", type="filepath")
122
+ num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=8, maximum=50, step=1, value=28)
123
+ upscale_factor = gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, value=4)
124
+ controlnet_conditioning_scale = gr.Slider(label="Controlnet Conditioning Scale", minimum=0.1, maximum=1.5, step=0.1, value=0.6)
125
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
126
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
127
+
128
+ input_image_display = gr.Image(label="Input Image Display")
129
+ output_image_display = gr.Image(label="Upscaled Image Display")
130
+
131
+ run_button.click(
132
+ infer,
133
+ inputs=[seed, randomize_seed, input_im, num_inference_steps, upscale_factor, controlnet_conditioning_scale],
134
+ outputs=[input_image_display, output_image_display, gr.Textbox(label="Used Seed")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  )
136
 
137
+ demo.queue().launch(share=False, show_api=True, show_error=True)