Spaces:
Sleeping
Sleeping
alexrods
commited on
Commit
•
b6bab6d
1
Parent(s):
519077a
Load files; app.py and utils.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from utils import load_model, generate
|
4 |
+
|
5 |
+
|
6 |
+
# main page
|
7 |
+
st.title("Butterflies generator")
|
8 |
+
st.write("LightGan Model to generate Butterflies")
|
9 |
+
|
10 |
+
# sidebar
|
11 |
+
st.sidebar.subheader("Butterfly generated with AI")
|
12 |
+
st.sidebar.image("assets/logo.png", width=200)
|
13 |
+
st.sidebar.caption("Demo")
|
14 |
+
|
15 |
+
# Load model
|
16 |
+
repo_id = "ceyda/butterfly_cropped_uniq1K_512"
|
17 |
+
modelo_gan = load_model(repo_id)
|
18 |
+
|
19 |
+
# Generate 4 btfs
|
20 |
+
n_btfs = 4
|
21 |
+
|
22 |
+
def run():
|
23 |
+
with st.spinner("Generating... "):
|
24 |
+
ims = generate(model_gan, n_btfs)
|
25 |
+
st.session_state["ims"] = ims
|
26 |
+
|
27 |
+
if "ims" not in st.session_state:
|
28 |
+
st.session_state["ims"] = None
|
29 |
+
run()
|
30 |
+
|
31 |
+
ims = st.session_state["ims"]
|
32 |
+
|
33 |
+
run_button = st.button(
|
34 |
+
"Generate Butterfly",
|
35 |
+
on_click = run,
|
36 |
+
help = "... flying ..."
|
37 |
+
)
|
38 |
+
|
39 |
+
if ims is not None:
|
40 |
+
cols = st.columns(n_btfs)
|
41 |
+
for j, im in enumerate(ims):
|
42 |
+
i = j % n_btfs
|
43 |
+
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 |
+
def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
|
7 |
+
gan = LightweightGAN.from_pretrained(model_name, version=model_version)
|
8 |
+
gan.eval()
|
9 |
+
return gan
|
10 |
+
|
11 |
+
|
12 |
+
def generate(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).detach().cpu().numpy().astype(np.uint8)
|
16 |
+
return ims
|