Views
No views yet
1git clone https://github.com/anothy1/multi-modal-tokenizers
2pip install ./multi-modal-tokenizerspip install multi-modal-tokenizersDalleTokenizer to encode and decode images.1import requests
2import PIL
3import io
4from multi_modal_tokenizers import DalleTokenizer, MixedModalTokenizer
5from IPython.display import display
6
7def download_image(url):
8 resp = requests.get(url)
9 resp.raise_for_status()
10 return PIL.Image.open(io.BytesIO(resp.content))
11
12# Download an image
13img = download_image('https://assets.bwbx.io/images/users/iqjWHBFdfxIU/iKIWgaiJUtss/v2/1000x-1.jpg')
14
15# Load the DalleTokenizer from Hugging Face repository
16image_tokenizer = DalleTokenizer.from_hf("anothy1/dalle-tokenizer")
17
18# Encode the image
19tokens = image_tokenizer.encode(img)
20print("Encoded tokens:", tokens)
21
22# Decode the tokens back to an image
23reconstructed = image_tokenizer.decode(tokens)
24
25# Display the reconstructed image
26display(reconstructed)MixedModalTokenizer for tokenizing and decoding mixed-modal inputs (text and images).1from transformers import AutoTokenizer
2from multi_modal_tokenizers import MixedModalTokenizer
3from PIL import Image
4
5# Load a pretrained text tokenizer from Hugging Face
6text_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
7
8# Create a MixedModalTokenizer
9mixed_tokenizer = MixedModalTokenizer(
10 text_tokenizer=text_tokenizer,
11 image_tokenizer=image_tokenizer,
12 device="cpu"
13)
14
15# Example usage
16text = "This is an example with <new_image> in the middle."
17img_path = "path/to/your/image.jpg"
18image = Image.open(img_path)
19
20# Encode the text and image
21encoded = mixed_tokenizer.encode(text=text, images=[image])
22print("Encoded mixed-modal tokens:", encoded)
23
24# Decode the sequence back to text and image
25decoded_text, decoded_images = mixed_tokenizer.decode(encoded)
26print("Decoded text:", decoded_text)
27for idx, img in enumerate(decoded_images):
28 img.save(f"decoded_image_{idx}.png")