File size: 10,620 Bytes
6aa4d81
 
 
16764be
0845b5a
 
6aa4d81
f592617
 
 
 
 
 
 
 
16764be
 
 
f592617
16764be
 
 
 
 
47d3f85
 
 
 
 
 
 
 
 
 
ed9bd08
0845b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
ed9bd08
0845b5a
 
5c1d384
ed9bd08
0845b5a
16764be
 
0845b5a
16764be
0845b5a
16764be
 
0845b5a
16764be
 
5c1d384
 
ed9bd08
0845b5a
 
16764be
0845b5a
 
ed9bd08
 
 
0845b5a
 
 
 
 
 
ed9bd08
0845b5a
16764be
0845b5a
 
 
 
 
 
16764be
0845b5a
 
 
 
 
5c1d384
0845b5a
 
 
 
 
5c1d384
0845b5a
 
 
5c1d384
0845b5a
 
 
16764be
0845b5a
 
 
 
 
 
5c1d384
0845b5a
 
 
 
 
 
 
5c1d384
0845b5a
 
 
 
 
 
16764be
0845b5a
 
 
 
 
 
 
 
 
 
 
 
 
16764be
0845b5a
 
 
 
 
 
 
5c1d384
0845b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
5c1d384
0845b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b406c
0845b5a
 
 
 
6aa4d81
0845b5a
 
 
5c1d384
0845b5a
 
 
 
 
 
 
 
ed9bd08
6aa4d81
0845b5a
16764be
6aa4d81
0845b5a
 
16764be
 
 
 
0845b5a
 
 
 
16764be
0845b5a
 
 
16764be
 
 
 
 
 
0845b5a
16764be
 
 
 
 
 
0845b5a
 
16764be
 
 
0845b5a
16764be
0845b5a
16764be
 
 
 
0845b5a
16764be
0845b5a
 
16764be
0845b5a
16764be
 
0845b5a
16764be
0845b5a
16764be
 
0845b5a
16764be
0845b5a
16764be
 
0845b5a
16764be
0845b5a
16764be
 
0845b5a
16764be
0845b5a
16764be
 
 
0845b5a
16764be
0845b5a
 
 
6aa4d81
 
34b406c
16764be
 
 
 
 
 
 
 
 
 
 
34b406c
6aa4d81
 
 
 
ed9bd08
16764be
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import os
import random
import torch
from pathlib import Path
from PIL import Image
import gradio as gr
from huggingface_hub import hf_hub_download
import sys

# 1. Configuração de Caminhos
current_dir = os.path.dirname(os.path.abspath(__file__))
comfyui_path = os.path.join(current_dir, "ComfyUI")
sys.path.append(comfyui_path)

# 2. Imports do ComfyUI
from nodes import NODE_CLASS_MAPPINGS
import folder_paths

# 3. Configuração de Diretórios
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
output_dir = os.path.join(BASE_DIR, "output")
os.makedirs(output_dir, exist_ok=True)
folder_paths.set_output_directory(output_dir)

# 4. Diagnóstico CUDA
print("Python version:", sys.version)
print("Torch version:", torch.__version__)
print("CUDA disponível:", torch.cuda.is_available())
print("Quantidade de GPUs:", torch.cuda.device_count())
if torch.cuda.is_available():
    print("GPU atual:", torch.cuda.get_device_name(0))
else:
    print("GPU não disponível. Usando CPU.")

# 5. Download de Modelos
def download_models():
    models = [
        ("black-forest-labs/FLUX.1-Redux-dev", "flux1-redux-dev.safetensors", "style_models"),
        ("comfyanonymous/flux_text_encoders", "t5xxl_fp16.safetensors", "text_encoders"),
        ("zer0int/CLIP-GmP-ViT-L-14", "ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors", "text_encoders"),
        ("black-forest-labs/FLUX.1-dev", "ae.safetensors", "vae"),
        ("black-forest-labs/FLUX.1-dev", "flux1-dev.sft", "diffusion_models"),
        ("google/siglip-so400m-patch14-384", "model.safetensors", "clip_vision"),
        ("black-forest-labs/FLUX.1-Redux-dev", "NFTNIK_FLUX.1[dev]_LoRA.safetensors", "lora")
    ]
    
    for repo_id, filename, model_type in models:
        model_dir = os.path.join(BASE_DIR, "models", model_type)
        os.makedirs(model_dir, exist_ok=True)
        print(f"Baixando {filename} de {repo_id}...")
        hf_hub_download(repo_id=repo_id, filename=filename, local_dir=model_dir)
        folder_paths.add_model_folder_path(model_type, model_dir)

# 6. Load custom nodes
def import_custom_nodes():
    import asyncio
    import execution
    from nodes import init_extra_nodes
    import server

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    server_instance = server.PromptServer(loop)
    execution.PromptQueue(server_instance)
    init_extra_nodes()

# 7. Main function to execute the workflow and generate an image
def generate_image(prompt, input_image, lora_weight, guidance, downsampling_factor, weight, seed, width, height, batch_size, steps):
    import_custom_nodes()
    
    try:
        with torch.inference_mode():
            device = "cuda" if torch.cuda.is_available() else "cpu"
            print(f"Using device: {device}")

            # Load CLIP
            dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
            dualcliploader_loaded = dualcliploader.load_clip(
                clip_name1="t5xxl_fp16.safetensors",
                clip_name2="ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors",
                type="flux",
                device=device
            )

            # Text Encoding
            cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
            encoded_text = cliptextencode.encode(
                text=prompt,
                clip=dualcliploader_loaded[0]
            )

            # Load Style Model
            stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
            style_model = stylemodelloader.load_style_model(
                style_model_name="flux1-redux-dev.safetensors"
            )

            # Load CLIP Vision
            clipvisionloader = NODE_CLASS_MAPPINGS["CLIPVisionLoader"]()
            clip_vision = clipvisionloader.load_clip(
                clip_name="model.safetensors"
            )

            # Load Input Image
            loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
            loaded_image = loadimage.load_image(image=input_image)

            # Load VAE
            vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
            vae = vaeloader.load_vae(vae_name="ae.safetensors")

            # Load UNET
            unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
            unet = unetloader.load_unet(
                unet_name="flux1-dev.sft",
                weight_dtype="fp8_e4m3fn"
            )

            # Load LoRA
            loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
            lora_model = loraloadermodelonly.load_lora_model_only(
                lora_name="NFTNIK_FLUX.1[dev]_LoRA.safetensors",
                strength_model=lora_weight,
                model=unet[0]
            )

            # Flux Guidance
            fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
            flux_guidance = fluxguidance.append(
                guidance=guidance,
                conditioning=encoded_text[0]
            )

            # Redux Advanced
            reduxadvanced = NODE_CLASS_MAPPINGS["ReduxAdvanced"]()
            redux_result = reduxadvanced.apply_stylemodel(
                downsampling_factor=downsampling_factor,
                downsampling_function="area",
                mode="keep aspect ratio",
                weight=weight,
                autocrop_margin=0.1,
                conditioning=flux_guidance[0],
                style_model=style_model[0],
                clip_vision=clip_vision[0],
                image=loaded_image[0]
            )

            # Empty Latent Image
            emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
            empty_latent = emptylatentimage.generate(
                width=width,
                height=height,
                batch_size=batch_size
            )

            # KSampler
            ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
            sampled = ksampler.sample(
                seed=seed,
                steps=steps,
                cfg=1,
                sampler_name="euler",
                scheduler="simple",
                denoise=1,
                model=lora_model[0],
                positive=redux_result[0],
                negative=flux_guidance[0],
                latent_image=empty_latent[0]
            )

            # VAE Decode
            vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
            decoded = vaedecode.decode(
                samples=sampled[0],
                vae=vae[0]
            )

            # Save the image in the output directory
            saveimage = NODE_CLASS_MAPPINGS["SaveImage"]()
            temp_filename = f"Flux_{random.randint(0, 99999)}"
            saveimage.save_images(
                filename_prefix=temp_filename,
                images=decoded[0]
            )

            # Add a delay to ensure the file system updates
            import time
            time.sleep(0.5)

            # Dynamically retrieve the correct file name
            saved_files = [f for f in os.listdir(output_dir) if f.startswith(temp_filename)]
            if not saved_files:
                raise FileNotFoundError(f"Output file not found: Expected files starting with {temp_filename}")

            # Get the full path of the saved file
            temp_path = os.path.join(output_dir, saved_files[0])
            print(f"Image saved at: {temp_path}")

            # Return the saved image for Gradio display
            output_image = Image.open(temp_path)
            return output_image

    except Exception as e:
        print(f"Error during generation: {str(e)}")
        return None

# 8. Gradio Interface
with gr.Blocks() as app:
    gr.Markdown("# FLUX Redux Image Generator")
    
    with gr.Row():
        with gr.Column():
            prompt_input = gr.Textbox(
                label="Prompt",
                placeholder="Enter your prompt here...",
                lines=5
            )
            input_image = gr.Image(
                label="Input Image",
                type="filepath"
            )
            
            with gr.Row():
                with gr.Column():
                    lora_weight = gr.Slider(
                        minimum=0,
                        maximum=2,
                        step=0.1,
                        value=0.6,
                        label="LoRA Weight"
                    )
                    guidance = gr.Slider(
                        minimum=0,
                        maximum=20,
                        step=0.1,
                        value=3.5,
                        label="Guidance"
                    )
                    downsampling_factor = gr.Slider(
                        minimum=1,
                        maximum=8,
                        step=1,
                        value=3,
                        label="Downsampling Factor"
                    )
                    weight = gr.Slider(
                        minimum=0,
                        maximum=2,
                        step=0.1,
                        value=1.0,
                        label="Model Weight"
                    )
                with gr.Column():
                    seed = gr.Number(
                        value=random.randint(1, 2**64),
                        label="Seed",
                        precision=0
                    )
                    width = gr.Number(
                        value=1024,
                        label="Width",
                        precision=0
                    )
                    height = gr.Number(
                        value=1024,
                        label="Height",
                        precision=0
                    )
                    batch_size = gr.Number(
                        value=1,
                        label="Batch Size",
                        precision=0
                    )
                    steps = gr.Number(
                        value=20,
                        label="Steps",
                        precision=0
                    )
            
            generate_btn = gr.Button("Generate Image")
        
        with gr.Column():
            output_image = gr.Image(label="Generated Image", type="pil")
    
    generate_btn.click(
        fn=generate_image,
        inputs=[
            prompt_input,
            input_image,
            lora_weight,
            guidance,
            downsampling_factor,
            weight,
            seed,
            width,
            height,
            batch_size,
            steps
        ],
        outputs=[output_image]
    )

if __name__ == "__main__":
    # Download_models()
    app.launch(share=True)