Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,62 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from diffusers import QwenImageEditPipeline
|
| 5 |
+
import spaces
|
| 6 |
|
| 7 |
+
# 1. Cargar el modelo base y el adaptador (LoRA)
|
| 8 |
+
# Esto ocurre cuando el Space arranca, preparándolo en memoria
|
| 9 |
+
pipe = QwenImageEditPipeline.from_pretrained(
|
| 10 |
+
"Qwen/Qwen-Image-Edit-2511",
|
| 11 |
+
torch_dtype=torch.bfloat16
|
| 12 |
)
|
| 13 |
+
pipe.load_lora_weights("WarmBloodAban/AnyoneCosplay")
|
| 14 |
+
pipe.to("cuda")
|
| 15 |
|
| 16 |
+
# 2. La función generadora con Zero-GPU
|
| 17 |
+
# @spaces.GPU le dice a Hugging Face: "préstame una GPU potente solo para estos segundos"
|
| 18 |
+
@spaces.GPU
|
| 19 |
+
def generate_cosplay(human_img, anime_img):
|
| 20 |
+
# El modelo espera una sola imagen unida.
|
| 21 |
+
# Pegamos la foto humana (Figura 1) y el anime (Figura 2) lado a lado.
|
| 22 |
+
w1, h1 = human_img.size
|
| 23 |
+
w2, h2 = anime_img.size
|
| 24 |
+
|
| 25 |
+
# Ajustamos la altura de la imagen anime para que coincida con la humana
|
| 26 |
+
new_w2 = int((h1 / h2) * w2)
|
| 27 |
+
anime_img_resized = anime_img.resize((new_w2, h1))
|
| 28 |
+
|
| 29 |
+
combined_img = Image.new('RGB', (w1 + new_w2, h1))
|
| 30 |
+
combined_img.paste(human_img, (0, 0))
|
| 31 |
+
combined_img.paste(anime_img_resized, (w1, 0))
|
| 32 |
+
|
| 33 |
+
# Palabra clave de activación requerida por el autor del modelo
|
| 34 |
+
prompt = "Let the person in Figure 1 cosplay the role in Figure 2"
|
| 35 |
+
|
| 36 |
+
with torch.inference_mode():
|
| 37 |
+
# Ejecutamos la inferencia con los parámetros recomendados para Qwen
|
| 38 |
+
output = pipe(
|
| 39 |
+
image=combined_img,
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
num_inference_steps=50,
|
| 42 |
+
true_cfg_scale=4.0
|
| 43 |
+
).images[0]
|
| 44 |
+
|
| 45 |
+
return output
|
| 46 |
|
| 47 |
+
# 3. Interfaz gráfica simple y ligera
|
| 48 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 49 |
+
gr.Markdown("# 🎭 Virtual Cosplay (Zero-GPU)")
|
| 50 |
+
gr.Markdown("Sube tu foto y la del personaje de anime. El modelo transferirá la ropa manteniendo tu rostro.")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
human_in = gr.Image(type="pil", label="Tu foto (Figura 1)")
|
| 54 |
+
anime_in = gr.Image(type="pil", label="Personaje Anime (Figura 2)")
|
| 55 |
+
|
| 56 |
+
btn = gr.Button("✨ Generar Cosplay", variant="primary")
|
| 57 |
+
output_image = gr.Image(label="Resultado final")
|
| 58 |
+
|
| 59 |
+
btn.click(fn=generate_cosplay, inputs=[human_in, anime_in], outputs=output_image)
|
| 60 |
+
|
| 61 |
+
# Iniciar la aplicación
|
| 62 |
+
demo.launch()
|