Spaces:
Sleeping
Sleeping
David Chavarria
commited on
Commit
·
26e33ea
1
Parent(s):
135a9d1
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from utils import carga_modelo,genera
|
3 |
+
|
4 |
+
## Pagina principal
|
5 |
+
st.title("generador de mariposas")
|
6 |
+
st.write("este es un modelo lightGan entrenado")
|
7 |
+
|
8 |
+
## Barra Lateral
|
9 |
+
st.sidebar.subheader("¡Esta mariposa no existe, ¿Puedes creeerlo?")
|
10 |
+
st.sidebar.image("assets/logo.png",width=200)
|
11 |
+
st.sidebar.caption("Demo creado en vivo")
|
12 |
+
|
13 |
+
## Cargamos el modelo
|
14 |
+
repo_id = "ceyda/butterfly_cropped_uniq1K_512"
|
15 |
+
modelo_gan = carga_modelo(repo_id)
|
16 |
+
|
17 |
+
## Generamos 4 mariposas
|
18 |
+
|
19 |
+
n_mariposas = 4
|
20 |
+
|
21 |
+
def corre():
|
22 |
+
with st.spinner("Generando espera..."):
|
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 |
+
corre_boton = st.button(
|
32 |
+
"Generar mariposas",
|
33 |
+
on_click=corre,
|
34 |
+
help="Estamos en vuelo, abrocha tu sinturon"
|
35 |
+
)
|
36 |
+
|
37 |
+
if ims is not None:
|
38 |
+
cols = st.columns(n_mariposas)
|
39 |
+
for j, im in enumerate(ims):
|
40 |
+
i = j % n_mariposas
|
41 |
+
cols[i].image(im,use_column_width=True)
|
utils.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
|
4 |
+
|
5 |
+
def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
|
6 |
+
gan = LightweightGAN.from_pretrained(model_name, version=model_version)
|
7 |
+
gan.eval()
|
8 |
+
return gan
|
9 |
+
|
10 |
+
def genera(gan, batch_size=1):
|
11 |
+
with torch.no_grad():
|
12 |
+
ims = gan.G(torch.randn(batch_size,gan.latent_dim)).clamp_(0.0,1.0) * 255
|
13 |
+
ims = ims.permute(0,2,3,1).detach().cpu().numpy().astype(np.uint8)
|
14 |
+
return ims
|