Ken Kina commited on
Commit
372a3dc
1 Parent(s): 72adfaf

feat: app.py and utils.py

Browse files
Files changed (2) hide show
  1. app.py +43 -0
  2. utils.py +14 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import load_model, generate
4
+
5
+ ## Main page
6
+ st.title("Butterfly generator")
7
+ st.write("Modelo Light GAN")
8
+
9
+ ## Lateral bar
10
+ st.sidebar.subheader("No existing butterfly")
11
+ st.sidebar.image("assets/logo.png", width=200)
12
+ st.sidebar.caption("In live demo")
13
+
14
+ ## Loading the model
15
+ repo_id = "ceyda/butterfly_croped_uniq1K_512"
16
+ model_gan = load_model(repo_id)
17
+
18
+ ## Generate 4 butterflies
19
+ n_butterflies = 4
20
+
21
+
22
+ def execute():
23
+ with st.spinner("Generating..."):
24
+ ims = generate(model_gan, n_butterflies)
25
+ st.session_state["ims"] = ims
26
+
27
+ if "ims" not in st.session_state:
28
+ st.session_state["ims"] = None
29
+ execute()
30
+
31
+ ims = st.session_state["ims"]
32
+
33
+ exec_button = st.button(
34
+ "Generate!",
35
+ on_click=execute,
36
+ help="Help message"
37
+ )
38
+
39
+ if ims is not None:
40
+ cols = st.columns(n_butterflies)
41
+ for j, im in enumerate(ims):
42
+ i = j % n_butterflies
43
+ cols[i].image(im, use_column_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 load_model(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 generate(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