BitokenPlus commited on
Commit
c9d215e
·
verified ·
1 Parent(s): d70ce95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
app.py CHANGED
@@ -1,45 +1,62 @@
1
  import gradio as gr
 
2
  import torch
3
  from torchvision import transforms
4
- from PIL import Image
5
- from fashion_gan import FashionGAN # Reemplaza esto con la clase o el modelo real de FashionGAN
6
 
7
- # Cargar el modelo preentrenado
8
- fashion_gan_model = FashionGAN()
9
- fashion_gan_model.load_state_dict(torch.load("fashion_gan_pretrained.pth")) # Cargar el modelo preentrenado
 
 
 
 
 
 
 
10
 
11
- # Definir la transformación de imágenes
12
  transform = transforms.Compose([
13
  transforms.Resize((256, 256)),
14
- transforms.ToTensor(),
15
- transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalización
16
  ])
17
 
18
- def try_on_virtual_outfit(person_image, outfit_image):
19
- # Transformar las imágenes
20
- person_image = transform(person_image).unsqueeze(0)
21
- outfit_image = transform(outfit_image).unsqueeze(0)
22
-
23
- # Pasar las imágenes por el modelo
24
- generated_image = fashion_gan_model(person_image, outfit_image) # Esto depende de cómo se implementa FashionGAN
25
-
26
- # Convertir la salida a imagen
27
- generated_image = generated_image.squeeze(0).permute(1, 2, 0).detach().numpy()
28
- generated_image = (generated_image * 255).astype('uint8')
29
-
30
- # Convertir a una imagen PIL
31
- generated_image = Image.fromarray(generated_image)
32
- return generated_image
33
-
34
- # Crear la interfaz de Gradio
35
- iface = gr.Interface(
36
- fn=try_on_virtual_outfit, # Función que procesará las imágenes
37
- inputs=[gr.Image(type="pil", label="Imagen del maniquí"), gr.Image(type="pil", label="Imagen de la prenda")],
38
- outputs=gr.Image(type="pil", label="Prueba de la prenda virtual"),
39
- live=True
 
 
 
 
 
 
 
 
 
 
 
40
  )
41
 
42
- # Iniciar la aplicación
43
- iface.launch()
 
44
 
45
 
 
1
  import gradio as gr
2
+ from PIL import Image
3
  import torch
4
  from torchvision import transforms
 
 
5
 
6
+ # Aquí deberías importar tu modelo real
7
+ # Este es un ejemplo genérico
8
+ class DummyFashionGAN(torch.nn.Module):
9
+ def forward(self, person, clothes):
10
+ # Este es solo un dummy para no lanzar errores
11
+ return person
12
+
13
+ # Cargar modelo (usa tu modelo real aquí)
14
+ model = DummyFashionGAN()
15
+ model.eval()
16
 
17
+ # Transformación de imágenes
18
  transform = transforms.Compose([
19
  transforms.Resize((256, 256)),
20
+ transforms.ToTensor()
 
21
  ])
22
 
23
+ # Función principal con depuración
24
+ def tryon(person_img, clothes_img):
25
+ try:
26
+ print("📤 Iniciando procesamiento de imágenes...")
27
+
28
+ person = transform(person_img).unsqueeze(0)
29
+ clothes = transform(clothes_img).unsqueeze(0)
30
+
31
+ print("✅ Imágenes cargadas y transformadas.")
32
+
33
+ with torch.no_grad():
34
+ output = model(person, clothes)
35
+
36
+ print("🎯 Generación de imagen completada.")
37
+
38
+ result = transforms.ToPILImage()(output.squeeze(0).clamp(0, 1))
39
+ return result
40
+
41
+ except Exception as e:
42
+ print(" Error durante el procesamiento:", str(e))
43
+ # Devolver una imagen roja como error
44
+ return Image.new("RGB", (256, 256), color="red")
45
+
46
+ # Interfaz de Gradio
47
+ demo = gr.Interface(
48
+ fn=tryon,
49
+ inputs=[
50
+ gr.Image(label="👤 Tu maniquí o imagen de cuerpo", type="pil"),
51
+ gr.Image(label="👕 Imagen de la prenda", type="pil")
52
+ ],
53
+ outputs=gr.Image(label="🪄 Resultado: Prueba virtual"),
54
+ title="👗 Probador Virtual AI",
55
+ description="Sube una imagen tuya (o maniquí) y una prenda para probarla virtualmente. Esta es una demo."
56
  )
57
 
58
+ # Lanzar app
59
+ if __name__ == "__main__":
60
+ demo.launch()
61
 
62