Back to APIs

Hugging Face

huggingface.co

Integrate advanced NLP models for text analysis and generation.

Using the Hugging Face API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Hugging Face background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using Hugging Face

Below are some working code examples of how you can use Hugging Face with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { HfInference } from "@huggingface/inference";
2
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
3
import z from "zod";
4
5
// Create a new Hugging Face inference client
6
// Get start with Hugging Face https://huggingface.co/docs/api-inference/quicktour
7
// SDK: https://www.npmjs.com/package/@huggingface/inference
8
const hf = new HfInference(process.env.HUGGING_FACE_API_KEY);
9
10
client.defineJob({
11
id: "hugging-face-inference",
12
name: "Hugging Face inference",
13
version: "1.0.0",
14
trigger: eventTrigger({
15
name: "hugging-face-inference",
16
schema: z.object({
17
// Hugging Face model name or ID.
18
// Example: "distilbert-base-uncased-finetuned-sst-2-english"
19
// More models: https://huggingface.co/models?pipeline_tag=text-classification
20
model: z.string(),
21
// Text to input to the model.
22
// Example: "Such nice weather outside!"
23
inputs: z.string(),
24
}),
25
}),
26
run: async (payload, io, ctx) => {
27
// Use io.runTask to make the SDK call resumable and log-friendly
28
await io.runTask(
29
"Hugging Face inference",
30
async () => {
31
// Call the Hugging Face API
32
return await hf.textClassification(payload);
33
},
34
35
// Add metadata to improve how the task displays in the logs
36
{ name: "Hugging Face inference", icon: "hugging-face" }
37
);
38
},
39
});