Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,10 @@ from PIL import Image
|
|
| 4 |
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
-
# 1. Definici贸n del modelo s煤per ligero (
|
| 8 |
MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 9 |
|
| 10 |
-
print("
|
| 11 |
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
torch_dtype=torch.float16,
|
|
@@ -15,34 +15,35 @@ pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
|
| 15 |
use_safetensors=True
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# 2. Descarga
|
| 19 |
pipe.enable_model_cpu_offload()
|
| 20 |
|
| 21 |
-
# 3. Cargamos IP-Adapter (El
|
| 22 |
-
print("Cargando IP-Adapter
|
| 23 |
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors")
|
| 24 |
-
# Ajustamos la fuerza con la que el anime afectar谩 la foto real (0.0 a 1.0)
|
| 25 |
-
pipe.set_ip_adapter_scale(0.65)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
@spaces.GPU
|
| 29 |
-
def process_cosplay_edit(base_image, reference_image, prompt, steps, strength, seed):
|
| 30 |
if base_image is None or reference_image is None:
|
| 31 |
-
raise gr.Error("
|
| 32 |
|
| 33 |
-
# Redimensionar
|
| 34 |
base_resized = base_image.resize((1024, 1024), Image.Resampling.LANCZOS)
|
| 35 |
ref_resized = reference_image.resize((1024, 1024), Image.Resampling.LANCZOS)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 38 |
|
| 39 |
-
#
|
| 40 |
with torch.inference_mode():
|
| 41 |
output = pipe(
|
| 42 |
prompt=prompt,
|
| 43 |
-
image=base_resized, #
|
| 44 |
-
ip_adapter_image=ref_resized, #
|
| 45 |
-
strength=float(strength), #
|
| 46 |
num_inference_steps=int(steps),
|
| 47 |
guidance_scale=7.5,
|
| 48 |
generator=generator
|
|
@@ -50,34 +51,39 @@ def process_cosplay_edit(base_image, reference_image, prompt, steps, strength, s
|
|
| 50 |
|
| 51 |
return output.images[0]
|
| 52 |
|
| 53 |
-
# ---
|
| 54 |
-
with gr.Blocks(theme=gr.themes.
|
| 55 |
-
gr.Markdown("# **
|
| 56 |
-
gr.Markdown("
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
| 60 |
-
base_input = gr.Image(label="
|
| 61 |
-
ref_input = gr.Image(label="
|
| 62 |
prompt_input = gr.Textbox(
|
| 63 |
-
label="Instrucci贸n",
|
| 64 |
-
value="masterpiece, best quality,
|
| 65 |
lines=3
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
generate_btn = gr.Button("
|
| 73 |
|
| 74 |
with gr.Column():
|
| 75 |
output_display = gr.Image(label="Resultado Final")
|
| 76 |
|
| 77 |
generate_btn.click(
|
| 78 |
fn=process_cosplay_edit,
|
| 79 |
-
inputs=[base_input, ref_input, prompt_input, inference_steps, strength_scale, seed_num],
|
| 80 |
outputs=output_display
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
| 4 |
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
# 1. Definici贸n del modelo SDXL s煤per ligero (FP16 pesa ~6.5GB)
|
| 8 |
MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 9 |
|
| 10 |
+
print("Iniciando descarga de SDXL FP16...")
|
| 11 |
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
torch_dtype=torch.float16,
|
|
|
|
| 15 |
use_safetensors=True
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# 2. Descarga a CPU para ahorrar RAM y VRAM
|
| 19 |
pipe.enable_model_cpu_offload()
|
| 20 |
|
| 21 |
+
# 3. Cargamos IP-Adapter (El extractor de ropa/estilo)
|
| 22 |
+
print("Cargando IP-Adapter (Inyector de estilo)...")
|
| 23 |
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors")
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# @spaces.GPU nos da acceso a una A100 gratuita durante la inferencia
|
| 26 |
@spaces.GPU
|
| 27 |
+
def process_cosplay_edit(base_image, reference_image, prompt, steps, strength, ip_scale, seed):
|
| 28 |
if base_image is None or reference_image is None:
|
| 29 |
+
raise gr.Error("Por favor, sube ambas im谩genes.")
|
| 30 |
|
| 31 |
+
# Redimensionar a la resoluci贸n 贸ptima de SDXL (1024x1024)
|
| 32 |
base_resized = base_image.resize((1024, 1024), Image.Resampling.LANCZOS)
|
| 33 |
ref_resized = reference_image.resize((1024, 1024), Image.Resampling.LANCZOS)
|
| 34 |
|
| 35 |
+
# Ajustamos qu茅 tanto peso tiene la imagen de referencia (la ropa)
|
| 36 |
+
pipe.set_ip_adapter_scale(float(ip_scale))
|
| 37 |
+
|
| 38 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 39 |
|
| 40 |
+
# Proceso de generaci贸n
|
| 41 |
with torch.inference_mode():
|
| 42 |
output = pipe(
|
| 43 |
prompt=prompt,
|
| 44 |
+
image=base_resized, # Foto 1: La persona real (Mantiene la pose)
|
| 45 |
+
ip_adapter_image=ref_resized, # Foto 2: El personaje/ropa de referencia
|
| 46 |
+
strength=float(strength), # 0.0 = Igual a Foto 1, 1.0 = Totalmente diferente
|
| 47 |
num_inference_steps=int(steps),
|
| 48 |
guidance_scale=7.5,
|
| 49 |
generator=generator
|
|
|
|
| 51 |
|
| 52 |
return output.images[0]
|
| 53 |
|
| 54 |
+
# --- Construcci贸n de la Interfaz ---
|
| 55 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 56 |
+
gr.Markdown("# **Cambiador de Ropa Cosplay (SDXL + IP-Adapter)**")
|
| 57 |
+
gr.Markdown("Extrae la ropa de la Foto 2 y apl铆cala sobre la persona en la Foto 1.")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
| 61 |
+
base_input = gr.Image(label="Foto 1: T煤 (Persona Base)", type="pil")
|
| 62 |
+
ref_input = gr.Image(label="Foto 2: El Atuendo (Personaje/Referencia)", type="pil")
|
| 63 |
prompt_input = gr.Textbox(
|
| 64 |
+
label="Instrucci贸n de texto (Prompt)",
|
| 65 |
+
value="masterpiece, best quality, ultra-realistic photo of the person wearing the exact clothes from the reference image, high resolution, detailed fabric",
|
| 66 |
lines=3
|
| 67 |
)
|
| 68 |
+
|
| 69 |
+
with gr.Accordion("Ajustes de Transferencia", open=False):
|
| 70 |
+
# Strength define qu茅 tanto se transforma la foto original
|
| 71 |
+
strength_scale = gr.Slider(minimum=0.3, maximum=0.99, value=0.85, step=0.05, label="Fuerza de Transformaci贸n (Strength)")
|
| 72 |
+
# IP Scale define cu谩nta atenci贸n prestar a la ropa de la Foto 2
|
| 73 |
+
ip_scale = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05, label="Influencia de la Ropa (IP Scale)")
|
| 74 |
+
inference_steps = gr.Slider(minimum=15, maximum=40, value=30, step=1, label="Pasos de Calidad")
|
| 75 |
+
seed_num = gr.Number(value=4242, label="Semilla")
|
| 76 |
|
| 77 |
+
generate_btn = gr.Button("Aplicar Ropa", variant="primary")
|
| 78 |
|
| 79 |
with gr.Column():
|
| 80 |
output_display = gr.Image(label="Resultado Final")
|
| 81 |
|
| 82 |
generate_btn.click(
|
| 83 |
fn=process_cosplay_edit,
|
| 84 |
+
inputs=[base_input, ref_input, prompt_input, inference_steps, strength_scale, ip_scale, seed_num],
|
| 85 |
outputs=output_display
|
| 86 |
)
|
| 87 |
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.queue().launch()
|