t-montes commited on
Commit
a0c6111
·
verified ·
1 Parent(s): da042e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -42
app.py CHANGED
@@ -26,7 +26,6 @@ else:
26
  power_device = "CPU"
27
  device = "cpu"
28
 
29
-
30
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
31
 
32
  model_path = snapshot_download(
@@ -34,10 +33,9 @@ model_path = snapshot_download(
34
  repo_type="model",
35
  ignore_patterns=["*.md", "*..gitattributes"],
36
  local_dir="FLUX.1-dev",
37
- token=huggingface_token, # type a new token-id.
38
  )
39
 
40
-
41
  # Load pipeline
42
  controlnet = FluxControlNetModel.from_pretrained(
43
  "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
@@ -48,14 +46,12 @@ pipe = FluxControlNetPipeline.from_pretrained(
48
  pipe.to(device)
49
 
50
  MAX_SEED = 1000000
51
- MAX_PIXEL_BUDGET = 1024 * 1024
52
-
53
 
54
  def process_input(input_image, upscale_factor, **kwargs):
55
  w, h = input_image.size
56
  w_original, h_original = w, h
57
  aspect_ratio = w / h
58
-
59
  was_resized = False
60
 
61
  if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
@@ -73,15 +69,13 @@ def process_input(input_image, upscale_factor, **kwargs):
73
  )
74
  was_resized = True
75
 
76
- # resize to multiple of 8
77
  w, h = input_image.size
78
  w = w - w % 8
79
  h = h - h % 8
80
 
81
  return input_image.resize((w, h)), w_original, h_original, was_resized
82
 
83
-
84
- @spaces.GPU#(duration=42)
85
  def infer(
86
  seed,
87
  randomize_seed,
@@ -98,7 +92,6 @@ def infer(
98
  input_image, upscale_factor
99
  )
100
 
101
- # rescale with upscale factor
102
  w, h = input_image.size
103
  control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
104
 
@@ -121,22 +114,19 @@ def infer(
121
  f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
122
  )
123
 
124
- # resize to target desired size
125
  image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
126
  image.save("output.jpg")
127
- # convert to numpy
128
- return [true_input_image, image, seed]
129
 
 
130
 
131
  with gr.Blocks(css=css) as demo:
132
- # with gr.Column(elem_id="col-container"):
133
  gr.Markdown(
134
  f"""
135
  # ⚡ Flux.1-dev Upscaler ControlNet ⚡
136
  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.
137
  Currently running on {power_device}.
138
 
139
- *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! 🚀
140
  """
141
  )
142
 
@@ -183,12 +173,8 @@ with gr.Blocks(css=css) as demo:
183
 
184
  examples = gr.Examples(
185
  examples=[
186
- # [42, False, "examples/image_1.jpg", 28, 4, 0.6],
187
  [42, False, "examples/image_2.jpg", 28, 4, 0.6],
188
- # [42, False, "examples/image_3.jpg", 28, 4, 0.6],
189
  [42, False, "examples/image_4.jpg", 28, 4, 0.6],
190
- # [42, False, "examples/image_5.jpg", 28, 4, 0.6],
191
- # [42, False, "examples/image_6.jpg", 28, 4, 0.6],
192
  ],
193
  inputs=[
194
  seed,
@@ -203,26 +189,6 @@ with gr.Blocks(css=css) as demo:
203
  cache_examples="lazy",
204
  )
205
 
206
- # examples = gr.Examples(
207
- # examples=[
208
- # #[42, False, "examples/image_1.jpg", 28, 4, 0.6],
209
- # [42, False, "examples/image_2.jpg", 28, 4, 0.6],
210
- # #[42, False, "examples/image_3.jpg", 28, 4, 0.6],
211
- # #[42, False, "examples/image_4.jpg", 28, 4, 0.6],
212
- # [42, False, "examples/image_5.jpg", 28, 4, 0.6],
213
- # [42, False, "examples/image_6.jpg", 28, 4, 0.6],
214
- # [42, False, "examples/image_7.jpg", 28, 4, 0.6],
215
- # ],
216
- # inputs=[
217
- # seed,
218
- # randomize_seed,
219
- # input_im,
220
- # num_inference_steps,
221
- # upscale_factor,
222
- # controlnet_conditioning_scale,
223
- # ],
224
- # )
225
-
226
  gr.Markdown("**Disclaimer:**")
227
  gr.Markdown(
228
  "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."
@@ -239,8 +205,7 @@ with gr.Blocks(css=css) as demo:
239
  controlnet_conditioning_scale,
240
  ],
241
  outputs=result,
242
- show_api=False,
243
- # show_progress="minimal",
244
  )
245
 
246
- demo.queue().launch(share=False, show_api=False)
 
26
  power_device = "CPU"
27
  device = "cpu"
28
 
 
29
  huggingface_token = os.getenv("HUGGINFACE_TOKEN")
30
 
31
  model_path = snapshot_download(
 
33
  repo_type="model",
34
  ignore_patterns=["*.md", "*..gitattributes"],
35
  local_dir="FLUX.1-dev",
36
+ token=huggingface_token,
37
  )
38
 
 
39
  # Load pipeline
40
  controlnet = FluxControlNetModel.from_pretrained(
41
  "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
 
46
  pipe.to(device)
47
 
48
  MAX_SEED = 1000000
49
+ MAX_PIXEL_BUDGET = 2048 * 2048
 
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
 
55
  was_resized = False
56
 
57
  if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
 
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,
 
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
 
 
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 2048x2048. 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
 
 
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,
 
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."
 
205
  controlnet_conditioning_scale,
206
  ],
207
  outputs=result,
208
+ show_api=True,
 
209
  )
210
 
211
+ demo.queue().launch(share=False, show_api=True)