Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from PIL import Image
|
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from nodes import NODE_CLASS_MAPPINGS
|
|
|
|
| 9 |
import folder_paths
|
| 10 |
|
| 11 |
# Diretório base e de saída
|
|
@@ -14,39 +15,61 @@ output_dir = os.path.join(BASE_DIR, "output")
|
|
| 14 |
os.makedirs(output_dir, exist_ok=True)
|
| 15 |
folder_paths.set_output_directory(output_dir)
|
| 16 |
|
| 17 |
-
# Baixar os modelos necessários
|
| 18 |
-
|
| 19 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
| 20 |
-
filename="flux1-redux-dev.safetensors",
|
| 21 |
-
local_dir="models/style_models")
|
| 22 |
|
| 23 |
hf_hub_download(repo_id="comfyanonymous/flux_text_encoders",
|
| 24 |
-
filename="t5xxl_fp16.safetensors",
|
| 25 |
-
local_dir="models/text_encoders")
|
| 26 |
|
| 27 |
hf_hub_download(repo_id="zer0int/CLIP-GmP-ViT-L-14",
|
| 28 |
-
filename="ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors",
|
| 29 |
-
local_dir="models/text_encoders")
|
| 30 |
|
| 31 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev",
|
| 32 |
-
filename="ae.safetensors",
|
| 33 |
-
local_dir="models/vae")
|
| 34 |
|
| 35 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev",
|
| 36 |
-
filename="flux1-dev.safetensors.safetensors",
|
| 37 |
-
local_dir="models/diffusion_models")
|
| 38 |
|
| 39 |
hf_hub_download(repo_id="google/siglip-so400m-patch14-384",
|
| 40 |
-
filename="model.safetensors",
|
| 41 |
-
local_dir="models/clip_vision")
|
| 42 |
|
| 43 |
hf_hub_download(repo_id="nftnik/NFTNIK-FLUX.1-dev-LoRA",
|
| 44 |
-
filename="NFTNIK_FLUX.1[dev]_LoRA.safetensors",
|
| 45 |
-
local_dir="models/lora")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Função para importar nodes personalizados
|
| 48 |
def import_custom_nodes():
|
| 49 |
-
"""Carregar nodes customizados."""
|
| 50 |
import asyncio
|
| 51 |
import execution
|
| 52 |
from nodes import init_extra_nodes
|
|
@@ -62,49 +85,32 @@ def import_custom_nodes():
|
|
| 62 |
# Função principal de geração
|
| 63 |
def generate_image(prompt, input_image, lora_weight, guidance, downsampling_factor, weight, seed, width, height, batch_size, steps):
|
| 64 |
import_custom_nodes()
|
| 65 |
-
|
| 66 |
try:
|
| 67 |
with torch.inference_mode():
|
| 68 |
-
# Carregar CLIP
|
| 69 |
-
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
| 70 |
-
dualcliploader_loaded = dualcliploader.load_clip(
|
| 71 |
-
clip_name1="models/text_encoders/t5xxl_fp16.safetensors",
|
| 72 |
-
clip_name2="models/clip_vision/ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors",
|
| 73 |
-
type="flux"
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
# Codificar texto
|
| 77 |
cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
|
| 78 |
encoded_text = cliptextencode.encode(
|
| 79 |
text=prompt,
|
| 80 |
-
clip=
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# Carregar
|
| 84 |
-
stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
|
| 85 |
-
style_model = stylemodelloader.load_style_model(
|
| 86 |
-
style_model_name="models/style_models/flux1-redux-dev.safetensors"
|
| 87 |
-
)
|
| 88 |
loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
|
| 89 |
lora_model = loraloadermodelonly.load_lora_model_only(
|
| 90 |
lora_name="models/lora/NFTNIK_FLUX.1[dev]_LoRA.safetensors",
|
| 91 |
strength_model=lora_weight,
|
| 92 |
-
model=
|
| 93 |
)
|
| 94 |
|
| 95 |
# Processar imagem de entrada
|
| 96 |
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
| 97 |
loaded_image = loadimage.load_image(image=input_image)
|
| 98 |
|
| 99 |
-
# Configurações adicionais e saída
|
| 100 |
-
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
| 101 |
-
vae = vaeloader.load_vae(vae_name="models/vae/ae.safetensors")
|
| 102 |
-
|
| 103 |
# Decodificar e salvar
|
| 104 |
vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
|
| 105 |
decoded = vaedecode.decode(
|
| 106 |
samples=lora_model[0],
|
| 107 |
-
vae=
|
| 108 |
)
|
| 109 |
|
| 110 |
temp_filename = f"Flux_{random.randint(0, 99999)}.png"
|
|
@@ -124,14 +130,6 @@ with gr.Blocks() as app:
|
|
| 124 |
prompt_input = gr.Textbox(label="Prompt", placeholder="Digite seu prompt aqui...", lines=5)
|
| 125 |
input_image = gr.Image(label="Imagem de Entrada", type="filepath")
|
| 126 |
lora_weight = gr.Slider(minimum=0, maximum=2, step=0.1, value=0.6, label="Peso LoRA")
|
| 127 |
-
guidance = gr.Slider(minimum=0, maximum=20, step=0.1, value=3.5, label="Orientação")
|
| 128 |
-
downsampling_factor = gr.Slider(minimum=1, maximum=8, step=1, value=3, label="Fator de Redução")
|
| 129 |
-
weight = gr.Slider(minimum=0, maximum=2, step=0.1, value=1.0, label="Peso do Modelo")
|
| 130 |
-
seed = gr.Number(value=random.randint(1, 2**64), label="Seed", precision=0)
|
| 131 |
-
width = gr.Number(value=1024, label="Largura", precision=0)
|
| 132 |
-
height = gr.Number(value=1024, label="Altura", precision=0)
|
| 133 |
-
batch_size = gr.Number(value=1, label="Tamanho do Lote", precision=0)
|
| 134 |
-
steps = gr.Number(value=20, label="Etapas", precision=0)
|
| 135 |
generate_btn = gr.Button("Gerar Imagem")
|
| 136 |
|
| 137 |
with gr.Column():
|
|
@@ -139,19 +137,7 @@ with gr.Blocks() as app:
|
|
| 139 |
|
| 140 |
generate_btn.click(
|
| 141 |
fn=generate_image,
|
| 142 |
-
inputs=[
|
| 143 |
-
prompt_input,
|
| 144 |
-
input_image,
|
| 145 |
-
lora_weight,
|
| 146 |
-
guidance,
|
| 147 |
-
downsampling_factor,
|
| 148 |
-
weight,
|
| 149 |
-
seed,
|
| 150 |
-
width,
|
| 151 |
-
height,
|
| 152 |
-
batch_size,
|
| 153 |
-
steps
|
| 154 |
-
],
|
| 155 |
outputs=[output_image]
|
| 156 |
)
|
| 157 |
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from nodes import NODE_CLASS_MAPPINGS
|
| 9 |
+
from comfy import model_management
|
| 10 |
import folder_paths
|
| 11 |
|
| 12 |
# Diretório base e de saída
|
|
|
|
| 15 |
os.makedirs(output_dir, exist_ok=True)
|
| 16 |
folder_paths.set_output_directory(output_dir)
|
| 17 |
|
| 18 |
+
# Baixar e carregar os modelos necessários
|
|
|
|
| 19 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-Redux-dev",
|
| 20 |
+
filename="flux1-redux-dev.safetensors",
|
| 21 |
+
local_dir="models/style_models")
|
| 22 |
|
| 23 |
hf_hub_download(repo_id="comfyanonymous/flux_text_encoders",
|
| 24 |
+
filename="t5xxl_fp16.safetensors",
|
| 25 |
+
local_dir="models/text_encoders")
|
| 26 |
|
| 27 |
hf_hub_download(repo_id="zer0int/CLIP-GmP-ViT-L-14",
|
| 28 |
+
filename="ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors",
|
| 29 |
+
local_dir="models/text_encoders")
|
| 30 |
|
| 31 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev",
|
| 32 |
+
filename="ae.safetensors",
|
| 33 |
+
local_dir="models/vae")
|
| 34 |
|
| 35 |
hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev",
|
| 36 |
+
filename="flux1-dev.safetensors.safetensors",
|
| 37 |
+
local_dir="models/diffusion_models")
|
| 38 |
|
| 39 |
hf_hub_download(repo_id="google/siglip-so400m-patch14-384",
|
| 40 |
+
filename="model.safetensors",
|
| 41 |
+
local_dir="models/clip_vision")
|
| 42 |
|
| 43 |
hf_hub_download(repo_id="nftnik/NFTNIK-FLUX.1-dev-LoRA",
|
| 44 |
+
filename="NFTNIK_FLUX.1[dev]_LoRA.safetensors",
|
| 45 |
+
local_dir="models/lora")
|
| 46 |
+
|
| 47 |
+
# Inicializar os nós e pré-carregar os modelos
|
| 48 |
+
intconstant = NODE_CLASS_MAPPINGS["INTConstant"]()
|
| 49 |
+
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
| 50 |
+
dualcliploader_357 = dualcliploader.load_clip(
|
| 51 |
+
clip_name1="models/text_encoders/t5xxl_fp16.safetensors",
|
| 52 |
+
clip_name2="models/text_encoders/ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors",
|
| 53 |
+
type="flux",
|
| 54 |
+
)
|
| 55 |
+
stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
|
| 56 |
+
stylemodelloader_441 = stylemodelloader.load_style_model(
|
| 57 |
+
style_model_name="models/style_models/flux1-redux-dev.safetensors"
|
| 58 |
+
)
|
| 59 |
+
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
| 60 |
+
vaeloader_359 = vaeloader.load_vae(vae_name="models/vae/ae.safetensors")
|
| 61 |
+
|
| 62 |
+
# Lista de modelos para carregamento na GPU
|
| 63 |
+
model_loaders = [dualcliploader_357, vaeloader_359, stylemodelloader_441]
|
| 64 |
+
valid_models = [
|
| 65 |
+
getattr(loader[0], 'patcher', loader[0])
|
| 66 |
+
for loader in model_loaders
|
| 67 |
+
if not isinstance(loader[0], dict) and not isinstance(getattr(loader[0], 'patcher', None), dict)
|
| 68 |
+
]
|
| 69 |
+
model_management.load_models_gpu(valid_models)
|
| 70 |
|
| 71 |
# Função para importar nodes personalizados
|
| 72 |
def import_custom_nodes():
|
|
|
|
| 73 |
import asyncio
|
| 74 |
import execution
|
| 75 |
from nodes import init_extra_nodes
|
|
|
|
| 85 |
# Função principal de geração
|
| 86 |
def generate_image(prompt, input_image, lora_weight, guidance, downsampling_factor, weight, seed, width, height, batch_size, steps):
|
| 87 |
import_custom_nodes()
|
|
|
|
| 88 |
try:
|
| 89 |
with torch.inference_mode():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Codificar texto
|
| 91 |
cliptextencode = NODE_CLASS_MAPPINGS["CLIPTextEncode"]()
|
| 92 |
encoded_text = cliptextencode.encode(
|
| 93 |
text=prompt,
|
| 94 |
+
clip=dualcliploader_357[0]
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Carregar LoRA
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
|
| 99 |
lora_model = loraloadermodelonly.load_lora_model_only(
|
| 100 |
lora_name="models/lora/NFTNIK_FLUX.1[dev]_LoRA.safetensors",
|
| 101 |
strength_model=lora_weight,
|
| 102 |
+
model=stylemodelloader_441[0]
|
| 103 |
)
|
| 104 |
|
| 105 |
# Processar imagem de entrada
|
| 106 |
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
| 107 |
loaded_image = loadimage.load_image(image=input_image)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Decodificar e salvar
|
| 110 |
vaedecode = NODE_CLASS_MAPPINGS["VAEDecode"]()
|
| 111 |
decoded = vaedecode.decode(
|
| 112 |
samples=lora_model[0],
|
| 113 |
+
vae=vaeloader_359[0]
|
| 114 |
)
|
| 115 |
|
| 116 |
temp_filename = f"Flux_{random.randint(0, 99999)}.png"
|
|
|
|
| 130 |
prompt_input = gr.Textbox(label="Prompt", placeholder="Digite seu prompt aqui...", lines=5)
|
| 131 |
input_image = gr.Image(label="Imagem de Entrada", type="filepath")
|
| 132 |
lora_weight = gr.Slider(minimum=0, maximum=2, step=0.1, value=0.6, label="Peso LoRA")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
generate_btn = gr.Button("Gerar Imagem")
|
| 134 |
|
| 135 |
with gr.Column():
|
|
|
|
| 137 |
|
| 138 |
generate_btn.click(
|
| 139 |
fn=generate_image,
|
| 140 |
+
inputs=[prompt_input, input_image, lora_weight],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
outputs=[output_image]
|
| 142 |
)
|
| 143 |
|