Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Last active June 4, 2023 12:18
Show Gist options
  • Save rsbohn/e2aa6d60d8bece376b6f7a622f47d3f1 to your computer and use it in GitHub Desktop.
Save rsbohn/e2aa6d60d8bece376b6f7a622f47d3f1 to your computer and use it in GitHub Desktop.
Placeholder image_generator for transformers.agent
from PIL import Image, ImageDraw, ImageFilter, ImageFont
import numpy as np
from transformers.tools import Tool
class PlaceholderImageTool(Tool):
"""Replacement 'image_generator'
- transformers HfAgent likes to use the image_generator tool.
- I don't have the disk space for that.
- This replacement provides a random placeholder image instead.
Install it thusly: `agent.toolbox['image_generator']=PlaceholderImageTool()`
"""
name = "image_generator"
description = "I will generate an image based on a prompt."
inputs = ["text"]
outputs = ["image"]
def sepia(self, dim:int=64) -> Image:
"produce a sepia image, size=(dim,dim,3)"
data = np.random.randint(0,255,(dim,dim,3), dtype=np.uint8)
image = Image.fromarray(data)
sepia_matrix = [
0.393, 0.769, 0.189, 0,
0.349, 0.686, 0.168, 0,
0.272, 0.534, 0.131, 0
]
applied = image.convert('RGB', sepia_matrix)
return applied
def __call__(self, prompt):
"returns a sepia image overlaid with a random digit (1,6 inclusive)"
DIM=64
image = self.sepia(DIM)
image = image.filter(ImageFilter.SMOOTH)
draw = ImageDraw.Draw(image)
digit = str(np.random.randint(1,7))
font = ImageFont.truetype("arial.ttf", size=48)
draw.text((DIM//2,DIM//2), digit, fill=(0,0,0), font=font, anchor="mm")
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment