Create paraphraser.py
Browse files- paraphraser.py +33 -0
paraphraser.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# paraphraser.py
|
2 |
+
import torch
|
3 |
+
from model_loader import paraphrase_model, paraphrase_tokenizer
|
4 |
+
|
5 |
+
def paraphrase_comment(comment, prompt_template="Paraphrase the following comment to make it non-toxic while preserving its meaning: \"{comment}\""):
|
6 |
+
"""
|
7 |
+
Paraphrase a toxic comment using the Granite 3.2-2B-Instruct model.
|
8 |
+
Returns the paraphrased comment.
|
9 |
+
"""
|
10 |
+
# Format the prompt with the input comment
|
11 |
+
prompt = prompt_template.format(comment=comment)
|
12 |
+
|
13 |
+
# Tokenize the prompt
|
14 |
+
inputs = paraphrase_tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
15 |
+
|
16 |
+
# Generate the paraphrased output
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = paraphrase_model.generate(
|
19 |
+
**inputs,
|
20 |
+
max_length=512,
|
21 |
+
num_return_sequences=1,
|
22 |
+
do_sample=True,
|
23 |
+
top_p=0.95,
|
24 |
+
temperature=0.7
|
25 |
+
)
|
26 |
+
|
27 |
+
# Decode the generated output
|
28 |
+
paraphrased_comment = paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
|
30 |
+
# Remove the prompt part from the output (if the model includes it)
|
31 |
+
paraphrased_comment = paraphrased_comment.replace(prompt, "").strip()
|
32 |
+
|
33 |
+
return paraphrased_comment
|