clementchadebec
commited on
Commit
•
c0490dd
1
Parent(s):
46fe43d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import random
|
3 |
+
import warnings
|
4 |
+
|
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 diffusers.utils import load_image
|
12 |
+
from gradio_imageslider import ImageSlider
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
css = """
|
16 |
+
#col-container {
|
17 |
+
margin: 0 auto;
|
18 |
+
max-width: 512px;
|
19 |
+
}
|
20 |
+
"""
|
21 |
+
|
22 |
+
if torch.cuda.is_available():
|
23 |
+
power_device = "GPU"
|
24 |
+
device = "cuda"
|
25 |
+
else:
|
26 |
+
power_device = "CPU"
|
27 |
+
device = "cpu"
|
28 |
+
|
29 |
+
# Load pipeline
|
30 |
+
controlnet = FluxControlNetModel.from_pretrained(
|
31 |
+
"jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
|
32 |
+
).to(device)
|
33 |
+
pipe = FluxControlNetPipeline.from_pretrained(
|
34 |
+
"black-forest-labs/FLUX.1-dev", controlnet=controlnet, torch_dtype=torch.bfloat16
|
35 |
+
)
|
36 |
+
pipe.to(device)
|
37 |
+
|
38 |
+
MAX_SEED = 1000000
|
39 |
+
MAX_PIXEL_BUDGET = 1024 * 1024
|
40 |
+
|
41 |
+
|
42 |
+
def process_input(input_image, upscale_factor, **kwargs):
|
43 |
+
w, h = input_image.size
|
44 |
+
w_original, h_original = w, h
|
45 |
+
aspect_ratio = w / h
|
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 |
+
|
58 |
+
# resize to multiple of 8
|
59 |
+
w, h = input_image.size
|
60 |
+
w = w - w % 8
|
61 |
+
h = h - h % 8
|
62 |
+
|
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))
|
86 |
+
|
87 |
+
generator = torch.Generator().manual_seed(seed)
|
88 |
+
|
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],
|
96 |
+
width=control_image.size[0],
|
97 |
+
generator=generator,
|
98 |
+
).images[0]
|
99 |
+
|
100 |
+
# resize to target desired size
|
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:
|
108 |
+
# with gr.Column(elem_id="col-container"):
|
109 |
+
gr.Markdown(
|
110 |
+
f"""
|
111 |
+
# ⚡ Flux.1-dev Upscaler ControlNet ⚡
|
112 |
+
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.
|
113 |
+
Currently running on {power_device}.
|
114 |
+
"""
|
115 |
+
)
|
116 |
+
|
117 |
+
with gr.Row():
|
118 |
+
run_button = gr.Button(value="Run")
|
119 |
+
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column(scale=4):
|
122 |
+
input_im = gr.Image(label="Input Image", type="pil")
|
123 |
+
with gr.Column(scale=1):
|
124 |
+
num_inference_steps = gr.Slider(
|
125 |
+
label="Number of Inference Steps",
|
126 |
+
minimum=8,
|
127 |
+
maximum=50,
|
128 |
+
step=1,
|
129 |
+
value=28,
|
130 |
+
)
|
131 |
+
upscale_factor = gr.Slider(
|
132 |
+
label="Upscale Factor",
|
133 |
+
minimum=1,
|
134 |
+
maximum=4,
|
135 |
+
step=1,
|
136 |
+
value=4,
|
137 |
+
)
|
138 |
+
seed = gr.Slider(
|
139 |
+
label="Seed",
|
140 |
+
minimum=0,
|
141 |
+
maximum=MAX_SEED,
|
142 |
+
step=1,
|
143 |
+
value=42,
|
144 |
+
)
|
145 |
+
|
146 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
147 |
+
|
148 |
+
with gr.Row():
|
149 |
+
result = ImageSlider(label="Input / Output", type="pil")
|
150 |
+
|
151 |
+
examples = gr.Examples(
|
152 |
+
examples=[
|
153 |
+
"examples/image_1.jpg",
|
154 |
+
"examples/image_1.jpg",
|
155 |
+
"examples/image_1.jpg",
|
156 |
+
"examples/image_1.jpg",
|
157 |
+
],
|
158 |
+
inputs=input_im,
|
159 |
+
)
|
160 |
+
|
161 |
+
gr.Markdown("**Disclaimer:**")
|
162 |
+
gr.Markdown(
|
163 |
+
"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."
|
164 |
+
)
|
165 |
+
gr.on(
|
166 |
+
[run_button.click],
|
167 |
+
fn=infer,
|
168 |
+
inputs=[seed, randomize_seed, input_im, num_inference_steps, upscale_factor],
|
169 |
+
outputs=result,
|
170 |
+
show_api=False,
|
171 |
+
# show_progress="minimal",
|
172 |
+
)
|
173 |
+
|
174 |
+
demo.queue().launch(share=True)
|