Update paraphraser.py
Browse files- paraphraser.py +45 -88
paraphraser.py
CHANGED
|
@@ -1,91 +1,48 @@
|
|
| 1 |
-
#
|
| 2 |
-
import
|
| 3 |
-
from model_loader import classifier_model
|
| 4 |
-
from paraphraser import paraphrase_comment
|
| 5 |
-
from metrics import compute_semantic_similarity, compute_emotion_shift, compute_empathy_score, compute_bleu_score, compute_rouge_score, compute_entailment_score
|
| 6 |
|
| 7 |
-
def
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
Returns the prediction label, confidence, color, toxicity score, bias score, paraphrased comment (if applicable), and its metrics.
|
| 12 |
"""
|
| 13 |
-
if not comment
|
| 14 |
-
return
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
empathy_score = None
|
| 54 |
-
bleu_score = None
|
| 55 |
-
rouge_scores = None
|
| 56 |
-
entailment_score = None
|
| 57 |
-
|
| 58 |
-
if label == "Toxic":
|
| 59 |
-
# Paraphrase the comment
|
| 60 |
-
paraphrased_comment = paraphrase_comment(comment)
|
| 61 |
-
|
| 62 |
-
# Re-evaluate the paraphrased comment
|
| 63 |
-
paraphrased_inputs = tokenizer(paraphrased_comment, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 64 |
-
with torch.no_grad():
|
| 65 |
-
paraphrased_outputs = model(**paraphrased_inputs)
|
| 66 |
-
paraphrased_logits = paraphrased_outputs.logits
|
| 67 |
-
|
| 68 |
-
paraphrased_predicted_class = torch.argmax(paraphrased_logits, dim=1).item()
|
| 69 |
-
paraphrased_label = "Toxic" if paraphrased_predicted_class == 1 else "Non-Toxic"
|
| 70 |
-
paraphrased_confidence = torch.softmax(paraphrased_logits, dim=1)[0][paraphrased_predicted_class].item()
|
| 71 |
-
paraphrased_color = "red" if paraphrased_label == "Toxic" else "green"
|
| 72 |
-
paraphrased_toxicity_score = torch.softmax(paraphrased_logits, dim=1)[0][1].item()
|
| 73 |
-
paraphrased_toxicity_score = round(paraphrased_toxicity_score, 2)
|
| 74 |
-
paraphrased_bias_score = 0.01 if paraphrased_label == "Non-Toxic" else 0.15 # Placeholder
|
| 75 |
-
paraphrased_bias_score = round(paraphrased_bias_score, 2)
|
| 76 |
-
|
| 77 |
-
# Compute additional Stage 3 metrics
|
| 78 |
-
semantic_similarity = compute_semantic_similarity(comment, paraphrased_comment)
|
| 79 |
-
original_emotion, paraphrased_emotion, emotion_shift_positive = compute_emotion_shift(comment, paraphrased_comment)
|
| 80 |
-
empathy_score = compute_empathy_score(paraphrased_comment)
|
| 81 |
-
bleu_score = compute_bleu_score(comment, paraphrased_comment)
|
| 82 |
-
rouge_scores = compute_rouge_score(comment, paraphrased_comment)
|
| 83 |
-
entailment_score = compute_entailment_score(comment, paraphrased_comment)
|
| 84 |
-
|
| 85 |
-
return (
|
| 86 |
-
f"Prediction: {label}", confidence, label_color, toxicity_score, bias_score,
|
| 87 |
-
paraphrased_comment, f"Prediction: {paraphrased_label}" if paraphrased_comment else None,
|
| 88 |
-
paraphrased_confidence, paraphrased_color, paraphrased_toxicity_score, paraphrased_bias_score,
|
| 89 |
-
semantic_similarity, f"Original: {original_emotion}, Paraphrased: {paraphrased_emotion}, Positive Shift: {emotion_shift_positive}" if original_emotion else None,
|
| 90 |
-
empathy_score, bleu_score, rouge_scores, entailment_score
|
| 91 |
-
)
|
|
|
|
| 1 |
+
# paraphraser.py
|
| 2 |
+
from model_loader import paraphraser_model
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def paraphrase_comment(comment):
|
| 5 |
"""
|
| 6 |
+
Paraphrase a toxic comment using the Granite 3.2-2B-Instruct model.
|
| 7 |
+
Returns the paraphrased comment.
|
|
|
|
| 8 |
"""
|
| 9 |
+
if not comment:
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
model = paraphraser_model.model
|
| 14 |
+
tokenizer = paraphraser_model.tokenizer
|
| 15 |
+
|
| 16 |
+
# Create a detailed prompt with guidelines and examples
|
| 17 |
+
prompt = (
|
| 18 |
+
"You are a content moderator tasked with rewriting toxic comments into neutral and constructive ones while maintaining the original meaning. "
|
| 19 |
+
"Follow these guidelines:\n"
|
| 20 |
+
"- Remove explicit hate speech, personal attacks, or offensive language.\n"
|
| 21 |
+
"- Keep the response neutral and professional.\n"
|
| 22 |
+
"- Ensure the rewritten comment retains the original intent but in a constructive tone.\n\n"
|
| 23 |
+
"Examples:\n"
|
| 24 |
+
"Toxic: \"You're so dumb! You never understand anything!\"\n"
|
| 25 |
+
"Neutral: \"I think there's some misunderstanding. Let's clarify things.\"\n"
|
| 26 |
+
"Toxic: \"This is the worst idea ever. Only an idiot would suggest this.\"\n"
|
| 27 |
+
"Neutral: \"I don't think this idea works well. Maybe we can explore other options.\"\n\n"
|
| 28 |
+
f"Now, rewrite this comment: \"{comment}\""
|
| 29 |
+
)
|
| 30 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 31 |
+
|
| 32 |
+
# Generate the paraphrased comment
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_length=512,
|
| 36 |
+
num_return_sequences=1,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
top_p=0.9,
|
| 39 |
+
do_sample=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
paraphrased_comment = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
+
# Remove the prompt part from the output
|
| 44 |
+
paraphrased_comment = paraphrased_comment.replace(prompt, "").strip()
|
| 45 |
+
return paraphrased_comment
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error paraphrasing comment: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|