Spaces:
Paused
Paused
Commit ·
80c639a
1
Parent(s): 2a04008
Committing in broken state for sharing with HF
Browse filesFiles changed (2)
- app.py+77-6
- requirements.txt+1-1
10 unmodified lines111213141516177 unmodified lines2526272829303132 unmodified lines64656667686970717273747510 unmodified lines868788899091929394951 unmodified line9798991001011021 unmodified line10410510610710810950 unmodified lines16016116216316416516616716816910 unmodified lines# the nature of the task (e.g., fatcual responses are lower entropy) or it could# be another_MODEL_IDENTIFIER = 'hf-internal-testing/tiny-random-gpt2'_PROMPTS: tuple[str] = ('prompt 1',7 unmodified linestorch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu"))_WATERMARK_CONFIG = transformers.generation.SynthIDTextWatermarkingConfig(ngram_len=5,keys=[654,32 unmodified linescontext_history_size=1024,)tokenizer = transformers.AutoTokenizer.from_pretrained(_MODEL_IDENTIFIER)tokenizer.pad_token_id = tokenizer.eos_token_idmodel = transformers.AutoModelForCausalLM.from_pretrained(_MODEL_IDENTIFIER)model.to(_TORCH_DEVICE)@spaces.GPUdef generate_outputs(10 unmodified linesmax_length=500,top_k=40,)return tokenizer.batch_decode(output_sequences)with gr.Blocks() as demo:prompt_inputs = [gr.Textbox(value=prompt, lines=4, label='Prompt')for prompt in _PROMPTS1 unmodified linegenerate_btn = gr.Button('Generate')with gr.Column(visible=False) as generations_col:generations_grp = gr.CheckboxGroup(label='All generations, in random order',info='Select the generations you think are watermarked!',1 unmodified linereveal_btn = gr.Button('Reveal', visible=False)with gr.Column(visible=False) as detections_col:revealed_grp = gr.CheckboxGroup(label='Ground truth for all generations',info=(50 unmodified linesvalue.append(choice)return {reveal_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(10 unmodified lines11121314151617187 unmodified lines2627282930313232 unmodified lines656667686970717273747576777879808182838485868788899091929394959610 unmodified lines1071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561 unmodified line1581591601611621631641651661671681 unmodified line17017117217317417517617717817918050 unmodified lines23123223323423523623723823924010 unmodified lines# the nature of the task (e.g., fatcual responses are lower entropy) or it could# be another_MODEL_IDENTIFIER = 'google/gemma-2b'_DETECTOR_IDENTIFIER = 'gg-hf/detector_2b_1.0_demo'_PROMPTS: tuple[str] = ('prompt 1',7 unmodified linestorch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu"))_WATERMARK_CONFIG_DICT = dict(ngram_len=5,keys=[654,32 unmodified linescontext_history_size=1024,)_WATERMARK_CONFIG = transformers.generation.SynthIDTextWatermarkingConfig(**_WATERMARK_CONFIG_DICT)tokenizer = transformers.AutoTokenizer.from_pretrained(_MODEL_IDENTIFIER)tokenizer.pad_token_id = tokenizer.eos_token_idmodel = transformers.AutoModelForCausalLM.from_pretrained(_MODEL_IDENTIFIER)model.to(_TORCH_DEVICE)logits_processor = transformers.generation.SynthIDTextWatermarkLogitsProcessor(**_WATERMARK_CONFIG_DICT,device=_TORCH_DEVICE,)detector_module = transformers.generation.BayesianDetectorModel.from_pretrained(_DETECTOR_IDENTIFIER,)detector_module.to(_TORCH_DEVICE)detector = transformers.generation.watermarking.BayesianDetectorModel(detector_module=detector_module,logits_processor=logits_processor,tokenizer=tokenizer,)@spaces.GPUdef generate_outputs(10 unmodified linesmax_length=500,top_k=40,)detections = detector(output_sequences)print(detections)return tokenizer.batch_decode(output_sequences)with gr.Blocks() as demo:gr.Markdown('''# Using SynthID Text in your Genreative AI projects[SynthID][synthid] is a Google DeepMind technology that watermarks andidentifies AI-generated content by embedding digital watermarks directlyinto AI-generated images, audio, text or video.SynthID Text is an open source implementation of this technology availablein Hugging Face Transformers that has two major components:* A [logits processor][synthid-hf-logits-processor] that is[configured][synthid-hf-config] on a per-model basis and activated whencalling `.generate()`; and* A [detector][synthid-hf-detector] trained to recognized watermarked textgenerated by a specific model with a specific configuraiton.This Space demonstrates:1. How to use SynthID Text to apply a watermark to text generated by yourmodel; and1. How to indetify that text using a ready-made detector.Note that this detector is trained specifically fore this demonstration. Youshould maintain a specific watermarking configuration for every model youuse and protect that configuration as you would any other secret. See the[end-to-end guide][synthid-hf-detector-e2e] for more on training your owndetectors, and the [SynthID Text documentaiton][raitk-synthid] for more onhow this technology works.[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.py[synthid-hf-detector-e2e]: https://github.com/huggingface/transformers/blob/v4.46.0/examples/research_projects/synthid_text/detector_bayesian.py[synthid-hf-logits-processor]: https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/generation/logits_process.py''')prompt_inputs = [gr.Textbox(value=prompt, lines=4, label='Prompt')for prompt in _PROMPTS1 unmodified linegenerate_btn = gr.Button('Generate')with gr.Column(visible=False) as generations_col:gr.Markdown('''# SynthID: Tool''')generations_grp = gr.CheckboxGroup(label='All generations, in random order',info='Select the generations you think are watermarked!',1 unmodified linereveal_btn = gr.Button('Reveal', visible=False)with gr.Column(visible=False) as detections_col:gr.Markdown('''# SynthID: Tool''')revealed_grp = gr.CheckboxGroup(label='Ground truth for all generations',info=(50 unmodified linesvalue.append(choice)return {reveal_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(
123456gradiospacestransformers @ git+https://github.com/sumedhghaisas2/transformers_private@synthid_text--extra-index-url https://download.pytorch.org/whl/cu113torch123456gradiospacestransformers>=4.46.0--extra-index-url https://download.pytorch.org/whl/cu113torch
app.py+77-6
requirements.txt+1-1