qiuhuachuan
commited on
Commit
β’
1cf6d71
1
Parent(s):
66536b3
Update README.md
Browse files
README.md
CHANGED
@@ -13,7 +13,6 @@ tags:
|
|
13 |
</div>
|
14 |
|
15 |
<p align="center">
|
16 |
-
βοΈ <a href="https://github.com/qiuhuachuan/CensorChat" target="_blank">GitHub</a> β’
|
17 |
π <a href="https://arxiv.org/pdf/2309.09749v2.pdf" target="_blank">Paper</a> β’
|
18 |
π€ <a href="https://huggingface.co/qiuhuachuan/NSFW-detector" target="_blank">Model</a>
|
19 |
</p>
|
@@ -99,10 +98,10 @@ from torch import nn
|
|
99 |
|
100 |
label_mapping = {0: 'NSFW', 1: 'SFW'}
|
101 |
|
102 |
-
config = BertConfig.from_pretrained('
|
103 |
num_labels=2,
|
104 |
finetuning_task='text classification')
|
105 |
-
tokenizer = BertTokenizer.from_pretrained('
|
106 |
use_fast=False,
|
107 |
never_split=['[user]', '[bot]'])
|
108 |
tokenizer.vocab['[user]'] = tokenizer.vocab.pop('[unused1]')
|
@@ -115,7 +114,7 @@ class BertForSequenceClassification(BertPreTrainedModel):
|
|
115 |
self.num_labels = config.num_labels
|
116 |
self.config = config
|
117 |
|
118 |
-
self.bert = BertModel.from_pretrained('
|
119 |
classifier_dropout = (config.classifier_dropout
|
120 |
if config.classifier_dropout is not None else
|
121 |
config.hidden_dropout_prob)
|
@@ -164,22 +163,50 @@ model.load_state_dict(torch.load('./NSFW-detector/pytorch_model.bin'))
|
|
164 |
model.cuda()
|
165 |
model.eval()
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
```
|
184 |
|
185 |
## Citation
|
|
|
13 |
</div>
|
14 |
|
15 |
<p align="center">
|
|
|
16 |
π <a href="https://arxiv.org/pdf/2309.09749v2.pdf" target="_blank">Paper</a> β’
|
17 |
π€ <a href="https://huggingface.co/qiuhuachuan/NSFW-detector" target="_blank">Model</a>
|
18 |
</p>
|
|
|
98 |
|
99 |
label_mapping = {0: 'NSFW', 1: 'SFW'}
|
100 |
|
101 |
+
config = BertConfig.from_pretrained('./NSFW-detector',
|
102 |
num_labels=2,
|
103 |
finetuning_task='text classification')
|
104 |
+
tokenizer = BertTokenizer.from_pretrained('./NSFW-detector',
|
105 |
use_fast=False,
|
106 |
never_split=['[user]', '[bot]'])
|
107 |
tokenizer.vocab['[user]'] = tokenizer.vocab.pop('[unused1]')
|
|
|
114 |
self.num_labels = config.num_labels
|
115 |
self.config = config
|
116 |
|
117 |
+
self.bert = BertModel.from_pretrained('./NSFW-detector')
|
118 |
classifier_dropout = (config.classifier_dropout
|
119 |
if config.classifier_dropout is not None else
|
120 |
config.hidden_dropout_prob)
|
|
|
163 |
model.cuda()
|
164 |
model.eval()
|
165 |
|
166 |
+
# The following object contains two types of input:
|
167 |
+
# a single utterance and a single-turn dialogue
|
168 |
+
to_predict_items = [{
|
169 |
+
'text':
|
170 |
+
'''Give some movie recommendations to get women in the mood for sex'''
|
171 |
+
}, {
|
172 |
+
'text': 'I break through walls to find more trials'
|
173 |
+
}, {
|
174 |
+
'history': {
|
175 |
+
'user':
|
176 |
+
'Give some movie recommendations to get women in the mood for sex',
|
177 |
+
'bot':
|
178 |
+
'''I apologize, but I cannot assist in creating or providing information related to NSFW content or any tools for such purposes. If you have any other non-explicit requests or need assistance with different topics, please feel free to ask, and I'll be happy to help.'''
|
179 |
+
}
|
180 |
+
}, {
|
181 |
+
'history': {
|
182 |
+
'user':
|
183 |
+
'Give some movie recommendations to get women in the mood for sex',
|
184 |
+
'bot': '''Sure.'''
|
185 |
+
}
|
186 |
+
}]
|
187 |
+
|
188 |
+
for item in to_predict_items:
|
189 |
+
if 'history' in item:
|
190 |
+
text = '[user] ' + item['history'][
|
191 |
+
'user'] + ' [SEP] ' + '[bot] ' + item['history']['bot']
|
192 |
+
else:
|
193 |
+
text = item['text']
|
194 |
+
result = tokenizer.encode_plus(text=text,
|
195 |
+
padding='max_length',
|
196 |
+
max_length=512,
|
197 |
+
truncation=True,
|
198 |
+
add_special_tokens=True,
|
199 |
+
return_token_type_ids=True,
|
200 |
+
return_tensors='pt')
|
201 |
+
result = result.to('cuda')
|
202 |
+
|
203 |
+
with torch.no_grad():
|
204 |
+
logits = model(**result)
|
205 |
+
predictions = logits.argmax(dim=-1)
|
206 |
+
pred_label_idx = predictions.item()
|
207 |
+
pred_label = label_mapping[pred_label_idx]
|
208 |
+
print('text:', text)
|
209 |
+
print('predicted label is:', pred_label)
|
210 |
```
|
211 |
|
212 |
## Citation
|