Gradio spaces are the perfect agent tools!
TLDR: You can use spaces on the hub as tools for agents, with Gradio and smolagents.
AI agents are becoming increasingly capable of handling complex tasks and interacting with the world in cool ways. To fully realize their potential, AI agents need a wide range of tools. Spaces are a handy platform for building and sharing these tools. Agent tools could perform basic functional tasks like transforming data, all the way up to sophisticated AI tasks like generating images.
Here are some advantages:
- Existing Capabilities: Access loads of pre-built applications from the AI community.
- Enhanced Compute: Take advantage of free and scalable compute through ZeroGPU and Pro subscriptions.
- Easy development: Build and test your tools as Gradio apps, and let Gradio take care of deployment and integration.
How it Works
Three steps to guide you through the setup of a space as an agent tool. Below are some detailed references to tutorials for each step.
Step 1: Identify a suitable space
First, you will need to find or build a space for your project. To help you out, I created a collection of some handy tools for tasks like data visualisation, measurement, and image generation. If you’re looking for something more specific, search the Hugging Face Hub for Spaces that align with your agent's needs.
If you want to build a space of your own, it’s straightforward to start from a Gradio Interface
. Almost anything you can build and deploy within the Interface object, is compatible with smolagents. If you’re looking for an example, check out the Plotly visualisation tool I made.
In the next section I will show how Gradio presents an API which smolagents can interact with.
Step 2: Integrate into Agent Framework
Next, you’ll need to integrate your agent with your space as a tool. Use smolagents
API to interact with the Gradio app on Spaces. Smolagents can then process the responses from the space to be incorporated into the agent's workflow.
You can directly import a Space from the Hub as a tool using the Tool.from_space() method. Provide the Space's ID on the Hub, its name, and a description to help your agent understand its function. This will handle parameters and responses using the Gradio client integration.
Let's import the FLUX.1-dev Space from the Hub and use it to generate an image:
image_generation_tool = Tool.from_space(
"black-forest-labs/FLUX.1-schnell", # You can also add localhost url here for testing 🤯
name="image_generator",
description="Generate an image from a prompt"
)
image_generation_tool("A sunny beach")
You can then use this tool like a generic python function. The inputs from the Gradio Interface
are mapped to function parameters and the outputs are presented in an object.
Step 3: Customise your agent’s tool usage
The smolagents library can handle your space like any other tool so you can customize the prompts to deal with your use case. For example, let's improve the prompt "a rabbit wearing a space suit" and generate an image:
from smolagents import CodeAgent, HfApiModel
model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
agent = CodeAgent(tools=[image_generation_tool], model=model)
agent.run(
"Improve this prompt, then generate an image of it.", prompt="A rabbit wearing a space suit"
)
Conclusion
By following these steps using Hugging Face Spaces, Gradio, and smolagents, you can create capable AI agents using existing spaces on the hub. If you want to dive deeper into this problem, check out this material.