Update classifier.py
Browse files- classifier.py +18 -2
classifier.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
# classifier.py
|
2 |
import torch
|
|
|
3 |
from model_loader import classifier_model
|
4 |
from paraphraser import paraphrase_comment
|
5 |
-
from metrics import compute_semantic_similarity, compute_empathy_score, compute_rouge_score
|
6 |
|
7 |
def classify_toxic_comment(comment):
|
8 |
"""
|
@@ -10,6 +11,9 @@ def classify_toxic_comment(comment):
|
|
10 |
If toxic, paraphrase the comment, re-evaluate, and compute essential metrics.
|
11 |
Returns the prediction label, confidence, color, toxicity score, bias score, paraphrased comment (if applicable), and its metrics.
|
12 |
"""
|
|
|
|
|
|
|
13 |
if not comment.strip():
|
14 |
return "Error: Please enter a comment.", None, None, None, None, None, None, None, None, None, None, None, None
|
15 |
|
@@ -18,6 +22,7 @@ def classify_toxic_comment(comment):
|
|
18 |
tokenizer = classifier_model.tokenizer
|
19 |
|
20 |
# Tokenize the input comment
|
|
|
21 |
inputs = tokenizer(comment, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
22 |
|
23 |
# Run inference
|
@@ -38,6 +43,7 @@ def classify_toxic_comment(comment):
|
|
38 |
# Simulate Bias Score (placeholder)
|
39 |
bias_score = 0.01 if label == "Non-Toxic" else 0.15
|
40 |
bias_score = round(bias_score, 2)
|
|
|
41 |
|
42 |
# If the comment is toxic, paraphrase it and compute essential metrics
|
43 |
paraphrased_comment = None
|
@@ -48,13 +54,17 @@ def classify_toxic_comment(comment):
|
|
48 |
paraphrased_bias_score = None
|
49 |
semantic_similarity = None
|
50 |
empathy_score = None
|
|
|
51 |
rouge_scores = None
|
52 |
|
53 |
if label == "Toxic":
|
54 |
# Paraphrase the comment
|
|
|
55 |
paraphrased_comment = paraphrase_comment(comment)
|
|
|
56 |
|
57 |
# Re-evaluate the paraphrased comment
|
|
|
58 |
paraphrased_inputs = tokenizer(paraphrased_comment, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
59 |
with torch.no_grad():
|
60 |
paraphrased_outputs = model(**paraphrased_inputs)
|
@@ -68,15 +78,21 @@ def classify_toxic_comment(comment):
|
|
68 |
paraphrased_toxicity_score = round(paraphrased_toxicity_score, 2)
|
69 |
paraphrased_bias_score = 0.01 if paraphrased_label == "Non-Toxic" else 0.15 # Placeholder
|
70 |
paraphrased_bias_score = round(paraphrased_bias_score, 2)
|
|
|
71 |
|
72 |
# Compute essential metrics
|
|
|
73 |
semantic_similarity = compute_semantic_similarity(comment, paraphrased_comment)
|
74 |
empathy_score = compute_empathy_score(paraphrased_comment)
|
|
|
75 |
rouge_scores = compute_rouge_score(comment, paraphrased_comment)
|
|
|
|
|
|
|
76 |
|
77 |
return (
|
78 |
f"Prediction: {label}", confidence, label_color, toxicity_score, bias_score,
|
79 |
paraphrased_comment, f"Prediction: {paraphrased_label}" if paraphrased_comment else None,
|
80 |
paraphrased_confidence, paraphrased_color, paraphrased_toxicity_score, paraphrased_bias_score,
|
81 |
-
semantic_similarity, empathy_score, rouge_scores
|
82 |
)
|
|
|
1 |
# classifier.py
|
2 |
import torch
|
3 |
+
import time
|
4 |
from model_loader import classifier_model
|
5 |
from paraphraser import paraphrase_comment
|
6 |
+
from metrics import compute_semantic_similarity, compute_empathy_score, compute_bleu_score, compute_rouge_score
|
7 |
|
8 |
def classify_toxic_comment(comment):
|
9 |
"""
|
|
|
11 |
If toxic, paraphrase the comment, re-evaluate, and compute essential metrics.
|
12 |
Returns the prediction label, confidence, color, toxicity score, bias score, paraphrased comment (if applicable), and its metrics.
|
13 |
"""
|
14 |
+
start_total = time.time()
|
15 |
+
print("Starting classification...")
|
16 |
+
|
17 |
if not comment.strip():
|
18 |
return "Error: Please enter a comment.", None, None, None, None, None, None, None, None, None, None, None, None
|
19 |
|
|
|
22 |
tokenizer = classifier_model.tokenizer
|
23 |
|
24 |
# Tokenize the input comment
|
25 |
+
start_classification = time.time()
|
26 |
inputs = tokenizer(comment, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
27 |
|
28 |
# Run inference
|
|
|
43 |
# Simulate Bias Score (placeholder)
|
44 |
bias_score = 0.01 if label == "Non-Toxic" else 0.15
|
45 |
bias_score = round(bias_score, 2)
|
46 |
+
print(f"Classification took {time.time() - start_classification:.2f} seconds")
|
47 |
|
48 |
# If the comment is toxic, paraphrase it and compute essential metrics
|
49 |
paraphrased_comment = None
|
|
|
54 |
paraphrased_bias_score = None
|
55 |
semantic_similarity = None
|
56 |
empathy_score = None
|
57 |
+
bleu_score = None
|
58 |
rouge_scores = None
|
59 |
|
60 |
if label == "Toxic":
|
61 |
# Paraphrase the comment
|
62 |
+
start_paraphrase = time.time()
|
63 |
paraphrased_comment = paraphrase_comment(comment)
|
64 |
+
print(f"Paraphrasing took {time.time() - start_paraphrase:.2f} seconds")
|
65 |
|
66 |
# Re-evaluate the paraphrased comment
|
67 |
+
start_reclassification = time.time()
|
68 |
paraphrased_inputs = tokenizer(paraphrased_comment, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
69 |
with torch.no_grad():
|
70 |
paraphrased_outputs = model(**paraphrased_inputs)
|
|
|
78 |
paraphrased_toxicity_score = round(paraphrased_toxicity_score, 2)
|
79 |
paraphrased_bias_score = 0.01 if paraphrased_label == "Non-Toxic" else 0.15 # Placeholder
|
80 |
paraphrased_bias_score = round(paraphrased_bias_score, 2)
|
81 |
+
print(f"Reclassification of paraphrased comment took {time.time() - start_reclassification:.2f} seconds")
|
82 |
|
83 |
# Compute essential metrics
|
84 |
+
start_metrics = time.time()
|
85 |
semantic_similarity = compute_semantic_similarity(comment, paraphrased_comment)
|
86 |
empathy_score = compute_empathy_score(paraphrased_comment)
|
87 |
+
bleu_score = compute_bleu_score(comment, paraphrased_comment)
|
88 |
rouge_scores = compute_rouge_score(comment, paraphrased_comment)
|
89 |
+
print(f"Metrics computation took {time.time() - start_metrics:.2f} seconds")
|
90 |
+
|
91 |
+
print(f"Total processing time: {time.time() - start_total:.2f} seconds")
|
92 |
|
93 |
return (
|
94 |
f"Prediction: {label}", confidence, label_color, toxicity_score, bias_score,
|
95 |
paraphrased_comment, f"Prediction: {paraphrased_label}" if paraphrased_comment else None,
|
96 |
paraphrased_confidence, paraphrased_color, paraphrased_toxicity_score, paraphrased_bias_score,
|
97 |
+
semantic_similarity, empathy_score, bleu_score, rouge_scores
|
98 |
)
|