Update paraphraser.py
Browse files- paraphraser.py +34 -34
paraphraser.py
CHANGED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
# paraphraser.py
|
| 2 |
from model_loader import paraphraser_model
|
|
|
|
| 3 |
|
| 4 |
def paraphrase_comment(comment):
|
| 5 |
"""
|
| 6 |
-
Paraphrase a
|
| 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 |
-
#
|
| 17 |
prompt = (
|
| 18 |
-
"You are a content moderator tasked with
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"-
|
| 22 |
-
"-
|
| 23 |
-
"-
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
"Neutral: \"You might be misunderstanding this.\"\n"
|
| 27 |
-
"Toxic: \"This is the worst idea ever. Only an idiot would suggest this.\"\n"
|
| 28 |
-
"Neutral: \"I don’t think this idea works well.\"\n"
|
| 29 |
-
"Toxic: \"You’re useless.\"\n"
|
| 30 |
-
"Neutral: \"This isn’t helping much.\"\n"
|
| 31 |
-
"Toxic: \"Shut up.\"\n"
|
| 32 |
-
"Neutral: \"Let’s take a break from this.\"\n\n"
|
| 33 |
-
f"Now, rewrite this comment: \"{comment}\""
|
| 34 |
)
|
|
|
|
|
|
|
| 35 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 36 |
|
| 37 |
-
# Generate the paraphrased comment
|
| 38 |
outputs = model.generate(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
num_beams=
|
| 42 |
-
|
| 43 |
-
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
paraphrased_comment = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
|
|
|
|
|
|
| 1 |
# paraphraser.py
|
| 2 |
from model_loader import paraphraser_model
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
def paraphrase_comment(comment):
|
| 6 |
"""
|
| 7 |
+
Paraphrase a given comment using the fine-tuned Granite 3.2-2B-Instruct model to make it non-toxic, empathetic, and professional while retaining the original intent.
|
| 8 |
Returns the paraphrased comment.
|
| 9 |
"""
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
start_time = time.time()
|
| 12 |
+
print("Starting paraphrasing...")
|
| 13 |
+
|
| 14 |
+
# Access the model and tokenizer
|
| 15 |
model = paraphraser_model.model
|
| 16 |
tokenizer = paraphraser_model.tokenizer
|
| 17 |
|
| 18 |
+
# Define the prompt for paraphrasing
|
| 19 |
prompt = (
|
| 20 |
+
f"You are a content moderator tasked with paraphrasing a comment to make it non-toxic, empathetic, and professional while retaining the original intent. "
|
| 21 |
+
f"The original comment is: \"{comment}\". "
|
| 22 |
+
f"Guidelines: "
|
| 23 |
+
f"- Remove any hate speech, offensive language, or toxic elements. "
|
| 24 |
+
f"- Use a neutral or positive tone. "
|
| 25 |
+
f"- Ensure the paraphrased comment is concise and clear. "
|
| 26 |
+
f"- Maintain the core message or intent of the original comment. "
|
| 27 |
+
f"Provide the paraphrased comment only, without additional explanation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
+
|
| 30 |
+
# Tokenize the prompt
|
| 31 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 32 |
|
| 33 |
+
# Generate the paraphrased comment
|
| 34 |
outputs = model.generate(
|
| 35 |
+
inputs["input_ids"],
|
| 36 |
+
max_length=512,
|
| 37 |
+
num_beams=5,
|
| 38 |
+
no_repeat_ngram_size=2,
|
| 39 |
+
early_stopping=True
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Decode the output
|
| 43 |
paraphrased_comment = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
# Clean up the output (remove the prompt and any extra text)
|
| 46 |
+
if prompt in paraphrased_comment:
|
| 47 |
+
paraphrased_comment = paraphrased_comment.replace(prompt, "").strip()
|
| 48 |
+
paraphrased_comment = paraphrased_comment.strip()
|
| 49 |
+
|
| 50 |
+
print(f"Paraphrasing completed in {time.time() - start_time:.2f} seconds")
|
| 51 |
+
return paraphrased_comment if paraphrased_comment else "Error: Unable to generate paraphrase."
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"Error during paraphrasing: {str(e)}")
|
| 55 |
+
return "Error: Unable to generate paraphrase."
|