Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,61 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
from diffusers import
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
pipe =
|
| 12 |
-
|
| 13 |
-
torch_dtype=torch.float16,
|
| 14 |
use_safetensors=True
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# Cargamos
|
| 18 |
-
pipe.
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@spaces.GPU
|
| 21 |
-
def
|
| 22 |
-
# Movemos el
|
| 23 |
pipe.to("cuda")
|
| 24 |
-
pipe.set_ip_adapter_scale(0.7)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
prompt=prompt,
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
return image
|
| 38 |
|
| 39 |
-
# Interfaz
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
with gr.Row():
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
+
from diffusers import QwenImageEditPlusPipeline, FlowMatchEulerDiscreteScheduler
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
# Configuraci贸n
|
| 8 |
+
BASE_MODEL = "prithivMLmods/Qwen-Image-Edit-AIO-FP8"
|
| 9 |
+
LORA_COSPLAY = "joyfox/Qwen-Image-Edit-Cosplay"
|
| 10 |
+
LORA_LIGHTNING = "lightx2v/Qwen-Image-Lightning"
|
| 11 |
|
| 12 |
+
# Inicializaci贸n b谩sica del modelo
|
| 13 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 14 |
+
BASE_MODEL,
|
| 15 |
+
torch_dtype=torch.float16, # Usamos float16 para ahorrar VRAM
|
| 16 |
use_safetensors=True
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Cargamos los adaptadores de una vez al inicio
|
| 20 |
+
pipe.load_lora_weights(LORA_COSPLAY, adapter_name="cosplay")
|
| 21 |
+
pipe.load_lora_weights(LORA_LIGHTNING, weight_name="Qwen-Image-Lightning-8steps-V1.0.safetensors", adapter_name="lightning")
|
| 22 |
+
pipe.set_adapters(["cosplay", "lightning"], adapter_weights=[0.85, 1.0])
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
+
def process_cosplay_edit(base_image, reference_image, prompt, steps, cfg, seed):
|
| 26 |
+
# Movemos el modelo a la GPU activamente al iniciar la generaci贸n
|
| 27 |
pipe.to("cuda")
|
|
|
|
| 28 |
|
| 29 |
+
# Redimensionamiento (m煤ltiplos de 8)
|
| 30 |
+
width, height = 1024, 1024
|
| 31 |
+
base_resized = base_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 32 |
+
ref_resized = reference_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 33 |
+
|
| 34 |
+
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 35 |
+
|
| 36 |
+
output = pipe(
|
| 37 |
+
image=[base_resized, ref_resized],
|
| 38 |
prompt=prompt,
|
| 39 |
+
width=width,
|
| 40 |
+
height=height,
|
| 41 |
+
num_inference_steps=int(steps),
|
| 42 |
+
true_cfg_scale=float(cfg),
|
| 43 |
+
generator=generator
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
# Al finalizar la funci贸n, el modelo se retira de la GPU autom谩ticamente por ZeroGPU
|
| 47 |
+
return output.images[0]
|
|
|
|
| 48 |
|
| 49 |
+
# Interfaz
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
with gr.Row():
|
| 52 |
+
base_input = gr.Image(label="Persona Real", type="pil")
|
| 53 |
+
ref_input = gr.Image(label="Referencia Anime", type="pil")
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
prompt_input = gr.Textbox(label="Prompt", value="high quality, cosplay style transfer")
|
| 56 |
+
generate_btn = gr.Button("Generar", variant="primary")
|
| 57 |
+
output_display = gr.Image(label="Resultado")
|
| 58 |
+
|
| 59 |
+
generate_btn.click(process_cosplay_edit, inputs=[base_input, ref_input, prompt_input, gr.Slider(4, 12, 8), gr.Slider(1, 5, 1.5), gr.Number(1337)], outputs=output_display)
|
| 60 |
|
| 61 |
+
demo.queue().launch()
|