weijunl commited on
Commit
f4e60e1
·
verified ·
1 Parent(s): 376161c

Upload run_dpst.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. run_dpst.py +67 -67
run_dpst.py CHANGED
@@ -1,67 +1,67 @@
1
-
2
- import os
3
- os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
4
-
5
- import argparse
6
- import DPST
7
-
8
- def main():
9
- parser = argparse.ArgumentParser(description="Run DPST text privatization")
10
- parser.add_argument("--mode", type=str, default="50k", choices=["50k", "100k", "200k"],
11
- help="Clustering mode (default: 50k)")
12
- parser.add_argument("--epsilon", type=float, default=10.0,
13
- help="Privacy budget epsilon (default: 10.0)")
14
- parser.add_argument("--input", type=str, default=None,
15
- help="Input text file path (optional)")
16
- parser.add_argument("--output", type=str, default=None,
17
- help="Output file path (optional, prints to stdout if not provided)")
18
- parser.add_argument("--model", type=str, default="meta-llama/Llama-3.2-1B-Instruct",
19
- help="HuggingFace model checkpoint")
20
- parser.add_argument("--token", type=str, default=os.environ.get("HF_TOKEN"),
21
- help="HuggingFace token for gated models (or set HF_TOKEN env var)")
22
- parser.add_argument("--no-dp", action="store_true",
23
- help="Disable DP (for comparison)")
24
-
25
- args = parser.parse_args()
26
-
27
- if args.input:
28
- print(f"Reading input from: {args.input}")
29
- with open(args.input, 'r', encoding='utf-8') as f:
30
- texts = [line.strip() for line in f if line.strip()]
31
- else:
32
- texts = [
33
- "Perfect matched part at even better price.: The part fit my BMW perfect and the price was great. Thank you!",
34
- "Fast response, quick delivery, product as advertised! Great Seller: Fast response, quick delivery, product as advertised! Great Seller",
35
- "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"
36
- ]
37
- print("No input file provided. Using demo texts:")
38
- for i, t in enumerate(texts, 1):
39
- print(f" {i}. {t}")
40
-
41
- print(f"\nInitializing DPST with mode={args.mode}, model={args.model}...")
42
- dpst_instance = DPST.DPST(mode=args.mode, hf_token=args.token, model_checkpoint=args.model)
43
-
44
- print(f"Running privatization with epsilon={args.epsilon}, DP={not args.no_dp}...")
45
- private_texts = dpst_instance.privatize(texts, epsilon=args.epsilon, DP=(not args.no_dp))
46
-
47
- print("\n" + "="*80)
48
- print("RESULTS")
49
- print("="*80)
50
-
51
- for i, (orig, priv) in enumerate(zip(texts, private_texts), 1):
52
- print(f"\n[{i}] Original:")
53
- print(f" {orig}")
54
- print(f" Privatized:")
55
- print(f" {priv}")
56
-
57
- if args.output:
58
- with open(args.output, 'w', encoding='utf-8') as f:
59
- for priv in private_texts:
60
- f.write(priv + "\n")
61
- print(f"\n✓ Saved privatized texts to: {args.output}")
62
-
63
- dpst_instance.cleanup()
64
- print("\n✓ Done!")
65
-
66
- if __name__ == "__main__":
67
- main()
 
1
+
2
+ import os
3
+ os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
4
+
5
+ import argparse
6
+ import DPST
7
+
8
+ def main():
9
+ parser = argparse.ArgumentParser(description="Run DPST text privatization")
10
+ parser.add_argument("--mode", type=str, default="50k", choices=["50k", "100k", "200k"],
11
+ help="Clustering mode (default: 50k)")
12
+ parser.add_argument("--epsilon", type=float, default=10.0,
13
+ help="Privacy budget epsilon (default: 10.0)")
14
+ parser.add_argument("--input", type=str, default=None,
15
+ help="Input text file path (optional)")
16
+ parser.add_argument("--output", type=str, default=None,
17
+ help="Output file path (optional, prints to stdout if not provided)")
18
+ parser.add_argument("--model", type=str, default="meta-llama/Llama-3.2-1B-Instruct",
19
+ help="HuggingFace model checkpoint")
20
+ parser.add_argument("--token", type=str, default=os.environ.get("HF_TOKEN"),
21
+ help="HuggingFace token for gated models (or set HF_TOKEN env var)")
22
+ parser.add_argument("--no-dp", action="store_true",
23
+ help="Disable DP (for comparison)")
24
+
25
+ args = parser.parse_args()
26
+
27
+ if args.input:
28
+ print(f"Reading input from: {args.input}")
29
+ with open(args.input, 'r', encoding='utf-8') as f:
30
+ texts = [line.strip() for line in f if line.strip()]
31
+ else:
32
+ texts = [
33
+ "Perfect matched part at even better price.: The part fit my BMW perfect and the price was great. Thank you!",
34
+ "Fast response, quick delivery, product as advertised! Great Seller: Fast response, quick delivery, product as advertised! Great Seller",
35
+ "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"
36
+ ]
37
+ print("No input file provided. Using demo texts:")
38
+ for i, t in enumerate(texts, 1):
39
+ print(f" {i}. {t}")
40
+
41
+ print(f"\nInitializing DPST with mode={args.mode}, model={args.model}...")
42
+ dpst_instance = DPST.DPST(mode=args.mode, hf_token=args.token, model_checkpoint=args.model)
43
+
44
+ print(f"Running privatization with epsilon={args.epsilon}, DP={not args.no_dp}...")
45
+ private_texts = dpst_instance.privatize(texts, epsilon=args.epsilon, DP=(not args.no_dp))
46
+
47
+ print("\n" + "="*80)
48
+ print("RESULTS")
49
+ print("="*80)
50
+
51
+ for i, (orig, priv) in enumerate(zip(texts, private_texts), 1):
52
+ print(f"\n[{i}] Original:")
53
+ print(f" {orig}")
54
+ print(f" Privatized:")
55
+ print(f" {priv}")
56
+
57
+ if args.output:
58
+ with open(args.output, 'w', encoding='utf-8') as f:
59
+ for priv in private_texts:
60
+ f.write(priv + "\n")
61
+ print(f"\n✓ Saved privatized texts to: {args.output}")
62
+
63
+ dpst_instance.cleanup()
64
+ print("\n✓ Done!")
65
+
66
+ if __name__ == "__main__":
67
+ main()