Hugging Face Hub의 transformers 를 사용하면 Hugging Face Hub에 있는 여러 AI 모델들을 사용할 수 있다.
설치
pip install transformers
pip install torch # 또는 tensorflow
# pip install datasets # dataset도 설치 가능하다.
간단한 사용 예제
from transformers import pipeline
# 텍스트 생성 파이프라인 로드 (GPT-2)
generator = pipeline("text-generation", model="gpt2")
# 텍스트 생성
result = generator("AI가 음악을 만들 수 있을까?", max_length=50, num_return_sequences=1)
print(result[0]["generated_text"])
✅ 3. 다른 파이프라인 예제
🔍 문장 분류 (감정 분석 등)
classifier = pipeline("sentiment-analysis") print(classifier("I love Hugging Face!"))
🧠 질문 답변
qa = pipeline("question-answering")
print(qa({ "question": "What is the capital of South Korea?", "context": "Seoul is the capital and largest metropolis of South Korea." }))
✅ 4. 모델 변경 방법
pipeline()에서 model="모델이름"으로 지정하면 Hugging Face Hub에서 자동 다운로드됩니다.
# 한국어 전용 모델
from transformers import pipeline
koqa = pipeline("question-answering", model="monologg/koelectra-small-v3-discriminator")
🎁 추천 모델 (한글 포함)
목적모델 이름 (Hugging Face Hub)
| 텍스트 생성 (영어) | gpt2, EleutherAI/gpt-neo-1.3B |
| 텍스트 생성 (한글) | skt/kogpt2-base-v2, beomi/kcbert-base |
| 질문답변 (영어) | distilbert-base-cased-distilled-squad |
| 번역 | Helsinki-NLP/opus-mt-en-ko |
| 감정 분석 | nlptown/bert-base-multilingual-uncased-sentiment |
참고: 위 내용 중에 번역하는 모델은 제대로 작동하지 않았다.