Spaces:
Paused
Paused
Commit ·
d0eb7f5
1
Parent(s): fed0a26
Docs for the Space
Browse filesFiles changed (2)
- app.py+93-13
- requirements.txt+1-1
144 unmodified lines14514614714814915015117 unmodified lines16917017117217317417517617717810 unmodified lines18919019119219319419519619719819920020111 unmodified lines2132142152162172182195 unmodified lines2252262272282292302313 unmodified lines23523623723823924024153 unmodified lines2952962972982993003013 unmodified lines305306307308309310311144 unmodified linesdetectors, and the [SynthID Text documentaiton][raitk-synthid] for more onhow this technology works.## Getting startedPractically speaking, SynthID Text is a logits processor, applied to yourmodel's generation pipeline after [Top-K and Top-P][cloud-parameter-values],17 unmodified lines`.generate()`, as shown in the snippet below.```pythonfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom transformers.generation import SynthIDTextWatermarkingConfig# Standard model and toeknizer initializationtokenizer = AutoTokenizer.from_pretrained('repo/id')model = AutoModelForCausalLM.from_pretrained('repo/id')10 unmodified lineswatermarked_text = tokenizer.batch_decode(output_sequences)```Enter up to three prompts then click the generate button. After you click,[Gemma 2B][gemma] will generate a watermarked and non-watermarked repsonsesfor each non-empty prompt.[cloud-parameter-values]: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/adjust-parameter-values[gemma]: https://huggingface.co/google/gemma-2b[raitk-synthid]: /responsible/docs/safeguards/synthid[synthid]: https://deepmind.google/technologies/synthid/[synthid-hf-config]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/configuration_utils.py[synthid-hf-detector]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/watermarking.py11 unmodified lineswith gr.Column(visible=False) as generations_col:gr.Markdown('''# SynthID: Tool''')generations_grp = gr.CheckboxGroup(5 unmodified lineswith gr.Column(visible=False) as detections_col:gr.Markdown('''# SynthID: Tool''')revealed_grp = gr.CheckboxGroup(3 unmodified lines'marked as correct or incorrect in the text.'),)detect_btn = gr.Button('Detect', visible=False)def generate(*prompts):standard, standard_detector = generate_outputs(prompts=prompts)53 unmodified linesreveal_btn: gr.Button(visible=False),detections_col: gr.Column(visible=True),revealed_grp: gr.CheckboxGroup(choices=choices, value=value),detect_btn: gr.Button(visible=True),}reveal_btn.click(3 unmodified linesreveal_btn,detections_col,revealed_grp,detect_btn],)144 unmodified lines14514614714814915015117 unmodified lines16917017117217317417517617717817918018110 unmodified lines19219319419519619719819920020120220320420520620720820921021111 unmodified lines2232242252262272282292302312322332342352362372382392402412422435 unmodified lines2492502512522532542552562572582592602612622632642652662672682692702713 unmodified lines27527627727827928028128228328428528628728828929029129229329429529629729829930053 unmodified lines3543553563573583593603 unmodified lines364365366367368369370371372373374375376377378379380381382383384385386387388389390391144 unmodified linesdetectors, and the [SynthID Text documentaiton][raitk-synthid] for more onhow this technology works.## Applying a watermarkPractically speaking, SynthID Text is a logits processor, applied to yourmodel's generation pipeline after [Top-K and Top-P][cloud-parameter-values],17 unmodified lines`.generate()`, as shown in the snippet below.```pythonfrom transformers import (AutoModelForCausalLM,AutoTokenizer,SynthIDTextWatermarkingConfig,)# Standard model and tokenizer initializationtokenizer = AutoTokenizer.from_pretrained('repo/id')model = AutoModelForCausalLM.from_pretrained('repo/id')10 unmodified lineswatermarked_text = tokenizer.batch_decode(output_sequences)```## Try it yourself.Lets use [Gemma 2B IT][gemma] to help you understand how watermarking works.Using the text boxes below enter up to three prompts then click the generatebutton. Some examples are provided to help get you started, but they arefully editable.Gemma will then generate watermarked and non-watermarked repsonses for eachnon-empty prompt you provided.[cloud-parameter-values]: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/adjust-parameter-values[gemma]: https://huggingface.co/google/gemma-2b[raitk-synthid]: https://ai.google.dev/responsible/docs/safeguards/synthid-text[synthid]: https://deepmind.google/technologies/synthid/[synthid-hf-config]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/configuration_utils.py[synthid-hf-detector]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/watermarking.py11 unmodified lineswith gr.Column(visible=False) as generations_col:gr.Markdown('''## Human recognition of watermarked textThe primary goal of SynthID Text is to apply a watermark to generated textwihtout affecting generation quality. Another way to think about this isthat generated text that carries a watermark should be imperceptible toyou, the reader, but easily perceived by a watermark detector.The responses from Gemma are shown below. Use the checkboxes to mark whichresponses you think are the watermarked, then click the "reveal" button tosee the true values.The [research paper][synthid-nature] has an in-depth study examining humanperception of watermared versus non-watermarked text.[synthid-nature]: https://www.nature.com/articles/s41586-024-08025-4''')generations_grp = gr.CheckboxGroup(5 unmodified lineswith gr.Column(visible=False) as detections_col:gr.Markdown('''## Detecting watermarked textThe only way to properly detect watermarked text is with a trainedclassifier. This Space uses a pre-trained classifier hosted on Huggin FaceHub. For production uses you will need to train your own classifiers torecognize your watermarks. A [Bayesian detector][synthid-hf-detector] isprovided in Transformers, along with an[end-to-end example][synthid-hf-detector-e2e] of how to train one of thesedetectors.You can see how your guesses compared to the actaul results below. Asabove, the responses are displayed in checkboxes. If the box is checked,then the text carries a watermark. Your correct guesses are annotated withthe "Correct" prefix.[synthid-hf-detector]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/watermarking.py[synthid-hf-detector-e2e]: https://github.com/huggingface/transformers/blob/v4.46.0/examples/research_projects/synthid_text/detector_bayesian.py''')revealed_grp = gr.CheckboxGroup(3 unmodified lines'marked as correct or incorrect in the text.'),)gr.Markdown('''## LimitationsSynthID Text watermarks are robust to some transformations, such ascropping pieces of text, modifying a few words, or mild paraphrasing, butthis method does have limitations.- Watermark application is less effective on factual responses, as thereis less opportunity to augment generation without decreasing accuracy.- Detector confidence scores can be greatly reduced when an AI-generatedtext is thoroughly rewritten, or translated to another language.SynthID Text is not built to directly stop motivated adversaries fromcausing harm. However, it can make it harder to use AI-generated contentfor malicious purposes, and it can be combined with other approaches togive better coverage across content types and platforms.''')reset_btn = gr.Button('Reset', visible=False)def generate(*prompts):standard, standard_detector = generate_outputs(prompts=prompts)53 unmodified linesreveal_btn: gr.Button(visible=False),detections_col: gr.Column(visible=True),revealed_grp: gr.CheckboxGroup(choices=choices, value=value),reset_btn: gr.Button(visible=True),}reveal_btn.click(3 unmodified linesreveal_btn,detections_col,revealed_grp,reset_btn],)def reset():return {generations_col: gr.Column(visible=False),detections_col: gr.Column(visible=False),revealed_grp: gr.CheckboxGroup(visible=False),reset_btn: gr.Button(visible=False),generate_btn: gr.Button(visible=True),}reset_btn.click(reset,inputs=[],outputs=[generations_col,detections_col,revealed_grp,reset_btn,generate_btn,],)
123456gradiospacestransformers>=4.46.0--extra-index-url https://download.pytorch.org/whl/cu113torch123456gradiospacestransformers @ git+https://github.com/sumedhghaisas2/transformers_private--extra-index-url https://download.pytorch.org/whl/cu113torch
app.py+93-13
requirements.txt+1-1