Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision.utils import save_image
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# 1. CARGAR MODELO DESDE HUGGING FACE
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# Reemplaza estos datos con los tuyos
|
| 13 |
+
REPO_ID = "Bmo411/VAE" # <-- cámbialo por el tuyo
|
| 14 |
+
MODEL_FILENAME = "vae_complete_model.pth"
|
| 15 |
+
|
| 16 |
+
# Descargar modelo automáticamente
|
| 17 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 18 |
+
|
| 19 |
+
# Inicializar modelo y cargar pesos
|
| 20 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
model = VAE()
|
| 22 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 23 |
+
model.to(device)
|
| 24 |
+
model.eval()
|
| 25 |
+
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# 3. FUNCIÓN PARA GENERAR IMAGEN
|
| 28 |
+
# -----------------------------
|
| 29 |
+
def generate_image(z_dim=40):
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
z = torch.randn(1, z_dim).to(device)
|
| 32 |
+
out = model.decode(z)
|
| 33 |
+
out = torch.sigmoid(out)
|
| 34 |
+
out = out.view(1, 1, 100, 100)
|
| 35 |
+
|
| 36 |
+
output_path = "generated_sample.png"
|
| 37 |
+
save_image(out, output_path)
|
| 38 |
+
|
| 39 |
+
img = Image.open(output_path)
|
| 40 |
+
return img
|
| 41 |
+
|
| 42 |
+
# -----------------------------
|
| 43 |
+
# 4. INTERFAZ GRADIO
|
| 44 |
+
# -----------------------------
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=generate_image,
|
| 47 |
+
inputs=gr.Slider(10, 100, value=40, step=1, label="Dimensión latente (z_dim)"),
|
| 48 |
+
outputs="image",
|
| 49 |
+
title="Generador de Imagen con VAE",
|
| 50 |
+
description="Genera una imagen aleatoria a partir del espacio latente del VAE entrenado."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch()
|