该目录包含用于分析和移除AI生成文本中SynthID水印的工具。该水印由Google DeepMind开发,并于2024年发表在《自然》杂志上。
N-gram 上下文:对于每个词元位置,SynthID 将之前的 ngram_len - 1 个词元(默认:4个词元)视为上下文。
哈希计算:计算以下内容的哈希:
G值分配:哈希用于为每个密钥层的每个可能的下一个词元分配一个二进制g值(0或1)。
概率修改:修改词元概率,以偏向g值为1的词元:
new_prob[i] = prob[i] * (1 + g[i] - mean(g * prob))
采样:模型从修改后的分布中采样。
有效性:90-100%
水印嵌入在特定的词元序列中,而非语义中。使用无水印的模型进行改写会完全重新生成词元序列。
from reverse_synthid import ParaphrasingAttack
attack = ParaphrasingAttack(model_name="gpt2")
clean_text = attack.paraphrase(watermarked_text)
有效性:50-70%
用同义词替换词元会破坏n-gram模式:
from reverse_synthid import TokenPerturbationAttack
attack = TokenPerturbationAttack()
clean_text = attack.substitute_synonyms(text, rate=0.3)
有效性:95-100%
用视觉上相同的Unicode字符替换会破坏哈希:
from reverse_synthid import HomoglyphAttack
attack = HomoglyphAttack()
clean_text = attack.apply_homoglyphs(text, rate=0.1)
有效性:30-50%
插入或删除单词会移动所有后续n-gram边界:
from reverse_synthid import TokenPerturbationAttack
attack = TokenPerturbationAttack()
clean_text = attack.insert_fillers(text, rate=0.1)
reverse_synthid.py - 主攻击工具包,包含多种方法analyze_watermark.py - 水印检测与分析工具test_removal.py - 演示水印移除的测试脚本python analyze_watermark.py --input text.txt --verbose
# 使用改写(最有效,需要模型)
python analysis/reverse_synthid.py --input text.txt --output clean.txt --method paraphrase
# 使用扰动(无需模型)
python analysis/reverse_synthid.py --input text.txt --output clean.txt --method perturb
# 使用组合攻击
python reverse_synthid.py --input text.txt --output clean.txt --method combined
python analysis/test_removal.py
水印依赖于:
当你改写文本时:
对于长度为N、水印强度为W的文本:
1 - (1-r)^(ngram_len)N*r 个n-gram边界此代码仅供教育和研究目的。它展示了水印方案中的漏洞,以帮助开发更健壮的方法。