import os from smolagents import Tool from huggingface_hub import InferenceClient SAVE_PATH = "/tmp/generated_image.png" class TextToImageTool(Tool): # description = "This tool creates an image according to a prompt, which is a text description. When using this tool in a code snippet, the last line of the snippet should be the the tool call (so the image is shown on screen)." description = f"This tool creates an image according to a prompt, which is a text description. For displaying the image, use the returned PIL object. This tool saves the image to `{SAVE_PATH}`, so there is no need to save it externally." name = "image_generator" inputs = {"prompt": {"type": "string", "description": "The image generator prompt. Don't hesitate to add details in the prompt to make the image look better, like 'high-res, photorealistic', etc."}, # "save_path": {"type": "string", "description": "A file path in `/tmp` to save the image to. The file path extenstion should be .png", "nullable": True} } output_type = "image" model_sdxl = "black-forest-labs/FLUX.1-schnell" client = InferenceClient(model_sdxl, token=os.environ["HUB_TOKEN"]) def forward(self, prompt): image = self.client.text_to_image(prompt) image.save(SAVE_PATH) return image