-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.py
38 lines (27 loc) · 922 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import List
import torch
from PIL.Image import Image
from diffusers import DiffusionPipeline
def clean_token_from_prefixes_and_suffixes(token: str) -> str:
"""
Removes all the known token prefixes and suffixes
Args:
token (`str`): string with token
Returns:
`str`: clean token
"""
# removes T5 prefix
token = token.lstrip('▁')
# removes BERT/GPT-2 prefix
token = token.lstrip('Ġ')
# removes CLIP suffix
token = token.rstrip('</w>')
return token
def transform_images_to_pil_format(all_generated_images: List[torch.Tensor], pipe: DiffusionPipeline) -> List[List[Image]]:
pil_images = []
for im in all_generated_images:
if isinstance(im, torch.Tensor):
im = im.detach().cpu().numpy()
im = pipe.numpy_to_pil(im)
pil_images.append(im)
return pil_images