florentine-families-graph / huggingface_spaces_tool_template.md
dwb2023's picture
update hf tool template
3628bde verified

A newer version of the Gradio SDK is available: 5.14.0

Upgrade

Hugging Face Spaces Tool Template for Agents

  • a quick tribute to Aymeric 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 and transformers implementations for some of these methods and you'll be fine

m-ric/text-to-image repo

  • 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"
}