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