File size: 1,198 Bytes
2c12a96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# paraphraser.py
import torch
from model_loader import paraphrase_model, paraphrase_tokenizer

def paraphrase_comment(comment, prompt_template="Paraphrase the following comment to make it non-toxic while preserving its meaning: \"{comment}\""):
    """
    Paraphrase a toxic comment using the Granite 3.2-2B-Instruct model.
    Returns the paraphrased comment.
    """
    # Format the prompt with the input comment
    prompt = prompt_template.format(comment=comment)

    # Tokenize the prompt
    inputs = paraphrase_tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)

    # Generate the paraphrased output
    with torch.no_grad():
        outputs = paraphrase_model.generate(
            **inputs,
            max_length=512,
            num_return_sequences=1,
            do_sample=True,
            top_p=0.95,
            temperature=0.7
        )

    # Decode the generated output
    paraphrased_comment = paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Remove the prompt part from the output (if the model includes it)
    paraphrased_comment = paraphrased_comment.replace(prompt, "").strip()

    return paraphrased_comment