dpst-replication / run_dpst.py
weijunl's picture
Upload run_dpst.py with huggingface_hub
913194f verified
import os
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
import argparse
import DPST
def main():
parser = argparse.ArgumentParser(description="Run DPST text privatization")
parser.add_argument("--mode", type=str, default="50k", choices=["50k", "100k", "200k"],
help="Clustering mode (default: 50k)")
parser.add_argument("--epsilon", type=float, default=10.0,
help="Privacy budget epsilon (default: 10.0)")
parser.add_argument("--input", type=str, default=None,
help="Input text file path (optional)")
parser.add_argument("--output", type=str, default=None,
help="Output file path (optional, prints to stdout if not provided)")
parser.add_argument("--model", type=str, default="meta-llama/Llama-3.2-1B-Instruct",
help="HuggingFace model checkpoint")
parser.add_argument("--token", type=str, default=os.environ.get("HF_TOKEN"),
help="HuggingFace token for gated models (or set HF_TOKEN env var)")
parser.add_argument("--no-dp", action="store_true",
help="Disable DP (for comparison)")
args = parser.parse_args()
if args.input:
print(f"Reading input from: {args.input}")
with open(args.input, 'r', encoding='utf-8') as f:
texts = [line.strip() for line in f if line.strip()]
else:
texts = [
"ok so in one weird moment 2 years ago i was angry, it caused an arousal, and then a weird bad thought popped into my head about harming the person making me angry. Back then i didnt have the ocd theme that causes such things, i just was like ugh no! why did i even have this thought?! and just ignored it (guess its called the ol ́good days!) i didnt get stuck in it, (...) is it like my mind throwing something random and all? or did my mind just crash because of the different kinds of feelings that were mixed together weirdly",
"Enjoyed the experience!: I thoroughly enjoyed the experience of creating my own photo album and adding my own narratives. I now have 6 of the Crewe photo books and I am really proud of them. The price is competitive and delivery and quality excellent. Will definitely use again (can’t wait!) and would highly recommend the service to anyone. Well done Jessops"
]
print("No input file provided. Using demo texts:")
for i, t in enumerate(texts, 1):
print(f" {i}. {t}")
print(f"\nInitializing DPST with mode={args.mode}, model={args.model}...")
dpst_instance = DPST.DPST(mode=args.mode, hf_token=args.token, model_checkpoint=args.model)
print(f"Running privatization with epsilon={args.epsilon}, DP={not args.no_dp}...")
private_texts = dpst_instance.privatize(texts, epsilon=args.epsilon, DP=(not args.no_dp), verbose=True)
print("\n" + "="*80)
print("RESULTS")
print("="*80)
for i, (orig, priv) in enumerate(zip(texts, private_texts), 1):
print(f"\n[{i}] Original:")
print(f" {orig}")
print(f" Privatized:")
print(f" {priv}")
if args.output:
with open(args.output, 'w', encoding='utf-8') as f:
for priv in private_texts:
f.write(priv + "\n")
print(f"\n✓ Saved privatized texts to: {args.output}")
dpst_instance.cleanup()
print("\n✓ Done!")
if __name__ == "__main__":
main()