Spaces:
Paused
Paused
Commit ·
2b6fecd
1
Parent(s): dcd31e5
Update to Transformers v4.46.0
Browse filesFiles changed (2)
- app.py+33-21
- requirements.txt+1-1
1234567891011122 unmodified lines151617181920212 unmodified lines2425262728293034 unmodified lines6566676869707172737475767778798081828384858687888990912 unmodified lines9495969798991001011021031041054 unmodified lines11011111211311411511611745 unmodified lines163164165166167168169from collections.abc import Sequenceimport jsonimport randomfrom typing import Optional, Tupleimport gradio as grimport spacesimport torchimport transformers# If the watewrmark is not detected, consider the use case. Could be because of# the nature of the task (e.g., fatcual responses are lower entropy) or it could2 unmodified lines_MODEL_IDENTIFIER = 'google/gemma-2b-it'_DETECTOR_IDENTIFIER = 'google/synthid-spaces-demo-detector'_PROMPTS: tuple[str] = ('Write an essay about my pets, a cat named Mika and a dog named Cleo.','Tell me everything you can about Portugal.','What is Hugging Face?',2 unmodified lines_TORCH_DEVICE = (torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu"))_ANSWERS: list[tuple[str, str]] = []_WATERMARK_CONFIG_DICT = dict(ngram_len=5,34 unmodified linescontext_history_size=1024,)_WATERMARK_CONFIG = transformers.generation.SynthIDTextWatermarkingConfig(**_WATERMARK_CONFIG_DICT)tokenizer = transformers.AutoTokenizer.from_pretrained(_MODEL_IDENTIFIER, padding_side="left")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.SynthIDTextWatermarkDetector(detector_module=detector_module,logits_processor=logits_processor,tokenizer=tokenizer,2 unmodified lines@spaces.GPUdef generate_outputs(prompts: Sequence[str],watermarking_config: Optional[transformers.generation.SynthIDTextWatermarkingConfig] = None,) -> Tuple[Sequence[str], torch.Tensor]:tokenized_prompts = tokenizer(prompts, return_tensors='pt', padding="longest").to(_TORCH_DEVICE)input_length = tokenized_prompts.input_ids.shape[1]output_sequences = model.generate(**tokenized_prompts,4 unmodified lines)output_sequences = output_sequences[:, input_length:]detections = detector(output_sequences)print(detections)return (tokenizer.batch_decode(output_sequences, skip_special_tokens=True), detections)with gr.Blocks() as demo:45 unmodified lines```json{"ngram_len": 5,"keys": [654, 400, 836, 123, 340, 443, 597, 160, 57,29, 590, 639, 13,715, 468, 990, 966, 226, 324, 585, 118, 504, 421, 521, 129, 669, 732, 225, 90, 960],"sampling_table_size": 65536,"sampling_table_seed": 0,"context_history_size": 10241234567891011121314151617182 unmodified lines212223242526272 unmodified lines3031323334353634 unmodified lines7172737475767778798081828384858687888990919293949596972 unmodified lines1001011021031041051061071081091101114 unmodified lines11611711811912012112212312412545 unmodified lines171172173174175176177178179180181from collections.abc import Sequenceimport randomfrom typing import Optional, List, Tupleimport gradio as grimport spacesimport torchfrom transformers import (AutoModelForCausalLM,AutoTokenizer,BayesianDetectorModel,SynthIDTextWatermarkingConfig,SynthIDTextWatermarkDetector,SynthIDTextWatermarkLogitsProcessor,)# If the watewrmark is not detected, consider the use case. Could be because of# the nature of the task (e.g., fatcual responses are lower entropy) or it could2 unmodified lines_MODEL_IDENTIFIER = 'google/gemma-2b-it'_DETECTOR_IDENTIFIER = 'google/synthid-spaces-demo-detector'_PROMPTS: Tuple[str] = ('Write an essay about my pets, a cat named Mika and a dog named Cleo.','Tell me everything you can about Portugal.','What is Hugging Face?',2 unmodified lines_TORCH_DEVICE = (torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu"))_ANSWERS: List[Tuple[str, str]] = []_WATERMARK_CONFIG_DICT = dict(ngram_len=5,34 unmodified linescontext_history_size=1024,)_WATERMARK_CONFIG = SynthIDTextWatermarkingConfig(**_WATERMARK_CONFIG_DICT)tokenizer = AutoTokenizer.from_pretrained(_MODEL_IDENTIFIER, padding_side="left")tokenizer.pad_token_id = tokenizer.eos_token_idmodel = AutoModelForCausalLM.from_pretrained(_MODEL_IDENTIFIER)model.to(_TORCH_DEVICE)logits_processor = SynthIDTextWatermarkLogitsProcessor(**_WATERMARK_CONFIG_DICT,device=_TORCH_DEVICE,)detector_module = BayesianDetectorModel.from_pretrained(_DETECTOR_IDENTIFIER)detector_module.to(_TORCH_DEVICE)detector = SynthIDTextWatermarkDetector(detector_module=detector_module,logits_processor=logits_processor,tokenizer=tokenizer,2 unmodified lines@spaces.GPUdef generate_outputs(prompts: Sequence[str],watermarking_config: Optional[SynthIDTextWatermarkingConfig] = None,) -> Tuple[Sequence[str], torch.Tensor]:tokenized_prompts = tokenizer(prompts, return_tensors='pt', padding="longest").to(_TORCH_DEVICE)input_length = tokenized_prompts.input_ids.shape[1]output_sequences = model.generate(**tokenized_prompts,4 unmodified lines)output_sequences = output_sequences[:, input_length:]detections = detector(output_sequences)return (tokenizer.batch_decode(output_sequences, skip_special_tokens=True),detections)with gr.Blocks() as demo:45 unmodified lines```json{"ngram_len": 5,"keys": [654, 400, 836, 123, 340, 443, 597, 160, 57, 29,590, 639, 13, 715, 468, 990, 966, 226, 324, 585,118, 504, 421, 521, 129, 669, 732, 225, 90, 960],"sampling_table_size": 65536,"sampling_table_seed": 0,"context_history_size": 1024
123456gradiospacestransformers @ git+https://github.com/sumedhghaisas2/transformers_private--extra-index-url https://download.pytorch.org/whl/cu113torch123456gradiospacestransformers>=4.46.0--extra-index-url https://download.pytorch.org/whl/cu113torch
app.py+33-21
requirements.txt+1-1