본문 바로가기
AI2024년 9월 2일5분 읽기

Hugging Face Transformers 파이프라인 실전 활용

YS
김영삼
조회 467
Hugging Face Transformers 파이프라인 실전 활용

Hugging Face Transformers 파이프라인 개요

Hugging Face Transformers는 사전 학습된 모델을 손쉽게 활용할 수 있는 Python 라이브러리입니다. pipeline API는 복잡한 모델 로딩과 전처리를 추상화하여, 단 몇 줄의 코드로 다양한 NLP 태스크를 수행할 수 있게 해줍니다.

설치 및 기본 사용법

pip install transformers torch

from transformers import pipeline

# 감성 분석 파이프라인
classifier = pipeline("sentiment-analysis")
result = classifier("I love this product! It's amazing.")
print(result)
# [{'label': 'POSITIVE', 'score': 0.9998}]

# 텍스트 생성
generator = pipeline("text-generation", model="gpt2")
output = generator("The future of AI is", max_length=50, num_return_sequences=1)
print(output[0]['generated_text'])

주요 파이프라인 태스크 종류

태스크파이프라인 이름설명
감성 분석sentiment-analysis텍스트의 긍정/부정 판별
개체명 인식ner인명, 지명, 기관명 추출
질의응답question-answering문맥 기반 질문 답변
번역translation다국어 번역
요약summarization긴 텍스트 자동 요약
Zero-Shot 분류zero-shot-classification라벨 없이 분류

고급 활용: 커스텀 모델 지정

from transformers import pipeline

# 한국어 감성 분석 모델
kor_classifier = pipeline(
    "sentiment-analysis",
    model="snunlp/KR-FinBert-SC",
    tokenizer="snunlp/KR-FinBert-SC"
)
result = kor_classifier("이 제품은 정말 훌륭합니다")
print(result)

# 질의응답
qa = pipeline("question-answering", model="deepset/roberta-base-squad2")
context = "Hugging Face는 2016년에 설립된 AI 회사로, Transformers 라이브러리를 개발했습니다."
answer = qa(question="Hugging Face는 언제 설립되었나요?", context=context)
print(answer)
# {'answer': '2016년', 'score': 0.95, 'start': 16, 'end': 21}

Zero-Shot Classification 활용

classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

text = "이 영화는 정말 재미있고 감동적이었습니다"
labels = ["엔터테인먼트", "스포츠", "정치", "기술"]

result = classifier(text, candidate_labels=labels)
print(result['labels'][0])  # 엔터테인먼트
print(result['scores'][0])  # 0.87

배치 처리와 GPU 가속

# GPU 사용 (device=0)
pipe = pipeline("sentiment-analysis", device=0)

# 배치 처리
texts = [
    "This movie was fantastic!",
    "The service was terrible.",
    "Average experience, nothing special."
]
results = pipe(texts, batch_size=8)
for text, res in zip(texts, results):
    print(f"{text} -> {res['label']} ({res['score']:.4f})")

모델 캐싱과 오프라인 사용

Transformers는 모델을 ~/.cache/huggingface/에 자동 캐싱합니다. 오프라인 환경에서는 TRANSFORMERS_OFFLINE=1 환경 변수를 설정하면 캐시된 모델만 사용합니다. 프로덕션 배포 시에는 모델을 미리 다운로드하여 Docker 이미지에 포함시키는 것이 권장됩니다.

실전 팁

  • pipelinemodel 파라미터로 Hugging Face Hub의 모든 모델을 지정할 수 있습니다
  • device=0으로 GPU를, device=-1로 CPU를 지정합니다
  • 대용량 모델은 model_kwargs={"torch_dtype": torch.float16}으로 메모리를 절약합니다
  • pipeline은 내부적으로 AutoModel/AutoTokenizer를 사용하므로, 세밀한 제어가 필요하면 직접 사용하세요
  • 배치 처리 시 batch_size를 적절히 설정하면 처리 속도가 크게 향상됩니다

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록