Spaces:
Runtime error
Runtime error
Commit
·
f45daac
1
Parent(s):
4a1ab12
first commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
from utils import load_model, genera
|
| 4 |
+
|
| 5 |
+
## Mean page
|
| 6 |
+
st.title("Butterflies Generator")
|
| 7 |
+
st.write("This is a Light GAN model trained and used by me")
|
| 8 |
+
|
| 9 |
+
## Sidebar
|
| 10 |
+
st.sidebar.subheader("This butterfly not is real, can you believe it?")
|
| 11 |
+
st.sidebar.image("assets/logo.png", width=200)
|
| 12 |
+
st.sidebar.caption("Demo created live")
|
| 13 |
+
|
| 14 |
+
## Load model
|
| 15 |
+
repo_id = "ceyda/butterfly_cropped_uniq1K_512"
|
| 16 |
+
model_gan = load_model(repo_id)
|
| 17 |
+
|
| 18 |
+
## We generated four butterflies
|
| 19 |
+
n_butterflies = 4
|
| 20 |
+
|
| 21 |
+
def run():
|
| 22 |
+
with st.spinner("Generating, wait a bit..."):
|
| 23 |
+
ims = genera(model_gan, n_butterflies)
|
| 24 |
+
st.session_state["ims"] = ims
|
| 25 |
+
|
| 26 |
+
if "ims" not in st.session_state:
|
| 27 |
+
st.session_state["ims"] = None
|
| 28 |
+
run()
|
| 29 |
+
|
| 30 |
+
ims = st.session_state["ims"]
|
| 31 |
+
|
| 32 |
+
run_button = st.button(
|
| 33 |
+
"Generate",
|
| 34 |
+
on_click=run,
|
| 35 |
+
help="We are in fly"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if ims is not None:
|
| 39 |
+
cols = st.columns(n_butterflies)
|
| 40 |
+
for j, im in enumerate(ims):
|
| 41 |
+
i = j % n_butterflies
|
| 42 |
+
cols[i].image(im, use_column_width=True)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
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_cropper_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).detach().cpu().numpy().astype(np.uint8)
|
| 14 |
+
return ims
|