latents-decoder / app.py
ciCic's picture
init
31d1b41
raw
history blame
No virus
1.63 kB
import gradio as gr
import os
import torch
from diffusers import AutoencoderTiny
from torchvision.transforms.functional import to_pil_image, to_tensor
device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
model_id = "madebyollin/taesd"
vae = AutoencoderTiny.from_pretrained(model_id, safetensors=True).to(device)
@torch.no_grad()
def decode(image):
t = to_tensor(image).unsqueeze(0).to(device)
unscaled_t = vae.unscale_latents(t)
reconstructed = vae.decoder(unscaled_t).clamp(0, 1)
return to_pil_image(reconstructed[0])
astronaut = os.path.join(os.path.dirname(__file__), "images/21.encoded.png")
def app():
return gr.Interface(decode,
gr.Image(type="pil",
image_mode="RGBA",
mirror_webcam=False,
label='64x64',
value=astronaut),
gr.Image(type="pil",
image_mode="RGB",
label='512x512',
show_share_button=True,
height=256,
width=256
),
examples=[
astronaut,
os.path.join(os.path.dirname(__file__), "images/18.encoded.png"),
os.path.join(os.path.dirname(__file__), "images/20.encoded.png")
])
if __name__ == "__main__":
app().launch()