# Hugging Face Spaces Tool Template for Agents - a quick tribute to [Aymeric](https://huggingface.co/m-ric) for this simple but effective exapmle of how to build and serve agents using Hugging Face spaces - the one thing I've noticed with some of the reusable tools created by the HF team is that they appear to still use a number of potentially older transformer libraries - just be aware that there are differences between the [smolagents](https://huggingface.co/docs/smolagents/main/en/reference/tools#smolagents.launch_gradio_demo) and [transformers](https://huggingface.co/docs/transformers/v4.48.0/en/main_classes/agent#transformers.launch_gradio_demo) implementations for some of these methods and you'll be fine ## [m-ric/text-to-image repo](https://huggingface.co/spaces/m-ric/text-to-image) - the structure and content below is a full extract of the entire repo - helps to understand the config and code required to publish and launch a reusable tool - refer to the comment under `app.py` for an example of where the older transformers import is still used ### File: README.md - Type: txt - Size: 247 bytes - Created: 2025-01-22 19:08:45 UTC - Modified: 2025-01-22 19:08:45 UTC #### Content: ``` --- title: Text To Image emoji: 👀 colorFrom: blue colorTo: indigo sdk: gradio sdk_version: 4.29.0 app_file: app.py pinned: false tags: - tool --- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference ``` ### File: app.py - Type: python - Size: 114 bytes - Created: 2025-01-22 19:08:45 UTC - Modified: 2025-01-22 19:08:45 UTC #### Content: ``` # the launch_gradio_demo import should likely be from smolagents from transformers import launch_gradio_demo from tool import TextToImageTool launch_gradio_demo(TextToImageTool) ``` ### File: requirements.txt - Type: txt - Size: 26 bytes - Created: 2025-01-22 19:08:45 UTC - Modified: 2025-01-22 19:08:45 UTC #### Content: ``` huggingface_hub smolagents ``` ### File: tool.py - Type: python - Size: 635 bytes - Created: 2025-01-22 19:08:45 UTC - Modified: 2025-01-22 19:08:45 UTC #### Content: ``` from smolagents import Tool from huggingface_hub import InferenceClient class TextToImageTool(Tool): description = "This tool creates an image according to a prompt, which is a text description." 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."}} output_type = "image" model_sdxl = "black-forest-labs/FLUX.1-schnell" client = InferenceClient(model_sdxl) def forward(self, prompt): return self.client.text_to_image(prompt) ``` ### File: tool_config.json - Type: json - Size: 414 bytes - Created: 2025-01-22 19:08:45 UTC - Modified: 2025-01-22 19:08:45 UTC #### Content: ``` { "description": "This is a tool that creates an image according to a prompt, which is a text description.", "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.\"}}", "name": "image_generator", "output_type": "image", "tool_class": "tool.TextToImageTool" } ```