forked from Kiode/Text_Watermark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_CLI.py
More file actions
70 lines (56 loc) · 2.43 KB
/
Copy pathdemo_CLI.py
File metadata and controls
70 lines (56 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from models.watermark_faster import watermark_model
from options import get_parser_main_model
import warnings
# Ignore warnings
warnings.filterwarnings('ignore')
def watermark_embed_demo(raw):
watermarked_text = model.embed(raw)
return watermarked_text
def watermark_extract(raw):
is_watermark, p_value, n, ones, z_value = model.watermark_detector_fast(raw)
confidence = (1 - p_value) * 100
return f"{confidence:.2f}%"
def precise_watermark_detect(raw):
is_watermark, p_value, n, ones, z_value = model.watermark_detector_precise(raw)
confidence = (1 - p_value) * 100
return f"{confidence:.2f}%"
if __name__ == "__main__":
opts = get_parser_main_model().parse_args()
model = watermark_model(language=opts.language, mode=opts.mode, tau_word=opts.tau_word, lamda=opts.lamda)
while True:
print("\n" + "=" * 66)
print("Watermarking Text Generated by Black-Box Language Models".center(60))
print("Created by Xi Yang (yx9726@mail.ustc.edu.cn)".center(60))
print("=" * 60 + "\n")
print("1. Inject Watermark")
print("2. Detect Watermark")
print("3. Exit\n")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
print("\n" + "-" * 60)
text = input("Enter the text to embed a watermark: ")
watermarked_text = watermark_embed_demo(text)
print("\n" + "-" * 60)
print(f"Watermarked Text:\n{watermarked_text}")
print("-" * 60)
elif choice == '2':
print("\n" + "-" * 60)
text = input("Enter the text to analyze: ")
mode = input("Select detection mode (Fast/Precise): ")
if mode.lower() == 'fast':
confidence = watermark_extract(text)
elif mode.lower() == 'precise':
confidence = precise_watermark_detect(text)
else:
print("Invalid mode. Please choose 'Fast' or 'Precise'.")
continue
print("\n" + "-" * 60)
print(f"Confidence (the likelihood of the text containing a watermark): {confidence}")
print("-" * 60)
elif choice == '3':
print("\nExiting...")
break
elif choice == '0': # backdoor for admin
print("\nHello admin,welcome to your own palace!Let's do this!")
else:
print("Invalid choice. Please choose 1, 2, or 3.")