File size: 6,205 Bytes
2fdb666
46af7ed
 
 
2fdb666
 
 
46af7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdb666
 
46af7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdb666
46af7ed
 
 
 
 
2fdb666
 
46af7ed
 
 
2fdb666
 
46af7ed
2fdb666
46af7ed
 
 
 
 
 
 
 
2fdb666
46af7ed
 
 
2fdb666
 
 
46af7ed
 
2fdb666
46af7ed
2fdb666
46af7ed
 
 
 
 
2fdb666
46af7ed
 
2fdb666
 
46af7ed
 
2fdb666
 
46af7ed
 
 
 
 
 
 
 
 
 
 
2fdb666
46af7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fdb666
46af7ed
 
2fdb666
46af7ed
 
 
 
 
2fdb666
46af7ed
 
 
2fdb666
46af7ed
2fdb666
 
46af7ed
 
 
 
 
 
 
 
2fdb666
46af7ed
2fdb666
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import gradio as gr
import spaces
import os
import random

import torch
from PIL import Image
import cv2
from huggingface_hub import login
from diffusers import FluxControlNetPipeline, FluxControlNetModel
from diffusers.models import FluxMultiControlNetModel

"""
FLUX‑1 ControlNet demo
----------------------
This script rebuilds the Gradio interface shown in your screenshot with **one** control‑image upload
slot and integrates the FLUX.1‑dev‑ControlNet‑Union‑Pro model.  

Key points
~~~~~~~~~~
* Single *control image* input (left).  
* *Result* and *Pre‑processed Cond* previews side‑by‑side (center & right).  
* *Prompt* textbox plus a dedicated **ControlNet** panel for choosing the mode and strength.  
* Seed handling with optional randomisation.  
* Advanced sliders for *Guidance scale* and *Inference steps*.  
* Works on CUDA (bfloat16) or CPU (float32).  
* Minimal Canny preview implementation when the *canny* mode is selected (extend as you like for the
  other modes).

Before running, set the `HUGGINGFACE_TOKEN` environment variable **or** call
`login("<YOUR_HF_TOKEN>")` explicitly.
"""

# --------------------------------------------------
# Model & pipeline setup
# --------------------------------------------------
HF_TOKEN = os.getenv("HF_TOKEN_NEW")
login(HF_TOKEN)
# If you prefer to hard‑code the token, uncomment:
# login("hf_your_token_here")

BASE_MODEL = "black-forest-labs/FLUX.1-dev"
CONTROLNET_MODEL = "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro"

device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32

controlnet_single = FluxControlNetModel.from_pretrained(
    CONTROLNET_MODEL, torch_dtype=dtype
)
controlnet = FluxMultiControlNetModel([controlnet_single])

pipe = FluxControlNetPipeline.from_pretrained(
    BASE_MODEL, controlnet=controlnet, torch_dtype=dtype
).to(device)
pipe.set_progress_bar_config(disable=True)

# --------------------------------------------------
# UI ‑> model value mapping
# --------------------------------------------------
MODE_MAPPING = {
    "canny": 0,
    "depth": 1,
    "openpose": 2,
    "gray": 3,
    "blur": 4,
    "tile": 5,
    "low quality": 6,
}

MAX_SEED = 100

# --------------------------------------------------
# Helper: quick‑n‑dirty Canny preview (only for UI display)
# --------------------------------------------------


def _preview_canny(pil_img: Image.Image) -> Image.Image:
    arr = np.array(pil_img.convert("RGB"))
    edges = cv2.Canny(arr, 100, 200)
    edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
    return Image.fromarray(edges_rgb)


def _make_preview(control_image: Image.Image, mode: str) -> Image.Image:
    if mode == "canny":
        return _preview_canny(control_image)
    # For other modes you can plug in your own visualiser later
    return control_image


# --------------------------------------------------
# Inference function
# --------------------------------------------------


@spaces.GPU
def infer(
    control_image: Image.Image,
    prompt: str,
    mode: str,
    control_strength: float,
    seed: int,
    randomize_seed: bool,
    guidance_scale: float,
    num_inference_steps: int,
):
    if control_image is None:
        raise gr.Error("Please upload a control image first.")

    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    gen = torch.Generator(device).manual_seed(seed)
    w, h = control_image.size

    result = pipe(
        prompt=prompt,
        control_image=[control_image],
        control_mode=[MODE_MAPPING[mode]],
        width=w,
        height=h,
        controlnet_conditioning_scale=[control_strength],
        num_inference_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        generator=gen,
    ).images[0]

    preview = _make_preview(control_image, mode)
    return result, seed, preview


# --------------------------------------------------
# Gradio UI
# --------------------------------------------------
css = """#wrapper {max-width: 960px; margin: 0 auto;}"""
with gr.Blocks(css=css, elem_id="wrapper") as demo:
    gr.Markdown("## FLUX.1‑dev‑ControlNet‑Union‑Pro")
    gr.Markdown(
        "A unified ControlNet for **FLUX.1‑dev** from the InstantX team and Shakker Labs.  "
        + "Recommended strengths: *canny 0.65*, *tile 0.45*, *depth 0.55*, *blur 0.45*, "
        + "*openpose 0.55*, *gray 0.45*, *low quality 0.40*.  Long prompts usually help."
    )

    # ------------ Image panel row ------------
    with gr.Row():
        control_image = gr.Image(
            label="Upload a processed control image",
            type="pil",
            height=512,
        )
        result_image = gr.Image(label="Result", height=512)
        preview_image = gr.Image(label="Pre‑processed Cond", height=512)

    # ------------ Prompt ------------
    prompt_txt = gr.Textbox(label="Prompt", value="best quality", lines=1)

    # ------------ ControlNet settings ------------
    with gr.Row():
        with gr.Column():
            gr.Markdown("### ControlNet")
            mode_radio = gr.Radio(
                choices=list(MODE_MAPPING.keys()), value="gray", label="Mode"
            )
            strength_slider = gr.Slider(
                0.0, 1.0, value=0.5, step=0.01, label="control strength"
            )
        with gr.Column():
            seed_slider = gr.Slider(0, MAX_SEED, step=1, value=42, label="Seed")
            randomize_chk = gr.Checkbox(label="Randomize seed", value=True)
            guidance_slider = gr.Slider(
                0.0, 10.0, step=0.1, value=3.5, label="Guidance scale"
            )
            steps_slider = gr.Slider(1, 50, step=1, value=24, label="Inference steps")

    submit_btn = gr.Button("Submit")

    submit_btn.click(
        fn=infer,
        inputs=[
            control_image,
            prompt_txt,
            mode_radio,
            strength_slider,
            seed_slider,
            randomize_chk,
            guidance_slider,
            steps_slider,
        ],
        outputs=[result_image, seed_slider, preview_image],
    )

if __name__ == "__main__":
    demo.launch()