ciCic commited on
Commit
5a3c9bd
1 Parent(s): 3e38ad5
Files changed (5) hide show
  1. app.py +47 -0
  2. images/34.png +0 -0
  3. images/6.png +0 -0
  4. images/7.png +0 -0
  5. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+
4
+ import torch
5
+ from diffusers import AutoencoderTiny
6
+ from torchvision.transforms.functional import to_pil_image, center_crop, resize, to_tensor
7
+
8
+ device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
9
+
10
+ model_id = "madebyollin/taesd"
11
+ vae = AutoencoderTiny.from_pretrained(model_id, safetensors=True).to(device)
12
+
13
+
14
+ @torch.no_grad()
15
+ def encode(image):
16
+ DIM = 512
17
+ processed = center_crop(resize(image, DIM), DIM)
18
+ tensor = to_tensor(processed).unsqueeze(0).to(device)
19
+ latents = vae.encoder(tensor)
20
+ scaled = vae.scale_latents(latents).mul_(255).round_().byte()
21
+ return to_pil_image(scaled[0])
22
+
23
+
24
+ astronaut = os.path.join(os.path.dirname(__file__), "images/6.png")
25
+
26
+
27
+ def app():
28
+ return gr.Interface(encode,
29
+ gr.Image(type="pil",
30
+ mirror_webcam=False,
31
+ label='512x512',
32
+ value=astronaut),
33
+ gr.Image(type="pil",
34
+ image_mode="RGBA",
35
+ label='64x64',
36
+ height=256,
37
+ width=256
38
+ ),
39
+ examples=[
40
+ astronaut,
41
+ os.path.join(os.path.dirname(__file__), "images/7.png"),
42
+ os.path.join(os.path.dirname(__file__), "images/34.png")
43
+ ], allow_flagging='never', title='Image Encoder')
44
+
45
+
46
+ if __name__ == "__main__":
47
+ app().launch()
images/34.png ADDED
images/6.png ADDED
images/7.png ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ transformers
4
+ diffusers
5
+ pillow
6
+ accelerate