Hugging Face offers a free Inference API that gives you access to 200,000+ models — text generation, image generation, classification, translation, embeddings, and more. No GPU needed.
Getting Your API Token
Sign up at huggingface.co → Settings → Access Tokens → Create new token.
Installation
npm install @huggingface/inference
Text Generation
import { HfInference } from "@huggingface/inference";
const hf = new HfInference("your-token");
const result = await hf.textGeneration({
model: "mistralai/Mistral-7B-Instruct-v0.3",
inputs: "Explain web scraping in simple terms:",
parameters: { max_new_tokens: 200, temperature: 0.7 }
});
console.log(result.generated_text);
Chat Completion
const response = await hf.chatCompletion({
model: "meta-llama/Llama-3.2-3B-Instruct",
messages: [
{ role: "system", content: "You are a helpful coding assistant." },
{ role: "user", content: "Write a Python web scraper for news headlines" }
],
max_tokens: 500
});
console.log(response.choices[0].message.content);
Image Generation
const image = await hf.textToImage({
model: "stabilityai/stable-diffusion-xl-base-1.0",
inputs: "A robot scraping data from a website, digital art"
});
// image is a Blob
const buffer = Buffer.from(await image.arrayBuffer());
fs.writeFileSync("robot.png", buffer);
Embeddings
const embeddings = await hf.featureExtraction({
model: "sentence-transformers/all-MiniLM-L6-v2",
inputs: "Web scraping best practices for developers"
});
console.log(`Dimensions: ${embeddings.length}`);
Text Classification (Sentiment)
const result = await hf.textClassification({
model: "distilbert-base-uncased-finetuned-sst-2-english",
inputs: "This API is amazing and so easy to use!"
});
console.log(result); // [{ label: "POSITIVE", score: 0.99 }]
Translation
const translated = await hf.translation({
model: "Helsinki-NLP/opus-mt-en-de",
inputs: "Web scraping is the process of extracting data from websites."
});
console.log(translated.translation_text);
Streaming
const stream = hf.chatCompletionStream({
model: "meta-llama/Llama-3.2-3B-Instruct",
messages: [{ role: "user", content: "Write a haiku about data" }]
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.
Top comments (0)