Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
datasets:
|
4 |
+
- OxAISH-AL-LLM/wiki_toxic
|
5 |
+
- textdetox/multilingual_toxic_spans
|
6 |
+
language:
|
7 |
+
- en
|
8 |
+
base_model:
|
9 |
+
- openai-community/gpt2
|
10 |
+
tags:
|
11 |
+
- not-for-all-audiences
|
12 |
+
---
|
13 |
+
|
14 |
+
# Model Card for Toxic Text GEN
|
15 |
+
|
16 |
+
This model is a decision Tranformer for text generation with controlled toxicity (0-1).
|
17 |
+
|
18 |
+
## Model Details
|
19 |
+
|
20 |
+
### Model Description
|
21 |
+
|
22 |
+
Made using a decision transformer, it can generate toxic sentences based on a toxicity control (defined as reward-to-go/rtg).
|
23 |
+
|
24 |
+
Current text generation is not very coherent due to lack of variety in training data and low compute.
|
25 |
+
|
26 |
+
- **Developed by:** [Ashed00]
|
27 |
+
- **Finetuned from model:** [GPT-2]
|
28 |
+
|
29 |
+
### Model Sources [optional]
|
30 |
+
|
31 |
+
|
32 |
+
- **Repository:** [https://github.com/Ashu-00/NLP-Implementations/tree/main/Decision_Transformer]
|
33 |
+
- **Demo:** Soon
|
34 |
+
|
35 |
+
## Uses
|
36 |
+
|
37 |
+
Fun, little experiment.
|
38 |
+
|
39 |
+
|
40 |
+
## Bias, Risks, and Limitations
|
41 |
+
|
42 |
+
This model is biased based on its training data. I take no responsibility for its generation.
|
43 |
+
|
44 |
+
Most generated text is non-coherent due to lack of variety of training data.
|
45 |
+
|
46 |
+
## How to Get Started with the Model
|
47 |
+
|
48 |
+
```python
|
49 |
+
|
50 |
+
import torch.nn.functional as F
|
51 |
+
|
52 |
+
def generate_conditioned_text2(model, tokenizer, prompt, target_rtg, max_length=50, temperature=1.0, top_k=50):
|
53 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
54 |
+
input_ids = inputs["input_ids"].to(device)
|
55 |
+
attention_mask = inputs["attention_mask"].to(device)
|
56 |
+
|
57 |
+
# Create RTG tensor with the target value for each token in the prompt
|
58 |
+
rtg = torch.tensor([[target_rtg] * input_ids.shape[1]], dtype=torch.float).to(device)
|
59 |
+
|
60 |
+
seq_length = input_ids.shape[1]
|
61 |
+
for _ in range(max_length):
|
62 |
+
with torch.no_grad():
|
63 |
+
# Slice rtg to match current sequence length
|
64 |
+
rtg_current = rtg[:, :seq_length]
|
65 |
+
outputs = model(
|
66 |
+
input_ids=input_ids,
|
67 |
+
attention_mask=attention_mask,
|
68 |
+
rtg=rtg_current,
|
69 |
+
return_dict=True
|
70 |
+
)
|
71 |
+
|
72 |
+
# Get next token logits and apply temperature scaling
|
73 |
+
next_token_logits = outputs["logits"][:, -1, :] / temperature
|
74 |
+
|
75 |
+
# Apply top-k filtering
|
76 |
+
top_k_logits, top_k_indices = torch.topk(next_token_logits, top_k)
|
77 |
+
probabilities = F.softmax(top_k_logits, dim=-1)
|
78 |
+
next_token = top_k_indices[0, torch.multinomial(probabilities, num_samples=1)]
|
79 |
+
|
80 |
+
# Append the predicted token to input_ids and update attention mask
|
81 |
+
|
82 |
+
input_ids = torch.cat([input_ids, next_token], dim=-1)
|
83 |
+
attention_mask = torch.cat([attention_mask, torch.ones_like(next_token)], dim=-1)
|
84 |
+
|
85 |
+
# Append the target reward for the new token
|
86 |
+
new_rtg = torch.tensor([[target_rtg]], dtype=torch.float).to(device)
|
87 |
+
rtg = torch.cat([rtg, new_rtg], dim=1)
|
88 |
+
|
89 |
+
# Stop if EOS token is generated
|
90 |
+
if next_token.item() == tokenizer.eos_token_id:
|
91 |
+
break
|
92 |
+
|
93 |
+
seq_length += 1
|
94 |
+
|
95 |
+
return tokenizer.decode(input_ids[0], skip_special_tokens=True)
|
96 |
+
|
97 |
+
less_toxic_text = generate_conditioned_text2(model, tokenizer, prompt, target_rtg=1)
|
98 |
+
more_toxic_text = generate_conditioned_text2(model, tokenizer, prompt, target_rtg=0.0)
|
99 |
+
avg_toxic = generate_conditioned_text2(model,tokenizer, prompt, target_rtg=0.5 )
|
100 |
+
|
101 |
+
print("More Toxic Text:", less_toxic_text)
|
102 |
+
print("Less Toxic Text:", more_toxic_text)
|
103 |
+
print("Avg Toxic Text:", avg_toxic)
|
104 |
+
|
105 |
+
```
|
106 |
+
|
107 |
+
## Training Details
|
108 |
+
|
109 |
+
Refer to the github for training datasets and procedure.
|