Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,86 +4,47 @@ from PIL import Image
|
|
| 4 |
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
-
#
|
| 8 |
MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 9 |
|
| 10 |
-
|
| 11 |
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 12 |
-
MODEL_ID,
|
| 13 |
-
torch_dtype=torch.float16,
|
| 14 |
-
variant="fp16",
|
| 15 |
use_safetensors=True
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
# 2. Cargamos IP-Adapter (El extractor de ropa/estilo)
|
| 21 |
-
print("Cargando IP-Adapter (Inyector de estilo)...")
|
| 22 |
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors")
|
| 23 |
|
| 24 |
-
# @spaces.GPU nos da acceso a una A100 gratuita durante la inferencia
|
| 25 |
@spaces.GPU
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
raise gr.Error("Por favor, sube ambas im谩genes.")
|
| 29 |
-
|
| 30 |
-
# EL TRUCO DE ZEROGPU: Movemos el modelo a la GPU solo cuando la funci贸n se activa
|
| 31 |
pipe.to("cuda")
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
prompt=prompt,
|
| 46 |
-
image=base_resized, # Foto 1: La persona real (Mantiene la pose)
|
| 47 |
-
ip_adapter_image=ref_resized, # Foto 2: El personaje/ropa de referencia
|
| 48 |
-
strength=float(strength), # 0.0 = Igual a Foto 1, 1.0 = Totalmente diferente
|
| 49 |
-
num_inference_steps=int(steps),
|
| 50 |
-
guidance_scale=7.5,
|
| 51 |
-
generator=generator
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
return output.images[0]
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
with gr.Blocks(
|
| 58 |
-
gr.Markdown("# **Cambiador de Ropa Cosplay (SDXL + IP-Adapter)**")
|
| 59 |
-
gr.Markdown("Extrae la ropa de la Foto 2 y apl铆cala sobre la persona en la Foto 1.")
|
| 60 |
-
|
| 61 |
with gr.Row():
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
with gr.Accordion("Ajustes de Transferencia", open=False):
|
| 72 |
-
strength_scale = gr.Slider(minimum=0.3, maximum=0.99, value=0.85, step=0.05, label="Fuerza de Transformaci贸n (Strength)")
|
| 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 |
-
|
| 89 |
-
demo.queue().launch()
|
|
|
|
| 4 |
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
# Usaremos un modelo que el sistema ya tiene en cach茅 o que descarga de forma ligera
|
| 8 |
MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 9 |
|
| 10 |
+
# Esta configuraci贸n es mucho m谩s amigable para el servidor gratuito
|
| 11 |
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
torch_dtype=torch.float16,
|
|
|
|
| 14 |
use_safetensors=True
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Cargamos el inyector de estilo (IP-Adapter)
|
|
|
|
|
|
|
|
|
|
| 18 |
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors")
|
| 19 |
|
|
|
|
| 20 |
@spaces.GPU
|
| 21 |
+
def generate_cosplay(base_image, reference_image, prompt):
|
| 22 |
+
# Movemos el pipeline a la GPU solo cuando se presiona el bot贸n
|
|
|
|
|
|
|
|
|
|
| 23 |
pipe.to("cuda")
|
| 24 |
+
pipe.set_ip_adapter_scale(0.7)
|
| 25 |
|
| 26 |
+
# Procesamiento
|
| 27 |
+
image = pipe(
|
| 28 |
+
prompt=prompt,
|
| 29 |
+
image=base_image.resize((1024, 1024)),
|
| 30 |
+
ip_adapter_image=reference_image.resize((1024, 1024)),
|
| 31 |
+
strength=0.7,
|
| 32 |
+
num_inference_steps=20
|
| 33 |
+
).images[0]
|
| 34 |
|
| 35 |
+
# Liberamos la GPU inmediatamente despu茅s
|
| 36 |
+
pipe.to("cpu")
|
| 37 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Interfaz simplificada para evitar errores de memoria
|
| 40 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 41 |
with gr.Row():
|
| 42 |
+
base_img = gr.Image(label="Persona (Foto 1)", type="pil")
|
| 43 |
+
ref_img = gr.Image(label="Ropa (Foto 2)", type="pil")
|
| 44 |
+
prompt = gr.Textbox(value="masterpiece, realistic photo, person wearing the outfit from reference", label="Prompt")
|
| 45 |
+
btn = gr.Button("Generar Resultado")
|
| 46 |
+
result = gr.Image()
|
| 47 |
+
|
| 48 |
+
btn.click(generate_cosplay, inputs=[base_img, ref_img, prompt], outputs=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
demo.launch()
|
|
|