Spaces:
Running
on
Zero
Running
on
Zero
Commit
•
8fd0cd1
1
Parent(s):
3ea2e8b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
from diffusers import FluxPriorReduxPipeline, FluxPipeline
|
5 |
+
from diffusers.utils import load_image
|
6 |
+
import spaces
|
7 |
+
|
8 |
+
MAX_SEED = np.iinfo(np.int32).max
|
9 |
+
MAX_IMAGE_SIZE = 2048
|
10 |
+
|
11 |
+
pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained(
|
12 |
+
"black-forest-labs/FLUX.1-Redux-dev",
|
13 |
+
revision="refs/pr/8",
|
14 |
+
torch_dtype=torch.bfloat16
|
15 |
+
).to("cuda")
|
16 |
+
|
17 |
+
pipe = FluxPipeline.from_pretrained(
|
18 |
+
"black-forest-labs/FLUX.1-dev" ,
|
19 |
+
text_encoder=None,
|
20 |
+
text_encoder_2=None,
|
21 |
+
torch_dtype=torch.bfloat16
|
22 |
+
).to("cuda")
|
23 |
+
|
24 |
+
@spaces.GPU
|
25 |
+
def infer(control_image, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
26 |
+
pipe_prior_output = pipe_prior_redux(control_image)
|
27 |
+
images = pipe(
|
28 |
+
guidance_scale=guidance_scale,
|
29 |
+
num_inference_steps=num_inference_steps,
|
30 |
+
generator=torch.Generator("cpu").manual_seed(seed),
|
31 |
+
**pipe_prior_output,
|
32 |
+
).images[0]
|
33 |
+
return images
|
34 |
+
css="""
|
35 |
+
#col-container {
|
36 |
+
margin: 0 auto;
|
37 |
+
max-width: 520px;
|
38 |
+
}
|
39 |
+
"""
|
40 |
+
|
41 |
+
with gr.Blocks(css=css) as demo:
|
42 |
+
|
43 |
+
with gr.Column(elem_id="col-container"):
|
44 |
+
gr.Markdown(f"""# FLUX.1 Redux [dev]
|
45 |
+
An adapter for FLUX [dev] to create image variations
|
46 |
+
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
47 |
+
""")
|
48 |
+
|
49 |
+
input_image = gr.Image(label="Image to create variations")
|
50 |
+
run_button = gr.Button("Run")
|
51 |
+
|
52 |
+
result = gr.Image(label="Result", show_label=False)
|
53 |
+
|
54 |
+
with gr.Accordion("Advanced Settings", open=False):
|
55 |
+
|
56 |
+
seed = gr.Slider(
|
57 |
+
label="Seed",
|
58 |
+
minimum=0,
|
59 |
+
maximum=MAX_SEED,
|
60 |
+
step=1,
|
61 |
+
value=0,
|
62 |
+
)
|
63 |
+
|
64 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
65 |
+
|
66 |
+
with gr.Row():
|
67 |
+
|
68 |
+
width = gr.Slider(
|
69 |
+
label="Width",
|
70 |
+
minimum=256,
|
71 |
+
maximum=MAX_IMAGE_SIZE,
|
72 |
+
step=32,
|
73 |
+
value=1024,
|
74 |
+
)
|
75 |
+
|
76 |
+
height = gr.Slider(
|
77 |
+
label="Height",
|
78 |
+
minimum=256,
|
79 |
+
maximum=MAX_IMAGE_SIZE,
|
80 |
+
step=32,
|
81 |
+
value=1024,
|
82 |
+
)
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
|
86 |
+
guidance_scale = gr.Slider(
|
87 |
+
label="Guidance Scale",
|
88 |
+
minimum=1,
|
89 |
+
maximum=15,
|
90 |
+
step=0.1,
|
91 |
+
value=3.5,
|
92 |
+
)
|
93 |
+
|
94 |
+
num_inference_steps = gr.Slider(
|
95 |
+
label="Number of inference steps",
|
96 |
+
minimum=1,
|
97 |
+
maximum=50,
|
98 |
+
step=1,
|
99 |
+
value=28,
|
100 |
+
)
|
101 |
+
|
102 |
+
gr.on(
|
103 |
+
triggers=[run_button.click],
|
104 |
+
fn = infer,
|
105 |
+
inputs = [input_image, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
106 |
+
outputs = [result, seed]
|
107 |
+
)
|
108 |
+
|
109 |
+
demo.launch()
|