推理提供商文件

HF 推理

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

HF 推理

所有支援的 HF 推理模型都可以在這裡找到。

HF 推理是 Hugging Face 提供支援的無伺服器推理 API。在推理提供商之前,此服務曾被稱為“推理 API (無伺服器)”。如果您有興趣將模型部署到 Hugging Face 管理的專用且可自動擴縮的基礎設施,請檢視推理端點

截至 2025 年 7 月,hf-inference 主要關注 CPU 推理(例如嵌入、文字排名、文字分類,或具有歷史重要性的小型 LLM,如 BERT 或 GPT-2)。

支援的任務

自動語音識別

瞭解更多關於自動語音識別的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

output = client.automatic_speech_recognition("sample1.flac", model="openai/whisper-large-v3")

聊天完成(LLM)

瞭解更多關於聊天補全 (LLM) 的資訊,請點選這裡

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

completion = client.chat.completions.create(
    model="HuggingFaceTB/SmolLM3-3B",
    messages=[
        {
            "role": "user",
            "content": "What is the capital of France?"
        }
    ],
)

print(completion.choices[0].message)

特徵提取

瞭解更多關於特徵提取的資訊,請點選這裡

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.feature_extraction(
    "Today is a sunny day and I will get some ice cream.",
    model="intfloat/multilingual-e5-large",
)

完形填空

瞭解更多關於完形填空的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.fill_mask(
    "The answer to the universe is undefined.",
    model="google-bert/bert-base-uncased",
)

影像分類

瞭解更多關於影像分類的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

output = client.image_classification("cats.jpg", model="Falconsai/nsfw_image_detection")

影像分割

瞭解更多關於影像分割的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

output = client.image_segmentation("cats.jpg", model="jonathandinu/face-parsing")

目標檢測

瞭解更多關於目標檢測的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

output = client.object_detection("cats.jpg", model="facebook/detr-resnet-50")

問答

瞭解更多關於問答的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

answer = client.question_answering(
    question="What is my name?",
    context="My name is Clara and I live in Berkeley.",
    model="deepset/roberta-base-squad2",
)

摘要

瞭解更多關於摘要的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.summarization(
    "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
    model="facebook/bart-large-cnn",
)

表格問答

瞭解更多關於表格問答的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

answer = client.table_question_answering(
    query="How many stars does the transformers repository have?",
    table={"Repository":["Transformers","Datasets","Tokenizers"],"Stars":["36542","4512","3934"],"Contributors":["651","77","34"],"Programming language":["Python","Python","Rust, Python and NodeJS"]},
    model="google/tapas-base-finetuned-wtq",
)

文字分類

瞭解更多關於文字分類的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.text_classification(
    "I like you. I love you",
    model="tabularisai/multilingual-sentiment-analysis",
)

文字生成

瞭解更多關於文字生成的資訊,請點選這裡

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

completion = client.chat.completions.create(
    model="HuggingFaceTB/SmolLM3-3B",
    messages="\"Can you please let us know more details about your \"",
)

print(completion.choices[0].message)

文字轉影像

瞭解更多關於文字到影像的資訊,請點選這裡

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

# output is a PIL.Image object
image = client.text_to_image(
    "Astronaut riding a horse",
    model="black-forest-labs/FLUX.1-dev",
)

標記分類

瞭解更多關於標記分類的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.token_classification(
    "My name is Sarah Jessica Parker but you can call me Jessica",
    model="dslim/bert-base-NER",
)

翻譯

瞭解更多關於翻譯的資訊,請點選此處

import os
from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key=os.environ["HF_TOKEN"],
)

result = client.translation(
    "Меня зовут Вольфганг и я живу в Берлине",
    model="google-t5/t5-small",
)

零樣本分類

瞭解更多關於零樣本分類的資訊,請點選此處

import os
import requests

API_URL = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli"
headers = {
    "Authorization": f"Bearer {os.environ['HF_TOKEN']}",
}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

output = query({
    "inputs": "Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!",
    "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
})
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.