|
|
|
from model_loader import paraphraser_model |
|
import time |
|
|
|
def paraphrase_comment(comment): |
|
""" |
|
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. |
|
Returns the paraphrased comment. |
|
""" |
|
try: |
|
start_time = time.time() |
|
print("Starting paraphrasing...") |
|
|
|
|
|
model = paraphraser_model.model |
|
tokenizer = paraphraser_model.tokenizer |
|
|
|
|
|
prompt = ( |
|
f"You are a content moderator tasked with paraphrasing a comment to make it non-toxic, empathetic, and professional while retaining the original intent. " |
|
f"The original comment is: \"{comment}\". " |
|
f"Guidelines: " |
|
f"- Remove any hate speech, offensive language, or toxic elements. " |
|
f"- Use a neutral or positive tone. " |
|
f"- Ensure the paraphrased comment is concise and clear. " |
|
f"- Maintain the core message or intent of the original comment. " |
|
f"Provide the paraphrased comment only, without additional explanation." |
|
) |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512) |
|
|
|
|
|
outputs = model.generate( |
|
inputs["input_ids"], |
|
max_length=512, |
|
num_beams=5, |
|
no_repeat_ngram_size=2, |
|
early_stopping=True |
|
) |
|
|
|
|
|
paraphrased_comment = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
if prompt in paraphrased_comment: |
|
paraphrased_comment = paraphrased_comment.replace(prompt, "").strip() |
|
paraphrased_comment = paraphrased_comment.strip() |
|
|
|
print(f"Paraphrasing completed in {time.time() - start_time:.2f} seconds") |
|
return paraphrased_comment if paraphrased_comment else "Error: Unable to generate paraphrase." |
|
|
|
except Exception as e: |
|
print(f"Error during paraphrasing: {str(e)}") |
|
return "Error: Unable to generate paraphrase." |