Your Own GPU-Powered Image Generator with HF Jobs
Ever wanted to generate images with powerful AI models like Stable Diffusion right from your terminal, but you don't have a dedicated GPU? Do you prefer the command line over web interfaces for your creative workflows? If so, this little guide is for you.
Today, we'll walk through the process of creating a personal, command-line tool to generate AI images with a single command, powered by the remote infrastructure of Hugging Face Jobs. By the end of this article, you'll be able to type one line in your terminal and, a few moments later, have a beautiful, AI-generated image saved directly to your Hugging Face account.
Create Your Image Gallery on the Hub
Before we can generate anything, we need a place to save our masterpieces. The easiest way is to create a public dataset repository, which will act as our personal gallery.
- Go to the
New Dataset
page: https://huggingface.co/new-dataset - Choose a name for your gallery, for example,
images
. - Make sure it's set to
Public
so you can easily view and share your creations.
That's it! Now you have a destination for your images, like HF_USERNAME/images
.
One Script is All You Need
Now, let's talk about the heart of our operation: the Python script. This is where the real magic happens, thanks to a powerful feature supported by uv
, the tool hf jobs
uses under the hood.
Forget about managing separate requirements.txt
files or building complex Docker images for simple tasks. uv
allows us to embed a script's dependencies directly inside the file itself.
Look at the very top of our script, hosted here:
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "huggingface-hub[hf_transfer]",
# "pillow",
# "torch",
# "diffusers",
# "accelerate",
# "transformers",
# "python-slugify",
# ]
# ///
This special block, enclosed in ///
, is an inline dependency specification. When hf jobs uv run
executes the script, it first reads this header, instantly creates a temporary environment, and installs the exact libraries listed.
The code and its requirements live together. This makes sharing and executing scripts incredibly simple and reliable.
The Magic Command
Now for the fun part. Open your terminal and get ready to run the magic command:
hf jobs uv run --flavor a10g-large \
https://huggingface.co/datasets/angt/scripts/resolve/main/generate-image.py \
--repo "$HF_USERNAME/images" \
--prompt "'A dramatic photo of a majestic cat sitting on a throne, cinematic lighting'" \
--hf-token $HF_TOKEN
Let's break down the command so we understand exactly what each part does:
hf jobs uv run
: The main command that tells to run a remote script usinguv
.--flavor a10g-large
: This is where we request our hardware. We're asking for a powerful A10G GPU to get the job done quickly.https://...
: The direct URL to our single, all-in-one Python script. You don't even need to download it!--repo "$HF_USERNAME/images"
: The ID of the dataset.--prompt "'...''"
: Your creative prompt. Note: You need to wrap your prompt in double quotes containing single quotes to avoid shell interpretation issues.--hf-token $HF_TOKEN
: Your Hugging Face API token.
And voilĂ ! After a few moments, the job completes and a new image will appear in your repository. Here is the majestic cat I got from my prompt:
You're in Control
The real magic here isn't just about this one script. It's the realization of how incredibly easy it is to adapt this workflow for any task. Think about it: need to process a dataset? Transcribe audio files? Generate text? The blueprint is the same: write a simple Python script with its dependencies embedded, and launch it into the cloud with a single command.
You now have the power to create your own suite of serverless AI tools for whatever you can imagine. The possibilities are endless.
So, what are you waiting for? Let's get hacking!