noridorimari commited on
Commit
e1ffdf4
ยท
verified ยท
1 Parent(s): efc332b

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +69 -0
README.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - ko
4
+ base_model:
5
+ - beomi/KcELECTRA-base
6
+ pipeline_tag: text-classification
7
+ tags:
8
+ - emotion
9
+ - sentiment
10
+ ---
11
+
12
+ ## Text์˜ ๊ฐ์ •์„ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
13
+
14
+ ### transformers๋กœ ๋ชจ๋ธ์„ ๋ฐ›์€ ํ›„ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰, sample_text์— ๋ฌธ์žฅ์„ ๋„ฃ์œผ๋ฉด ๋ถ„์„ํ•ด์ค๋‹ˆ๋‹ค.
15
+
16
+ ```
17
+ import torch
18
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
19
+ import torch.nn.functional as F
20
+ import os
21
+
22
+ # ๋ชจ๋ธ์ด๋ž‘ ํ† ํฌ๋‚˜์ด์ € ๋ถˆ๋Ÿฌ์˜ค๊ธฐ.
23
+ MODEL_DIR = os.path.join(OUTPUT_DIR, "best_model")
24
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
25
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
26
+ model.eval().to("cuda" if torch.cuda.is_available() else "cpu")
27
+
28
+ # ๊ฐ์ • ์นดํ…Œ๊ณ ๋ฆฌ - ๋งจ ์œ„์— ์ €์žฅํ•ด๋†“์€ ๊ทธ๊ฑฐ
29
+ CATEGORIES = ["happy", "embarrass", "anger", "unrest", "damaged", "sadness"]
30
+
31
+ # ์ถ”๋ก  ํ•จ์ˆ˜. ์—ฌ๊ธฐ์— txt๋ฅผ ๋„ฃ์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ด.
32
+ def predict_emotion(texts):
33
+ if isinstance(texts, str):
34
+ texts = [texts]
35
+
36
+ device = "cuda" if torch.cuda.is_available() else "cpu"
37
+ inputs = tokenizer(
38
+ texts,
39
+ padding=True,
40
+ truncation=True,
41
+ max_length=64,
42
+ return_tensors="pt"
43
+ ).to(device)
44
+
45
+ with torch.no_grad():
46
+ outputs = model(**inputs)
47
+ probs = F.softmax(outputs.logits, dim=-1)
48
+ preds = probs.argmax(dim=-1)
49
+
50
+ results = []
51
+ for i, text in enumerate(texts):
52
+ results.append({
53
+ "text": text,
54
+ "pred_label": CATEGORIES[preds[i].item()],
55
+ "probabilities": {CATEGORIES[j]: round(probs[i][j].item(), 4) for j in range(len(CATEGORIES))}
56
+ })
57
+ return results
58
+
59
+ # ๋„ฃ์„ ๋ฌธ์žฅ ์˜ˆ์‹œ. ๋‚˜์ค‘์—๋Š” ์‚ฌ์šฉ์ž์˜ ๋‹ค์ด์–ด๋ฆฌ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐˆ ๊ฒƒ.
60
+ sample_texts = [
61
+ "๋‚˜ ์˜ค๋Š˜ ๊ธฐ๋ป์„œ ์šธ์—ˆ์–ด"
62
+ ]
63
+
64
+ for r in predict_emotion(sample_texts):
65
+ print(f"๋ฌธ์žฅ: {r['text']}")
66
+ print(f"์˜ˆ์ธก ๊ฐ์ •: {r['pred_label']}")
67
+ print("ํ™•๋ฅ  ๋ถ„ํฌ:", r["probabilities"])
68
+ print("-" * 60)
69
+ ```