AIPlane2 / app.py
PrakhAI's picture
Update app.py
fae14cf
raw
history blame
1.54 kB
import streamlit as st
from PIL import Image
import jax
import jax.numpy as jnp # JAX NumPy
import numpy as np
from huggingface_hub import HfFileSystem
from flax.serialization import msgpack_restore, from_state_dict
import time
from generator import Generator, LATENT_DIM
import math
generator = Generator()
variables = generator.init(jax.random.PRNGKey(0), jnp.zeros([1, LATENT_DIM]), training=False)
fs = HfFileSystem()
with fs.open("PrakhAI/AIPlane2/g_checkpoint.msgpack", "rb") as f:
g_state = from_state_dict(variables, msgpack_restore(f.read()))
def sample_latent(batch, key):
return jax.random.normal(key, shape=(batch, LATENT_DIM))
def to_img(normalized):
return ((normalized+1)*255./2.).astype(np.uint8)
def generate_images(previous=None):
latents = sample_latent(16, jax.random.PRNGKey(int(1_000_000 * time.time())))
if previous:
latents = np.repeat([previous], repeats=16, axis=0) + 0.25 * latents
(g_out128, _, _, _, _, _) = generator.apply({'params': g_state['params'], 'batch_stats': g_state['batch_stats']}, latents, training=False)
img = np.array(to_img(g_out128))
for row in range(4):
with st.container():
for (col_idx, col) in enumerate(st.columns(4)):
with col:
idx = row*grid_width + col_idx
st.image(Image.fromarray(img[idx]))
st.button(label="Generate similar", on_click=generate_images, args=latents[idx])
st.write("The model and its details are at https://huggingface.co/PrakhAI/AIPlane2")
if st.button('Generate Random'):
generate_images()