appinitdev commited on
Commit
c5be68f
·
verified ·
1 Parent(s): c291298

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -64
app.py CHANGED
@@ -1,76 +1,54 @@
1
  import torch
2
- import spaces
 
3
  import gradio as gr
4
- from diffusers import FluxInpaintPipeline
5
- from PIL import Image
6
 
7
- # 1. Carga del modelo en modo Inpaint
8
- model_id = "black-forest-labs/FLUX.1-schnell"
9
- print("Cargando pipeline de Inpainting en GPU...")
10
- pipe = FluxInpaintPipeline.from_pretrained(
11
  model_id,
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 con Inpainting
20
- @spaces.GPU
21
- def generate_cosplay_inpaint(input_dict, prompt, strength, steps):
22
- # input_dict contiene la imagen original y los trazos del pincel
23
- if input_dict is None or input_dict["background"] is None:
24
- return None
25
-
26
- # 3.1 Procesar imagen original
27
- image = input_dict["background"].convert("RGB").resize((1024, 1024))
28
-
29
- # 3.2 Construir la máscara desde los trazos del usuario
30
- layers = input_dict.get("layers", [])
31
- if not layers:
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
- return result
 
 
 
57
 
58
- # 4. Interfaz interactiva con editor de imagen
59
  demo = gr.Interface(
60
- fn=generate_cosplay_inpaint,
61
- inputs=[
62
- gr.ImageEditor(
63
- type="pil",
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__":