본문으로 건너뛰기

© 2026 Molayo

Dev.to헤드라인2026. 06. 02. 09:24

Hugging Face Inference API 연동하기

요약

Hugging Face의 Inference API를 사용하여 Node.js 환경에서 오픈 모델을 호출하는 방법을 설명합니다. @huggingface/inference 패키지를 활용해 텍스트 요약 및 분류 등의 작업을 구현하는 가이드를 제공합니다.

핵심 포인트

  • Inference API를 통한 HTTP 기반 모델 호출 방법
  • @huggingface/inference 패키지 활용법
  • Access Token을 이용한 클라이언트 설정
  • 텍스트 요약 및 분류 기능 구현 예시

Hugging Face는 NLP(자연어 처리), 비전(Vision) 및 기타 작업을 위한 수천 개의 오픈 모델을 호스팅합니다. Inference API(Inference Providers를 통해 제공)를 사용하면 HTTP를 통해 이러한 모델을 호출할 수 있습니다. huggingface.js에서 제공하는 @huggingface/inference 패키지는 Node.js 클라이언트입니다.

사전 요구 사항

  • Hugging Face 계정
  • Inference 호출 권한이 있는 Access token (inference 접근 권한이 있는 Read 또는 Fine-grained 유형의 토큰 생성)
  • Node.js 버전 26
  • @huggingface/inference 설치 (npm i @huggingface/inference)

일부 모델(특히 이미지 생성 모델)은 Inference Providers를 통해 라우팅됩니다. 모델이 요구하는 경우 계정 설정에서 프로바이더(Providers) 및 결제(Billing)를 활성화하세요.

클라이언트 설정

InferenceClient에 토큰을 전달합니다. 프로덕션 환경에서는 환경 변수에서 읽어오세요.

import { InferenceClient } from '@huggingface/inference';

const client = new InferenceClient(process.env.HF_TOKEN);

요약 (Summarization)

긴 텍스트를 짧은 요약본으로 줄입니다. 요약(Summarization)을 위해 학습된 모델을 선택하고 해당 모델의 최대 입력 길이(max input length)를 준수하세요.

const result = await client.summarization({
  model: 'facebook/bart-large-cnn',
  inputs: `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 initially criticized by some artists and intellectuals, but it became a global cultural icon of France and one of the most recognizable structures in the world.`, 
...

텍스트 분류 (Text classification)

신뢰도 점수(confidence scores)와 함께 레이블을 할당합니다. 일반적인 사용 사례: 짧은 텍스트에 대한 감성 분석(sentiment analysis).

const labels = await client.textClassification({
  model: 'distilbert-base-uncased-finetuned-sst-2-english',
  inputs: 'I really enjoyed this workshop.',
...

텍스트를 이미지로 (Text to image)

텍스트 프롬프트로부터 이미지를 생성합니다. 클라이언트는 Blob을 반환하므로, 이를 디스크에 쓰거나 앱에서 제공해야 합니다.

import { writeFileSync } from 'node:fs';

const image = await client.textToImage({
...

provider: 'hf-inference'를 사용하려면 해당 작업에 대해 hf-inference 카탈로그의 모델을 사용하세요 (예: black-forest-labs/FLUX.1-schnell). stabilityai/stable-diffusion-2-1와 같은 이전 Hub 리포지토리는 더 이상 존재하지 않거나 제공자 매핑이 누락되었을 수 있습니다.

이미지 모델은 종종 속도가 느리고 제공자 사용 요금이 발생할 수 있습니다.

데모 (Demo)

각 작업에 대한 실행 가능한 스크립트는 huggingface-inference-api-demo 폴더에 있습니다. 코드 데모를 통해 접근할 수 있습니다.

AI 자동 생성 콘텐츠

본 콘텐츠는 Dev.to AI tag의 원문을 AI가 자동으로 요약·번역·분석한 것입니다. 원 저작권은 원저작자에게 있으며, 정확한 내용은 반드시 원문을 확인해 주세요.

원문 바로가기
0

댓글

0