Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,24 @@ from huggingface_hub import hf_hub_download
|
|
| 9 |
import spaces
|
| 10 |
from typing import Union, Sequence, Mapping, Any
|
| 11 |
|
| 12 |
-
#
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
comfyui_path = os.path.join(current_dir, "ComfyUI")
|
| 15 |
sys.path.append(comfyui_path)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
import folder_paths
|
| 19 |
from nodes import NODE_CLASS_MAPPINGS, init_extra_nodes
|
| 20 |
-
import comfy.utils
|
| 21 |
|
| 22 |
-
# Configuração
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
print("Python version:", sys.version)
|
| 24 |
print("Torch version:", torch.__version__)
|
| 25 |
print("CUDA disponível:", torch.cuda.is_available())
|
|
@@ -27,70 +34,68 @@ print("Quantidade de GPUs:", torch.cuda.device_count())
|
|
| 27 |
if torch.cuda.is_available():
|
| 28 |
print("GPU atual:", torch.cuda.get_device_name(0))
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
|
| 32 |
-
output_dir = os.path.join(BASE_DIR, "output")
|
| 33 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 34 |
-
folder_paths.set_output_directory(output_dir)
|
| 35 |
-
|
| 36 |
-
# Inicializar nós extras
|
| 37 |
print("Inicializando ComfyUI...")
|
| 38 |
init_extra_nodes()
|
| 39 |
|
| 40 |
-
# Helper
|
| 41 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
| 42 |
try:
|
| 43 |
return obj[index]
|
| 44 |
except KeyError:
|
| 45 |
return obj["result"][index]
|
| 46 |
|
| 47 |
-
#
|
| 48 |
def download_models():
|
| 49 |
print("Baixando modelos...")
|
| 50 |
models = [
|
| 51 |
-
("black-forest-labs/FLUX.1-Redux-dev", "flux1-redux-dev.safetensors", "
|
| 52 |
-
("comfyanonymous/flux_text_encoders", "t5xxl_fp16.safetensors", "
|
| 53 |
-
("zer0int/CLIP-GmP-ViT-L-14", "ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors", "
|
| 54 |
-
("black-forest-labs/FLUX.1-dev", "ae.safetensors", "
|
| 55 |
-
("black-forest-labs/FLUX.1-dev", "flux1-dev.safetensors", "
|
| 56 |
-
("google/siglip-so400m-patch14-384", "model.safetensors", "
|
| 57 |
]
|
| 58 |
|
| 59 |
-
for repo_id, filename,
|
| 60 |
try:
|
| 61 |
-
os.
|
|
|
|
| 62 |
print(f"Baixando {filename} de {repo_id}...")
|
| 63 |
-
hf_hub_download(repo_id=repo_id, filename=filename, local_dir=
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Erro ao baixar {filename} de {repo_id}: {str(e)}")
|
| 66 |
continue
|
| 67 |
|
| 68 |
-
# Download
|
|
|
|
| 69 |
download_models()
|
| 70 |
|
| 71 |
-
# Inicializar modelos
|
| 72 |
print("Inicializando modelos...")
|
| 73 |
with torch.inference_mode():
|
| 74 |
# CLIP
|
| 75 |
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
| 76 |
dualcliploader_357 = dualcliploader.load_clip(
|
| 77 |
-
clip_name1="
|
| 78 |
-
clip_name2="
|
| 79 |
-
type="flux"
|
| 80 |
)
|
| 81 |
|
| 82 |
# Style Model
|
| 83 |
stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
|
| 84 |
stylemodelloader_441 = stylemodelloader.load_style_model(
|
| 85 |
-
style_model_name="
|
| 86 |
)
|
| 87 |
|
| 88 |
# VAE
|
| 89 |
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
| 90 |
vaeloader_359 = vaeloader.load_vae(
|
| 91 |
-
vae_name="
|
| 92 |
)
|
| 93 |
|
|
|
|
| 94 |
@spaces.GPU
|
| 95 |
def generate_image(prompt, input_image, lora_weight, guidance, downsampling_factor, weight, seed, width, height, batch_size, steps, progress=gr.Progress(track_tqdm=True)):
|
| 96 |
try:
|
|
@@ -113,14 +118,6 @@ def generate_image(prompt, input_image, lora_weight, guidance, downsampling_fact
|
|
| 113 |
conditioning=encoded_text[0]
|
| 114 |
)
|
| 115 |
|
| 116 |
-
# Carregar LoRA
|
| 117 |
-
loraloadermodelonly = NODE_CLASS_MAPPINGS["LoraLoaderModelOnly"]()
|
| 118 |
-
lora_model = loraloadermodelonly.load_lora_model_only(
|
| 119 |
-
lora_name="models/lora/NFTNIK_FLUX.1[dev]_LoRA.safetensors",
|
| 120 |
-
strength_model=lora_weight,
|
| 121 |
-
model=stylemodelloader_441[0]
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
# Redux Advanced
|
| 125 |
reduxadvanced = NODE_CLASS_MAPPINGS["ReduxAdvanced"]()
|
| 126 |
redux_result = reduxadvanced.apply_stylemodel(
|
|
@@ -150,7 +147,7 @@ def generate_image(prompt, input_image, lora_weight, guidance, downsampling_fact
|
|
| 150 |
sampler_name="euler",
|
| 151 |
scheduler="simple",
|
| 152 |
denoise=1,
|
| 153 |
-
model=
|
| 154 |
positive=redux_result[0],
|
| 155 |
negative=flux_guidance[0],
|
| 156 |
latent_image=empty_latent[0]
|
|
@@ -173,7 +170,7 @@ def generate_image(prompt, input_image, lora_weight, guidance, downsampling_fact
|
|
| 173 |
print(f"Erro ao gerar imagem: {str(e)}")
|
| 174 |
return None
|
| 175 |
|
| 176 |
-
# Interface Gradio
|
| 177 |
with gr.Blocks() as app:
|
| 178 |
gr.Markdown("# FLUX Redux Image Generator")
|
| 179 |
|
|
@@ -249,7 +246,7 @@ with gr.Blocks() as app:
|
|
| 249 |
generate_btn = gr.Button("Generate Image")
|
| 250 |
|
| 251 |
with gr.Column():
|
| 252 |
-
output_image = gr.Image(label="Generated Image", type="
|
| 253 |
|
| 254 |
generate_btn.click(
|
| 255 |
fn=generate_image,
|
|
|
|
| 9 |
import spaces
|
| 10 |
from typing import Union, Sequence, Mapping, Any
|
| 11 |
|
| 12 |
+
# 1. Configuração de Caminhos e Imports
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
comfyui_path = os.path.join(current_dir, "ComfyUI")
|
| 15 |
sys.path.append(comfyui_path)
|
| 16 |
|
| 17 |
+
# 2. Imports do ComfyUI
|
| 18 |
import folder_paths
|
| 19 |
from nodes import NODE_CLASS_MAPPINGS, init_extra_nodes
|
|
|
|
| 20 |
|
| 21 |
+
# 3. Configuração de Diretórios
|
| 22 |
+
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
|
| 23 |
+
output_dir = os.path.join(BASE_DIR, "output")
|
| 24 |
+
models_dir = os.path.join(BASE_DIR, "models")
|
| 25 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 26 |
+
os.makedirs(models_dir, exist_ok=True)
|
| 27 |
+
folder_paths.set_output_directory(output_dir)
|
| 28 |
+
|
| 29 |
+
# 4. Diagnóstico CUDA
|
| 30 |
print("Python version:", sys.version)
|
| 31 |
print("Torch version:", torch.__version__)
|
| 32 |
print("CUDA disponível:", torch.cuda.is_available())
|
|
|
|
| 34 |
if torch.cuda.is_available():
|
| 35 |
print("GPU atual:", torch.cuda.get_device_name(0))
|
| 36 |
|
| 37 |
+
# 5. Inicialização do ComfyUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
print("Inicializando ComfyUI...")
|
| 39 |
init_extra_nodes()
|
| 40 |
|
| 41 |
+
# 6. Helper Functions
|
| 42 |
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any:
|
| 43 |
try:
|
| 44 |
return obj[index]
|
| 45 |
except KeyError:
|
| 46 |
return obj["result"][index]
|
| 47 |
|
| 48 |
+
# 7. Download de Modelos
|
| 49 |
def download_models():
|
| 50 |
print("Baixando modelos...")
|
| 51 |
models = [
|
| 52 |
+
("black-forest-labs/FLUX.1-Redux-dev", "flux1-redux-dev.safetensors", "style_models"),
|
| 53 |
+
("comfyanonymous/flux_text_encoders", "t5xxl_fp16.safetensors", "text_encoders"),
|
| 54 |
+
("zer0int/CLIP-GmP-ViT-L-14", "ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors", "text_encoders"),
|
| 55 |
+
("black-forest-labs/FLUX.1-dev", "ae.safetensors", "vae"),
|
| 56 |
+
("black-forest-labs/FLUX.1-dev", "flux1-dev.safetensors", "diffusion_models"),
|
| 57 |
+
("google/siglip-so400m-patch14-384", "model.safetensors", "clip_vision")
|
| 58 |
]
|
| 59 |
|
| 60 |
+
for repo_id, filename, model_type in models:
|
| 61 |
try:
|
| 62 |
+
model_dir = os.path.join(models_dir, model_type)
|
| 63 |
+
os.makedirs(model_dir, exist_ok=True)
|
| 64 |
print(f"Baixando {filename} de {repo_id}...")
|
| 65 |
+
hf_hub_download(repo_id=repo_id, filename=filename, local_dir=model_dir)
|
| 66 |
+
# Adicionar o diretório ao folder_paths
|
| 67 |
+
folder_paths.add_model_folder_path(model_type, model_dir)
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Erro ao baixar {filename} de {repo_id}: {str(e)}")
|
| 70 |
continue
|
| 71 |
|
| 72 |
+
# 8. Download e Inicialização dos Modelos
|
| 73 |
+
print("Baixando modelos...")
|
| 74 |
download_models()
|
| 75 |
|
|
|
|
| 76 |
print("Inicializando modelos...")
|
| 77 |
with torch.inference_mode():
|
| 78 |
# CLIP
|
| 79 |
dualcliploader = NODE_CLASS_MAPPINGS["DualCLIPLoader"]()
|
| 80 |
dualcliploader_357 = dualcliploader.load_clip(
|
| 81 |
+
clip_name1="t5xxl_fp16.safetensors",
|
| 82 |
+
clip_name2="ViT-L-14-TEXT-detail-improved-hiT-GmP-HF.safetensors",
|
| 83 |
+
type="flux"
|
| 84 |
)
|
| 85 |
|
| 86 |
# Style Model
|
| 87 |
stylemodelloader = NODE_CLASS_MAPPINGS["StyleModelLoader"]()
|
| 88 |
stylemodelloader_441 = stylemodelloader.load_style_model(
|
| 89 |
+
style_model_name="flux1-redux-dev.safetensors"
|
| 90 |
)
|
| 91 |
|
| 92 |
# VAE
|
| 93 |
vaeloader = NODE_CLASS_MAPPINGS["VAELoader"]()
|
| 94 |
vaeloader_359 = vaeloader.load_vae(
|
| 95 |
+
vae_name="ae.safetensors"
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# 9. Função de Geração
|
| 99 |
@spaces.GPU
|
| 100 |
def generate_image(prompt, input_image, lora_weight, guidance, downsampling_factor, weight, seed, width, height, batch_size, steps, progress=gr.Progress(track_tqdm=True)):
|
| 101 |
try:
|
|
|
|
| 118 |
conditioning=encoded_text[0]
|
| 119 |
)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# Redux Advanced
|
| 122 |
reduxadvanced = NODE_CLASS_MAPPINGS["ReduxAdvanced"]()
|
| 123 |
redux_result = reduxadvanced.apply_stylemodel(
|
|
|
|
| 147 |
sampler_name="euler",
|
| 148 |
scheduler="simple",
|
| 149 |
denoise=1,
|
| 150 |
+
model=stylemodelloader_441[0],
|
| 151 |
positive=redux_result[0],
|
| 152 |
negative=flux_guidance[0],
|
| 153 |
latent_image=empty_latent[0]
|
|
|
|
| 170 |
print(f"Erro ao gerar imagem: {str(e)}")
|
| 171 |
return None
|
| 172 |
|
| 173 |
+
# 10. Interface Gradio
|
| 174 |
with gr.Blocks() as app:
|
| 175 |
gr.Markdown("# FLUX Redux Image Generator")
|
| 176 |
|
|
|
|
| 246 |
generate_btn = gr.Button("Generate Image")
|
| 247 |
|
| 248 |
with gr.Column():
|
| 249 |
+
output_image = gr.Image(label="Generated Image", type="filepath")
|
| 250 |
|
| 251 |
generate_btn.click(
|
| 252 |
fn=generate_image,
|