반응형
좋아~ 오늘부터 Hugging Face 기초편 진짜로 드가자!
🔥 Day 8 – Hugging Face 첫걸음 + pipeline 써보기
📘 Hugging Face란?
- 사람처럼 말하는 모델들을 쉽게 쓸 수 있게 만들어주는 Python 라이브러리야.
- 대표 라이브러리:
- transformers: 사전 학습된 언어 모델 제공 (GPT, BERT, RoBERTa 등)
- datasets: 학습용 텍스트/이미지 데이터셋들
- tokenizers: 텍스트를 모델이 이해할 수 있게 토큰으로 쪼개줌
🛠 설치 (이미 했을 수도 있지만, 다시 확인)
pip install transformers datasets
🚀 Step 1: pipeline 체험해보기
from transformers import pipeline
# 감정 분석 파이프라인 불러오기
classifier = pipeline("sentiment-analysis")
# 문장 넣고 감정 예측해보기
result = classifier("나는 오늘 너무 행복하고 기분이 좋아!")
print(result)
📌 출력 예시
[{'label': 'POSITIVE', 'score': 0.9998}]
🎯 Step 2: 여러 문장 한꺼번에 넣기
sentences = [
"나는 오늘 너무 행복하다!",
"기분이 좀 우울해.",
"무서운 꿈을 꿨다.",
"짜증나고 화가 난다."
]
results = classifier(sentences)
for sentence, result in zip(sentences, results):
print(f"{sentence} => {result['label']} (score: {result['score']:.2f})")
✏️ 오늘 과제
🧪 Problem 1
sentences 리스트 안 문장들의 감정 태그를 'POSITIVE', 'NEGATIVE'로 분류한 딕셔너리를 만들어봐!
{
"나는 오늘 너무 행복하다!": "POSITIVE",
"기분이 좀 우울해.": "NEGATIVE",
...
}
🧪 Problem 2
감정이 'NEGATIVE'인 문장만 골라 리스트로 출력해봐!
오늘 목표는 Hugging Face pipeline을 처음 써보고 감정 분석을 해보는 것이야.
준비되면 바로 코드 짜서 보여줘도 되고, 궁금한 점 물어봐도 좋아!
드가자~
# problem 01
sentences = [
"나는 오늘 너무 행복하다.",
"기분이 좀 우울해.",
"무서운 꿈을 꿨다.",
"짜증나고 화가 난다."
]
results = classifier(sentences)
sentence_dict = {sentence : result['label'] for sentence , result in zip(sentences , results)}
print(sentence_dict)
# problem 02
filtered_sentence = []
for sentence, emotion in sentence_dict.items():
if emotion == "NEGATIVE":
filtered_sentence.append(sentence)
print(filtered_sentence)
'2025 SCG' 카테고리의 다른 글
20250509_ChatGpt와 공부하기_Day10 (0) | 2025.05.09 |
---|---|
20250508_ChatGpt와 공부하기_Day9 (0) | 2025.05.08 |
20250506_ChatGpt와 공부하기_Day7 (0) | 2025.05.06 |
20250505_ChatGpt와 공부하기_Day6 (0) | 2025.05.05 |
20250504_ChatGpt와 공부하기_Day5 (0) | 2025.05.04 |