Spaces:
Sleeping
Sleeping
Josue Aaron Soriano Rivero
commited on
Commit
•
576457a
1
Parent(s):
aa55a25
Utils y app
Browse files
app.py
CHANGED
@@ -1,4 +1,43 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
from utils import carga_modelo, genera
|
4 |
+
|
5 |
+
## Página principal
|
6 |
+
|
7 |
+
st.title("Generador de mariposas")
|
8 |
+
st.write("Este es un modelo LightGAN entrenado y utilizado con Platzi")
|
9 |
+
|
10 |
+
## Barra Lateral
|
11 |
+
st.sidebar.subheader("Esta mariposa no existe en la vida real D:")
|
12 |
+
st.sidebar.image("assets/logo.png", width=200)
|
13 |
+
st.sidebar.caption("Demo creado en Platzi")
|
14 |
+
|
15 |
+
## Cargamos el modelo
|
16 |
+
repo_id = "ceyda/butterfly_cropped_uniq1K_512"
|
17 |
+
modelo_gan = carga_modelo(repo_id)
|
18 |
+
|
19 |
+
## Generamos 4 mariposas
|
20 |
+
n_mariposas = 4
|
21 |
+
|
22 |
+
def corre():
|
23 |
+
with st.spinner("Generando espera un poquito..."):
|
24 |
+
ims = genera(modelo_gan, n_mariposas)
|
25 |
+
st.session_state["ims"] = ims
|
26 |
+
|
27 |
+
if "ims" not in st.session_state:
|
28 |
+
st.session_state["ims"] = None
|
29 |
+
corre()
|
30 |
+
|
31 |
+
ims = st.session_state["ims"]
|
32 |
+
|
33 |
+
corre_btn = st.button(
|
34 |
+
"Genera mariposas",
|
35 |
+
on_click=corre(),
|
36 |
+
help="Estamos en vuelo, abrocha tu cinturón"
|
37 |
+
)
|
38 |
+
|
39 |
+
if ims is not None:
|
40 |
+
cols = st.columns(n_mariposas)
|
41 |
+
for j, im in enumerate(ims):
|
42 |
+
i = j % n_mariposas
|
43 |
+
cols[i].image(im, use_colum_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(nombre_modelo="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
|
6 |
+
gan = LightweightGAN.from_pretrained(nombre_modelo, 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).deatch().cpu().numpy().astype(np.uint8)
|
14 |
+
return ims
|