Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -34,10 +34,10 @@ folder_paths.set_output_directory(output_dir)
|
|
34 |
# Configurar caminhos dos modelos e verificar estrutura
|
35 |
MODEL_FOLDERS = ["style_models", "text_encoders", "vae", "unet", "clip_vision"]
|
36 |
for model_folder in MODEL_FOLDERS:
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
# 4. Diagnóstico CUDA
|
43 |
logger.info(f"Python version: {sys.version}")
|
@@ -45,301 +45,321 @@ logger.info(f"Torch version: {torch.__version__}")
|
|
45 |
logger.info(f"CUDA disponível: {torch.cuda.is_available()}")
|
46 |
logger.info(f"Quantidade de GPUs: {torch.cuda.device_count()}")
|
47 |
if torch.cuda.is_available():
|
48 |
-
|
49 |
|
50 |
# 5. Inicialização do ComfyUI
|
51 |
logger.info("Inicializando ComfyUI...")
|
52 |
try:
|
53 |
-
|
54 |
except Exception as e:
|
55 |
-
|
56 |
-
|
57 |
|
58 |
# 6. Helper Functions
|
59 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
def verify_file_exists(folder: str, filename: str) -> bool:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
# 7. Download de Modelos
|
73 |
logger.info("Baixando modelos necessários...")
|
74 |
try:
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
except Exception as e:
|
94 |
-
|
95 |
-
|
96 |
|
97 |
# 8. Inicialização dos Modelos
|
98 |
logger.info("Inicializando modelos...")
|
99 |
try:
|
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 |
except Exception as e:
|
157 |
-
|
158 |
-
|
159 |
|
160 |
# 9. Função de Geração
|
161 |
@spaces.GPU
|
162 |
-
def generate_image(
|
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 |
# 10. Interface Gradio
|
249 |
with gr.Blocks() as app:
|
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 |
if __name__ == "__main__":
|
345 |
-
|
|
|
|
34 |
# Configurar caminhos dos modelos e verificar estrutura
|
35 |
MODEL_FOLDERS = ["style_models", "text_encoders", "vae", "unet", "clip_vision"]
|
36 |
for model_folder in MODEL_FOLDERS:
|
37 |
+
folder_path = os.path.join(models_dir, model_folder)
|
38 |
+
os.makedirs(folder_path, exist_ok=True)
|
39 |
+
folder_paths.add_model_folder_path(model_folder, folder_path)
|
40 |
+
logger.info(f"Pasta de modelo configurada: {model_folder}")
|
41 |
|
42 |
# 4. Diagnóstico CUDA
|
43 |
logger.info(f"Python version: {sys.version}")
|
|
|
45 |
logger.info(f"CUDA disponível: {torch.cuda.is_available()}")
|
46 |
logger.info(f"Quantidade de GPUs: {torch.cuda.device_count()}")
|
47 |
if torch.cuda.is_available():
|
48 |
+
logger.info(f"GPU atual: {torch.cuda.get_device_name(0)}")
|
49 |
|
50 |
# 5. Inicialização do ComfyUI
|
51 |
logger.info("Inicializando ComfyUI...")
|
52 |
try:
|
53 |
+
init_extra_nodes()
|
54 |
except Exception as e:
|
55 |
+
logger.warning(f"Aviso na inicialização de nós extras: {str(e)}")
|
56 |
+
logger.info("Continuando mesmo com avisos nos nós extras...")
|
57 |
|
58 |
# 6. Helper Functions
|
59 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
60 |
+
try:
|
61 |
+
return obj[index]
|
62 |
+
except KeyError:
|
63 |
+
return obj["result"][index]
|
64 |
|
65 |
def verify_file_exists(folder: str, filename: str) -> bool:
|
66 |
+
file_path = os.path.join(models_dir, folder, filename)
|
67 |
+
exists = os.path.exists(file_path)
|
68 |
+
if not exists:
|
69 |
+
logger.error(f"Arquivo não encontrado: {file_path}")
|
70 |
+
return exists
|
71 |
|
72 |
# 7. Download de Modelos
|
73 |
logger.info("Baixando modelos necessários...")
|
74 |
try:
|
75 |
+
hf_hub_download(
|
76 |
+
repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
77 |
+
filename="flux1-redux-dev.safetensors",
|
78 |
+
local_dir=os.path.join(models_dir, "style_models")
|
79 |
+
)
|
80 |
+
hf_hub_download(
|
81 |
+
repo_id="comfyanonymous/flux_text_encoders",
|
82 |
+
filename="t5xxl_fp16.safetensors",
|
83 |
+
local_dir=os.path.join(models_dir, "text_encoders")
|
84 |
+
)
|
85 |
+
hf_hub_download(
|
86 |
+
repo_id="zer0int/CLIP-GmP-ViT-L-14",
|
87 |
+
filename="ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors",
|
88 |
+
local_dir=os.path.join(models_dir, "text_encoders")
|
89 |
+
)
|
90 |
+
hf_hub_download(
|
91 |
+
repo_id="black-forest-labs/FLUX.1-dev",
|
92 |
+
filename="ae.safetensors",
|
93 |
+
local_dir=os.path.join(models_dir, "vae")
|
94 |
+
)
|
95 |
+
hf_hub_download(
|
96 |
+
repo_id="black-forest-labs/FLUX.1-dev",
|
97 |
+
filename="flux1-dev.safetensors",
|
98 |
+
local_dir=os.path.join(models_dir, "unet")
|
99 |
+
)
|
100 |
+
hf_hub_download(
|
101 |
+
repo_id="Comfy-Org/sigclip_vision_384",
|
102 |
+
filename="sigclip_vision_patch14_384.safetensors",
|
103 |
+
local_dir=os.path.join(models_dir, "clip_vision")
|
104 |
+
)
|
105 |
except Exception as e:
|
106 |
+
logger.error(f"Erro ao baixar modelos: {str(e)}")
|
107 |
+
raise
|
108 |
|
109 |
# 8. Inicialização dos Modelos
|
110 |
logger.info("Inicializando modelos...")
|
111 |
try:
|
112 |
+
# Use torch.no_grad() em vez de torch.inference_mode()
|
113 |
+
# para evitar o erro de version counter.
|
114 |
+
with torch.no_grad():
|
115 |
+
# CLIP
|
116 |
+
logger.info("Carregando CLIP...")
|
117 |
+
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
118 |
+
CLIP_MODEL = dualcliploader.load_clip(
|
119 |
+
clip_name1="t5xxl_fp16.safetensors",
|
120 |
+
clip_name2="ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors",
|
121 |
+
type="flux"
|
122 |
+
)
|
123 |
+
if CLIP_MODEL is None:
|
124 |
+
raise ValueError("Falha ao carregar CLIP model")
|
125 |
|
126 |
+
# CLIP Vision
|
127 |
+
logger.info("Carregando CLIP Vision...")
|
128 |
+
clipvisionloader = NODE_CLASS_MAPPINGS["CLIPVisionLoader"]()
|
129 |
+
CLIP_VISION = clipvisionloader.load_clip(
|
130 |
+
clip_name="sigclip_vision_patch14_384.safetensors"
|
131 |
+
)
|
132 |
+
if CLIP_VISION is None:
|
133 |
+
raise ValueError("Falha ao carregar CLIP Vision model")
|
134 |
|
135 |
+
# Style Model
|
136 |
+
logger.info("Carregando Style Model...")
|
137 |
+
stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
|
138 |
+
STYLE_MODEL = stylemodelloader.load_style_model(
|
139 |
+
style_model_name="flux1-redux-dev.safetensors"
|
140 |
+
)
|
141 |
+
if STYLE_MODEL is None:
|
142 |
+
raise ValueError("Falha ao carregar Style Model")
|
143 |
|
144 |
+
# VAE
|
145 |
+
logger.info("Carregando VAE...")
|
146 |
+
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
147 |
+
VAE_MODEL = vaeloader.load_vae(
|
148 |
+
vae_name="ae.safetensors"
|
149 |
+
)
|
150 |
+
if VAE_MODEL is None:
|
151 |
+
raise ValueError("Falha ao carregar VAE model")
|
152 |
|
153 |
+
# UNET
|
154 |
+
logger.info("Carregando UNET...")
|
155 |
+
unetloader = NODE_CLASS_MAPPINGS["UNETLoader"]()
|
156 |
+
UNET_MODEL = unetloader.load_unet(
|
157 |
+
unet_name="flux1-dev.safetensors",
|
158 |
+
weight_dtype="fp8_e4m3fn" # Ajuste a seu hardware, se necessário
|
159 |
+
)
|
160 |
+
if UNET_MODEL is None:
|
161 |
+
raise ValueError("Falha ao carregar UNET model")
|
162 |
|
163 |
+
logger.info("Carregando modelos na GPU...")
|
164 |
+
model_loaders = [CLIP_MODEL, VAE_MODEL, CLIP_VISION, UNET_MODEL]
|
165 |
+
model_management.load_models_gpu([
|
166 |
+
loader[0].patcher if hasattr(loader[0], 'patcher') else loader[0]
|
167 |
+
for loader in model_loaders
|
168 |
+
])
|
169 |
+
logger.info("Modelos carregados com sucesso")
|
170 |
except Exception as e:
|
171 |
+
logger.error(f"Erro ao inicializar modelos: {str(e)}")
|
172 |
+
raise
|
173 |
|
174 |
# 9. Função de Geração
|
175 |
@spaces.GPU
|
176 |
+
def generate_image(
|
177 |
+
prompt, input_image, lora_weight, guidance, downsampling_factor,
|
178 |
+
weight, seed, width, height, batch_size, steps,
|
179 |
+
progress=gr.Progress(track_tqdm=True)
|
180 |
+
):
|
181 |
+
try:
|
182 |
+
# Aqui também: no_grad() para evitar cálculo de gradientes
|
183 |
+
with torch.no_grad():
|
184 |
+
logger.info(f"Iniciando geração com prompt: {prompt}")
|
185 |
|
186 |
+
# Codificar texto
|
187 |
+
cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
|
188 |
+
encoded_text = cliptextencode.encode(
|
189 |
+
text=prompt,
|
190 |
+
clip=CLIP_MODEL[0]
|
191 |
+
)
|
192 |
|
193 |
+
# Carregar e processar imagem
|
194 |
+
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
195 |
+
loaded_image = loadimage.load_image(image=input_image)
|
196 |
+
if loaded_image is None:
|
197 |
+
raise ValueError("Erro ao carregar a imagem de entrada")
|
198 |
+
logger.info("Imagem carregada com sucesso")
|
199 |
|
200 |
+
# Flux Guidance
|
201 |
+
fluxguidance = NODE_CLASS_MAPPINGS["FluxGuidance"]()
|
202 |
+
flux_guidance = fluxguidance.append(
|
203 |
+
guidance=guidance,
|
204 |
+
conditioning=encoded_text[0]
|
205 |
+
)
|
206 |
|
207 |
+
# Redux Advanced
|
208 |
+
reduxadvanced = NODE_CLASS_MAPPINGS["ReduxAdvanced"]()
|
209 |
+
redux_result = reduxadvanced.apply_stylemodel(
|
210 |
+
downsampling_factor=downsampling_factor,
|
211 |
+
downsampling_function="area",
|
212 |
+
mode="keep aspect ratio",
|
213 |
+
weight=weight,
|
214 |
+
conditioning=flux_guidance[0],
|
215 |
+
style_model=STYLE_MODEL[0],
|
216 |
+
clip_vision=CLIP_VISION[0],
|
217 |
+
image=loaded_image[0]
|
218 |
+
)
|
219 |
|
220 |
+
# Criar latente vazio
|
221 |
+
emptylatentimage = NODE_CLASS_MAPPINGS["EmptyLatentImage"]()
|
222 |
+
empty_latent = emptylatentimage.generate(
|
223 |
+
width=width,
|
224 |
+
height=height,
|
225 |
+
batch_size=batch_size
|
226 |
+
)
|
227 |
|
228 |
+
# KSampler
|
229 |
+
logger.info("Iniciando sampling...")
|
230 |
+
ksampler = NODE_CLASS_MAPPINGS["KSampler"]()
|
231 |
+
sampled = ksampler.sample(
|
232 |
+
seed=seed,
|
233 |
+
steps=steps,
|
234 |
+
cfg=1,
|
235 |
+
sampler_name="euler",
|
236 |
+
scheduler="simple",
|
237 |
+
denoise=1,
|
238 |
+
model=UNET_MODEL[0],
|
239 |
+
positive=redux_result[0],
|
240 |
+
negative=flux_guidance[0],
|
241 |
+
latent_image=empty_latent[0]
|
242 |
+
)
|
243 |
|
244 |
+
# VAE Decode
|
245 |
+
logger.info("Decodificando imagem...")
|
246 |
+
vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
|
247 |
+
decoded = vaedecode.decode(
|
248 |
+
samples=sampled[0],
|
249 |
+
vae=VAE_MODEL[0]
|
250 |
+
)
|
251 |
|
252 |
+
# Salvar imagem
|
253 |
+
temp_filename = f"Flux_{random.randint(0, 99999)}.png"
|
254 |
+
temp_path = os.path.join(output_dir, temp_filename)
|
255 |
+
try:
|
256 |
+
Image.fromarray((decoded[0] * 255).astype("uint8")).save(temp_path)
|
257 |
+
logger.info(f"Imagem salva em: {temp_path}")
|
258 |
+
return temp_path
|
259 |
+
except Exception as e:
|
260 |
+
logger.error(f"Erro ao salvar imagem: {str(e)}")
|
261 |
+
return None
|
262 |
|
263 |
+
except Exception as e:
|
264 |
+
logger.error(f"Erro ao gerar imagem: {str(e)}")
|
265 |
+
return None
|
266 |
|
267 |
# 10. Interface Gradio
|
268 |
with gr.Blocks() as app:
|
269 |
+
gr.Markdown("# FLUX Redux Image Generator")
|
270 |
|
271 |
+
with gr.Row():
|
272 |
+
with gr.Column():
|
273 |
+
prompt_input = gr.Textbox(
|
274 |
+
label="Prompt",
|
275 |
+
placeholder="Enter your prompt here...",
|
276 |
+
lines=5
|
277 |
+
)
|
278 |
+
input_image = gr.Image(
|
279 |
+
label="Input Image",
|
280 |
+
type="filepath"
|
281 |
+
)
|
282 |
|
283 |
+
with gr.Row():
|
284 |
+
with gr.Column():
|
285 |
+
lora_weight = gr.Slider(
|
286 |
+
minimum=0,
|
287 |
+
maximum=2,
|
288 |
+
step=0.1,
|
289 |
+
value=0.6,
|
290 |
+
label="LoRA Weight"
|
291 |
+
)
|
292 |
+
guidance = gr.Slider(
|
293 |
+
minimum=0,
|
294 |
+
maximum=20,
|
295 |
+
step=0.1,
|
296 |
+
value=3.5,
|
297 |
+
label="Guidance"
|
298 |
+
)
|
299 |
+
downsampling_factor = gr.Slider(
|
300 |
+
minimum=1,
|
301 |
+
maximum=8,
|
302 |
+
step=1,
|
303 |
+
value=3,
|
304 |
+
label="Downsampling Factor"
|
305 |
+
)
|
306 |
+
weight = gr.Slider(
|
307 |
+
minimum=0,
|
308 |
+
maximum=2,
|
309 |
+
step=0.1,
|
310 |
+
value=1.0,
|
311 |
+
label="Model Weight"
|
312 |
+
)
|
313 |
+
with gr.Column():
|
314 |
+
seed = gr.Number(
|
315 |
+
value=random.randint(1, 2**64),
|
316 |
+
label="Seed",
|
317 |
+
precision=0
|
318 |
+
)
|
319 |
+
width = gr.Number(
|
320 |
+
value=1024,
|
321 |
+
label="Width",
|
322 |
+
precision=0
|
323 |
+
)
|
324 |
+
height = gr.Number(
|
325 |
+
value=1024,
|
326 |
+
label="Height",
|
327 |
+
precision=0
|
328 |
+
)
|
329 |
+
batch_size = gr.Number(
|
330 |
+
value=1,
|
331 |
+
label="Batch Size",
|
332 |
+
precision=0
|
333 |
+
)
|
334 |
+
steps = gr.Number(
|
335 |
+
value=20,
|
336 |
+
label="Steps",
|
337 |
+
precision=0
|
338 |
+
)
|
339 |
|
340 |
+
generate_btn = gr.Button("Generate Image")
|
341 |
|
342 |
+
with gr.Column():
|
343 |
+
output_image = gr.Image(label="Generated Image", type="filepath")
|
344 |
|
345 |
+
generate_btn.click(
|
346 |
+
fn=generate_image,
|
347 |
+
inputs=[
|
348 |
+
prompt_input,
|
349 |
+
input_image,
|
350 |
+
lora_weight,
|
351 |
+
guidance,
|
352 |
+
downsampling_factor,
|
353 |
+
weight,
|
354 |
+
seed,
|
355 |
+
width,
|
356 |
+
height,
|
357 |
+
batch_size,
|
358 |
+
steps
|
359 |
+
],
|
360 |
+
outputs=[output_image]
|
361 |
+
)
|
362 |
|
363 |
if __name__ == "__main__":
|
364 |
+
# Ajuste caso queira compartilhar publicamente, exemplo: app.launch(server_name="0.0.0.0", share=True)
|
365 |
+
app.launch()
|