Views
No views yet
transformers, datasets, torch, pandas, scikit-learn 라이브러리를 설치합니다.Dataset으로 변환.label_int 컬럼을 labels로 변경.monologg/kobert 토크나이저를 이용해 입력 텍스트를 토큰화.input_ids, attention_mask, labels만 남겨 학습 준비 완료.monologg/kobert 모델을 불러와 5개의 감정 레이블을 분류하도록 설정.learning_rate=2e-5, num_train_epochs=10, batch_size=16.monologg/kobert 기반이며, 분류 레이블은 다음과 같습니다:
1# 토크나이저 및 모델 로드
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
4# KoBERT 토크나이저와 모델 로드
5tokenizer = AutoTokenizer.from_pretrained("monologg/kobert", trust_remote_code=True)
6model = AutoModelForSequenceClassification.from_pretrained("rkdaldus/ko-sent5-classification")
7
8# 사용자 입력 텍스트 감정 분석
9text = "오늘 정말 행복해!"
10inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
11with torch.no_grad():
12 outputs = model(**inputs)
13predicted_label = torch.argmax(outputs.logits, dim=1).item()
14
15# 감정 레이블 정의
16emotion_labels = {
17 0: ("Angry", "😡"),
18 1: ("Fear", "😨"),
19 2: ("Happy", "😊"),
20 3: ("Tender", "🥰"),
21 4: ("Sad", "😢")
22}
23
24# 예측된 감정 출력
25print(f"예측된 감정: {emotion_labels[predicted_label][0]} {emotion_labels[predicted_label][1]}")