Update refine_paraphrases.py
Browse files- refine_paraphrases.py +36 -36
refine_paraphrases.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
# refine_paraphrases.py
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from paraphraser import paraphrase_comment
|
| 4 |
-
from
|
| 5 |
-
from model_loader import paraphraser_model
|
| 6 |
-
from datasets import load_dataset
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Configuration
|
| 10 |
DATA_PATH = "JanviMl/toxi_refined_paraphrases"
|
| 11 |
OUTPUT_PATH = "iterated_paraphrases.csv"
|
| 12 |
-
MAX_ITERATIONS =
|
| 13 |
TARGET_SCORES = {
|
| 14 |
"empathy": 0.9,
|
| 15 |
"toxicity": 0.1,
|
|
@@ -18,33 +17,25 @@ TARGET_SCORES = {
|
|
| 18 |
"reward": 0.25
|
| 19 |
}
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt = (
|
| 27 |
-
f"
|
| 28 |
-
f"
|
| 29 |
-
f"
|
| 30 |
-
f"
|
| 31 |
-
f"
|
| 32 |
-
f"
|
|
|
|
| 33 |
)
|
| 34 |
return paraphrase_comment(prompt)
|
| 35 |
|
| 36 |
-
def meets_targets(scores: dict) -> bool:
|
| 37 |
-
"""
|
| 38 |
-
Check if paraphrase scores meet target thresholds.
|
| 39 |
-
"""
|
| 40 |
-
return (
|
| 41 |
-
scores["empathy"] >= TARGET_SCORES["empathy"] and
|
| 42 |
-
scores["toxicity"] <= TARGET_SCORES["toxicity"] and
|
| 43 |
-
scores["bias"] <= TARGET_SCORES["bias"] and
|
| 44 |
-
scores["hallucination"] <= TARGET_SCORES["hallucination"] and
|
| 45 |
-
scores["reward"] >= TARGET_SCORES["reward"]
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
def refine_paraphrase(row: pd.Series) -> tuple:
|
| 49 |
"""
|
| 50 |
Iteratively refine a single paraphrase.
|
|
@@ -62,12 +53,22 @@ def refine_paraphrase(row: pd.Series) -> tuple:
|
|
| 62 |
issues = row["Human_Evaluation_Reasoning"]
|
| 63 |
iteration = 0
|
| 64 |
reasoning = []
|
|
|
|
| 65 |
|
| 66 |
while iteration < MAX_ITERATIONS and not meets_targets(current_scores):
|
|
|
|
| 67 |
# Generate new paraphrase
|
| 68 |
new_paraphrase = generate_new_paraphrase(original, current_paraphrase, current_scores, issues)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Evaluate new paraphrase
|
| 70 |
new_scores = compute_reward_scores(original, new_paraphrase)
|
|
|
|
| 71 |
# Log reasoning
|
| 72 |
reasoning.append(
|
| 73 |
f"Iteration {iteration + 1}: Generated '{new_paraphrase}' with scores {new_scores}. "
|
|
@@ -82,17 +83,17 @@ def refine_paraphrase(row: pd.Series) -> tuple:
|
|
| 82 |
reasoning.append("Rejected new paraphrase; no improvement in reward score.")
|
| 83 |
iteration += 1
|
| 84 |
|
|
|
|
| 85 |
return current_paraphrase, current_scores, "; ".join(reasoning)
|
| 86 |
|
| 87 |
def main():
|
| 88 |
# Load dataset from Hugging Face Hub
|
| 89 |
try:
|
| 90 |
-
df = load_dataset(DATA_PATH, split="train").to_pandas()
|
| 91 |
except Exception as e:
|
| 92 |
print(f"Error loading dataset: {str(e)}")
|
| 93 |
return
|
| 94 |
-
|
| 95 |
-
# Process each row
|
| 96 |
results = []
|
| 97 |
for idx, row in df.iterrows():
|
| 98 |
new_paraphrase, new_scores, reasoning = refine_paraphrase(row)
|
|
@@ -111,20 +112,19 @@ def main():
|
|
| 111 |
"Iteration_Reasoning": reasoning
|
| 112 |
}
|
| 113 |
results.append(result)
|
| 114 |
-
|
| 115 |
-
# Save results
|
| 116 |
result_df = pd.DataFrame(results)
|
| 117 |
result_df.to_csv(OUTPUT_PATH, index=False)
|
| 118 |
print(f"Refinement complete. Results saved to {OUTPUT_PATH}")
|
| 119 |
-
|
| 120 |
# Push to Hugging Face Hub
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
dataset = Dataset.from_pandas(result_df)
|
| 124 |
dataset.push_to_hub("JanviMl/toxi_iterated_paraphrases", token=os.getenv("HF_TOKEN"))
|
| 125 |
print("Pushed to Hugging Face Hub: JanviMl/toxi_iterated_paraphrases")
|
| 126 |
except Exception as e:
|
| 127 |
-
print(f"Error pushing to Hub: {str(e)}")
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
main()
|
|
|
|
| 1 |
# refine_paraphrases.py
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
import pandas as pd
|
| 4 |
from paraphraser import paraphrase_comment
|
| 5 |
+
from metrics import compute_reward_scores
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
# Configuration
|
| 9 |
DATA_PATH = "JanviMl/toxi_refined_paraphrases"
|
| 10 |
OUTPUT_PATH = "iterated_paraphrases.csv"
|
| 11 |
+
MAX_ITERATIONS = 1
|
| 12 |
TARGET_SCORES = {
|
| 13 |
"empathy": 0.9,
|
| 14 |
"toxicity": 0.1,
|
|
|
|
| 17 |
"reward": 0.25
|
| 18 |
}
|
| 19 |
|
| 20 |
+
def meets_targets(scores):
|
| 21 |
+
return (scores["empathy"] >= TARGET_SCORES["empathy"] and
|
| 22 |
+
scores["toxicity"] <= TARGET_SCORES["toxicity"] and
|
| 23 |
+
scores["bias"] <= TARGET_SCORES["bias"] and
|
| 24 |
+
scores["hallucination"] <= TARGET_SCORES["hallucination"] and
|
| 25 |
+
scores["reward"] >= TARGET_SCORES["reward"])
|
| 26 |
+
|
| 27 |
+
def generate_new_paraphrase(original, current_paraphrase, current_scores, issues):
|
| 28 |
prompt = (
|
| 29 |
+
f"Original comment: '{original}'. "
|
| 30 |
+
f"Current paraphrase: '{current_paraphrase}'. "
|
| 31 |
+
f"Current scores: {current_scores}. "
|
| 32 |
+
f"Human feedback: {issues}. "
|
| 33 |
+
f"Generate a new paraphrase that improves empathy (>= {TARGET_SCORES['empathy']}), "
|
| 34 |
+
f"reduces toxicity (<= {TARGET_SCORES['toxicity']}), bias (<= {TARGET_SCORES['bias']}), "
|
| 35 |
+
f"and hallucination (<= {TARGET_SCORES['hallucination']}), and increases reward (>= {TARGET_SCORES['reward']})."
|
| 36 |
)
|
| 37 |
return paraphrase_comment(prompt)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def refine_paraphrase(row: pd.Series) -> tuple:
|
| 40 |
"""
|
| 41 |
Iteratively refine a single paraphrase.
|
|
|
|
| 53 |
issues = row["Human_Evaluation_Reasoning"]
|
| 54 |
iteration = 0
|
| 55 |
reasoning = []
|
| 56 |
+
print(f"Processing comment: {original}")
|
| 57 |
|
| 58 |
while iteration < MAX_ITERATIONS and not meets_targets(current_scores):
|
| 59 |
+
print(f"Starting iteration {iteration + 1} for comment: {original}")
|
| 60 |
# Generate new paraphrase
|
| 61 |
new_paraphrase = generate_new_paraphrase(original, current_paraphrase, current_scores, issues)
|
| 62 |
+
print(f"Generated paraphrase: {new_paraphrase}")
|
| 63 |
+
|
| 64 |
+
# Check if paraphrasing failed
|
| 65 |
+
if "Error: Unable to generate paraphrase" in new_paraphrase:
|
| 66 |
+
reasoning.append(f"Iteration {iteration + 1}: Paraphrasing failed - {new_paraphrase}")
|
| 67 |
+
break
|
| 68 |
+
|
| 69 |
# Evaluate new paraphrase
|
| 70 |
new_scores = compute_reward_scores(original, new_paraphrase)
|
| 71 |
+
print(f"New scores: {new_scores}")
|
| 72 |
# Log reasoning
|
| 73 |
reasoning.append(
|
| 74 |
f"Iteration {iteration + 1}: Generated '{new_paraphrase}' with scores {new_scores}. "
|
|
|
|
| 83 |
reasoning.append("Rejected new paraphrase; no improvement in reward score.")
|
| 84 |
iteration += 1
|
| 85 |
|
| 86 |
+
print(f"Finished processing comment: {original}")
|
| 87 |
return current_paraphrase, current_scores, "; ".join(reasoning)
|
| 88 |
|
| 89 |
def main():
|
| 90 |
# Load dataset from Hugging Face Hub
|
| 91 |
try:
|
| 92 |
+
df = load_dataset(DATA_PATH, split="train").to_pandas()[:1] # Process only 1 row
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error loading dataset: {str(e)}")
|
| 95 |
return
|
| 96 |
+
|
|
|
|
| 97 |
results = []
|
| 98 |
for idx, row in df.iterrows():
|
| 99 |
new_paraphrase, new_scores, reasoning = refine_paraphrase(row)
|
|
|
|
| 112 |
"Iteration_Reasoning": reasoning
|
| 113 |
}
|
| 114 |
results.append(result)
|
| 115 |
+
|
| 116 |
+
# Save results to CSV
|
| 117 |
result_df = pd.DataFrame(results)
|
| 118 |
result_df.to_csv(OUTPUT_PATH, index=False)
|
| 119 |
print(f"Refinement complete. Results saved to {OUTPUT_PATH}")
|
| 120 |
+
|
| 121 |
# Push to Hugging Face Hub
|
| 122 |
try:
|
| 123 |
+
dataset = load_dataset("pandas", data_files=OUTPUT_PATH)
|
|
|
|
| 124 |
dataset.push_to_hub("JanviMl/toxi_iterated_paraphrases", token=os.getenv("HF_TOKEN"))
|
| 125 |
print("Pushed to Hugging Face Hub: JanviMl/toxi_iterated_paraphrases")
|
| 126 |
except Exception as e:
|
| 127 |
+
print(f"Error pushing to Hugging Face Hub: {str(e)}")
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
main()
|