agudelozc commited on
Commit
615dea3
1 Parent(s): e835770

subiendo utils y app

Browse files
Files changed (2) hide show
  1. app.py +44 -0
  2. utils.py +15 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import carga_modelo, genera
4
+
5
+ ##Pagina principal
6
+
7
+ st.title('Generador de mariposas')
8
+ st.write('Este es un modelo Light GAN entrenado')
9
+
10
+ #Barra lateral
11
+ st.sidebar.subheader('!Esta mariposa no existe, puedes creelo')
12
+ st.sidebar.image('assets/logo.png', width=200)
13
+ st.sidebar.caption('Demo creado')
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
+
23
+ def corre():
24
+ with st.spinner('Generando, espero un poco...')
25
+ ims = genera(modelo_gan, n_mariposas)
26
+ st.session_state['ims']= ims
27
+
28
+ if 'ims' not in st.session_state:
29
+ st.session_state['ims'] = None
30
+ corre()
31
+
32
+ ims = st.session_state['ims']
33
+
34
+ corre_boton = st.button(
35
+ 'Genera mariposas porfa',
36
+ on_click=corre,
37
+ help='Estamos en vuelo, abrocha tu cinturon'
38
+ )
39
+
40
+ if ims is not None:
41
+ cols = st.columns(n_mariposas)
42
+ for j, im in enumerate(ims):
43
+ i = j % n_mariposas
44
+ cols[i].image(im, use_column_widht=True)
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from huggan.pytorch.lightweights_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).deatch().cpu().numpy().astype(np.uint8)
14
+ return ims
15
+