Updating
Browse files- model.py +157 -157
- requirements.txt +4 -4
- utils.py +21 -21
model.py
CHANGED
@@ -1,157 +1,157 @@
|
|
1 |
-
import os
|
2 |
-
import torch
|
3 |
-
from torch import nn
|
4 |
-
from transformers import AutoModel
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
-
|
7 |
-
token=os.getenv("HF_TOKEN")
|
8 |
-
repo_id = "Siyunb323/CreativityEvaluation"
|
9 |
-
model = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese")
|
10 |
-
|
11 |
-
class BERTregressor(nn.Module):
|
12 |
-
def __init__(self, bert, hidden_size=768, num_linear=1, dropout=0.1,
|
13 |
-
o_type='cls', t_type= 'C', use_sigmoid=False):
|
14 |
-
|
15 |
-
super(BERTregressor, self).__init__()
|
16 |
-
self.encoder = bert
|
17 |
-
self.o_type = o_type
|
18 |
-
self.t_type = t_type
|
19 |
-
self.sigmoid = use_sigmoid
|
20 |
-
|
21 |
-
if num_linear == 2:
|
22 |
-
layers = [nn.Linear(hidden_size, 128),
|
23 |
-
nn.ReLU(),
|
24 |
-
nn.Dropout(dropout),
|
25 |
-
nn.Linear(128, 1)]
|
26 |
-
elif num_linear == 1:
|
27 |
-
layers = [nn.Dropout(dropout),
|
28 |
-
nn.Linear(hidden_size, 1)]
|
29 |
-
|
30 |
-
if use_sigmoid:
|
31 |
-
layers.append(nn.Sigmoid())
|
32 |
-
|
33 |
-
self.output = nn.Sequential(*layers)
|
34 |
-
|
35 |
-
def forward(self, inputs, return_attention=False):
|
36 |
-
|
37 |
-
X = {'input_ids':inputs['input_ids'],
|
38 |
-
'token_type_ids':inputs['token_type_ids'],
|
39 |
-
'attention_mask':inputs['attention_mask'],
|
40 |
-
'output_attentions':return_attention}
|
41 |
-
encoded_X = self.encoder(**X)
|
42 |
-
if self.o_type == 'cls':
|
43 |
-
output = self.output(encoded_X.last_hidden_state[:, 0, :])
|
44 |
-
elif self.o_type == 'pooler':
|
45 |
-
output = self.output(encoded_X.pooler_output)
|
46 |
-
|
47 |
-
output = 4 * output.squeeze(-1) + 1 if self.sigmoid and self.t_type == 'C' else output.squeeze(-1)
|
48 |
-
|
49 |
-
return output if not return_attention else (output, encoded_X.attentions)
|
50 |
-
|
51 |
-
class Effectiveness(nn.Module):
|
52 |
-
def __init__(self, num_layers, hidden_size=768, use_sigmoid=True, dropout=0.2, **kwargs):
|
53 |
-
super(Effectiveness, self).__init__(**kwargs)
|
54 |
-
self.sigmoid = use_sigmoid
|
55 |
-
|
56 |
-
if num_layers == 2:
|
57 |
-
layers = [
|
58 |
-
nn.Linear(hidden_size, 128),
|
59 |
-
nn.ReLU(),
|
60 |
-
nn.Dropout(dropout),
|
61 |
-
nn.Linear(128, 1)
|
62 |
-
]
|
63 |
-
else:
|
64 |
-
layers = [
|
65 |
-
nn.ReLU(),
|
66 |
-
nn.Dropout(dropout),
|
67 |
-
nn.Linear(hidden_size, 1)
|
68 |
-
]
|
69 |
-
|
70 |
-
if use_sigmoid:
|
71 |
-
layers.append(nn.Sigmoid()) # 仅在需要时添加 Sigmoid 层
|
72 |
-
|
73 |
-
self.output = nn.Sequential(*layers)
|
74 |
-
|
75 |
-
def forward(self, X):
|
76 |
-
output = self.output(X)
|
77 |
-
|
78 |
-
# 如果使用 Sigmoid 层,调整输出范围到 [1, 5]
|
79 |
-
if self.sigmoid:
|
80 |
-
return 4 * output.squeeze(-1) + 1
|
81 |
-
else:
|
82 |
-
return output.squeeze(-1)
|
83 |
-
|
84 |
-
class Creativity(nn.Module):
|
85 |
-
"""BERT的下一句预测任务"""
|
86 |
-
def __init__(self, num_layers, hidden_size=768, use_sigmoid=True, dropout=0.2, **kwargs):
|
87 |
-
super(Creativity, self).__init__(**kwargs)
|
88 |
-
self.sigmoid = use_sigmoid
|
89 |
-
|
90 |
-
if num_layers == 2:
|
91 |
-
layers = [
|
92 |
-
nn.Linear(hidden_size, 128),
|
93 |
-
nn.ReLU(),
|
94 |
-
nn.Dropout(dropout),
|
95 |
-
nn.Linear(128, 1)
|
96 |
-
]
|
97 |
-
else:
|
98 |
-
layers = [
|
99 |
-
nn.ReLU(),
|
100 |
-
nn.Dropout(dropout),
|
101 |
-
nn.Linear(hidden_size, 1)
|
102 |
-
]
|
103 |
-
|
104 |
-
if use_sigmoid:
|
105 |
-
layers.append(nn.Sigmoid()) # 仅在需要时添加 Sigmoid 层
|
106 |
-
|
107 |
-
self.output = nn.Sequential(*layers)
|
108 |
-
|
109 |
-
def forward(self, X):
|
110 |
-
output = self.output(X)
|
111 |
-
|
112 |
-
# 如果使用 Sigmoid 层,调整输出范围到 [1, 5]
|
113 |
-
if self.sigmoid:
|
114 |
-
return 4 * output.squeeze(-1) + 1
|
115 |
-
else:
|
116 |
-
return output.squeeze(-1)
|
117 |
-
|
118 |
-
class BERT2Phase(nn.Module):
|
119 |
-
def __init__(self, bert, hidden_size=768, type='cls',
|
120 |
-
num_linear=1, dropout=0.1, use_sigmoid=False):
|
121 |
-
|
122 |
-
super(BERT2Phase, self).__init__()
|
123 |
-
self.encoder = bert
|
124 |
-
self.type = type
|
125 |
-
self.sigmoid = use_sigmoid
|
126 |
-
|
127 |
-
self.effectiveness = Effectiveness(num_linear, hidden_size, use_sigmoid, dropout)
|
128 |
-
self.creativity = Creativity(num_linear, hidden_size, use_sigmoid, dropout)
|
129 |
-
|
130 |
-
def forward(self, inputs, return_attention=False):
|
131 |
-
X = {'input_ids':inputs['input_ids'],
|
132 |
-
'token_type_ids':inputs['token_type_ids'],
|
133 |
-
'attention_mask':inputs['attention_mask'],
|
134 |
-
'output_attentions':return_attention}
|
135 |
-
encoded_X = self.encoder(**X)
|
136 |
-
|
137 |
-
if self.type == 'cls':
|
138 |
-
e_pred = self.effectiveness(encoded_X.last_hidden_state[:, 0, :])
|
139 |
-
c_pred = self.creativity(encoded_X.last_hidden_state[:, 0, :])
|
140 |
-
elif self.type == 'pooler':
|
141 |
-
e_pred = self.effectiveness(encoded_X.pooler_output)
|
142 |
-
c_pred = self.creativity(encoded_X.pooler_output)
|
143 |
-
|
144 |
-
return (c_pred, e_pred) if not return_attention else (c_pred, e_pred, encoded_X.attentions)
|
145 |
-
|
146 |
-
def load_model(model_name, pooling_method):
|
147 |
-
pooling = pooling_method if pooling_method == 'cls' else 'pooler'
|
148 |
-
if model_name == "One-phase Fine-tuned BERT":
|
149 |
-
loaded_net = BERTregressor(model, hidden_size=768, num_linear=1, dropout=0.1, o_type=pooling, t_type='C', use_sigmoid=True)
|
150 |
-
filename = 'model' + f"/OnePhase_BERT_{pooling_method}.pth"
|
151 |
-
elif model_name == "Two-phase Fine-tuned BERT":
|
152 |
-
loaded_net = BERT2Phase(model, hidden_size=768, num_linear=1, dropout=0.1, type=pooling, use_sigmoid=True)
|
153 |
-
filename = 'model' + f"/TwoPhase_BERT_{pooling_method}.pth"
|
154 |
-
model_path = hf_hub_download(repo_id=repo_id, filename=filename, use_auth_token=token)
|
155 |
-
loaded_net.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
156 |
-
loaded_net.eval()
|
157 |
-
return loaded_net
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from transformers import AutoModel
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
token=os.getenv("HF_TOKEN")
|
8 |
+
repo_id = "Siyunb323/CreativityEvaluation"
|
9 |
+
model = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese")
|
10 |
+
|
11 |
+
class BERTregressor(nn.Module):
|
12 |
+
def __init__(self, bert, hidden_size=768, num_linear=1, dropout=0.1,
|
13 |
+
o_type='cls', t_type= 'C', use_sigmoid=False):
|
14 |
+
|
15 |
+
super(BERTregressor, self).__init__()
|
16 |
+
self.encoder = bert
|
17 |
+
self.o_type = o_type
|
18 |
+
self.t_type = t_type
|
19 |
+
self.sigmoid = use_sigmoid
|
20 |
+
|
21 |
+
if num_linear == 2:
|
22 |
+
layers = [nn.Linear(hidden_size, 128),
|
23 |
+
nn.ReLU(),
|
24 |
+
nn.Dropout(dropout),
|
25 |
+
nn.Linear(128, 1)]
|
26 |
+
elif num_linear == 1:
|
27 |
+
layers = [nn.Dropout(dropout),
|
28 |
+
nn.Linear(hidden_size, 1)]
|
29 |
+
|
30 |
+
if use_sigmoid:
|
31 |
+
layers.append(nn.Sigmoid())
|
32 |
+
|
33 |
+
self.output = nn.Sequential(*layers)
|
34 |
+
|
35 |
+
def forward(self, inputs, return_attention=False):
|
36 |
+
|
37 |
+
X = {'input_ids':inputs['input_ids'],
|
38 |
+
'token_type_ids':inputs['token_type_ids'],
|
39 |
+
'attention_mask':inputs['attention_mask'],
|
40 |
+
'output_attentions':return_attention}
|
41 |
+
encoded_X = self.encoder(**X)
|
42 |
+
if self.o_type == 'cls':
|
43 |
+
output = self.output(encoded_X.last_hidden_state[:, 0, :])
|
44 |
+
elif self.o_type == 'pooler':
|
45 |
+
output = self.output(encoded_X.pooler_output)
|
46 |
+
|
47 |
+
output = 4 * output.squeeze(-1) + 1 if self.sigmoid and self.t_type == 'C' else output.squeeze(-1)
|
48 |
+
|
49 |
+
return output if not return_attention else (output, encoded_X.attentions)
|
50 |
+
|
51 |
+
class Effectiveness(nn.Module):
|
52 |
+
def __init__(self, num_layers, hidden_size=768, use_sigmoid=True, dropout=0.2, **kwargs):
|
53 |
+
super(Effectiveness, self).__init__(**kwargs)
|
54 |
+
self.sigmoid = use_sigmoid
|
55 |
+
|
56 |
+
if num_layers == 2:
|
57 |
+
layers = [
|
58 |
+
nn.Linear(hidden_size, 128),
|
59 |
+
nn.ReLU(),
|
60 |
+
nn.Dropout(dropout),
|
61 |
+
nn.Linear(128, 1)
|
62 |
+
]
|
63 |
+
else:
|
64 |
+
layers = [
|
65 |
+
nn.ReLU(),
|
66 |
+
nn.Dropout(dropout),
|
67 |
+
nn.Linear(hidden_size, 1)
|
68 |
+
]
|
69 |
+
|
70 |
+
if use_sigmoid:
|
71 |
+
layers.append(nn.Sigmoid()) # 仅在需要时添加 Sigmoid 层
|
72 |
+
|
73 |
+
self.output = nn.Sequential(*layers)
|
74 |
+
|
75 |
+
def forward(self, X):
|
76 |
+
output = self.output(X)
|
77 |
+
|
78 |
+
# 如果使用 Sigmoid 层,调整输出范围到 [1, 5]
|
79 |
+
if self.sigmoid:
|
80 |
+
return 4 * output.squeeze(-1) + 1
|
81 |
+
else:
|
82 |
+
return output.squeeze(-1)
|
83 |
+
|
84 |
+
class Creativity(nn.Module):
|
85 |
+
"""BERT的下一句预测任务"""
|
86 |
+
def __init__(self, num_layers, hidden_size=768, use_sigmoid=True, dropout=0.2, **kwargs):
|
87 |
+
super(Creativity, self).__init__(**kwargs)
|
88 |
+
self.sigmoid = use_sigmoid
|
89 |
+
|
90 |
+
if num_layers == 2:
|
91 |
+
layers = [
|
92 |
+
nn.Linear(hidden_size, 128),
|
93 |
+
nn.ReLU(),
|
94 |
+
nn.Dropout(dropout),
|
95 |
+
nn.Linear(128, 1)
|
96 |
+
]
|
97 |
+
else:
|
98 |
+
layers = [
|
99 |
+
nn.ReLU(),
|
100 |
+
nn.Dropout(dropout),
|
101 |
+
nn.Linear(hidden_size, 1)
|
102 |
+
]
|
103 |
+
|
104 |
+
if use_sigmoid:
|
105 |
+
layers.append(nn.Sigmoid()) # 仅在需要时添加 Sigmoid 层
|
106 |
+
|
107 |
+
self.output = nn.Sequential(*layers)
|
108 |
+
|
109 |
+
def forward(self, X):
|
110 |
+
output = self.output(X)
|
111 |
+
|
112 |
+
# 如果使用 Sigmoid 层,调整输出范围到 [1, 5]
|
113 |
+
if self.sigmoid:
|
114 |
+
return 4 * output.squeeze(-1) + 1
|
115 |
+
else:
|
116 |
+
return output.squeeze(-1)
|
117 |
+
|
118 |
+
class BERT2Phase(nn.Module):
|
119 |
+
def __init__(self, bert, hidden_size=768, type='cls',
|
120 |
+
num_linear=1, dropout=0.1, use_sigmoid=False):
|
121 |
+
|
122 |
+
super(BERT2Phase, self).__init__()
|
123 |
+
self.encoder = bert
|
124 |
+
self.type = type
|
125 |
+
self.sigmoid = use_sigmoid
|
126 |
+
|
127 |
+
self.effectiveness = Effectiveness(num_linear, hidden_size, use_sigmoid, dropout)
|
128 |
+
self.creativity = Creativity(num_linear, hidden_size, use_sigmoid, dropout)
|
129 |
+
|
130 |
+
def forward(self, inputs, return_attention=False):
|
131 |
+
X = {'input_ids':inputs['input_ids'],
|
132 |
+
'token_type_ids':inputs['token_type_ids'],
|
133 |
+
'attention_mask':inputs['attention_mask'],
|
134 |
+
'output_attentions':return_attention}
|
135 |
+
encoded_X = self.encoder(**X)
|
136 |
+
|
137 |
+
if self.type == 'cls':
|
138 |
+
e_pred = self.effectiveness(encoded_X.last_hidden_state[:, 0, :])
|
139 |
+
c_pred = self.creativity(encoded_X.last_hidden_state[:, 0, :])
|
140 |
+
elif self.type == 'pooler':
|
141 |
+
e_pred = self.effectiveness(encoded_X.pooler_output)
|
142 |
+
c_pred = self.creativity(encoded_X.pooler_output)
|
143 |
+
|
144 |
+
return (c_pred, e_pred) if not return_attention else (c_pred, e_pred, encoded_X.attentions)
|
145 |
+
|
146 |
+
def load_model(model_name, pooling_method):
|
147 |
+
pooling = pooling_method if pooling_method == 'cls' else 'pooler'
|
148 |
+
if model_name == "One-phase Fine-tuned BERT":
|
149 |
+
loaded_net = BERTregressor(model, hidden_size=768, num_linear=1, dropout=0.1, o_type=pooling, t_type='C', use_sigmoid=True)
|
150 |
+
filename = 'model' + f"/OnePhase_BERT_{pooling_method}.pth"
|
151 |
+
elif model_name == "Two-phase Fine-tuned BERT":
|
152 |
+
loaded_net = BERT2Phase(model, hidden_size=768, num_linear=1, dropout=0.1, type=pooling, use_sigmoid=True)
|
153 |
+
filename = 'model' + f"/TwoPhase_BERT_{pooling_method}.pth"
|
154 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename, use_auth_token=token)
|
155 |
+
loaded_net.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
156 |
+
loaded_net.eval()
|
157 |
+
return loaded_net
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
torch
|
2 |
-
fugashi
|
3 |
-
unidic_lite
|
4 |
-
transformers
|
5 |
huggingface_hub
|
|
|
1 |
+
torch
|
2 |
+
fugashi
|
3 |
+
unidic_lite
|
4 |
+
transformers
|
5 |
huggingface_hub
|
utils.py
CHANGED
@@ -1,22 +1,22 @@
|
|
1 |
-
import os
|
2 |
-
import tempfile
|
3 |
-
import fugashi
|
4 |
-
import unidic_lite
|
5 |
-
from transformers import AutoTokenizer
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
|
7 |
-
|
8 |
-
def save_dataframe_to_file(dataframe, file_format="csv"):
|
9 |
-
temp_dir = tempfile.gettempdir() # 获取系统临时目录
|
10 |
-
file_path = os.path.join(temp_dir, f"output.{file_format}")
|
11 |
-
if file_format == "csv":
|
12 |
-
dataframe.to_csv(file_path, index=False, encoding='utf-8-sig')
|
13 |
-
elif file_format == "xlsx":
|
14 |
-
dataframe.to_excel(file_path, index=False, encoding='utf-8-sig')
|
15 |
-
return file_path
|
16 |
-
|
17 |
-
def tokenize_Df(examples):
|
18 |
-
return tokenizer(list(examples['prompt']), list(examples['response']),
|
19 |
-
return_tensors="pt",
|
20 |
-
padding='max_length',
|
21 |
-
max_length=60,
|
22 |
truncation='longest_first')
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
+
import fugashi
|
4 |
+
import unidic_lite
|
5 |
+
from transformers import AutoTokenizer
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
|
7 |
+
|
8 |
+
def save_dataframe_to_file(dataframe, file_format="csv"):
|
9 |
+
temp_dir = tempfile.gettempdir() # 获取系统临时目录
|
10 |
+
file_path = os.path.join(temp_dir, f"output.{file_format}")
|
11 |
+
if file_format == "csv":
|
12 |
+
dataframe.to_csv(file_path, index=False, encoding='utf-8-sig')
|
13 |
+
elif file_format == "xlsx":
|
14 |
+
dataframe.to_excel(file_path, index=False, encoding='utf-8-sig')
|
15 |
+
return file_path
|
16 |
+
|
17 |
+
def tokenize_Df(examples):
|
18 |
+
return tokenizer(list(examples['prompt']), list(examples['response']),
|
19 |
+
return_tensors="pt",
|
20 |
+
padding='max_length',
|
21 |
+
max_length=60,
|
22 |
truncation='longest_first')
|