Update app.py
Browse files1234562 unmodified lines910111213141518 unmodified lines343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158import osimport torchfrom diffusers import StableDiffusionPipeline, UNet2DConditionModelfrom peft import LoraConfig, get_peft_modelfrom transformers import CLIPTextModelfrom PIL import Image2 unmodified linesimport gradio as grimport safetensors.torch# Configurações básicasMODEL_NAME = "runwayml/stable-diffusion-v1-5"OUTPUT_DIR = "lora_output"os.makedirs(OUTPUT_DIR, exist_ok=True)18 unmodified linesimage = self.transform(image)return {"pixel_values": image, "caption": self.caption}def train_lora(images, trigger_word, num_epochs=10, learning_rate=1e-4, lora_rank=4, batch_size=1):device = "cuda" if torch.cuda.is_available() else "cpu"# Carrega o modelopipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)pipe.to(device)# Configura LoRA no UNetunet_lora_config = LoraConfig(r=lora_rank,lora_alpha=lora_rank,target_modules=["to_q", "to_v", "to_k", "to_out.0"],lora_dropout=0.0,bias="none",)pipe.unet = get_peft_model(pipe.unet, unet_lora_config)# Configura LoRA no Text Encoder (opcional, mas recomendado)text_encoder_lora_config = LoraConfig(r=lora_rank,lora_alpha=lora_rank,target_modules=["q_proj", "v_proj"],lora_dropout=0.0,bias="none",)pipe.text_encoder = get_peft_model(pipe.text_encoder, text_encoder_lora_config)# Prepara datasetimage_paths = [img.name for img in images]dataset = ImageDataset(image_paths, f"a photo of {trigger_word}")dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)# Otimizadorparams_to_optimize = (list(pipe.unet.parameters()) + list(pipe.text_encoder.parameters()))optimizer = torch.optim.AdamW(params_to_optimize, lr=learning_rate)# Treinamentopipe.unet.train()pipe.text_encoder.train()for epoch in range(num_epochs):for batch in dataloader:optimizer.zero_grad()# Encode textotext_inputs = pipe.tokenizer(batch["caption"],padding="max_length",max_length=pipe.tokenizer.model_max_length,truncation=True,return_tensors="pt",)text_input_ids = text_inputs.input_ids.to(device)encoder_hidden_states = pipe.text_encoder(text_input_ids)[0]# Encode imagem (latentes)latents = pipe.vae.encode(batch["pixel_values"].to(device, dtype=torch.float16)).latent_dist.sample()latents = latents * 0.18215# Simula timestep e ruído (simplificado para demonstração)noise = torch.randn_like(latents)timesteps = torch.randint(0, 1000, (latents.shape[0],), device=latents.device).long()noisy_latents = pipe.scheduler.add_noise(latents, noise, timesteps)# Prediçãonoise_pred = pipe.unet(noisy_latents, timesteps, encoder_hidden_states).sample# Loss e backwardloss = torch.nn.functional.mse_loss(noise_pred, noise)loss.backward()optimizer.step()print(f"Epoch {epoch+1}/{num_epochs} - Loss: {loss.item():.4f}")# Salva LoRAlora_weights = {}for name, module in pipe.unet.named_modules():if hasattr(module, "lora_A"):lora_weights[f"lora_unet_{name}.lora_A.weight"] = module.lora_A.default.weightlora_weights[f"lora_unet_{name}.lora_B.weight"] = module.lora_B.default.weightfor name, module in pipe.text_encoder.named_modules():if hasattr(module, "lora_A"):lora_weights[f"lora_te_{name}.lora_A.weight"] = module.lora_A.default.weightlora_weights[f"lora_te_{name}.lora_B.weight"] = module.lora_B.default.weightlora_path = os.path.join(OUTPUT_DIR, "lora_model.safetensors")safetensors.torch.save_file(lora_weights, lora_path)del pipetorch.cuda.empty_cache()return lora_path# Interface Gradiowith gr.Blocks(title="Treinador LoRA Simplificado") as demo:gr.Markdown("# 🧠 Treinador LoRA para Stable Diffusion (Hugging Face)")gr.Markdown("Faça upload de 3-10 imagens do mesmo conceito. Use um 'trigger word' único (ex: `shs_dog`).")with gr.Row():with gr.Column():image_input = gr.File(label="📁 Faça upload das imagens (JPG/PNG)", file_count="multiple", file_types=["image"])trigger_word = gr.Textbox(label="🔤 Trigger Word (ex: my_cat)", placeholder="shs_dog")epochs = gr.Slider(1, 50, value=10, step=1, label="🔁 Número de Epochs")lr = gr.Number(value=1e-4, label="📈 Taxa de Aprendizado")rank = gr.Slider(2, 32, value=4, step=2, label="📊 Rank da LoRA")batch = gr.Slider(1, 4, value=1, step=1, label="📦 Batch Size (mantenha 1 no HF)")train_btn = gr.Button("🚀 Iniciar Treinamento", variant="primary")with gr.Column():output_file = gr.File(label="💾 Download da LoRA Treinada (.safetensors)")log_box = gr.Textbox(label="📋 Log de Treinamento", lines=10)train_btn.click(fn=train_lora,inputs=[image_input, trigger_word, epochs, lr, rank, batch],outputs=output_file)demo.launch()1234562 unmodified lines910111213141518 unmodified lines343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182import osimport torchfrom diffusers import StableDiffusionPipelinefrom peft import LoraConfig, get_peft_modelfrom transformers import CLIPTextModelfrom PIL import Image2 unmodified linesimport gradio as grimport safetensors.torch# ConfiguraçõesMODEL_NAME = "runwayml/stable-diffusion-v1-5"OUTPUT_DIR = "lora_output"os.makedirs(OUTPUT_DIR, exist_ok=True)18 unmodified linesimage = self.transform(image)return {"pixel_values": image, "caption": self.caption}def train_lora(images, trigger_word, num_epochs=5, learning_rate=1e-4, lora_rank=4):try:device = "cuda" if torch.cuda.is_available() else "cpu"print(f"Usando dispositivo: {device}")# Carrega modelo com half precision para economizar memóriapipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME,torch_dtype=torch.float16,safety_checker=None,requires_safety_checker=False).to(device)# Ativa LoRA no UNetunet_lora_config = LoraConfig(r=lora_rank,lora_alpha=lora_rank,target_modules=["to_q", "to_v", "to_k", "to_out.0"],lora_dropout=0.0,bias="none",)pipe.unet.add_adapter(unet_lora_config)pipe.unet.enable_adapters()# Ativa LoRA no Text Encodertext_encoder_lora_config = LoraConfig(r=lora_rank,lora_alpha=lora_rank,target_modules=["q_proj", "v_proj"],lora_dropout=0.0,bias="none",)pipe.text_encoder.add_adapter(text_encoder_lora_config)pipe.text_encoder.enable_adapters()# Prepara datasetimage_paths = [img.name for img in images]if not image_paths:raise ValueError("Nenhuma imagem foi enviada.")dataset = ImageDataset(image_paths, f"a photo of {trigger_word}")dataloader = DataLoader(dataset, batch_size=1, shuffle=True)# Otimizadorparams_to_optimize = (list(pipe.unet.parameters()) + list(pipe.text_encoder.parameters()))optimizer = torch.optim.AdamW(params_to_optimize, lr=learning_rate)# Treinamento simplificadopipe.unet.train()pipe.text_encoder.train()for epoch in range(num_epochs):total_loss = 0.0for step, batch in enumerate(dataloader):optimizer.zero_grad()# Textotext_inputs = pipe.tokenizer(batch["caption"],padding="max_length",max_length=pipe.tokenizer.model_max_length,truncation=True,return_tensors="pt",)text_input_ids = text_inputs.input_ids.to(device)encoder_hidden_states = pipe.text_encoder(text_input_ids)[0]# Imagem → latentespixel_values = batch["pixel_values"].to(device, dtype=torch.float16)latents = pipe.vae.encode(pixel_values).latent_dist.sample()latents = latents * 0.18215# Adiciona ruídonoise = torch.randn_like(latents)timesteps = torch.randint(0, 1000, (latents.shape[0],), device=latents.device).long()noisy_latents = pipe.scheduler.add_noise(latents, noise, timesteps)# Prediz o ruídonoise_pred = pipe.unet(noisy_latents, timesteps, encoder_hidden_states).sampleloss = torch.nn.functional.mse_loss(noise_pred, noise)loss.backward()optimizer.step()total_loss += loss.item()print(f"Epoch {epoch+1}, Step {step+1}, Loss: {loss.item():.4f}")avg_loss = total_loss / len(dataloader)print(f"Epoch {epoch+1}/{num_epochs} finalizado. Loss média: {avg_loss:.4f}")# Salva pesos da LoRAlora_weights = {}# UNetfor name, module in pipe.unet.named_modules():if hasattr(module, "lora_A") and hasattr(module, "lora_B"):lora_weights[f"lora_unet_{name}.lora_A.weight"] = module.lora_A["default"].weightlora_weights[f"lora_unet_{name}.lora_B.weight"] = module.lora_B["default"].weight# Text Encoderfor name, module in pipe.text_encoder.named_modules():if hasattr(module, "lora_A") and hasattr(module, "lora_B"):lora_weights[f"lora_te_{name}.lora_A.weight"] = module.lora_A["default"].weightlora_weights[f"lora_te_{name}.lora_B.weight"] = module.lora_B["default"].weight# Salvalora_path = os.path.join(OUTPUT_DIR, "lora_model.safetensors")safetensors.torch.save_file(lora_weights, lora_path)# Libera memóriadel pipe, optimizer, dataloader, datasettorch.cuda.empty_cache()return lora_pathexcept Exception as e:error_msg = f"Erro durante o treinamento: {str(e)}"print(error_msg)raise gr.Error(error_msg)# Interfacewith gr.Blocks(title="Treinador LoRA HF") as demo:gr.Markdown("# 🧠 Treinador LoRA para Stable Diffusion")gr.Markdown("Envie 3-8 imagens do mesmo objeto. Use um trigger word único (ex: `my_cat`).")with gr.Row():with gr.Column():image_input = gr.File(label="📁 Upload de Imagens (JPG/PNG)", file_count="multiple", file_types=["image"])trigger_word = gr.Textbox(label="🔤 Trigger Word", placeholder="ex: my_dog")epochs = gr.Slider(1, 10, value=3, step=1, label="🔁 Epochs (recomendado: 3-5)")lr = gr.Number(value=1e-4, label="📈 Learning Rate", precision=6)rank = gr.Slider(2, 16, value=4, step=2, label="📊 LoRA Rank")train_btn = gr.Button("🚀 Treinar LoRA", variant="primary")with gr.Column():output_file = gr.File(label="💾 Download LoRA (.safetensors)")train_btn.click(fn=train_lora,inputs=[image_input, trigger_word, epochs, lr, rank],outputs=output_file)if __name__ == "__main__":demo.launch()
app.py+138-114