Spaces:
Running
Running
Image generator added
Browse files
app.py
CHANGED
@@ -1,7 +1,38 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
st.markdown('This is my description')
|
5 |
|
6 |
-
|
7 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
from utils import load_model, generate
|
|
|
4 |
|
5 |
+
st.title('Image Generator with GAN')
|
6 |
+
st.markdown('Butterflies generator')
|
7 |
+
|
8 |
+
# Sidebar
|
9 |
+
st.sidebar.subheader('This butterfly does not exist, it is completely generated!')
|
10 |
+
st.sidebar.image('assets/logo.png', width=200)
|
11 |
+
st.sidebar.caption('Demo was just created.')
|
12 |
+
|
13 |
+
# Loading model
|
14 |
+
repo_id = 'ceyda/butterfly_cropped_uniq1K_512'
|
15 |
+
gan_model = load_model(repo_id)
|
16 |
+
|
17 |
+
# Generate 4 butterflies
|
18 |
+
n_butterflies = 4
|
19 |
+
|
20 |
+
def run():
|
21 |
+
with st.spinner('Generating...'):
|
22 |
+
ims = generate(gan_model, n_butterflies)
|
23 |
+
st.session_state['ims'] = ims
|
24 |
+
|
25 |
+
if 'ims' not in st.session_state:
|
26 |
+
st.session_state['ims'] = None
|
27 |
+
run()
|
28 |
+
|
29 |
+
ims = st.session_state['ims']
|
30 |
+
|
31 |
+
run_button = st.button(
|
32 |
+
'Generate butterflies',
|
33 |
+
on_click=run,
|
34 |
+
help='We are about to start to generate'
|
35 |
+
)
|
36 |
+
|
37 |
+
if ims is not None:
|
38 |
+
cols = st.columns(n_butterflies)
|
utils.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
from huggan.pytorch.lightweight_gan.lighweight_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).detach().cpu().numpy().astype(np.uint8)
|
14 |
+
return ims
|