Spaces:
Build error
Build error
Aplicacion con mas marposas
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
from utils import carga_modelo, genera
|
| 4 |
+
|
| 5 |
+
##pagina principal
|
| 6 |
+
st.title("Generador de mariposas")
|
| 7 |
+
st.write("Esto es un modelo Light GAN entrenado y utilizado con Platzi")
|
| 8 |
+
|
| 9 |
+
## barra lateral
|
| 10 |
+
st.sidebar.subheader("esta mariposa no existe! Puedes creerlo??")
|
| 11 |
+
st.sidebar.image("asset/logo.png", width=200)
|
| 12 |
+
st.sidebar.caption("Demo creado en un curso de Platzi")
|
| 13 |
+
|
| 14 |
+
## Cargamos el modelo
|
| 15 |
+
repo_id = "https://huggingface.co/ceyda/butterfly_cropped_uniq1K_512"
|
| 16 |
+
modelo_gan = carga_modelo(repo_id)
|
| 17 |
+
|
| 18 |
+
## Generamos 4 mariposas
|
| 19 |
+
n_mariposas = 4s
|
| 20 |
+
|
| 21 |
+
def corre():
|
| 22 |
+
with st.spinner("Generando, espera un poco..."):
|
| 23 |
+
ims = genera(modelo_gan, n_mariposas)
|
| 24 |
+
st.session_state("ims") = ims
|
| 25 |
+
|
| 26 |
+
if ("ims" not in st.session_state):
|
| 27 |
+
st.session_state("ims") = None
|
| 28 |
+
corre()
|
| 29 |
+
|
| 30 |
+
ims = st.session_state("ims")
|
| 31 |
+
|
| 32 |
+
corre_buton = st.button(
|
| 33 |
+
"Genera mariposas porfa",
|
| 34 |
+
on_click= corre,
|
| 35 |
+
help="Estamos en vuelo, abrocha el cinturón."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if ims is not None:
|
| 39 |
+
cols = st.columns(n_mariposas)
|
| 40 |
+
for j,im in enumerate(ims):
|
| 41 |
+
i = j % n_mariposas
|
| 42 |
+
cols[i].image(im, use_column_sidth=True)
|
utils.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## crear funciones par ala aplicaciones
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
|
| 5 |
+
|
| 6 |
+
def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
|
| 7 |
+
gan = LightweightGAN.from_pretrained(model_name, version=model_version)
|
| 8 |
+
gan.eval()
|
| 9 |
+
return gan
|
| 10 |
+
|
| 11 |
+
def genera(gan, batch_size=1):
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0)*255
|
| 14 |
+
ims = ims.permute(0,2,3,1).detach().cp().numpy().astype(np.uint8)
|
| 15 |
+
return ims
|