Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,7 @@ import gradio as gr
|
|
| 4 |
from diffusers import FluxImg2ImgPipeline
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# 1. Carga del modelo
|
| 8 |
-
# Usamos FLUX.1-schnell para velocidad en Spaces
|
| 9 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 10 |
print("Cargando pipeline en GPU...")
|
| 11 |
pipe = FluxImg2ImgPipeline.from_pretrained(
|
|
@@ -13,48 +12,48 @@ pipe = FluxImg2ImgPipeline.from_pretrained(
|
|
| 13 |
torch_dtype=torch.bfloat16
|
| 14 |
).to("cuda")
|
| 15 |
|
| 16 |
-
# 2. Carga del LoRA
|
| 17 |
print("Inyectando LoRA...")
|
| 18 |
pipe.load_lora_weights("joyfox/Kontext-Cosplay-Lora", weight_name="kontext_cosplay_lora_20000.safetensors")
|
| 19 |
|
| 20 |
-
# 3. Funci贸n de generaci贸n
|
| 21 |
@spaces.GPU
|
| 22 |
-
def generate_cosplay(image, prompt):
|
| 23 |
if image is None:
|
| 24 |
return None
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
image = image.convert("RGB").resize((1024, 1024))
|
| 28 |
|
| 29 |
-
#
|
| 30 |
full_prompt = f"{prompt}, r2cos"
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# Ejecuci贸n
|
| 36 |
-
# strength=0.6 significa que mantenemos 40% de la original y 60% es la transformaci贸n
|
| 37 |
result = pipe(
|
| 38 |
prompt=full_prompt,
|
| 39 |
image=image,
|
| 40 |
-
strength=
|
| 41 |
-
num_inference_steps=
|
| 42 |
-
guidance_scale=0.0
|
| 43 |
-
generator=generator
|
| 44 |
).images[0]
|
| 45 |
|
| 46 |
return result
|
| 47 |
|
| 48 |
-
# 4. Interfaz Gradio
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=generate_cosplay,
|
| 51 |
inputs=[
|
| 52 |
gr.Image(type="pil", label="Foto de Referencia"),
|
| 53 |
-
gr.Textbox(label="Descripci贸n del personaje
|
|
|
|
|
|
|
| 54 |
],
|
| 55 |
outputs=gr.Image(label="Resultado Cosplay"),
|
| 56 |
-
title="Kontext-Cosplay AI
|
| 57 |
-
description="
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from diffusers import FluxImg2ImgPipeline
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# 1. Carga del modelo optimizado en memoria
|
|
|
|
| 8 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 9 |
print("Cargando pipeline en GPU...")
|
| 10 |
pipe = FluxImg2ImgPipeline.from_pretrained(
|
|
|
|
| 12 |
torch_dtype=torch.bfloat16
|
| 13 |
).to("cuda")
|
| 14 |
|
| 15 |
+
# 2. Carga del LoRA de Cosplay
|
| 16 |
print("Inyectando LoRA...")
|
| 17 |
pipe.load_lora_weights("joyfox/Kontext-Cosplay-Lora", weight_name="kontext_cosplay_lora_20000.safetensors")
|
| 18 |
|
| 19 |
+
# 3. Funci贸n de generaci贸n corregida
|
| 20 |
@spaces.GPU
|
| 21 |
+
def generate_cosplay(image, prompt, strength, steps):
|
| 22 |
if image is None:
|
| 23 |
return None
|
| 24 |
+
|
| 25 |
+
# Redimensionamos para mantener estabilidad en FLUX
|
| 26 |
image = image.convert("RGB").resize((1024, 1024))
|
| 27 |
|
| 28 |
+
# Trigger indispensable del LoRA
|
| 29 |
full_prompt = f"{prompt}, r2cos"
|
| 30 |
|
| 31 |
+
# Eliminamos la semilla fija estricta para permitir variedad por intento,
|
| 32 |
+
# pero puedes volver a usar un generador si deseas replicabilidad exacta.
|
| 33 |
|
| 34 |
+
# Ejecuci贸n con los nuevos par谩metros din谩micos
|
|
|
|
| 35 |
result = pipe(
|
| 36 |
prompt=full_prompt,
|
| 37 |
image=image,
|
| 38 |
+
strength=strength, # Control din谩mico desde la UI
|
| 39 |
+
num_inference_steps=steps, # Elevado para dar tiempo al LoRA
|
| 40 |
+
guidance_scale=0.0
|
|
|
|
| 41 |
).images[0]
|
| 42 |
|
| 43 |
return result
|
| 44 |
|
| 45 |
+
# 4. Interfaz de Gradio interactiva
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=generate_cosplay,
|
| 48 |
inputs=[
|
| 49 |
gr.Image(type="pil", label="Foto de Referencia"),
|
| 50 |
+
gr.Textbox(label="Descripci贸n del personaje (Ej: Naruto, Cyberpunk, Samurai)"),
|
| 51 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05, label="Fuerza de Transformaci贸n (Strength)"),
|
| 52 |
+
gr.Slider(minimum=10, maximum=30, value=16, step=1, label="Pasos de Inferencia Totales (Steps)")
|
| 53 |
],
|
| 54 |
outputs=gr.Image(label="Resultado Cosplay"),
|
| 55 |
+
title="Kontext-Cosplay AI Pro",
|
| 56 |
+
description="Ajusta los deslizadores si la imagen cambia muy poco."
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|