Upload 15 files
Browse files- RecommendationSystem/.idea/.gitignore +3 -0
- RecommendationSystem/.idea/.name +1 -0
- RecommendationSystem/.idea/RecommendationSystem.iml +8 -0
- RecommendationSystem/.idea/inspectionProfiles/profiles_settings.xml +6 -0
- RecommendationSystem/.idea/misc.xml +4 -0
- RecommendationSystem/.idea/modules.xml +8 -0
- RecommendationSystem/.idea/vcs.xml +6 -0
- RecommendationSystem/.idea/workspace.xml +101 -0
- RecommendationSystem/Ensemble_model_train.py +222 -0
- RecommendationSystem/downstream_train.py +160 -0
- RecommendationSystem/get_label.py +66 -0
- RecommendationSystem/pre_train.py +154 -0
- RecommendationSystem/predict +75 -0
- RecommendationSystem/predict.py +75 -0
- RecommendationSystem/recommendation_scoring.py +360 -0
RecommendationSystem/.idea/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
RecommendationSystem/.idea/.name
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
recommendation_scoring.py
|
RecommendationSystem/.idea/RecommendationSystem.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="inheritedJdk" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
RecommendationSystem/.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
RecommendationSystem/.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (base)" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
RecommendationSystem/.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/RecommendationSystem.iml" filepath="$PROJECT_DIR$/.idea/RecommendationSystem.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
RecommendationSystem/.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
RecommendationSystem/.idea/workspace.xml
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ChangeListManager">
|
| 4 |
+
<list default="true" id="1b6a6ba6-1134-41d6-8f6c-029f91497682" name="Changes" comment="update">
|
| 5 |
+
<change beforePath="$PROJECT_DIR$/recommendation_scoring.py" beforeDir="false" afterPath="$PROJECT_DIR$/recommendation_scoring.py" afterDir="false" />
|
| 6 |
+
</list>
|
| 7 |
+
<option name="SHOW_DIALOG" value="false" />
|
| 8 |
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
| 9 |
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
| 10 |
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
| 11 |
+
</component>
|
| 12 |
+
<component name="FileTemplateManagerImpl">
|
| 13 |
+
<option name="RECENT_TEMPLATES">
|
| 14 |
+
<list>
|
| 15 |
+
<option value="Python Script" />
|
| 16 |
+
</list>
|
| 17 |
+
</option>
|
| 18 |
+
</component>
|
| 19 |
+
<component name="Git.Settings">
|
| 20 |
+
<option name="PUSH_TAGS">
|
| 21 |
+
<GitPushTagMode>
|
| 22 |
+
<option name="argument" value="--tags" />
|
| 23 |
+
<option name="title" value="All" />
|
| 24 |
+
</GitPushTagMode>
|
| 25 |
+
</option>
|
| 26 |
+
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
|
| 27 |
+
</component>
|
| 28 |
+
<component name="MarkdownSettingsMigration">
|
| 29 |
+
<option name="stateVersion" value="1" />
|
| 30 |
+
</component>
|
| 31 |
+
<component name="ProjectId" id="2gjjgyvtTP65rJX7Wj72IqGShdo" />
|
| 32 |
+
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
|
| 33 |
+
<ConfirmationsSetting value="2" id="Add" />
|
| 34 |
+
</component>
|
| 35 |
+
<component name="ProjectViewState">
|
| 36 |
+
<option name="hideEmptyMiddlePackages" value="true" />
|
| 37 |
+
<option name="showLibraryContents" value="true" />
|
| 38 |
+
</component>
|
| 39 |
+
<component name="PropertiesComponent">{
|
| 40 |
+
"keyToString": {
|
| 41 |
+
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
| 42 |
+
"RunOnceActivity.ShowReadmeOnStart": "true"
|
| 43 |
+
}
|
| 44 |
+
}</component>
|
| 45 |
+
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
| 46 |
+
<component name="TaskManager">
|
| 47 |
+
<task active="true" id="Default" summary="Default task">
|
| 48 |
+
<changelist id="1b6a6ba6-1134-41d6-8f6c-029f91497682" name="Changes" comment="" />
|
| 49 |
+
<created>1716217995342</created>
|
| 50 |
+
<option name="number" value="Default" />
|
| 51 |
+
<option name="presentableId" value="Default" />
|
| 52 |
+
<updated>1716217995342</updated>
|
| 53 |
+
</task>
|
| 54 |
+
<task id="LOCAL-00001" summary="rollback">
|
| 55 |
+
<created>1716218179029</created>
|
| 56 |
+
<option name="number" value="00001" />
|
| 57 |
+
<option name="presentableId" value="LOCAL-00001" />
|
| 58 |
+
<option name="project" value="LOCAL" />
|
| 59 |
+
<updated>1716218179029</updated>
|
| 60 |
+
</task>
|
| 61 |
+
<task id="LOCAL-00002" summary="update">
|
| 62 |
+
<created>1716218546785</created>
|
| 63 |
+
<option name="number" value="00002" />
|
| 64 |
+
<option name="presentableId" value="LOCAL-00002" />
|
| 65 |
+
<option name="project" value="LOCAL" />
|
| 66 |
+
<updated>1716218546785</updated>
|
| 67 |
+
</task>
|
| 68 |
+
<task id="LOCAL-00003" summary="update">
|
| 69 |
+
<created>1716218859894</created>
|
| 70 |
+
<option name="number" value="00003" />
|
| 71 |
+
<option name="presentableId" value="LOCAL-00003" />
|
| 72 |
+
<option name="project" value="LOCAL" />
|
| 73 |
+
<updated>1716218859894</updated>
|
| 74 |
+
</task>
|
| 75 |
+
<task id="LOCAL-00004" summary="update">
|
| 76 |
+
<created>1716454266936</created>
|
| 77 |
+
<option name="number" value="00004" />
|
| 78 |
+
<option name="presentableId" value="LOCAL-00004" />
|
| 79 |
+
<option name="project" value="LOCAL" />
|
| 80 |
+
<updated>1716454266936</updated>
|
| 81 |
+
</task>
|
| 82 |
+
<option name="localTasksCounter" value="5" />
|
| 83 |
+
<servers />
|
| 84 |
+
</component>
|
| 85 |
+
<component name="Vcs.Log.Tabs.Properties">
|
| 86 |
+
<option name="TAB_STATES">
|
| 87 |
+
<map>
|
| 88 |
+
<entry key="MAIN">
|
| 89 |
+
<value>
|
| 90 |
+
<State />
|
| 91 |
+
</value>
|
| 92 |
+
</entry>
|
| 93 |
+
</map>
|
| 94 |
+
</option>
|
| 95 |
+
</component>
|
| 96 |
+
<component name="VcsManagerConfiguration">
|
| 97 |
+
<MESSAGE value="rollback" />
|
| 98 |
+
<MESSAGE value="update" />
|
| 99 |
+
<option name="LAST_COMMIT_MESSAGE" value="update" />
|
| 100 |
+
</component>
|
| 101 |
+
</project>
|
RecommendationSystem/Ensemble_model_train.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from torch.utils.data import Dataset, DataLoader
|
| 5 |
+
from torch.nn import CrossEntropyLoss
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from transformers import (
|
| 8 |
+
AutoTokenizer,
|
| 9 |
+
AutoModelForSequenceClassification,
|
| 10 |
+
Trainer,
|
| 11 |
+
TrainingArguments,
|
| 12 |
+
EvalPrediction,
|
| 13 |
+
TrainerCallback
|
| 14 |
+
)
|
| 15 |
+
from sklearn.metrics import accuracy_score
|
| 16 |
+
|
| 17 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# SentimentDataset ํด๋์ค ์ ์
|
| 22 |
+
class SentimentDataset(Dataset):
|
| 23 |
+
def __init__(self, encodings, labels):
|
| 24 |
+
self.encodings = encodings
|
| 25 |
+
self.labels = labels
|
| 26 |
+
|
| 27 |
+
def __getitem__(self, idx):
|
| 28 |
+
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
| 29 |
+
item['labels'] = torch.tensor(self.labels[idx])
|
| 30 |
+
return item
|
| 31 |
+
|
| 32 |
+
def __len__(self):
|
| 33 |
+
return len(self.labels)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# CSV ํ์ผ ๋ก๋
|
| 37 |
+
df = pd.read_csv('/content/drive/MyDrive/dataset_hr.csv') # 'your_dataset.csv'๋ฅผ ์ค์ ํ์ผ ๊ฒฝ๋ก๋ก ๋ณ๊ฒฝํ์ธ์.
|
| 38 |
+
|
| 39 |
+
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
|
| 40 |
+
texts = df['person_corpus'].tolist()
|
| 41 |
+
labels = df['label'].tolist() # ๋ผ๋ฒจ ๋ฐ์ดํฐ๋ ์ ์๋ก ๋ณํ๋์ด ์์ด์ผ ํฉ๋๋ค.
|
| 42 |
+
|
| 43 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 44 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 45 |
+
tokenizer_koelectra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 46 |
+
|
| 47 |
+
# ๋ฐ์ดํฐ์
ํ ํฐํ
|
| 48 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 49 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128)
|
| 50 |
+
|
| 51 |
+
# ๋ฐ์ดํฐ์
์์ฑ
|
| 52 |
+
dataset_roberta = SentimentDataset(encodings_roberta, labels)
|
| 53 |
+
dataset_koelectra = SentimentDataset(encodings_koelectra, labels)
|
| 54 |
+
|
| 55 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 56 |
+
roberta_model_path = "/content/drive/MyDrive/robust_model/roberta_model"
|
| 57 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 58 |
+
electra_model_path = "/content/drive/MyDrive/robust_model/electra_model"
|
| 59 |
+
|
| 60 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 61 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 62 |
+
model_koelectra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# CustomTrainer ํด๋์ค ์ ์
|
| 66 |
+
class CustomTrainer(Trainer):
|
| 67 |
+
def __init__(self, *args, label_weights=None, **kwargs):
|
| 68 |
+
super().__init__(*args, **kwargs)
|
| 69 |
+
self.label_weights = label_weights if label_weights is not None else {}
|
| 70 |
+
|
| 71 |
+
def compute_loss(self, model, inputs, return_outputs=False):
|
| 72 |
+
outputs = model(**inputs)
|
| 73 |
+
logits = outputs.get('logits')
|
| 74 |
+
labels = inputs.get('labels')
|
| 75 |
+
if self.label_weights:
|
| 76 |
+
weight = torch.tensor([self.label_weights.get(label.item(), 1.0) for label in labels]).to(labels.device)
|
| 77 |
+
loss_fct = CrossEntropyLoss(weight=weight)
|
| 78 |
+
else:
|
| 79 |
+
loss_fct = CrossEntropyLoss()
|
| 80 |
+
loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
|
| 81 |
+
return (loss, outputs) if return_outputs else loss
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# TrainingArguments ์ค์
|
| 85 |
+
training_args = TrainingArguments(
|
| 86 |
+
output_dir='./results',
|
| 87 |
+
num_train_epochs=100,
|
| 88 |
+
per_device_train_batch_size=16,
|
| 89 |
+
warmup_steps=50,
|
| 90 |
+
weight_decay=0.01,
|
| 91 |
+
logging_dir='./logs',
|
| 92 |
+
logging_steps=10,
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# ๋ผ๋ฒจ๋ณ ๊ฐ์ค์น ์ค์
|
| 96 |
+
label_weights = {0: 8.0, 1: 2.5, 2: 15.0, 3: 1.0}
|
| 97 |
+
|
| 98 |
+
# CustomTrainer ์ธ์คํด์ค ์์ฑ
|
| 99 |
+
trainer_roberta = CustomTrainer(
|
| 100 |
+
model=model_roberta,
|
| 101 |
+
args=training_args,
|
| 102 |
+
train_dataset=None, # dataset_roberta๋ ์ฌ์ฉ์๊ฐ ์ ์ํด์ผ ํจ
|
| 103 |
+
label_weights=label_weights,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
trainer_electra = CustomTrainer(
|
| 107 |
+
model=model_koelectra,
|
| 108 |
+
args=training_args,
|
| 109 |
+
train_dataset=None, # dataset_electra๋ ์ฌ์ฉ์๊ฐ ์ ์ํด์ผ ํจ
|
| 110 |
+
label_weights=label_weights,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# logits๋ฅผ ํ๋ฅ ๋ก ๋ณํํ๋ ํจ์
|
| 115 |
+
def logits_to_probs(logits):
|
| 116 |
+
return torch.softmax(logits, dim=-1)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class EnsembleModel(nn.Module):
|
| 120 |
+
def __init__(self, model_roberta, model_koelectra):
|
| 121 |
+
super(EnsembleModel, self).__init__()
|
| 122 |
+
self.roberta_model = model_roberta
|
| 123 |
+
self.koelectra_model = model_koelectra
|
| 124 |
+
self.star1 = nn.Parameter(torch.tensor(0.5))
|
| 125 |
+
self.star2 = nn.Parameter(torch.tensor(0.5))
|
| 126 |
+
|
| 127 |
+
def forward(self, texts, tokenizer_roberta, tokenizer_koelectra, device):
|
| 128 |
+
self.star1.data = torch.clamp(self.star1.data, 0.0, 1.0)
|
| 129 |
+
self.star2.data = torch.clamp(self.star2.data, 0.0, 1.0)
|
| 130 |
+
|
| 131 |
+
# Tokenize inputs
|
| 132 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128, return_tensors='pt')
|
| 133 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128,
|
| 134 |
+
return_tensors='pt')
|
| 135 |
+
|
| 136 |
+
roberta_input_ids = encodings_roberta['input_ids'].to(device)
|
| 137 |
+
roberta_attention_mask = encodings_roberta['attention_mask'].to(device)
|
| 138 |
+
|
| 139 |
+
koelectra_input_ids = encodings_koelectra['input_ids'].to(device)
|
| 140 |
+
koelectra_attention_mask = encodings_koelectra['attention_mask'].to(device)
|
| 141 |
+
|
| 142 |
+
# Get model outputs
|
| 143 |
+
roberta_outputs = self.roberta_model(roberta_input_ids, attention_mask=roberta_attention_mask)
|
| 144 |
+
koelectra_outputs = self.koelectra_model(koelectra_input_ids, attention_mask=koelectra_attention_mask)
|
| 145 |
+
|
| 146 |
+
# Convert logits to probabilities
|
| 147 |
+
roberta_probs = F.softmax(roberta_outputs.logits, dim=1)
|
| 148 |
+
koelectra_probs = F.softmax(koelectra_outputs.logits, dim=1)
|
| 149 |
+
|
| 150 |
+
# Ensemble probabilities
|
| 151 |
+
ensemble_probs = (roberta_probs + koelectra_probs) / 2
|
| 152 |
+
|
| 153 |
+
final_labels = []
|
| 154 |
+
|
| 155 |
+
for probs in ensemble_probs:
|
| 156 |
+
pred_label = torch.argmax(probs).item()
|
| 157 |
+
if pred_label == 1 and probs[pred_label] > self.star1:
|
| 158 |
+
pred_label = 3
|
| 159 |
+
elif pred_label == 0 and probs[pred_label] > self.star2:
|
| 160 |
+
pred_label = 2
|
| 161 |
+
final_labels.append(pred_label)
|
| 162 |
+
|
| 163 |
+
return torch.tensor(final_labels).to(device), (roberta_outputs.logits + koelectra_outputs.logits) / 2
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
from torch.optim import Adam
|
| 167 |
+
|
| 168 |
+
# ๋ฐ์ดํฐ์
๋ฐ ํ๋ จ ์ค์
|
| 169 |
+
ensemble_model = EnsembleModel(model_roberta, model_koelectra).to(device)
|
| 170 |
+
|
| 171 |
+
# ์ตํฐ๋ง์ด์ ์ด๊ธฐํ
|
| 172 |
+
optimizer = Adam(ensemble_model.parameters(), lr=1e-5)
|
| 173 |
+
|
| 174 |
+
# DataLoader ์์ฑ
|
| 175 |
+
batch_size = 16 # ์ํ๋ ๋ฐฐ์น ํฌ๊ธฐ๋ก ์ค์ ํ์ธ์
|
| 176 |
+
train_dataloader_roberta = DataLoader(dataset_roberta, batch_size=batch_size, shuffle=True)
|
| 177 |
+
train_dataloader_koelectra = DataLoader(dataset_koelectra, batch_size=batch_size, shuffle=True)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def loss_fn(outputs, labels):
|
| 181 |
+
return F.cross_entropy(outputs, labels)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# Training loop
|
| 185 |
+
for epoch in range(5):
|
| 186 |
+
ensemble_model.train()
|
| 187 |
+
|
| 188 |
+
for batch_roberta, batch_koelectra in zip(train_dataloader_roberta, train_dataloader_koelectra):
|
| 189 |
+
texts = tokenizer_roberta.batch_decode(batch_roberta['input_ids'], skip_special_tokens=True)
|
| 190 |
+
labels = batch_roberta['labels'].to(device)
|
| 191 |
+
|
| 192 |
+
optimizer.zero_grad()
|
| 193 |
+
|
| 194 |
+
# Calculate model predictions
|
| 195 |
+
final_labels, logits = ensemble_model(texts, tokenizer_roberta, tokenizer_koelectra, device)
|
| 196 |
+
|
| 197 |
+
# Calculate the loss
|
| 198 |
+
loss = loss_fn(logits, labels)
|
| 199 |
+
|
| 200 |
+
loss.backward()
|
| 201 |
+
optimizer.step()
|
| 202 |
+
|
| 203 |
+
print(f"Epoch {epoch + 1}/50, Loss: {loss.item()}")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# ์์ธก ํจ์
|
| 207 |
+
def predict_with_ensemble_modified(texts, ensemble_model, tokenizer_roberta, tokenizer_koelectra, device):
|
| 208 |
+
ensemble_model.eval()
|
| 209 |
+
with torch.no_grad():
|
| 210 |
+
final_labels = ensemble_model(texts, tokenizer_roberta, tokenizer_koelectra, device)
|
| 211 |
+
|
| 212 |
+
# star1๊ณผ star2 ๊ฐ์ ์ถ๋ ฅ
|
| 213 |
+
print("star1 value:", ensemble_model.star1.item())
|
| 214 |
+
print("star2 value:", ensemble_model.star2.item())
|
| 215 |
+
|
| 216 |
+
return final_labels
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# ์ฌ์ฉ ์์
|
| 220 |
+
texts = ["์ฌ๊ธฐ์ ๋ถ์ํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์."]
|
| 221 |
+
final_labels = predict_with_ensemble_modified(texts, ensemble_model, tokenizer_roberta, tokenizer_koelectra, device)
|
| 222 |
+
print("์ต์ข
๋ ์ด๋ธ:", final_labels)
|
RecommendationSystem/downstream_train.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from torch.utils.data import Dataset, DataLoader
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForSequenceClassification,
|
| 8 |
+
Trainer,
|
| 9 |
+
TrainingArguments,
|
| 10 |
+
EvalPrediction
|
| 11 |
+
)
|
| 12 |
+
from sklearn.metrics import accuracy_score
|
| 13 |
+
from transformers import TrainerCallback
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# SentimentDataset ํด๋์ค ์ ์
|
| 22 |
+
class SentimentDataset(Dataset):
|
| 23 |
+
def __init__(self, encodings, labels):
|
| 24 |
+
self.encodings = encodings
|
| 25 |
+
self.labels = labels
|
| 26 |
+
|
| 27 |
+
def __getitem__(self, idx):
|
| 28 |
+
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
| 29 |
+
item['labels'] = torch.tensor(self.labels[idx])
|
| 30 |
+
return item
|
| 31 |
+
|
| 32 |
+
def __len__(self):
|
| 33 |
+
return len(self.labels)
|
| 34 |
+
|
| 35 |
+
# CSV ํ์ผ ๋ก๋
|
| 36 |
+
df = pd.read_csv('path') # hr_data
|
| 37 |
+
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
|
| 38 |
+
texts = df['person_corpus'].tolist()
|
| 39 |
+
labels = df['label'].tolist() # ๋ผ๋ฒจ ๋ฐ์ดํฐ๋ ์ ์๋ก ๋ณํ๋์ด ์์ด์ผ ํฉ๋๋ค.
|
| 40 |
+
|
| 41 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 42 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 43 |
+
tokenizer_electra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 44 |
+
|
| 45 |
+
# ๋ฐ์ดํฐ์
ํ ํฐํ
|
| 46 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 47 |
+
encodings_electra = tokenizer_electra(texts, truncation=True, padding=True, max_length=128)
|
| 48 |
+
|
| 49 |
+
# ๋ฐ์ดํฐ์
์์ฑ
|
| 50 |
+
dataset_roberta = SentimentDataset(encodings_roberta, labels)
|
| 51 |
+
dataset_electra = SentimentDataset(encodings_electra, labels)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 55 |
+
roberta_model_path = "path"
|
| 56 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 57 |
+
electra_model_path = "path"
|
| 58 |
+
|
| 59 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 60 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 61 |
+
model_electra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 62 |
+
|
| 63 |
+
# CustomTrainer ํด๋์ค ์ ์
|
| 64 |
+
class CustomTrainer(Trainer):
|
| 65 |
+
def __init__(self, *args, label_weights=None, **kwargs):
|
| 66 |
+
super().__init__(*args, **kwargs)
|
| 67 |
+
self.label_weights = label_weights if label_weights is not None else {}
|
| 68 |
+
|
| 69 |
+
def compute_loss(self, model, inputs, return_outputs=False):
|
| 70 |
+
outputs = model(**inputs)
|
| 71 |
+
logits = outputs.get('logits')
|
| 72 |
+
labels = inputs.get('labels')
|
| 73 |
+
if self.label_weights:
|
| 74 |
+
weight = torch.tensor([self.label_weights.get(label.item(), 1.0) for label in labels]).to(labels.device)
|
| 75 |
+
loss_fct = CrossEntropyLoss(weight=weight)
|
| 76 |
+
else:
|
| 77 |
+
loss_fct = CrossEntropyLoss()
|
| 78 |
+
loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
|
| 79 |
+
return (loss, outputs) if return_outputs else loss
|
| 80 |
+
|
| 81 |
+
# TrainingArguments ์ค์
|
| 82 |
+
training_args = TrainingArguments(
|
| 83 |
+
output_dir='./results',
|
| 84 |
+
num_train_epochs=100,
|
| 85 |
+
per_device_train_batch_size=16,
|
| 86 |
+
warmup_steps=500,
|
| 87 |
+
weight_decay=0.01,
|
| 88 |
+
logging_dir='./logs',
|
| 89 |
+
logging_steps=10,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# ๋ผ๋ฒจ๋ณ ๊ฐ์ค์น ์ค์
|
| 93 |
+
label_weights = {0: 8.0, 1: 2.5, 2: 5.0, 3: 1.0}
|
| 94 |
+
|
| 95 |
+
# CustomTrainer ์ธ์คํด์ค ์์ฑ
|
| 96 |
+
trainer_roberta = CustomTrainer(
|
| 97 |
+
model=model_roberta,
|
| 98 |
+
args=training_args,
|
| 99 |
+
train_dataset=dataset_roberta, # dataset_roberta๋ ์ฌ์ฉ์๊ฐ ์ ์ํด์ผ ํจ
|
| 100 |
+
label_weights=label_weights,
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
trainer_electra = CustomTrainer(
|
| 104 |
+
model=model_electra,
|
| 105 |
+
args=training_args,
|
| 106 |
+
train_dataset=dataset_electra, # dataset_electra๋ ์ฌ์ฉ์๊ฐ ์ ์ํด์ผ ํจ
|
| 107 |
+
label_weights=label_weights,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# ๋ก์ง์ ํ๋ฅ ๋ก ๋ณํํ๋ ํจ์
|
| 111 |
+
def logits_to_probs(logits):
|
| 112 |
+
return torch.softmax(logits, dim=1)
|
| 113 |
+
|
| 114 |
+
def predict_with_ensemble_modified(texts, roberta_model, koelectra_model, tokenizer_roberta, tokenizer_koelectra, device):
|
| 115 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 116 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128)
|
| 117 |
+
|
| 118 |
+
roberta_dataset = SentimentDataset(encodings_roberta, [0]*len(texts)) # ๋ผ๋ฒจ์ ์์ธก์ ์ํด ์ฌ์ฉ๋์ง ์์ผ๋ฏ๋ก ์์ ๊ฐ์ผ๋ก ์ค์
|
| 119 |
+
koelectra_dataset = SentimentDataset(encodings_koelectra, [0]*len(texts))
|
| 120 |
+
|
| 121 |
+
roberta_dataloader = DataLoader(roberta_dataset, batch_size=32, shuffle=False)
|
| 122 |
+
koelectra_dataloader = DataLoader(koelectra_dataset, batch_size=32, shuffle=False)
|
| 123 |
+
|
| 124 |
+
roberta_model.to(device)
|
| 125 |
+
koelectra_model.to(device)
|
| 126 |
+
|
| 127 |
+
roberta_model.eval()
|
| 128 |
+
koelectra_model.eval()
|
| 129 |
+
|
| 130 |
+
final_labels = [] # ์ต์ข
๋ผ๋ฒจ์ ์ ์ฅํ ๋ฆฌ์คํธ๋ฅผ ๋ฃจํ ์ธ๋ถ์์ ์ด๊ธฐํ
|
| 131 |
+
|
| 132 |
+
with torch.no_grad():
|
| 133 |
+
for roberta_batch, koelectra_batch in zip(roberta_dataloader, koelectra_dataloader):
|
| 134 |
+
roberta_input_ids, roberta_attention_mask = roberta_batch['input_ids'].to(device), roberta_batch['attention_mask'].to(device)
|
| 135 |
+
koelectra_input_ids, koelectra_attention_mask = koelectra_batch['input_ids'].to(device), koelectra_batch['attention_mask'].to(device)
|
| 136 |
+
|
| 137 |
+
roberta_outputs = roberta_model(roberta_input_ids, roberta_attention_mask)
|
| 138 |
+
koelectra_outputs = koelectra_model(koelectra_input_ids, koelectra_attention_mask)
|
| 139 |
+
|
| 140 |
+
roberta_probs = logits_to_probs(roberta_outputs.logits).cpu().numpy()
|
| 141 |
+
koelectra_probs = logits_to_probs(koelectra_outputs.logits).cpu().numpy()
|
| 142 |
+
|
| 143 |
+
ensemble_probs = (roberta_probs + koelectra_probs) / 2
|
| 144 |
+
|
| 145 |
+
for probs in ensemble_probs:
|
| 146 |
+
pred_label = np.argmax(probs)
|
| 147 |
+
if pred_label == 1 and probs[pred_label] > 0.6: # ๊ธ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.5 ์ด์์ธ ๊ฒฝ์ฐ
|
| 148 |
+
pred_label = 3 # ๋งค์ฐ ๊ธ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 149 |
+
elif pred_label == 0 and probs[pred_label] > 0.7: # ๋ถ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.5 ์ด์์ธ ๊ฒฝ์ฐ
|
| 150 |
+
pred_label = 2 # ๋งค์ฐ ๋ถ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 151 |
+
final_labels.append(pred_label) # ์์ ๋ ๋ผ๋ฒจ์ ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ์ ์ถ๊ฐ
|
| 152 |
+
|
| 153 |
+
return final_labels # ์์ : ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ ๋ฐํ
|
| 154 |
+
|
| 155 |
+
# ํจ์ ํธ์ถ์ device ๋ณ์ ์ฌ์ฉ
|
| 156 |
+
final_labels = predict_with_ensemble_modified(texts, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device)
|
| 157 |
+
|
| 158 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 159 |
+
for text, label in zip(texts, final_labels):
|
| 160 |
+
print(f"Text: {text} - Prediction: {label}")
|
RecommendationSystem/get_label.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#get label
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
import openai
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_completion(prompt, model="gpt-3.5-turbo", api_key=""):
|
| 10 |
+
openai.api_key = api_key
|
| 11 |
+
messages = [{"role": "user", "content": prompt}]
|
| 12 |
+
|
| 13 |
+
response = openai.ChatCompletion.create(
|
| 14 |
+
model=model,
|
| 15 |
+
messages=messages,
|
| 16 |
+
temperature=0,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
return response.choices[0].message["content"]
|
| 20 |
+
|
| 21 |
+
def get_label(person_name, person_corpus: str, api_key: str) -> str:
|
| 22 |
+
prompt = f'''### ์ญํ :
|
| 23 |
+
๋ค์ ์ค๋ช
์ ๊ธฐ๋ฐ์ผ๋ก {person_name}์ ๋ํ ํ๊ฐ๊ฐ "๋งค์ฐ ๊ธ์ ", "๊ธ์ ", "๋ถ์ ", "๋งค์ฐ ๋ถ์ " ์ค ์ด๋ ๊ฒ์ ํด๋นํ๋์ง ๋ผ๋ฒจ๋ง ํ์์ค. ์ด๋ ์๋ ๊ท์น์ ๋ฐ๋ฆ
๋๋ค.
|
| 24 |
+
|
| 25 |
+
### ์ค๋ช
:
|
| 26 |
+
{person_corpus}
|
| 27 |
+
|
| 28 |
+
### ๊ท์น:
|
| 29 |
+
1. ์ฃผ์ด์ง ์ค๋ช
์ ๋ํด ํ๋์ ["๋งค์ฐ ๊ธ์ "์ ํ ํฐ๊ฐ์ผ๋ก 3, "๊ธ์ "์ ํ ํฐ๊ฐ์ผ๋ก 1, "๋ถ์ "์ ํ ํฐ๊ฐ์ผ๋ก 0, "๋งค์ฐ ๋ถ์ "์ ํ ํฐ๊ฐ์ผ๋ก 2]๋ง์ ์ ํํ์ฌ ๋ผ๋ฒจ๋งํ๋ค.
|
| 30 |
+
2. ์ค๋ช
์ "### ์ค๋ช
:" ๋ถ๋ถ๋ถํฐ ์์๋๋ค.
|
| 31 |
+
3. ์ถ๋ ฅ์ "### ์ถ๋ ฅ"์ ๊ฐ์ด json ํ์์ผ๋ก ์ถ๋ ฅํ๋ค.
|
| 32 |
+
4. ๋ผ๋ฒจ๋ง์ ์ค๋ช
์ ๋ฌธ์ ๋ํด์ ํ๋ค.
|
| 33 |
+
5. ์ค๋ฆฝ์ ์ ํ์ง์ ์๋ค.
|
| 34 |
+
6. ์ถ๋ ฅ์ ํ์์ธ์ ์๋ฌด๊ฒ๋ ์์ฑํ์ง ์๋๋ค.
|
| 35 |
+
|
| 36 |
+
### ์ถ๋ ฅ
|
| 37 |
+
'''
|
| 38 |
+
|
| 39 |
+
response = get_completion(prompt, "gpt-3.5-turbo", api_key)
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
path_file = "path" # ๋ฐ์ดํฐ์
ํ์ผ ๊ฒฝ๋ก ์ง์
|
| 44 |
+
api_key = "OpenAi api key" # OpenAI API ํค ์ง์
|
| 45 |
+
pos_start = 0
|
| 46 |
+
len_slice = 500
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
df_people = pd.read_csv(path_file)[pos_start:pos_start+len_slice]
|
| 51 |
+
df_people = df_people.drop_duplicates(["person_name"])
|
| 52 |
+
df_people = df_people.dropna()
|
| 53 |
+
|
| 54 |
+
list_result = []
|
| 55 |
+
|
| 56 |
+
for idx, row in tqdm(df_people.iterrows(), total=df_people.shape[0]):
|
| 57 |
+
try:
|
| 58 |
+
label_response = get_label(row["person_name"], row["person_corpus"], api_key)
|
| 59 |
+
list_result.append(label_response)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"idx: {idx}, err: {e}")
|
| 62 |
+
list_result.append(f"idx: {idx}, err: {e}")
|
| 63 |
+
|
| 64 |
+
df_people["label"] = list_result
|
| 65 |
+
|
| 66 |
+
df_people.to_csv(f"./labeled_people_{pos_start}_to_{pos_start + len_slice}.csv", index=False)
|
RecommendationSystem/pre_train.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pre_train.py
|
| 2 |
+
#ec2-train
|
| 3 |
+
|
| 4 |
+
from transformers import AutoModelForSequenceClassification
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
| 7 |
+
from torch.utils.data import Dataset, DataLoader
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from transformers import EvalPrediction
|
| 11 |
+
from sklearn.metrics import accuracy_score
|
| 12 |
+
from transformers import TrainerCallback
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
|
| 18 |
+
# SentimentDataset ํด๋์ค ์ ์
|
| 19 |
+
class SentimentDataset(Dataset):
|
| 20 |
+
def __init__(self, encodings, labels):
|
| 21 |
+
self.encodings = encodings
|
| 22 |
+
self.labels = labels
|
| 23 |
+
|
| 24 |
+
def __getitem__(self, idx):
|
| 25 |
+
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
|
| 26 |
+
item['labels'] = torch.tensor(self.labels[idx])
|
| 27 |
+
return item
|
| 28 |
+
|
| 29 |
+
def __len__(self):
|
| 30 |
+
return len(self.labels)
|
| 31 |
+
|
| 32 |
+
# CSV ํ์ผ ๋ก๋
|
| 33 |
+
import pandas as pd
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
df = pd.read_csv('/home/ec2-user/train_org.csv') # 'your_dataset.csv'๋ฅผ ์ค์ ํ์ผ ๊ฒฝ๋ก๋ก ๋ณ๊ฒฝํ์ธ์.
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
|
| 40 |
+
texts = df['document'].tolist() # ๋ฆฌ๋ทฐ ํ
์คํธ
|
| 41 |
+
labels = df['label'].tolist() # ๋ผ๋ฒจ ๋ฐ์ดํฐ๋ ์ ์๋ก ๋ณํ๋์ด ์์ด์ผ ํฉ๋๋ค..
|
| 42 |
+
labels = [int(label) for label in labels] # ๋ผ๋ฒจ์ ์ ์ํ์ผ๋ก ๋ณํ
|
| 43 |
+
texts = [str(text) for text in texts if text is not None]
|
| 44 |
+
|
| 45 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 46 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 47 |
+
tokenizer_electra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 48 |
+
|
| 49 |
+
# ๋ฐ์ดํฐ์
ํ ํฐํ
|
| 50 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 51 |
+
encodings_electra = tokenizer_electra(texts, truncation=True, padding=True, max_length=128)
|
| 52 |
+
|
| 53 |
+
# ๋ฐ์ดํฐ์
์์ฑ
|
| 54 |
+
dataset_roberta = SentimentDataset(encodings_roberta, labels)
|
| 55 |
+
dataset_electra = SentimentDataset(encodings_electra, labels)
|
| 56 |
+
|
| 57 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 58 |
+
roberta_model_path = "/home/ec2-user/roberta_model"
|
| 59 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 60 |
+
electra_model_path = "/home/ec2-user/electra_model"
|
| 61 |
+
|
| 62 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 63 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 64 |
+
model_electra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ์ฌ์ฉ์ ์ง์ ์ฝ๋ฐฑ ํด๋์ค ์ ์
|
| 68 |
+
class PrintProgressCallback(TrainerCallback):
|
| 69 |
+
def on_epoch_end(self, args, state, control, **kwargs):
|
| 70 |
+
print(f"Epoch {state.epoch} ์ข
๋ฃ. ์งํ ์ํ: {state.global_step}/{state.max_steps} ({(state.global_step/state.max_steps)*100:.2f}%)")
|
| 71 |
+
if 'eval_metrics' in kwargs:
|
| 72 |
+
print(f"Epoch {state.epoch} Accuracy: {kwargs['eval_metrics']['eval_accuracy']:.4f}")
|
| 73 |
+
|
| 74 |
+
# ์์์ ์ ์ํ compute_metrics ํจ์ ์ฌ์ฉ
|
| 75 |
+
def compute_metrics(p: EvalPrediction):
|
| 76 |
+
preds = np.argmax(p.predictions, axis=1)
|
| 77 |
+
return {"accuracy": accuracy_score(p.label_ids, preds)}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class CustomTrainer(Trainer):
|
| 81 |
+
def __init__(self, label_weights, *args, **kwargs):
|
| 82 |
+
super().__init__(*args, **kwargs)
|
| 83 |
+
self.label_weights = label_weights
|
| 84 |
+
|
| 85 |
+
def compute_loss(self, model, inputs, return_outputs=False):
|
| 86 |
+
labels = inputs.get("labels")
|
| 87 |
+
# ๋ชจ๋ธ์ ์ถ๋ ฅ์์ ๋ก์ค ๊ณ์ฐ
|
| 88 |
+
outputs = model(**inputs)
|
| 89 |
+
logits = outputs.get('logits')
|
| 90 |
+
# ๊ฐ์ค์น ์ ์ฉํ์ฌ ๋ก์ค ๊ณ์ฐ
|
| 91 |
+
loss_fct = torch.nn.CrossEntropyLoss(weight=self.label_weights)
|
| 92 |
+
loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1))
|
| 93 |
+
return (loss, outputs) if return_outputs else loss
|
| 94 |
+
|
| 95 |
+
# def on_step_end(self, args, state, control, **kwargs):
|
| 96 |
+
# super().on_step_end(args, state, control, **kwargs)
|
| 97 |
+
# # ๋งค 1000 ์คํ
๋ง๋ค ๋ชจ๋ธ ์ ์ฅ
|
| 98 |
+
# if state.global_step % 10 == 0:
|
| 99 |
+
# output_dir1 = f"{args.output_dir1}/checkpoint-{state.global_step}"
|
| 100 |
+
# self.save_model(output_dir1)
|
| 101 |
+
# print(f"๋ชจ๋ธ์ {output_dir1}์ ์ ์ฅํ์ต๋๋ค.")
|
| 102 |
+
# if state.global_step % 10 == 0:
|
| 103 |
+
# output_dir2 = f"{args.output_dir2}/checkpoint-{state.global_step}"
|
| 104 |
+
# self.save_model(output_dir2)
|
| 105 |
+
# print(f"๋ชจ๋ธ์ {output_dir2}์ ์ ์ฅํ์ต๋๋ค.")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# TrainingArguments ์ค์
|
| 110 |
+
training_args = TrainingArguments(
|
| 111 |
+
output_dir= './result',
|
| 112 |
+
num_train_epochs=1,
|
| 113 |
+
per_device_train_batch_size=32,
|
| 114 |
+
warmup_steps=160000000,
|
| 115 |
+
weight_decay=0.01,
|
| 116 |
+
logging_dir='./logs',
|
| 117 |
+
logging_steps=800,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ๋ผ๋ฒจ ๊ฐ์ค์น ์ค์
|
| 122 |
+
# ์์: ๊ฐ ๋ผ๋ฒจ์ ๋ํ ๊ฐ์ค์น๊ฐ ๋์ผํ๋ค๊ณ ๊ฐ์
|
| 123 |
+
label_weights = torch.tensor([1.0, 1.0, 1.0, 1.0]).to(device)
|
| 124 |
+
|
| 125 |
+
# CustomTrainer ์ธ์คํด์ค ์์ฑ ์ ์ฌ์ฉ์ ์ง์ ์ฝ๋ฐฑ ์ถ๊ฐ
|
| 126 |
+
trainer_roberta = CustomTrainer(
|
| 127 |
+
model=model_roberta,
|
| 128 |
+
args=training_args,
|
| 129 |
+
train_dataset=dataset_roberta,
|
| 130 |
+
label_weights=label_weights,
|
| 131 |
+
compute_metrics=compute_metrics,
|
| 132 |
+
callbacks=[PrintProgressCallback()], # ์ฝ๋ฐฑ ์ถ๊ฐ
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
trainer_electra = CustomTrainer(
|
| 136 |
+
model=model_electra,
|
| 137 |
+
args=training_args,
|
| 138 |
+
train_dataset=dataset_electra,
|
| 139 |
+
label_weights=label_weights,
|
| 140 |
+
compute_metrics=compute_metrics,
|
| 141 |
+
callbacks=[PrintProgressCallback()], # ์ฝ๋ฐฑ ์ถ๊ฐ
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# ํ๋ จ ์์
|
| 145 |
+
trainer_roberta.train()
|
| 146 |
+
trainer_electra.train()
|
| 147 |
+
|
| 148 |
+
# ํ๋ จ๋ RoBERTa ๋ชจ๋ธ ์ ์ฅ
|
| 149 |
+
model_save_path = "/home/ec2-user/to/save/roberta_model"
|
| 150 |
+
trainer_roberta.save_model(model_save_path)
|
| 151 |
+
|
| 152 |
+
# ํ๋ จ๋ Electra ๋ชจ๋ธ ์ ์ฅ
|
| 153 |
+
model_save_path = "/home/ec2-user/to/save/electra_model"
|
| 154 |
+
trainer_electra.save_model(model_save_path)
|
RecommendationSystem/predict
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
|
| 8 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 9 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 10 |
+
tokenizer_electra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 11 |
+
|
| 12 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 13 |
+
roberta_model_path = "/content/drive/MyDrive/to/save/roberta_model"
|
| 14 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 15 |
+
electra_model_path = "/content/drive/MyDrive/to/save/electra_model"
|
| 16 |
+
|
| 17 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 18 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 19 |
+
model_electra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 20 |
+
# predict_with_ensemble_modified
|
| 21 |
+
# ์ํํธ๋งฅ์ค ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ก์ง์ ํ๋ฅ ๋ก ๋ณํํ๋ ํจ์
|
| 22 |
+
def logits_to_probs(logits):
|
| 23 |
+
return torch.nn.functional.softmax(logits, dim=1)
|
| 24 |
+
|
| 25 |
+
def predict_with_ensemble_modified(texts, roberta_model, koelectra_model, tokenizer_roberta, tokenizer_koelectra, device):
|
| 26 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 27 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128)
|
| 28 |
+
roberta_dataset = SentimentDataset(encodings_roberta, [0]*len(texts))
|
| 29 |
+
koelectra_dataset = SentimentDataset(encodings_koelectra, [0]*len(texts))
|
| 30 |
+
|
| 31 |
+
roberta_dataloader = DataLoader(roberta_dataset, batch_size=32, shuffle=False)
|
| 32 |
+
koelectra_dataloader = DataLoader(koelectra_dataset, batch_size=32, shuffle=False)
|
| 33 |
+
|
| 34 |
+
roberta_model.to(device)
|
| 35 |
+
koelectra_model.to(device)
|
| 36 |
+
|
| 37 |
+
roberta_model.eval()
|
| 38 |
+
koelectra_model.eval()
|
| 39 |
+
|
| 40 |
+
final_labels = []
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
for roberta_batch, koelectra_batch in zip(roberta_dataloader, koelectra_dataloader):
|
| 44 |
+
roberta_input_ids, roberta_attention_mask = roberta_batch['input_ids'].to(device), roberta_batch['attention_mask'].to(device)
|
| 45 |
+
koelectra_input_ids, koelectra_attention_mask = koelectra_batch['input_ids'].to(device), koelectra_batch['attention_mask'].to(device)
|
| 46 |
+
|
| 47 |
+
roberta_outputs = roberta_model(roberta_input_ids, roberta_attention_mask)
|
| 48 |
+
koelectra_outputs = koelectra_model(koelectra_input_ids, koelectra_attention_mask)
|
| 49 |
+
|
| 50 |
+
roberta_probs = logits_to_probs(roberta_outputs.logits).cpu().numpy()
|
| 51 |
+
koelectra_probs = logits_to_probs(koelectra_outputs.logits).cpu().numpy()
|
| 52 |
+
|
| 53 |
+
ensemble_probs = (roberta_probs + koelectra_probs) / 2
|
| 54 |
+
|
| 55 |
+
final_labels = []
|
| 56 |
+
|
| 57 |
+
for probs in ensemble_probs:
|
| 58 |
+
pred_label = np.argmax(probs)
|
| 59 |
+
if pred_label == 1 and probs[pred_label] > 0.6: # ๊ธ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.6 ์ด์์ธ ๊ฒฝ์ฐ
|
| 60 |
+
pred_label = 3 # ๋งค์ฐ ๊ธ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 61 |
+
elif pred_label == 0 and probs[pred_label] > 0.7: # ๋ถ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.7 ์ด์์ธ ๊ฒฝ์ฐ
|
| 62 |
+
pred_label = 2 # ๋งค์ฐ ๋ถ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 63 |
+
final_labels.append(pred_label) # ์์ ๋ ๋ผ๋ฒจ์ ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ์ ์ถ๊ฐ
|
| 64 |
+
|
| 65 |
+
return final_labels # ์์ : ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ ๋ฐํ
|
| 66 |
+
|
| 67 |
+
# ์์ธกํ ํ
์คํธ ์ํ์ค
|
| 68 |
+
texts_to_predict = ["์ผ์ ๋ณดํต์ผ๋ก ํ๊ณ ์ฌ๋์ ์ข์.","์ผ๋ ๋งค์ฐ ์ํ๊ณ ์ฌ๋๋ ์ข๊ณ ์ฑ์คํจ", "์ผ์ ์๋ชปํ์ง๋ง ๋ง์ ์ ๋ค์", "๋ถ์ฑ์คํ๊ณ ๋งค์ฐ ํ์์์ ๊ทธ๋ฅ ์๋๊ฒ ๋์"]
|
| 69 |
+
|
| 70 |
+
# ํจ์ ํธ์ถ์ device ๋ณ์ ์ฌ์ฉ
|
| 71 |
+
final_labels = predict_with_ensemble_modified(texts_to_predict, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device)
|
| 72 |
+
|
| 73 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 74 |
+
for text, label in zip(texts_to_predict, final_labels):
|
| 75 |
+
print(f"Text: {text} - Prediction: {label}")
|
RecommendationSystem/predict.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
|
| 8 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 9 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 10 |
+
tokenizer_electra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 11 |
+
|
| 12 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 13 |
+
roberta_model_path = "/content/drive/MyDrive/to/save/roberta_model"
|
| 14 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 15 |
+
electra_model_path = "/content/drive/MyDrive/to/save/electra_model"
|
| 16 |
+
|
| 17 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 18 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 19 |
+
model_electra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 20 |
+
# predict_with_ensemble_modified
|
| 21 |
+
# ์ํํธ๋งฅ์ค ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ก์ง์ ํ๋ฅ ๋ก ๋ณํํ๋ ํจ์
|
| 22 |
+
def logits_to_probs(logits):
|
| 23 |
+
return torch.nn.functional.softmax(logits, dim=1)
|
| 24 |
+
|
| 25 |
+
def predict_with_ensemble_modified(texts, roberta_model, koelectra_model, tokenizer_roberta, tokenizer_koelectra, device):
|
| 26 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128)
|
| 27 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128)
|
| 28 |
+
roberta_dataset = SentimentDataset(encodings_roberta, [0]*len(texts))
|
| 29 |
+
koelectra_dataset = SentimentDataset(encodings_koelectra, [0]*len(texts))
|
| 30 |
+
|
| 31 |
+
roberta_dataloader = DataLoader(roberta_dataset, batch_size=32, shuffle=False)
|
| 32 |
+
koelectra_dataloader = DataLoader(koelectra_dataset, batch_size=32, shuffle=False)
|
| 33 |
+
|
| 34 |
+
roberta_model.to(device)
|
| 35 |
+
koelectra_model.to(device)
|
| 36 |
+
|
| 37 |
+
roberta_model.eval()
|
| 38 |
+
koelectra_model.eval()
|
| 39 |
+
|
| 40 |
+
final_labels = []
|
| 41 |
+
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
for roberta_batch, koelectra_batch in zip(roberta_dataloader, koelectra_dataloader):
|
| 44 |
+
roberta_input_ids, roberta_attention_mask = roberta_batch['input_ids'].to(device), roberta_batch['attention_mask'].to(device)
|
| 45 |
+
koelectra_input_ids, koelectra_attention_mask = koelectra_batch['input_ids'].to(device), koelectra_batch['attention_mask'].to(device)
|
| 46 |
+
|
| 47 |
+
roberta_outputs = roberta_model(roberta_input_ids, roberta_attention_mask)
|
| 48 |
+
koelectra_outputs = koelectra_model(koelectra_input_ids, koelectra_attention_mask)
|
| 49 |
+
|
| 50 |
+
roberta_probs = logits_to_probs(roberta_outputs.logits).cpu().numpy()
|
| 51 |
+
koelectra_probs = logits_to_probs(koelectra_outputs.logits).cpu().numpy()
|
| 52 |
+
|
| 53 |
+
ensemble_probs = (roberta_probs + koelectra_probs) / 2
|
| 54 |
+
|
| 55 |
+
final_labels = []
|
| 56 |
+
|
| 57 |
+
for probs in ensemble_probs:
|
| 58 |
+
pred_label = np.argmax(probs)
|
| 59 |
+
if pred_label == 1 and probs[pred_label] > 0.6: # ๊ธ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.6 ์ด์์ธ ๊ฒฝ์ฐ
|
| 60 |
+
pred_label = 3 # ๋งค์ฐ ๊ธ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 61 |
+
elif pred_label == 0 and probs[pred_label] > 0.7: # ๋ถ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.7 ์ด์์ธ ๊ฒฝ์ฐ
|
| 62 |
+
pred_label = 2 # ๋งค์ฐ ๋ถ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 63 |
+
final_labels.append(pred_label) # ์์ ๋ ๋ผ๋ฒจ์ ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ์ ์ถ๊ฐ
|
| 64 |
+
|
| 65 |
+
return final_labels # ์์ : ์ต์ข
๋ผ๋ฒจ ๋ฆฌ์คํธ ๋ฐํ
|
| 66 |
+
|
| 67 |
+
# ์์ธกํ ํ
์คํธ ์ํ์ค
|
| 68 |
+
texts_to_predict = ["์ผ์ ๋ณดํต์ผ๋ก ํ๊ณ ์ฌ๋์ ์ข์.","์ผ๋ ๋งค์ฐ ์ํ๊ณ ์ฌ๋๋ ์ข๊ณ ์ฑ์คํจ", "์ผ์ ์๋ชปํ์ง๋ง ๋ง์ ์ ๋ค์", "๋ถ์ฑ์คํ๊ณ ๋งค์ฐ ํ์์์ ๊ทธ๋ฅ ์๋๊ฒ ๋์"]
|
| 69 |
+
|
| 70 |
+
# ํจ์ ํธ์ถ์ device ๋ณ์ ์ฌ์ฉ
|
| 71 |
+
final_labels = predict_with_ensemble_modified(texts_to_predict, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device)
|
| 72 |
+
|
| 73 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
| 74 |
+
for text, label in zip(texts_to_predict, final_labels):
|
| 75 |
+
print(f"Text: {text} - Prediction: {label}")
|
RecommendationSystem/recommendation_scoring.py
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from geopy.distance import geodesic
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ํ์ธ ๋ฐ device ์ค์
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
# ํ ํฌ๋์ด์ ๋ก๋ฉ
|
| 10 |
+
tokenizer_roberta = AutoTokenizer.from_pretrained("klue/roberta-large")
|
| 11 |
+
tokenizer_electra = AutoTokenizer.from_pretrained("monologg/koelectra-base-v3-discriminator")
|
| 12 |
+
|
| 13 |
+
# ๋ก๋ํ RoBERTa ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 14 |
+
roberta_model_path = "/content/drive/MyDrive/robust_model/roberta_model"
|
| 15 |
+
# ๋ก๋ํ Electra ๋ชจ๋ธ ๊ฒฝ๋ก
|
| 16 |
+
electra_model_path = "/content/drive/MyDrive/robust_model/electra_model"
|
| 17 |
+
|
| 18 |
+
# ์ ์ฅ๋ ๋ชจ๋ธ ๋ก๋
|
| 19 |
+
model_roberta = AutoModelForSequenceClassification.from_pretrained(roberta_model_path)
|
| 20 |
+
model_electra = AutoModelForSequenceClassification.from_pretrained(electra_model_path)
|
| 21 |
+
|
| 22 |
+
def calculate_distance(point1, point2):
|
| 23 |
+
# ๋ ์ง์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ ํจ์
|
| 24 |
+
distance_to_station = geodesic(point1, point2).kilometers
|
| 25 |
+
return distance_to_station
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def label_to_value(label):
|
| 30 |
+
if label == 0:
|
| 31 |
+
return -30
|
| 32 |
+
elif label == 1:
|
| 33 |
+
return 30
|
| 34 |
+
elif label == 2:
|
| 35 |
+
return -50
|
| 36 |
+
elif label == 3:
|
| 37 |
+
return 50
|
| 38 |
+
else:
|
| 39 |
+
return 0
|
| 40 |
+
|
| 41 |
+
def logits_to_probs(logits):
|
| 42 |
+
return torch.nn.functional.softmax(logits, dim=1)
|
| 43 |
+
|
| 44 |
+
def predict_with_ensemble_modified(texts, roberta_model, koelectra_model, tokenizer_roberta, tokenizer_koelectra, device):
|
| 45 |
+
encodings_roberta = tokenizer_roberta(texts, truncation=True, padding=True, max_length=128, return_tensors="pt")
|
| 46 |
+
encodings_koelectra = tokenizer_koelectra(texts, truncation=True, padding=True, max_length=128, return_tensors="pt")
|
| 47 |
+
|
| 48 |
+
roberta_input_ids, roberta_attention_mask = encodings_roberta['input_ids'].to(device), encodings_roberta['attention_mask'].to(device)
|
| 49 |
+
koelectra_input_ids, koelectra_attention_mask = encodings_koelectra['input_ids'].to(device), encodings_koelectra['attention_mask'].to(device)
|
| 50 |
+
|
| 51 |
+
roberta_model.to(device)
|
| 52 |
+
koelectra_model.to(device)
|
| 53 |
+
|
| 54 |
+
roberta_model.eval()
|
| 55 |
+
koelectra_model.eval()
|
| 56 |
+
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
roberta_outputs = roberta_model(roberta_input_ids, roberta_attention_mask)
|
| 59 |
+
koelectra_outputs = koelectra_model(koelectra_input_ids, koelectra_attention_mask)
|
| 60 |
+
|
| 61 |
+
roberta_probs = logits_to_probs(roberta_outputs.logits).cpu().numpy()
|
| 62 |
+
koelectra_probs = logits_to_probs(koelectra_outputs.logits).cpu().numpy()
|
| 63 |
+
|
| 64 |
+
ensemble_probs = (roberta_probs + koelectra_probs) / 2
|
| 65 |
+
|
| 66 |
+
final_labels = []
|
| 67 |
+
|
| 68 |
+
for probs in ensemble_probs:
|
| 69 |
+
pred_label = np.argmax(probs)
|
| 70 |
+
if pred_label == 1 and probs[pred_label] > 0.6: # ๊ธ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.8 ์ด์์ธ ๊ฒฝ์ฐ
|
| 71 |
+
pred_label = 3 # ๋งค์ฐ ๊ธ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 72 |
+
elif pred_label == 0 and probs[pred_label] > 0.7: # ๋ถ์ ์ด๋ฉด์ ํ๋ฅ ์ด 0.8 ์ด์์ธ ๊ฒฝ์ฐ
|
| 73 |
+
pred_label = 2 # ๋งค์ฐ ๋ถ์ ์ผ๋ก ๋ณ๊ฒฝ
|
| 74 |
+
final_labels.append(pred_label)
|
| 75 |
+
|
| 76 |
+
return final_labels
|
| 77 |
+
|
| 78 |
+
def get_coordinates_worksites(address):
|
| 79 |
+
# ์ฃผ์์ ๋ฐ๋ฅธ ์ขํ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋์
๋๋ฆฌ
|
| 80 |
+
coordinates_worksites = {
|
| 81 |
+
"์์ธ": {'work_location_x': 37.5647, 'work_location_y': 126.9770},
|
| 82 |
+
"๊ฐ๋จ๊ตฌ": {'work_location_x': 37.4979, 'work_location_y': 127.0276},
|
| 83 |
+
"๊ฐ๋๊ตฌ": {'work_location_x': 37.5385, 'work_location_y': 127.1238},
|
| 84 |
+
"๊ฐ๋ถ๊ตฌ": {'work_location_x': 37.6380, 'work_location_y': 127.0251},
|
| 85 |
+
"๊ฐ์๊ตฌ": {'work_location_x': 37.5316, 'work_location_y': 126.8467},
|
| 86 |
+
"๊ด์
๊ตฌ": {'work_location_x': 37.4813, 'work_location_y': 126.9527},
|
| 87 |
+
"๊ด์ง๊ตฌ": {'work_location_x': 37.5351, 'work_location_y': 127.0857},
|
| 88 |
+
"๊ตฌ๋ก๊ตฌ": {'work_location_x': 37.4853, 'work_location_y': 126.9015},
|
| 89 |
+
"๊ธ์ฒ๊ตฌ": {'work_location_x': 37.4665, 'work_location_y': 126.8893},
|
| 90 |
+
"๋
ธ์๊ตฌ": {'work_location_x': 37.6445, 'work_location_y': 127.0643},
|
| 91 |
+
"๋๋ด๊ตฌ": {'work_location_x': 37.6531, 'work_location_y': 127.0476},
|
| 92 |
+
"๋๋๋ฌธ๊ตฌ": {'work_location_x': 37.5716, 'work_location_y': 127.0095},
|
| 93 |
+
"๋์๊ตฌ": {'work_location_x': 37.5082, 'work_location_y': 126.9637},
|
| 94 |
+
"๋งํฌ๊ตฌ": {'work_location_x': 37.5572, 'work_location_y': 126.9236},
|
| 95 |
+
"์๋๋ฌธ๊ตฌ": {'work_location_x': 37.5658, 'work_location_y': 126.9666},
|
| 96 |
+
"์์ด๊ตฌ": {'work_location_x': 37.4919, 'work_location_y': 127.0072},
|
| 97 |
+
"์ฑ๋๊ตฌ": {'work_location_x': 37.5612, 'work_location_y': 127.0379},
|
| 98 |
+
"์ฑ๋ถ๊ตฌ": {'work_location_x': 37.5926, 'work_location_y': 127.0163},
|
| 99 |
+
"์กํ๊ตฌ": {'work_location_x': 37.5133, 'work_location_y': 127.1001},
|
| 100 |
+
"์์ฒ๊ตฌ": {'work_location_x': 37.5263, 'work_location_y': 126.8643},
|
| 101 |
+
"์๋ฑํฌ๊ตฌ": {'work_location_x': 37.5171, 'work_location_y': 126.9173},
|
| 102 |
+
"์ฉ์ฐ๏ฟฝ๏ฟฝ๏ฟฝ": {'work_location_x': 37.5298, 'work_location_y': 126.9644},
|
| 103 |
+
"์ํ๊ตฌ": {'work_location_x': 37.6190, 'work_location_y': 126.9215},
|
| 104 |
+
"์ข
๋ก๊ตฌ": {'work_location_x': 37.5704, 'work_location_y': 126.9910},
|
| 105 |
+
"์ค๊ตฌ": {'work_location_x': 37.5647, 'work_location_y': 126.9770},
|
| 106 |
+
"์ค๋๊ตฌ": {'work_location_x': 37.5960, 'work_location_y': 127.085},
|
| 107 |
+
"์ธ์ฒ": {'work_location_x': 37.5715, 'work_location_y': 126.7354},
|
| 108 |
+
"์ธ์ฒ ๊ฐํ๊ตฐ": {'work_location_x': 37.5692, 'work_location_y': 126.6732},
|
| 109 |
+
"์ธ์ฒ ๊ณ์๊ตฌ": {'work_location_x': 37.5715, 'work_location_y': 126.7354},
|
| 110 |
+
"์ธ์ฒ ๋จ๋๊ตฌ": {'work_location_x': 37.4488, 'work_location_y': 126.7013},
|
| 111 |
+
"์ธ์ฒ ๋๊ตฌ": {'work_location_x': 37.4751, 'work_location_y': 126.6322},
|
| 112 |
+
"์ธ์ฒ ๋ฏธ์ถํ๊ตฌ": {'work_location_x': 37.4420, 'work_location_y': 126.6995},
|
| 113 |
+
"์ธ์ฒ ๋ถํ๊ตฌ": {'work_location_x': 37.4891, 'work_location_y': 126.7245},
|
| 114 |
+
"์ธ์ฒ ์๊ตฌ": {'work_location_x': 37.5692, 'work_location_y': 126.6732},
|
| 115 |
+
"์ธ์ฒ ์ฐ์๊ตฌ": {'work_location_x': 37.3866, 'work_location_y': 126.6392},
|
| 116 |
+
"์ธ์ฒ ์น์ง๊ตฐ": {'work_location_x': 37.5111, 'work_location_y': 126.5230},
|
| 117 |
+
"์ธ์ฒ ์ค๊ตฌ": {'work_location_x': 37.4764, 'work_location_y': 126.6169},
|
| 118 |
+
"๊ฐํ๊ตฐ": {'๊ฐํ์ญ': {'work_location_x': 37.8147, 'work_location_y': 127.5102}},
|
| 119 |
+
"๊ณ ์์ ์ผ์ฐ์๊ตฌ": {'work_location_x': 37.6580, 'work_location_y': 126.7942},
|
| 120 |
+
"๊ณ ์์ ์ผ์ฐ๋๊ตฌ": {'work_location_x': 37.6586, 'work_location_y': 126.7698},
|
| 121 |
+
"๊ณ ์์ ๋์๊ตฌ": {'work_location_x': 37.6347, 'work_location_y': 126.8328},
|
| 122 |
+
"๊ณผ์ฒ์": {'work_location_x': 37.4339, 'work_location_y': 126.9966},
|
| 123 |
+
"๊ด๋ช
์": {'work_location_x': 37.4163, 'work_location_y': 126.8840},
|
| 124 |
+
"๊ด์ฃผ์": {'work_location_x': 37.4095, 'work_location_y': 127.2550},
|
| 125 |
+
"๊ตฌ๋ฆฌ์": {'work_location_x': 37.6038, 'work_location_y': 127.1436},
|
| 126 |
+
"๊ตฐํฌ์": {'work_location_x': 37.3532, 'work_location_y': 126.9488},
|
| 127 |
+
"๊นํฌ์": {"work_location_x": 37.5613, "work_location_y": 126.8019},
|
| 128 |
+
"๋จ์์ฃผ์": {"work_location_x": 37.6423, "work_location_y": 127.1264},
|
| 129 |
+
"๋๋์ฒ์": {"work_location_x": 37.8922, "work_location_y": 127.0603},
|
| 130 |
+
"๋ถ์ฒ์ ์์ฌ๊ตฌ": {"subway_location_x": 37.4827, "subway_location_y": 126.7950},
|
| 131 |
+
"๋ถ์ฒ์ ์๋ฏธ๊ตฌ": {"subway_location_x": 37.5047, "subway_location_y": 126.7630},
|
| 132 |
+
"๋ถ์ฒ์ ์ค์ ๊ตฌ": {"subway_location_x": 37.5142, "subway_location_y": 126.7928},
|
| 133 |
+
"์ฑ๋จ์ ์์ ๊ตฌ": {"work_location_x": 37.4519, "work_location_y": 127.1584},
|
| 134 |
+
"์ฑ๋จ์ ๋ถ๋น๊ตฌ": {"work_location_x": 37.3595, "work_location_y": 127.1086},
|
| 135 |
+
"์ฑ๋จ์ ์ค์๊ตฌ": {"work_location_x": 37.4321, "work_location_y": 127.1150},
|
| 136 |
+
"์์์ ์ฅ์๊ตฌ": {"work_location_x": 37.2986, "work_location_y": 127.0107},
|
| 137 |
+
"์์์ ๊ถ์ ๊ตฌ": {"work_location_x": 37.2699, "work_location_y": 127.0286},
|
| 138 |
+
"์์์ ํ๋ฌ๊ตฌ": {"work_location_x": 37.2657, "work_location_y": 126.9996},
|
| 139 |
+
"์์์ ์ํต๊ตฌ": {"work_location_x": 37.2886, "work_location_y": 127.0511},
|
| 140 |
+
"์ํฅ์": {"work_location_x": 37.3800, "work_location_y": 126.8035},
|
| 141 |
+
"์์ฐ์ ๋จ์๊ตฌ": {"work_location_x": 37.3180, "work_location_y": 126.8386},
|
| 142 |
+
"์์ฐ์ ์๋ก๊ตฌ": {"work_location_x": 37.3083, "work_location_y": 126.8530},
|
| 143 |
+
|
| 144 |
+
"์์ฑ์": {"work_location_x": 37.0100, "work_location_y": 127.2701},
|
| 145 |
+
"์์์ ๋ง์๊ตฌ": {"work_location_x": 37.4352, "work_location_y": 126.9021},
|
| 146 |
+
"์์์ ๋์๊ตฌ": {"work_location_x": 37.0100, "work_location_y": 127.2701},
|
| 147 |
+
"์์ฃผ์": {"work_location_x": 37.7840, "work_location_y": 127.0457},
|
| 148 |
+
"์ํ๊ตฐ": {"work_location_x": 37.4910, "work_location_y": 127.4874},
|
| 149 |
+
"์ฌ์ฃผ์": {"work_location_x": 37.2822, "work_location_y": 127.6280},
|
| 150 |
+
"์ฐ์ฒ๊ตฐ": {"work_location_x": 38.0960, "work_location_y": 127.0741},
|
| 151 |
+
"์ค์ฐ์": {"work_location_x": 37.1451, "work_location_y": 127.0660},
|
| 152 |
+
"์ฉ์ธ์ ์ฒ์ธ๊ตฌ": {"work_location_x": 37.2821, "work_location_y": 127.1216},
|
| 153 |
+
"์ฉ์ธ์ ๊ธฐํฅ๊ตฌ": {"work_location_x": 37.2749, "work_location_y": 127.1150},
|
| 154 |
+
"์ฉ์ธ์ ์์ง๊ตฌ": {"work_location_x": 37.3377, "work_location_y": 127.0988},
|
| 155 |
+
"์์์": {"work_location_x": 37.3204, "work_location_y": 126.9483},
|
| 156 |
+
"์์ ๋ถ์": {"work_location_x": 37.7377, "work_location_y": 127.0474},
|
| 157 |
+
"์ด์ฒ์": {"work_location_x": 37.2725, "work_location_y": 127.4348},
|
| 158 |
+
"ํ์ฃผ์": {"work_location_x": 37.7126, "work_location_y": 126.7610},
|
| 159 |
+
"ํํ์": {"work_location_x": 36.9908, "work_location_y": 127.0856},
|
| 160 |
+
"ํฌ์ฒ์": {"work_location_x": 37.8945, "work_location_y": 127.2006},
|
| 161 |
+
"ํ๋จ์": {"work_location_x": 37.5511, "work_location_y": 127.2060},
|
| 162 |
+
"ํ์ฑ์": {"work_location_x": 37.1988, "work_location_y": 127.1034}
|
| 163 |
+
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
# ์ฃผ์ด์ง ์ฃผ์์ ์ขํ ์ ๋ณด๋ฅผ ์ถ์ถ
|
| 167 |
+
coordinates = coordinates_worksites.get(address)
|
| 168 |
+
if coordinates is not None:
|
| 169 |
+
# ์ขํ ์ ๋ณด๊ฐ ์์ผ๋ฉด x, y ์ขํ๋ง์ ํํ ํํ๋ก ๋ฐํ
|
| 170 |
+
return (coordinates["work_location_x"], coordinates["work_location_y"])
|
| 171 |
+
else:
|
| 172 |
+
# ์ฃผ์์ ํด๋นํ๋ ์ขํ ์ ๋ณด๊ฐ ์์ผ๋ฉด None ๋ฐํ
|
| 173 |
+
return None
|
| 174 |
+
|
| 175 |
+
# ์ฃผ์ด์ง ์ฃผ์์ ๋ฐ๋ผ ์ขํ ๋ฐํ
|
| 176 |
+
return coordinates_worksites.get(address) # ์ฃผ์๊ฐ ์์ผ๋ฉด None ๋ฐํ
|
| 177 |
+
|
| 178 |
+
def get_coordinates_employee(address):
|
| 179 |
+
# ์ฃผ์์ ๋ฐ๋ฅธ ์ขํ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋์
๋๋ฆฌ
|
| 180 |
+
coordinates_employee = {
|
| 181 |
+
"์์ธ": {"subway_location_x": 37.5647, "subway_location_y": 126.9770},
|
| 182 |
+
"๊ฐ๋จ๊ตฌ": {"subway_location_x": 37.4979, "subway_location_y": 127.0276},
|
| 183 |
+
"๊ฐ๋๊ตฌ": {"subway_location_x": 37.5385, "subway_location_y": 127.1238},
|
| 184 |
+
"๊ฐ๋ถ๊ตฌ": {"subway_location_x": 37.6380, "subway_location_y": 127.0251},
|
| 185 |
+
"๊ฐ์๊ตฌ": {"subway_location_x": 37.5316, "subway_location_y": 126.8467},
|
| 186 |
+
"๊ด์
๊ตฌ": {"subway_location_x": 37.4813, "subway_location_y": 126.9527},
|
| 187 |
+
"๊ด์ง๊ตฌ": {"subway_location_x": 37.5351, "subway_location_y": 127.0857},
|
| 188 |
+
"๊ตฌ๋ก๊ตฌ": {"subway_location_x": 37.4853, "subway_location_y": 126.9015},
|
| 189 |
+
"๊ธ์ฒ๊ตฌ": {"subway_location_x": 37.4665, "subway_location_y": 126.8893},
|
| 190 |
+
"๋
ธ์๊ตฌ": {"subway_location_x": 37.6445, "subway_location_y": 127.0643},
|
| 191 |
+
"๋๋ด๊ตฌ": {"subway_location_x": 37.6531, "subway_location_y": 127.0476},
|
| 192 |
+
"๋๋๋ฌธ๊ตฌ": {"subway_location_x": 37.5716, "subway_location_y": 127.0095},
|
| 193 |
+
"๋์๊ตฌ": {"subway_location_x": 37.5082, "subway_location_y": 126.9637},
|
| 194 |
+
"๋งํฌ๊ตฌ": {"subway_location_x": 37.5572, "subway_location_y": 126.9236},
|
| 195 |
+
"์๋๋ฌธ๊ตฌ": {"subway_location_x": 37.5658, "subway_location_y": 126.9666},
|
| 196 |
+
"์์ด๊ตฌ": {"subway_location_x": 37.4919, "subway_location_y": 127.0072},
|
| 197 |
+
"์ฑ๋๊ตฌ": {"subway_location_x": 37.5612, "subway_location_y": 127.0379},
|
| 198 |
+
"์ฑ๋ถ๊ตฌ": {"subway_location_x": 37.5926, "subway_location_y": 127.0163},
|
| 199 |
+
"์กํ๊ตฌ": {"subway_location_x": 37.5133, "subway_location_y": 127.1001},
|
| 200 |
+
"์์ฒ๊ตฌ": {"subway_location_x": 37.5263, "subway_location_y": 126.8643},
|
| 201 |
+
"์๋ฑํฌ๊ตฌ": {"subway_location_x": 37.5171, "subway_location_y": 126.9173},
|
| 202 |
+
"์ฉ์ฐ๊ตฌ": {"subway_location_x": 37.5298, "subway_location_y": 126.9644},
|
| 203 |
+
"์ํ๊ตฌ": {"subway_location_x": 37.6190, "subway_location_y": 126.9215},
|
| 204 |
+
"์ข
๋ก๊ตฌ": {"subway_location_x": 37.5704, "subway_location_y": 126.9910},
|
| 205 |
+
"์ค๊ตฌ": {"subway_location_x": 37.5647, "subway_location_y": 126.9770},
|
| 206 |
+
"์ค๋๊ตฌ": {"subway_location_x": 37.5960, "subway_location_y": 127.085},
|
| 207 |
+
"์ธ์ฒ": {"subway_location_x": 37.5715, "subway_location_y": 126.7354},
|
| 208 |
+
"์ธ์ฒ ๊ฐํ๊ตฐ": {"subway_location_x": 37.5692, "subway_location_y": 126.6732},
|
| 209 |
+
"์ธ์ฒ ๊ณ์๊ตฌ": {"subway_location_x": 37.5715, "subway_location_y": 126.7354},
|
| 210 |
+
"์ธ์ฒ๋จ๋๊ตฌ": {"subway_location_x": 37.4488, "subway_location_y": 126.7013},
|
| 211 |
+
"์ธ์ฒ ๋๊ตฌ": {"subway_location_x": 37.4751, "subway_location_y": 126.6322},
|
| 212 |
+
"์ธ์ฒ ๋ฏธ์ถํ๊ตฌ": {"subway_location_x": 37.4420, "subway_location_y": 126.6995},
|
| 213 |
+
"์ธ์ฒ ๋ถํ๊ตฌ": {"subway_location_x": 37.4891, "subway_location_y": 126.7245},
|
| 214 |
+
"์ธ์ฒ ์๊ตฌ": {"subway_location_x": 37.5692, "subway_location_y": 126.6732},
|
| 215 |
+
"์ธ์ฒ ์ฐ์๊ตฌ": {"subway_location_x": 37.3866, "subway_location_y": 126.6392},
|
| 216 |
+
"์ธ์ฒ ์น์ง๊ตฐ": {"subway_location_x": 37.5111, "subway_location_y": 126.5230},
|
| 217 |
+
"์ธ์ฒ ์ค๊ตฌ": {"subway_location_x": 37.4764, "subway_location_y": 126.6169},
|
| 218 |
+
"๊ฐํ๊ตฐ": {"subway_location_x": 37.8147, "subway_location_y": 127.5102},
|
| 219 |
+
"๊ณ ์์ ์ผ์ฐ์๊ตฌ": {"subway_location_x": 37.6580, "subway_location_y": 126.7942},
|
| 220 |
+
"๊ณ ์์ ์ผ์ฐ๋๊ตฌ": {"subway_location_x": 37.6586, "subway_location_y": 126.7698},
|
| 221 |
+
"๊ณ ์์ ๋์๊ตฌ": {"subway_location_x": 37.6347, "subway_location_y": 126.8328},
|
| 222 |
+
"๊ณผ์ฒ์": {"subway_location_x": 37.4339, "subway_location_y": 126.9966},
|
| 223 |
+
"๊ด๋ช
์": {"subway_location_x": 37.4163, "subway_location_y": 126.8840},
|
| 224 |
+
"๊ด์ฃผ์": {"subway_location_x": 37.4095, "subway_location_y": 127.2550},
|
| 225 |
+
"๊ตฌ๋ฆฌ์": {"subway_location_x": 37.6038, "subway_location_y": 127.1436},
|
| 226 |
+
"๊ตฐํฌ์": {"subway_location_x": 37.3532, "subway_location_y": 126.9488},
|
| 227 |
+
"๊นํฌ์": {"subway_location_x": 37.5613, "subway_location_y": 126.8019},
|
| 228 |
+
"๋จ์์ฃผ์": {"subway_location_x": 37.6423, "subway_location_y": 127.1264},
|
| 229 |
+
"๋๋์ฒ์": {"subway_location_x": 37.8922, "subway_location_y": 127.0603},
|
| 230 |
+
"๋ถ์ฒ์ ์์ฌ๊ตฌ": {"subway_location_x": 37.4827, "subway_location_y": 126.7950},
|
| 231 |
+
"๋ถ์ฒ์ ์๋ฏธ๊ตฌ": {"subway_location_x": 37.5047, "subway_location_y": 126.7630},
|
| 232 |
+
"๋ถ์ฒ์ ์ค์ ๊ตฌ": {"subway_location_x": 37.5142, "subway_location_y": 126.7928},
|
| 233 |
+
|
| 234 |
+
"์ฑ๋จ์ ์์ ๊ตฌ": {"subway_location_x": 37.4519, "subway_location_y": 127.1584},
|
| 235 |
+
"์ฑ๋จ์ ๋ถ๋น๊ตฌ": {"subway_location_x": 37.3595, "subway_location_y": 127.1086},
|
| 236 |
+
"์ฑ๋จ์ ์ค์๊ตฌ": {"subway_location_x": 37.4321, "subway_location_y": 127.1150},
|
| 237 |
+
"์์์ ์ฅ์๊ตฌ": {"subway_location_x": 37.2986, "subway_location_y": 127.0107},
|
| 238 |
+
"์์์ ๊ถ์ ๊ตฌ": {"subway_location_x": 37.2699, "subway_location_y": 127.0286},
|
| 239 |
+
"์์์ ํ๋ฌ๊ตฌ": {"subway_location_x": 37.2657, "subway_location_y": 126.9996},
|
| 240 |
+
"์์์ ์ํต๊ตฌ": {"subway_location_x": 37.2886, "subway_location_y": 127.0511},
|
| 241 |
+
"์ํฅ์": {"subway_location_x": 37.3800, "subway_location_y": 126.8035},
|
| 242 |
+
"์์ฐ์ ๋จ์๊ตฌ": {"subway_location_x": 37.3180, "subway_location_y": 126.8386},
|
| 243 |
+
"์์ฐ์ ์๋ก๊ตฌ": {"subway_location_x": 37.3083, "subway_location_y": 126.8530},
|
| 244 |
+
|
| 245 |
+
"์์ฑ์": {"subway_location_x": 37.0100, "subway_location_y": 127.2701},
|
| 246 |
+
"์์์ ๋ง์๊ตฌ": {"subway_location_x": 37.4352, "subway_location_y": 126.9021},
|
| 247 |
+
"์์์ ๋์๊ตฌ": {"subway_location_x": 37.0100, "subway_location_y": 127.2701},
|
| 248 |
+
"์์ฃผ์": {"subway_location_x": 37.7840, "subway_location_y": 127.0457},
|
| 249 |
+
"์ํ๊ตฐ": {"subway_location_x": 37.4910, "subway_location_y": 127.4874},
|
| 250 |
+
"์ฌ์ฃผ์": {"subway_location_x": 37.2822, "subway_location_y": 127.6280},
|
| 251 |
+
"์ฐ์ฒ๊ตฐ": {"subway_location_x": 38.0960, "subway_location_y": 127.0741},
|
| 252 |
+
"์ค์ฐ์": {"subway_location_x": 37.1451, "subway_location_y": 127.0660},
|
| 253 |
+
"์ฉ์ธ์ ์ฒ์ธ๊ตฌ": {"subway_location_x": 37.2821, "subway_location_y": 127.1216},
|
| 254 |
+
"์ฉ์ธ์ ๊ธฐํฅ๊ตฌ": {"subway_location_x": 37.2749, "subway_location_y": 127.1150},
|
| 255 |
+
"์ฉ์ธ์ ์์ง๊ตฌ": {"subway_location_x": 37.3377, "subway_location_y": 127.0988},
|
| 256 |
+
"์์์": {"subway_location_x": 37.3204, "subway_location_y": 126.9483},
|
| 257 |
+
"์์ ๋ถ์": {"subway_location_x": 37.7377, "subway_location_y": 127.0474},
|
| 258 |
+
"์ด์ฒ์": {"subway_location_x": 37.2725, "subway_location_y": 127.4348},
|
| 259 |
+
"ํ์ฃผ์": {"subway_location_x": 37.7126, "subway_location_y": 126.7610},
|
| 260 |
+
"ํํ์": {"subway_location_x": 36.9908, "subway_location_y": 127.0856},
|
| 261 |
+
"ํฌ์ฒ์": {"subway_location_x": 37.8945, "subway_location_y": 127.2006},
|
| 262 |
+
"ํ๋จ์": {"subway_location_x": 37.5511, "subway_location_y": 127.2060},
|
| 263 |
+
"ํ์ฑ์": {"subway_location_x": 37.1988, "subway_location_y": 127.1034}
|
| 264 |
+
|
| 265 |
+
}
|
| 266 |
+
# ์ฃผ์ด์ง ์ฃผ์์ ์ขํ ์ ๋ณด๋ฅผ ์ถ์ถ
|
| 267 |
+
coordinates = coordinates_employee.get(address)
|
| 268 |
+
if coordinates is not None:
|
| 269 |
+
# ์ขํ ์ ๋ณด๊ฐ ์์ผ๋ฉด x, y ์ขํ๋ง์ ํํ ํํ๋ก ๋ฐํ
|
| 270 |
+
return (coordinates["subway_location_x"], coordinates["subway_location_y"])
|
| 271 |
+
else:
|
| 272 |
+
#์์ผ๋ฉด x,y์ขํ๋ฅผ ๊ฐ๊ฐ 50์ ํํ๋ก ๋ฐํ
|
| 273 |
+
return (50, 50)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
# calculate_score_for_person ํจ์ ๋ด์์์ ์์
|
| 278 |
+
def calculate_score_for_person(person_info, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device):
|
| 279 |
+
# ๊ฐ ๋ณ์์ ๊ฐ์ค์น
|
| 280 |
+
weight_distance = 0.2
|
| 281 |
+
weight_attendance = 0.3
|
| 282 |
+
weight_work_frequency = 0.2
|
| 283 |
+
weight_label_value = 0.3
|
| 284 |
+
# ์ฑ๋ณ์ ๋ฐ๋ผ ๊ฐ์ค์น ์กฐ์
|
| 285 |
+
gender_weight = 1 if person_info['sex'] == '๋จ์' or person_info['sex'] == '์ฌ์' else 0
|
| 286 |
+
|
| 287 |
+
work_location = get_coordinates_worksites(person_info['worksites_local']) # ์ฃผ์๊ฐ ์์ผ๋ฉด None ๋ฐํ)
|
| 288 |
+
subway_station = get_coordinates_employee(person_info['employee_local'])
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
# ํ์ฅ๊ณผ ์งํ์ฒ ์ญ์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ (km ๋จ์๋ก ํ์ฐ)
|
| 292 |
+
distance_to_station = calculate_distance(work_location, subway_station)
|
| 293 |
+
|
| 294 |
+
# ์ถ์๋ฅ ๊ณ์ฐ
|
| 295 |
+
actual_work_days = person_info['actual_work_days']
|
| 296 |
+
applied_work_days = person_info['applied_work_days']
|
| 297 |
+
attendance_rate = actual_work_days / applied_work_days * 100
|
| 298 |
+
|
| 299 |
+
# ์ถ์๋ฅ ์ ๋ฐ๋ผ ์ ์ ํ ๋น
|
| 300 |
+
if attendance_rate >= 100:
|
| 301 |
+
attendance_score = 50
|
| 302 |
+
elif attendance_rate >= 90:
|
| 303 |
+
attendance_score = 20
|
| 304 |
+
else:
|
| 305 |
+
attendance_score = 0
|
| 306 |
+
|
| 307 |
+
# ๊ทผ๋ฌดํ์์ ๋ฐ๋ผ ์ ์ ํ ๋น
|
| 308 |
+
if person_info['work_frequency'] >= 15:
|
| 309 |
+
work_frequency_score = 50
|
| 310 |
+
elif person_info['work_frequency'] >= 10:
|
| 311 |
+
work_frequency_score = 30
|
| 312 |
+
elif person_info['work_frequency'] >= 5:
|
| 313 |
+
work_frequency_score = 20
|
| 314 |
+
else:
|
| 315 |
+
work_frequency_score = 0
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
# ๊ฑฐ๋ฆฌ์ ๋ฐ๋ผ ์ ์ ํ ๋น
|
| 319 |
+
if distance_to_station < 10:
|
| 320 |
+
distance_score = 50
|
| 321 |
+
elif distance_to_station < 30:
|
| 322 |
+
distance_score = 40
|
| 323 |
+
elif distance_to_station < 50:
|
| 324 |
+
distance_score = 30
|
| 325 |
+
elif distance_to_station < 100:
|
| 326 |
+
distance_score = 20
|
| 327 |
+
elif distance_to_station < 200:
|
| 328 |
+
distance_score = 10
|
| 329 |
+
else:
|
| 330 |
+
distance_score = 0
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
# ํ
์คํธ ์์ธก์ ์ํ ์ฝ๋ ์ถ๊ฐ
|
| 334 |
+
texts_to_predict = [person_info['review']] # ํ
์คํธ๋ฅผ ๋ฆฌ์คํธ๋ก ๋ณํ
|
| 335 |
+
final_labels = predict_with_ensemble_modified(texts_to_predict, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device)
|
| 336 |
+
|
| 337 |
+
for label in final_labels:
|
| 338 |
+
sentiment_score = label_to_value(label)
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
# ๊ฐ ๋ณ์์ ๊ฐ์ค์น๋ฅผ ๊ณฑํ์ฌ ํฉ์ฐ๋ ์ ์๋ฅผ ๊ณ์ฐ
|
| 344 |
+
total_score = (distance_score * weight_distance) + \
|
| 345 |
+
(attendance_score * weight_attendance) + \
|
| 346 |
+
(work_frequency_score * weight_work_frequency) + \
|
| 347 |
+
(sentiment_score * weight_label_value)
|
| 348 |
+
|
| 349 |
+
return total_score*gender_weight
|
| 350 |
+
|
| 351 |
+
# ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ ์ฌ๋๋ค์ ๋ํ ์ ๋ณด ์์
|
| 352 |
+
people_info = [
|
| 353 |
+
{'employee_local': "์ฑ๋ถ๊ตฌ", 'worksites_local': "์ฑ๋ถ๊ตฌ", 'sex': '๋จ์', 'actual_work_days': 20, 'applied_work_days': 20, 'work_frequency': 20, 'review': "์์ผ๋ก ์ผ์ ๋งก๊ฒจ๋ ์ข์ ์ฌ๋์."},
|
| 354 |
+
{'employee_local': '๊ณ ์์ ์ผ์ฐ์๊ตฌ', 'worksites_local': '์ฑ๋ถ๊ตฌ', 'sex': '์ฌ์', 'actual_work_days': 10, 'applied_work_days': 10, 'work_frequency': 25, 'review': "๋ถ์ฑ์คํ๊ณ ๋งค์ฐ ํ์์์ ๊ทธ๋ฅ ์ถ๊ทผํ์ง ์๋๊ฒ ๋์"}
|
| 355 |
+
]
|
| 356 |
+
|
| 357 |
+
# ๊ฐ ์ฌ๋๋ค์ ์ ์ ๊ณ์ฐ
|
| 358 |
+
for person_info in people_info:
|
| 359 |
+
score = calculate_score_for_person(person_info, model_roberta, model_electra, tokenizer_roberta, tokenizer_electra, device)
|
| 360 |
+
print("์ด์ :", score)
|