File size: 12,429 Bytes
6aa4d81
651b8c4
e656061
6aa4d81
e656061
02c8fdb
0845b5a
6aa4d81
0c0098b
e656061
8858e41
02c8fdb
 
 
 
aa3232a
8858e41
 
 
aa3232a
8858e41
 
 
 
e656061
 
57ecc99
e656061
57ecc99
8858e41
57ecc99
aa3232a
8858e41
 
f8708de
 
 
 
57ecc99
aa3232a
8858e41
 
 
 
e656061
f8708de
a7d1628
aa3232a
8858e41
 
f8708de
8858e41
f8708de
 
a7d1628
aa3232a
0c0098b
f8708de
 
 
 
0c0098b
8858e41
f8708de
 
 
 
 
e656061
aa3232a
8858e41
8a4e228
f8708de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4e228
f8708de
 
651b8c4
aa3232a
8858e41
 
f8708de
 
 
 
 
 
 
 
 
 
 
e656061
f8708de
 
 
 
 
 
 
 
e656061
f8708de
 
 
 
 
 
 
 
651b8c4
f8708de
 
 
 
 
 
 
 
8858e41
f8708de
 
 
 
 
02c8fdb
f8708de
 
 
16764be
f8708de
 
 
 
 
 
 
8858e41
f8708de
 
5c1d384
aa3232a
8858e41
f8708de
 
02c8fdb
f8708de
 
 
 
 
02c8fdb
f8708de
 
 
 
 
 
e656061
f8708de
 
 
 
 
 
16764be
f8708de
 
 
 
 
 
16764be
f8708de
 
 
 
 
 
 
 
 
 
 
 
5c1d384
02c8fdb
f8708de
 
 
 
 
 
5c1d384
f8708de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0845b5a
f8708de
 
 
 
 
 
 
0845b5a
aa3232a
02c8fdb
aa3232a
02c8fdb
 
aa3232a
02c8fdb
 
 
 
 
e656061
f8708de
 
 
0845b5a
8858e41
6aa4d81
f8708de
8858e41
f8708de
 
 
 
 
 
 
 
 
 
 
8858e41
f8708de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8858e41
f8708de
8858e41
f8708de
 
8858e41
f8708de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aa4d81
 
f8708de
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import os
import sys
import random
import torch
from pathlib import Path
import numpy as np
import gradio as gr
from huggingface_hub import hf_hub_download
import spaces
from typing import Union, Sequence, Mapping, Any
import logging
from nodes import NODE_CLASS_MAPPINGS, init_extra_nodes, SaveImage  # <-- Node SaveImage
from comfy import model_management
import folder_paths

# 1. Configurar logging para debug
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# 2. Configuração de Caminhos e Imports
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)

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

# 4. Configurar caminhos dos modelos e verificar estrutura
MODEL_FOLDERS = ["style_models", "text_encoders", "vae", "unet", "clip_vision"]
for model_folder in MODEL_FOLDERS:
    folder_path = os.path.join(models_dir, model_folder)
    os.makedirs(folder_path, exist_ok=True)
    folder_paths.add_model_folder_path(model_folder, folder_path)
    logger.info(f"Pasta de modelo configurada: {model_folder}")

# 5. Diagnóstico CUDA
logger.info(f"Python version: {sys.version}")
logger.info(f"Torch version: {torch.__version__}")
logger.info(f"CUDA disponível: {torch.cuda.is_available()}")
logger.info(f"Quantidade de GPUs: {torch.cuda.device_count()}")
if torch.cuda.is_available():
    logger.info(f"GPU atual: {torch.cuda.get_device_name(0)}")

# 6. Inicialização do ComfyUI
logger.info("Inicializando ComfyUI...")
try:
    init_extra_nodes()
except Exception as e:
    logger.warning(f"Aviso na inicialização de nós extras: {str(e)}")
    logger.info("Continuando mesmo com avisos nos nós extras...")

# 7. Helper Functions
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
    try:
        return obj[index]
    except KeyError:
        return obj["result"][index]

def verify_file_exists(folder: str, filename: str) -> bool:
    file_path = os.path.join(models_dir, folder, filename)
    exists = os.path.exists(file_path)
    if not exists:
        logger.error(f"Arquivo não encontrado: {file_path}")
    return exists

# 8. Download de Modelos
logger.info("Baixando modelos necessários...")
try:
    hf_hub_download(
        repo_id="black-forest-labs/FLUX.1-Redux-dev",
        filename="flux1-redux-dev.safetensors",
        local_dir=os.path.join(models_dir, "style_models")
    )
    hf_hub_download(
        repo_id="comfyanonymous/flux_text_encoders",
        filename="t5xxl_fp16.safetensors",
        local_dir=os.path.join(models_dir, "text_encoders")
    )
    hf_hub_download(
        repo_id="zer0int/CLIP-GmP-ViT-L-14",
        filename="ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors",
        local_dir=os.path.join(models_dir, "text_encoders")
    )
    hf_hub_download(
        repo_id="black-forest-labs/FLUX.1-dev",
        filename="ae.safetensors",
        local_dir=os.path.join(models_dir, "vae")
    )
    hf_hub_download(
        repo_id="black-forest-labs/FLUX.1-dev",
        filename="flux1-dev.safetensors",
        local_dir=os.path.join(models_dir, "unet")
    )
    hf_hub_download(
        repo_id="Comfy-Org/sigclip_vision_384",
        filename="sigclip_vision_patch14_384.safetensors",
        local_dir=os.path.join(models_dir, "clip_vision")
    )
except Exception as e:
    logger.error(f"Erro ao baixar modelos: {str(e)}")
    raise

# 9. Inicialização dos Modelos
logger.info("Inicializando modelos...")
try:
    with torch.no_grad():
        # CLIP
        logger.info("Carregando CLIP...")
        dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
        CLIP_MODEL = dualcliploader.load_clip(
            clip_name1="t5xxl_fp16.safetensors",
            clip_name2="ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors",
            type="flux"
        )
        if CLIP_MODEL is None:
            raise ValueError("Falha ao carregar CLIP model")

        # CLIP Vision
        logger.info("Carregando CLIP Vision...")
        clipvisionloader = NODE_CLASS_MAPPINGS["CLIPVisionLoader"]()
        CLIP_VISION = clipvisionloader.load_clip(
            clip_name="sigclip_vision_patch14_384.safetensors"
        )
        if CLIP_VISION is None:
            raise ValueError("Falha ao carregar CLIP Vision model")

        # Style Model
        logger.info("Carregando Style Model...")
        stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
        STYLE_MODEL = stylemodelloader.load_style_model(
            style_model_name="flux1-redux-dev.safetensors"
        )
        if STYLE_MODEL is None:
            raise ValueError("Falha ao carregar Style Model")

        # VAE
        logger.info("Carregando VAE...")
        vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
        VAE_MODEL = vaeloader.load_vae(
            vae_name="ae.safetensors"
        )
        if VAE_MODEL is None:
            raise ValueError("Falha ao carregar VAE model")

        # UNET
        logger.info("Carregando UNET...")
        unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
        UNET_MODEL = unetloader.load_unet(
            unet_name="flux1-dev.safetensors",
            weight_dtype="fp8_e4m3fn"  # ajuste se preciso
        )
        if UNET_MODEL is None:
            raise ValueError("Falha ao carregar UNET model")

        logger.info("Carregando modelos na GPU...")
        model_loaders = [CLIP_MODEL, VAE_MODEL, CLIP_VISION, UNET_MODEL]
        model_management.load_models_gpu([
            loader[0].patcher if hasattr(loader[0], 'patcher') else loader[0]
            for loader in model_loaders
        ])
        logger.info("Modelos carregados com sucesso")
except Exception as e:
    logger.error(f"Erro ao inicializar modelos: {str(e)}")
    raise

# 10. Função de Geração
@spaces.GPU
def generate_image(
    prompt, input_image, lora_weight, guidance, downsampling_factor,
    weight, seed, width, height, batch_size, steps,
    progress=gr.Progress(track_tqdm=True)
):
    try:
        with torch.no_grad():
            logger.info(f"Iniciando geração com prompt: {prompt}")

            # Codificar texto
            cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
            encoded_text = cliptextencode.encode(
                text=prompt,
                clip=CLIP_MODEL[0]
            )

            # Carregar e processar imagem
            loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
            loaded_image = loadimage.load_image(image=input_image)
            if loaded_image is None:
                raise ValueError("Erro ao carregar a imagem de entrada")
            logger.info("Imagem carregada com sucesso")

            # 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,
                conditioning=flux_guidance[0],
                style_model=STYLE_MODEL[0],
                clip_vision=CLIP_VISION[0],
                image=loaded_image[0]
            )

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

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

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

            # Salvar Imagem
            logger.info("Salvando imagem via node SaveImage...")
            decoded_tensor = decoded[0] 
            saveimage_node = NODE_CLASS_MAPPINGS["SaveImage"]()
            result_dict = saveimage_node.save_images(
                filename_prefix="Flux_", 
                images=decoded_tensor
            )
            saved_path = os.path.join(output_dir, result_dict["ui"]["images"][0]["filename"])
            logger.info(f"Imagem salva em: {saved_path}")
            return saved_path

    except Exception as e:
        logger.error(f"Erro ao gerar imagem: {str(e)}")
        return None

# 10. Interface Gradio
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="filepath")
   
    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__":
    app.launch()