Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,54 @@
|
|
| 1 |
import torch
|
| 2 |
-
import
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
from diffusers import FluxInpaintPipeline
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
# 1.
|
| 8 |
-
model_id = "
|
| 9 |
-
|
| 10 |
-
pipe = FluxInpaintPipeline.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
-
torch_dtype=torch.bfloat16
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
raise gr.Error("¡Debes usar el pincel para pintar sobre la ropa!")
|
| 33 |
-
|
| 34 |
-
# Creamos una máscara negra vacía del mismo tamaño
|
| 35 |
-
mask = Image.new("L", image.size, 0)
|
| 36 |
-
|
| 37 |
-
# Sumamos todos los trazos (capas) que el usuario dibujó
|
| 38 |
-
for layer in layers:
|
| 39 |
-
layer_resized = layer.convert("RGBA").resize((1024, 1024))
|
| 40 |
-
alpha_channel = layer_resized.split()[3] # Extraemos por dónde pasó el pincel
|
| 41 |
-
mask.paste(alpha_channel, (0, 0), alpha_channel)
|
| 42 |
-
|
| 43 |
-
# 3.3 Configurar el prompt
|
| 44 |
-
full_prompt = f"{prompt}, r2cos"
|
| 45 |
-
|
| 46 |
-
# 3.4 Ejecutar el modelo Inpaint
|
| 47 |
-
result = pipe(
|
| 48 |
-
prompt=full_prompt,
|
| 49 |
-
image=image,
|
| 50 |
-
mask_image=mask, # Aquí le pasamos la máscara a la IA
|
| 51 |
-
strength=strength,
|
| 52 |
-
num_inference_steps=steps,
|
| 53 |
-
guidance_scale=0.0
|
| 54 |
-
).images[0]
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
demo = gr.Interface(
|
| 60 |
-
fn=
|
| 61 |
-
inputs=[
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
label="1. Sube tu foto y PINTA LA ROPA (deja la cara intacta)",
|
| 65 |
-
brush=gr.Brush(colors=["#FF0000"], color_mode="fixed") # Pincel rojo para ver bien
|
| 66 |
-
),
|
| 67 |
-
gr.Textbox(label="2. Descripción (Ej: detailed samurai armor, cyberpunk)"),
|
| 68 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="Fuerza (Strength)"),
|
| 69 |
-
gr.Slider(minimum=10, maximum=30, value=20, step=1, label="Pasos (Steps)")
|
| 70 |
-
],
|
| 71 |
-
outputs=gr.Image(label="Resultado Cosplay"),
|
| 72 |
-
title="Kontext-Cosplay AI Inpaint",
|
| 73 |
-
description="Pinta de rojo la ropa y el fondo. La IA transformará solo la zona pintada, respetando la cara original."
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 3 |
+
from qwen_vl_utils import process_vision_info
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# 1. Cargar el modelo
|
| 7 |
+
model_id = "joyfox/Qwen-Image-Edit-Cosplay"
|
| 8 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
|
|
| 9 |
model_id,
|
| 10 |
+
torch_dtype=torch.bfloat16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 14 |
|
| 15 |
+
def edit_image(image, instruction):
|
| 16 |
+
# Preparar el prompt siguiendo la estructura de Qwen2-VL
|
| 17 |
+
messages = [
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": [
|
| 21 |
+
{"type": "image", "image": image},
|
| 22 |
+
{"type": "text", "text": f"Instruction: {instruction}. Edit the image accordingly."},
|
| 23 |
+
],
|
| 24 |
+
}
|
| 25 |
+
]
|
| 26 |
|
| 27 |
+
# Procesar entrada
|
| 28 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 29 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 30 |
+
inputs = processor(
|
| 31 |
+
text=[text],
|
| 32 |
+
images=image_inputs,
|
| 33 |
+
videos=video_inputs,
|
| 34 |
+
padding=True,
|
| 35 |
+
return_tensors="pt"
|
| 36 |
+
).to("cuda")
|
| 37 |
+
|
| 38 |
+
# Generar (aquí el modelo devuelve la imagen editada)
|
| 39 |
+
generated_ids = model.generate(**inputs, max_new_tokens=2048)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Decodificar y procesar la salida (simplificado para el ejemplo)
|
| 42 |
+
# Nota: Qwen-VL suele devolver el resultado a través de una salida de pipeline
|
| 43 |
+
# específica definida en su model card.
|
| 44 |
+
return generated_ids
|
| 45 |
|
| 46 |
+
# Interfaz
|
| 47 |
demo = gr.Interface(
|
| 48 |
+
fn=edit_image,
|
| 49 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Instrucción de Cosplay")],
|
| 50 |
+
outputs=gr.Image(),
|
| 51 |
+
title="Qwen-VL Cosplay Editor"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|