qiuhuachuan commited on
Commit
1cf6d71
β€’
1 Parent(s): 66536b3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +47 -20
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('qiuhuachuan/NSFW-detector',
103
  num_labels=2,
104
  finetuning_task='text classification')
105
- tokenizer = BertTokenizer.from_pretrained('qiuhuachuan/NSFW-detector',
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('bert-base-cased')
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
- text = '''I'm open to exploring a variety of toys, including vibrators, wands, and clamps. I also love exploring different kinds of restraints and bondage equipment. I'm open to trying out different kinds of toys and exploring different levels of intensity.'''
168
- result = tokenizer.encode_plus(text=text,
169
- padding='max_length',
170
- max_length=512,
171
- truncation=True,
172
- add_special_tokens=True,
173
- return_token_type_ids=True,
174
- return_tensors='pt')
175
- result = result.to('cuda')
176
-
177
- with torch.no_grad():
178
- logits = model(**result)
179
- predictions = logits.argmax(dim=-1)
180
- pred_label_idx = predictions.item()
181
- pred_label = label_mapping[pred_label_idx]
182
- print('predicted label is:', pred_label)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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