Wire It, Run It, Deploy It: AI Workflows in Gradio
Wire It, Run It, Deploy It: AI Workflows in Gradio
连接、运行、部署:Gradio 中的 AI 工作流
Most interesting AI apps are pipelines. You generate an image, then cut out its background if you want to, or edit it into something new. You write a script, then generate a voice for it, or swap the voice while keeping the script the same. We usually wire these steps together in Python, and the moment something looks off we go back to print-debugging to find which step produced the odd value. 大多数有趣的 AI 应用本质上都是流水线(pipelines)。你生成一张图片,然后根据需要抠除背景,或者将其编辑成新的内容。你写一段脚本,然后为其生成配音,或者在保持脚本不变的情况下更换配音。我们通常在 Python 中将这些步骤串联起来,一旦出现问题,我们就得回到 print 调试,去查找究竟是哪一步产生了异常值。
gr.Workflow, built right into Gradio, makes the pipeline the interface. You describe your steps as a graph of typed nodes, and Gradio serves a drag-and-drop canvas where every node is runnable and every intermediate result is visible. The same graph is also a REST API and a one-command deploy to Hugging Face Spaces.
直接内置于 Gradio 中的 gr.Workflow 将流水线变成了交互界面。你只需将步骤描述为类型化节点的图表,Gradio 就会提供一个拖拽式画布,其中每个节点都是可运行的,且每个中间结果都是可见的。同一个图表同时也是一个 REST API,并且支持一键部署到 Hugging Face Spaces。
The best way to get the idea is to see a few workflows in action. Every app below is a live Huggingface Space you can open, run, and duplicate. 理解这一概念的最佳方式是看看几个实际运行的工作流。下方每个应用都是一个实时运行的 Hugging Face Space,你可以直接打开、运行并复制它们。
Edit an Image
编辑图片
Upload an image, type an edit (“turn it into a snowy winter scene”, “add sunglasses”, “make the car red”), and get the edited photo back. The whole app is a single node calling Qwen-Image-Edit on Hugging Face Inference Providers. 上传一张图片,输入编辑指令(例如“把它变成雪景”、“添加太阳镜”、“把车变成红色”),即可获得编辑后的照片。整个应用仅由一个节点组成,该节点调用了 Hugging Face 推理提供商(Inference Providers)上的 Qwen-Image-Edit 模型。 👉 Try the Image Editor (尝试图片编辑器)
Chain real models into a media studio
将真实模型串联成媒体工作室
One graph, three pipelines. Start with a prompt and generate an image with FLUX, then pass it to a background-removal Gradio Space to turn it into a sticker. A topic becomes a voiceover through a text-to-speech Gradio Space, while the same topic becomes a catchy episode title through an LLM call. That’s one canvas, two model calls through Hugging Face Inference Providers, and two calls to Gradio Spaces. Since this is a workflow, each of the three outputs also gets its own REST endpoint: /sticker, /voiceover, and /episode_title. You can call any of them directly from code without opening the UI. 一个图表,三条流水线。从一个提示词开始,用 FLUX 生成一张图片,然后将其传递给一个背景移除的 Gradio Space,将其变成贴纸。通过一个文本转语音的 Gradio Space,主题被转化为配音;同时,通过 LLM 调用,同一个主题被转化为吸引人的剧集标题。这就是一个画布,通过 Hugging Face 推理提供商进行了两次模型调用,并对 Gradio Spaces 进行了两次调用。由于这是一个工作流,三个输出中的每一个都有自己的 REST 端点:/sticker、/voiceover 和 /episode_title。你无需打开 UI,即可直接从代码中调用它们。 👉 Try the AI Media Studio (尝试 AI 媒体工作室)
Fan-out image generation in parallel
并行分发图像生成
Type in one idea, and it turns into a set of generated artwork all at once: a base image from FLUX, two AI re-imaginings of that image (a soft watercolor version and a neon cyberpunk take), and a gallery title written by an LLM. Each image is generated directly from the prompt by a model node using Inference Providers, while the title comes from an fn node that calls an LLM. This is the fan-out pattern in action: one idea can feed multiple operators simultaneously, all generating in parallel. 输入一个想法,它会立即生成一套艺术作品:一张来自 FLUX 的基础图像、两张 AI 对该图像的重新构思(柔和的水彩版和霓虹赛博朋克版),以及一个由 LLM 编写的画廊标题。每张图像都由使用推理提供商的模型节点直接根据提示词生成,而标题则来自调用 LLM 的 fn 节点。这就是“分发模式”(fan-out pattern)的实际应用:一个想法可以同时输入给多个算子,所有算子并行生成。 👉 Try the Generative Art Lab (尝试生成艺术实验室)
Profile a Hugging Face dataset
分析 Hugging Face 数据集
Type in a Hugging Face dataset ID, such as stanfordnlp/imdb or mteb/tweet_sentiment_extraction, and a single input fans out to four operator nodes that analyze the dataset live using the Datasets Server API. You get an overview card, a preview of the first few rows, per-column statistics, and a distribution chart, all computed independently and in parallel. That’s the power of workflows! 输入一个 Hugging Face 数据集 ID(例如 stanfordnlp/imdb 或 mteb/tweet_sentiment_extraction),单个输入会分发到四个算子节点,这些节点使用 Datasets Server API 实时分析数据集。你将获得一张概览卡片、前几行的预览、各列统计数据以及分布图,所有这些都是独立且并行计算的。这就是工作流的力量! 👉 Try Data Detective (尝试数据侦探)
Run your own GPU model
运行你自己的 GPU 模型
Every node so far reaches out to Hugging Face. But an fn node is just Python, which means it can also run a model inside the Space on a GPU. Decorate the bound function with @spaces.GPU and, when the node runs, ZeroGPU grabs a GPU for that call, runs the model, and releases it. We don’t always need to rely on Inference Providers or existing Gradio Spaces. Check out this demo that animates a still image using Lightricks/LTX-Video loaded through Diffusers, running entirely through one node. gr.Workflow doesn’t need to know anything about your GPU setup. It simply calls the bound function.
到目前为止,每个节点都是连接到 Hugging Face。但 fn 节点本质上就是 Python,这意味着它也可以在 Space 内部的 GPU 上运行模型。只需用 @spaces.GPU 装饰绑定的函数,当节点运行时,ZeroGPU 会为该调用分配一个 GPU,运行模型,然后释放它。我们并不总是需要依赖推理提供商或现有的 Gradio Spaces。看看这个演示,它使用通过 Diffusers 加载的 Lightricks/LTX-Video 将静态图像动画化,整个过程通过一个节点运行。gr.Workflow 不需要了解你的 GPU 设置,它只需调用绑定的函数即可。
👉 Try the ZeroGPU Animator (尝试 ZeroGPU 动画制作器)
How it works, in a nutshell
简而言之,它是如何工作的
Every workflow is a graph with three kinds of nodes: references (your inputs), operators (the steps that do work), and subjects (your outputs). An operator can be your own Python function, a model on Hugging Face Inference Providers, another Gradio Space, or a row from a Hub dataset. You connect them by dragging between typed ports, hit Run, and watch each result appear in place. 每个工作流都是一个包含三种节点的图表:引用(你的输入)、算子(执行工作的步骤)和主体(你的输出)。算子可以是你的 Python 函数、Hugging Face 推理提供商上的模型、另一个 Gradio Space,或者是 Hub 数据集中的一行数据。你只需在类型化的端口之间拖拽即可连接它们,点击“运行”,然后观察每个结果在相应位置出现。
Call it from code
从代码中调用
Every workflow you build is also an API, with no extra work. Each output becomes a REST endpoint named after its label, and you can call it from Python with the Gradio client. 你构建的每个工作流同时也是一个 API,无需额外工作。每个输出都会成为一个以其标签命名的 REST 端点,你可以使用 Gradio 客户端从 Python 中调用它。
(Code examples omitted for brevity, please refer to the original article for implementation details.) (代码示例从略,实现细节请参考原文。)
Build your own
构建你自己的工作流
The fastest way in is to open any demo above, click Duplicate, and start rewiring. From Python, it is as short as: 最快入门的方法是打开上面的任何演示,点击“复制”(Duplicate),然后开始重新连接。在 Python 中,代码非常简洁:
import gradio as gr
def your_function(text: str) -> str:
pass
gr.Workflow(bind=[your_function]).launch()
For the full walkthrough, the operator kinds, the JSON schema, and reusable patterns, see the official gr.Workflow guide in the Gradio docs. You can even build something as involved as AUTOMATIC1111 with gr.Workflow. Keep an eye out for our next post, where we walk through building it step by step. Here is a sneak peek 😉👇
有关完整演练、算子类型、JSON 模式和可重用模式,请参阅 Gradio 文档中的官方 gr.Workflow 指南。你甚至可以使用 gr.Workflow 构建像 AUTOMATIC1111 那样复杂的应用。请关注我们的下一篇文章,我们将逐步演示如何构建它。这里先剧透一下 😉👇