root commited on
Commit
7d7ab74
1 Parent(s): 26d4f77

subiendo utils, add para crear demo (streamlit

Browse files
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Comunidad Platzi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
LICENSE:Zone.Identifier ADDED
File without changes
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Mariposas
3
- emoji: 馃捇
4
  colorFrom: blue
5
- colorTo: gray
6
  sdk: streamlit
7
- sdk_version: 1.42.1
8
  app_file: app.py
9
- pinned: false
10
- short_description: Demo de clasificaci贸n de mariposas
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Butterfly GAN
3
+ emoji: 馃
4
  colorFrom: blue
5
+ colorTo: yellow
6
  sdk: streamlit
7
+ sdk_version: 1.2.0
8
  app_file: app.py
9
+ pinned: true
10
+ license: apache-2.0
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
README.md:Zone.Identifier ADDED
File without changes
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import carga_modelo, genera
4
+
5
+ ## P谩gina principal
6
+ st.title("Butterfly GAN (GAN de mariposas)")
7
+ st.write(
8
+ "Modelo Light-GAN entrenado con 1000 im谩genes de mariposas tomadas de la colecci贸n del Museo Smithsonian."
9
+ )
10
+
11
+ ## Barra lateral
12
+ st.sidebar.subheader("隆Esta mariposa no existe! Ni en Am茅rica Latina 馃く.")
13
+ st.sidebar.image("assets/logo.png", width=200)
14
+ st.sidebar.caption(
15
+ f"[Modelo](https://huggingface.co/ceyda/butterfly_cropped_uniq1K_512) y [Dataset](https://huggingface.co/datasets/huggan/smithsonian_butterflies_subset) usados."
16
+ )
17
+ st.sidebar.caption(f"*Disclaimers:*")
18
+ st.sidebar.caption(
19
+ "* Este demo es una versi贸n simplificada del creado por [Ceyda Cinarel](https://github.com/cceyda) y [Jonathan Whitaker](https://datasciencecastnet.home.blog/) ([link](https://huggingface.co/spaces/huggan/butterfly-gan)) durante el hackathon [HugGan](https://github.com/huggingface/community-events). Cualquier error se atribuye a [Omar Espejel](https://twitter.com/espejelomar)."
20
+ )
21
+ st.sidebar.caption(
22
+ "* Modelo basado en el [paper](https://openreview.net/forum?id=1Fqg133qRaI) *Towards Faster and Stabilized GAN Training for High-fidelity Few-shot Image Synthesis*."
23
+ )
24
+
25
+ ## Cargamos modelo
26
+ repo_id = "ceyda/butterfly_cropped_uniq1K_512"
27
+ version_modelo = "57d36a15546909557d9f967f47713236c8288838"
28
+ modelo_gan = carga_modelo(repo_id, version_modelo)
29
+
30
+ ## Generamos 4 mariposas
31
+ n_mariposas = 4
32
+
33
+ ## Funci贸n que genera mariposas y lo guarda como un estado de la sesi贸n
34
+ def corre():
35
+ with st.spinner("Generando, espera un poco..."):
36
+ ims = genera(modelo_gan, n_mariposas)
37
+ st.session_state["ims"] = ims
38
+
39
+
40
+ ## Si no hay una imagen generada entonces generala
41
+ if "ims" not in st.session_state:
42
+ st.session_state["ims"] = None
43
+ corre()
44
+
45
+ ## ims contiene las im谩genes generadas
46
+ ims = st.session_state["ims"]
47
+
48
+ ## Si la usuaria da click en el bot贸n entonces corremos la funci贸n genera()
49
+ corre_boton = st.button(
50
+ "Genera mariposas, porfa.",
51
+ on_click=corre,
52
+ help="Estamos en pleno vuelo, puede tardar.",
53
+ )
54
+
55
+ if ims is not None:
56
+ cols = st.columns(n_mariposas)
57
+ for j, im in enumerate(ims):
58
+ i = j % n_mariposas
59
+ cols[i].image(im, use_column_width=True)
app.py:Zone.Identifier ADDED
File without changes
assets/logo.png ADDED
assets/logo.png:Zone.Identifier ADDED
File without changes
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ libgl1
packages.txt:Zone.Identifier ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/community-events.git@3fea10c5d5a50c69f509e34cd580fe9139905d04#egg=huggan
2
+ transformers
3
+ faiss-cpu
4
+ paddlehub
5
+ paddlepaddle
requirements.txt:Zone.Identifier ADDED
File without changes
utils.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
13
+ ## Usamos el modelo GAN para generar im谩genes
14
+ def genera(gan, batch_size=1):
15
+ with torch.no_grad():
16
+ ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
17
+ ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
18
+ return ims
utils.py:Zone.Identifier ADDED
File without changes