EliseoBaquero commited on
Commit
43d10d4
·
verified ·
1 Parent(s): 76f072b

subiendo utils y app

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. utils.py +15 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import carga_modelo, genera
4
+ st.title("Generador de mariposas")
5
+ st.write("Este es un modelo Light GAN entrenado y utilizado con Platzi")
6
+
7
+ st.sidebar.subheader("Esta mariposa es creada artificialmente, no es real")
8
+ st.sidebar.image("assets/logo.png", width=200)
9
+ st.sidebar.caption("Demo creado en vivo.")
10
+
11
+ repo_id = "ceyda/butterfly_cropped_uniq1K_512"
12
+ modelo_gan = carga_modelo(repo_id)
13
+
14
+ n_mariposas = 4
15
+
16
+ def corre():
17
+ with st.spinner("Generando, Espera"):
18
+ ims = genera(modelo_gan, n_mariposas)
19
+ st.session_state["ims"] = ims
20
+
21
+ if "ims" not st.session_state:
22
+ st.session_state["ims"] = None
23
+ corre()
24
+
25
+ ims = st.session_state["ims"]
26
+
27
+ corre_boton = st.button(
28
+ "Genera mariposas"
29
+ on_click=corre,
30
+ help="Estamos en vuelo, mira atentamente"
31
+ )
32
+
33
+ if ims is not None:
34
+ cols = st.columns(n_mariposas)
35
+ for j, im in enumerate(ims):
36
+ i = j % n_mariposas
37
+ cols[i].image(im, use_column_width=True)
38
+
39
+
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from huggan.pytorch.lightweight_gan.Ligthweight_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).deatch().cpu().numpy().astype(np.uint8)
14
+
15
+ return ims