Spaces:
Runtime error
Runtime error
johnometalman
commited on
Commit
路
cafe3a1
1
Parent(s):
b542626
Desarrollo de App y utils
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 light GAN entrenado para generaci贸n de mariposas')
|
9 |
+
|
10 |
+
## Barra Lateral
|
11 |
+
st.sidebar.subheader('Esta mariposa no existe, 驴Puedes creerlo?')
|
12 |
+
st.sidebar.image('assets/logo.png', width=200)
|
13 |
+
st.sidebar.caption('Demo creado en vivo')
|
14 |
+
|
15 |
+
|
16 |
+
## Cargamos el modelo
|
17 |
+
repo_id = 'ceyda/butterfly_cropped_uniq1K_512'
|
18 |
+
modelo_gan = carga_modelo(repo_id)
|
19 |
+
|
20 |
+
|
21 |
+
## No de Mariposas
|
22 |
+
n_mariposas = 4
|
23 |
+
|
24 |
+
|
25 |
+
## Core de la app
|
26 |
+
|
27 |
+
def corre():
|
28 |
+
with st.spinner('Generando espera un poco....'):
|
29 |
+
ims = genera(modelo_gan, n_mariposas)
|
30 |
+
st.session_state('ims') = ims
|
31 |
+
|
32 |
+
if 'ims' not in st.session_state:
|
33 |
+
st.session_state['ims'] = None
|
34 |
+
corre()
|
35 |
+
|
36 |
+
ims = st.session_state['ims']
|
37 |
+
|
38 |
+
corre_boton = st.button(
|
39 |
+
'Genera mariposa por favor',
|
40 |
+
on_click=corre,
|
41 |
+
help='Estamos en vuelo, abrocha tu cintur贸n'
|
42 |
+
)
|
43 |
+
|
44 |
+
if ims is not None:
|
45 |
+
cols = st.columns(n_mariposas)
|
46 |
+
for j, im in enumerate(ims):
|
47 |
+
i = j % n_mariposas
|
48 |
+
cols[i].image(im, use_column_width=True)
|
utils.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
|
4 |
+
|
5 |
+
|
6 |
+
## Cargamos el modelo desde el Hub de Hugging Face
|
7 |
+
def carga_modelo(model_name='ceyda/butterfly_cropped_uniq1K_512', model_version=None):
|
8 |
+
gan = LightweightGAN.from_pretrained(model_name, version=model_version)
|
9 |
+
gan.eval()
|
10 |
+
return gan
|
11 |
+
|
12 |
+
def genera(gan, batch_size=1):
|
13 |
+
with torch.no_grad():
|
14 |
+
ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp(0.0, 1.0) * 255
|
15 |
+
ims = ims.permute(0,2,3,1).deatch().cpu().numpy().astype(np.unit8)
|
16 |
+
return ims
|