Calculate the number of tokens for a list of images.
(images: list[str])
| 42 | |
| 43 | |
| 44 | def calc_image_tokens(images: list[str]): |
| 45 | """ |
| 46 | Calculate the number of tokens for a list of images. |
| 47 | """ |
| 48 | tokens = 0 |
| 49 | for image in images: |
| 50 | with open(image, "rb") as f: |
| 51 | width, height = Image.open(f).size |
| 52 | if width > 1024 or height > 1024: |
| 53 | if width > height: |
| 54 | height = int(height * 1024 / width) |
| 55 | width = 1024 |
| 56 | else: |
| 57 | width = int(width * 1024 / height) |
| 58 | height = 1024 |
| 59 | h = ceil(height / 512) |
| 60 | w = ceil(width / 512) |
| 61 | tokens += 85 + 170 * h * w |
| 62 | return tokens |
| 63 | |
| 64 | |
| 65 | class LLM: |