이 모델은 한국어 감정 분류를 위해 KoELECTRA를 파인튜닝한 모델입니다. 6가지 주요 감정(분노, 행복, 불안, 당황, 슬픔, 상처)을 분류할 수 있습니다.
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# 모델과 토크나이저 로드
5model_name = "Jinuuuu/KoELECTRA_fine_tunning_emotion"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9# 감정 분석 함수
10def analyze_emotion(text):
11 # 토크나이징
12 inputs = tokenizer(
13 text,
14 return_tensors="pt",
15 truncation=True,
16 max_length=512,
17 padding=True
18 )
19
20 # 예측
21 with torch.no_grad():
22 outputs = model(**inputs)
23
24 # 확률 계산
25 probs = torch.softmax(outputs.logits, dim=1)
26
27 # 감정 레이블
28 emotion_labels = ['angry', 'anxious', 'embarrassed', 'happy', 'heartache', 'sad']
29
30 # 결과 반환
31 emotion_probs = {}
32 for i, label in enumerate(emotion_labels):
33 emotion_probs[label] = float(probs[0][i])
34
35 return emotion_probs
36
37# 사용 예시
38text = "오늘은 정말 행복한 하루였다."
39result = analyze_emotion(text)
40
41print("감정 분석 결과:")
42for emotion, prob in sorted(result.items(), key=lambda x: x[1], reverse=True):
43 print(f"{emotion}: {prob:.3f}")
1from transformers import pipeline
2
3# 파이프라인 생성
4classifier = pipeline(
5 "text-classification",
6 model="Jinuuuu/KoELECTRA_fine_tunning_emotion",
7 tokenizer="Jinuuuu/KoELECTRA_fine_tunning_emotion"
8)
9
10# 감정 분석
11texts = [
12 "오늘은 정말 행복한 하루였다.",
13 "너무 화가 나서 참을 수 없다.",
14 "내일 시험이 걱정된다."
15]
16
17results = classifier(texts)
18for text, result in zip(texts, results):
19 print(f"텍스트: {text}")
20 print(f"감정: {result['label']} (확률: {result['score']:.3f})")
21 print()
이 모델은 학습 데이터의 편향을 반영할 수 있습니다. 특정 주제나 표현에 대해 편향된 결과를 보일 수 있으므로, 실제 서비스에 적용할 때는 충분한 검증과 모니터링이 필요합니다.
1@misc{koelectra_emotion_2024,
2 title={KoELECTRA Fine-tuned for Korean Emotion Classification},
3 author={Jinuuuu},
4 year={2024},
5 publisher={Hugging Face},
6 howpublished={\url{https://huggingface.co/Jinuuuu/KoELECTRA_fine_tunning_emotion}}
7}
모델에 대한 문의사항이나 개선 제안이 있으시면 GitHub 이슈나 Hugging Face 모델 페이지를 통해 연락주세요.