File size: 3,219 Bytes
992e1b1
 
 
47d9a54
 
 
 
 
 
992e1b1
 
 
 
47d9a54
 
 
 
992e1b1
 
 
47d9a54
 
 
 
 
992e1b1
47d9a54
 
 
992e1b1
47d9a54
992e1b1
47d9a54
992e1b1
47d9a54
 
 
992e1b1
47d9a54
992e1b1
47d9a54
992e1b1
47d9a54
992e1b1
47d9a54
 
992e1b1
47d9a54
992e1b1
47d9a54
 
992e1b1
47d9a54
 
992e1b1
47d9a54
 
992e1b1
47d9a54
4f54552
47d9a54
 
 
 
 
 
 
 
 
992e1b1
47d9a54
 
 
 
992e1b1
47d9a54
 
 
992e1b1
47d9a54
 
 
 
 
 
 
992e1b1
47d9a54
 
992e1b1
47d9a54
 
992e1b1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
base_model: meta-llama/Llama-3.2-3B-Instruct
library_name: peft
language:
- ko
- en
metrics:
- accuracy
pipeline_tag: text-classification
---

# Model Card for Model ID

- llama3.2-3B ๋ชจ๋ธ์„ prompt๋ฅผ ๊ณ ์ •ํ•˜๊ณ  lora ๋ฐฉ์‹์œผ๋กœ ํ•™์Šตํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
- ๊ธฐ์จ, ๋‹นํ™ฉ, ๋ถ„๋…ธ, ๋ถˆ์•ˆ, ์ƒ์ฒ˜, ์Šฌํ”” ์ด 6๊ฐ€์ง€ ๊ฐ์ •์„ ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค.
- ๋ฐ์ดํ„ฐ๋Š” AIHUB์˜ [๊ฐ์„ฑ ๋Œ€ํ™” ๋ง๋ญ‰์น˜](https://www.aihub.or.kr/aihubdata/data/view.do?currMenu=115&topMenu=100&aihubDataSe=data&dataSetSn=86)๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
- ๋‚˜์ด์™€ ์„ฑ๋ณ„๋„ ํ•™์Šต์‹œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

## Uses

```
import re
import torch
from transformers import AutoTokenizer
from peft import AutoPeftModelForCausalLM

model = None
tokenizer = None
device = None

PROMPT="""<|prompt|>You are an AI assistant tasked with analyzing the emotional content of a diary entry. Your goal is to determine the most closely matching emotion from a predefined list.

Here is the diary entry you need to analyze:

<diary_entry>
age: {age} | gender: {gender} | diary: {sentence}
</diary_entry>

Please carefully read and analyze the content of this diary entry. Consider the overall tone, the events described, and the language used by the writer.

Based on your analysis, choose the emotion that best matches the overall sentiment of the diary entry from the following list:

['๋ถ„๋…ธ', '๋ถˆ์•ˆ', '์ƒ์ฒ˜', '์Šฌํ””', '๋‹นํ™ฉ', '๊ธฐ์จ']

Translate these emotions to English for your understanding:
['๋ถ„๋…ธ(anger)', '๋ถˆ์•ˆ(anxiety)', '์ƒ์ฒ˜(hurt)', '์Šฌํ””(sadness)', '๋‹นํ™ฉ(embarrassment)', '๊ธฐ์จ(happiness)']

After you've made your decision, respond with only the chosen emotion in Korean. Do not provide any explanation or additional text.

Your response should be formatted as follows:
<emotion>[chosen emotion in korean]</emotion>

Once you've provided the emotion, end the conversation. Do not engage in any further dialogue or provide any additional information.
<|assistant|>"""

def load_model():
    global model, tokenizer, device

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    path = './llama-3.2-3B-sentiment-kr-LoRA'
    
    tokenizer = AutoTokenizer.from_pretrained(path)
    model = AutoPeftModelForCausalLM.from_pretrained(
        path,
        attn_implementation="flash_attention_2",
        torch_dtype=torch.float16,
        device_map=device,
    )
    model.eval()

def generate(text, age, gender):
    global model, tokenizer, device
    text = PROMPT.format(age=age, gender=gender, sentence=text)
    inputs = tokenizer(text, return_tensors="pt").to(device)

    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=11, pad_token_id=tokenizer.pad_token_id)
        decoded_output = tokenizer.decode(outputs[0])

        try:
            pred = decoded_output.split("<|assistant|>")[1]
            pred = re.search(r'<emotion>(.*?)</emotion>', pred).group(1)
        except:
            pred = 'error'
            
    return pred

print(generate("์˜ค๋Š˜ ์นœ๊ตฌ๋ž‘ ์‹ธ์› ์–ด.", "", ""))
```

## Accuracy
๋ฐ์ดํ„ฐ ํ•™์Šต์‹œ ์ผ๋ถ€๋ฅผ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์ •ํ™•๋„ ์ธก์ • ๊ฒฐ๊ณผ ์•ฝ 70%๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

### Framework versions

- PEFT 0.13.0