code: 
  
  
  '''simple docstring'''
  
  
  
  
  
  import json
  import os
  import re
  import unicodedata
  from typing import Optional, Tuple
  
  from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace
  from ...utils import logging
  
  
  SCREAMING_SNAKE_CASE__				:    Dict         =    logging.get_logger(__name__)
  
  SCREAMING_SNAKE_CASE__				:    Optional[int]         =    {
      '''vocab_file''': '''vocab.json''',
      '''merges_file''': '''merges.txt''',
  }
  
  SCREAMING_SNAKE_CASE__				:    str         =    {
      '''vocab_file''': {'''openai-gpt''': '''https://huggingface.co/openai-gpt/resolve/main/vocab.json'''},
      '''merges_file''': {'''openai-gpt''': '''https://huggingface.co/openai-gpt/resolve/main/merges.txt'''},
  }
  
  SCREAMING_SNAKE_CASE__				:    List[Any]         =    {
      '''openai-gpt''': 512,
  }
  
  
  
  
  def       a__						(		snake_case__				:       Optional[int]    ):
    _UpperCAmelCase		:       int       =    text.strip()
    if not text:
      return []
    _UpperCAmelCase		:       int       =    text.split()
    return tokens
  
  
  class 		_SCREAMING_SNAKE_CASE    (	UpperCamelCase_    ):
  
  
       def __init__(     self       ,				A_=True       ,				A_=None       ,				A_=True       ,				A_=None       ,				A_=True       ,				):
         if never_split is None:
           _UpperCAmelCase		:       int       =    []
         _UpperCAmelCase		:       Optional[Any]       =    do_lower_case
         _UpperCAmelCase		:       int       =    set(__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       Dict       =    tokenize_chinese_chars
         _UpperCAmelCase		:       List[str]       =    strip_accents
         _UpperCAmelCase		:       Union[str, Any]       =    do_split_on_punc
  
  
       def 					__snake_case(     self       ,				A_       ,				A_=None						):
         _UpperCAmelCase		:       Union[str, Any]       =    self.never_split.union(set(__SCREAMING_SNAKE_CASE						)						) if never_split else self.never_split
         _UpperCAmelCase		:       Union[str, Any]       =    self._clean_text(__SCREAMING_SNAKE_CASE						)
  
         # This was added on November 1st, 2018 for the multilingual and Chinese
         # models. This is also applied to the English models now, but it doesn't
         # matter since the English models were not trained on any Chinese data
         # and generally don't have any Chinese data in them (there are Chinese
         # characters in the vocabulary because Wikipedia does have some Chinese
         # words in the English Wikipedia.).
         if self.tokenize_chinese_chars:
           _UpperCAmelCase		:       Optional[int]       =    self._tokenize_chinese_chars(__SCREAMING_SNAKE_CASE						)
         # prevents treating the same character with different unicode codepoints as different characters
         _UpperCAmelCase		:       List[Any]       =    unicodedata.normalize("""NFC"""       ,				__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       List[Any]       =    whitespace_tokenize(__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       List[Any]       =    []
         for token in orig_tokens:
           if token not in never_split:
             if self.do_lower_case:
               _UpperCAmelCase		:       Tuple       =    token.lower()
               if self.strip_accents is not False:
                 _UpperCAmelCase		:       Union[str, Any]       =    self._run_strip_accents(__SCREAMING_SNAKE_CASE						)
             elif self.strip_accents:
               _UpperCAmelCase		:       Tuple       =    self._run_strip_accents(__SCREAMING_SNAKE_CASE						)
           split_tokens.extend(self._run_split_on_punc(__SCREAMING_SNAKE_CASE       ,				__SCREAMING_SNAKE_CASE						)						)
  
         _UpperCAmelCase		:       Optional[int]       =    whitespace_tokenize(""" """.join(__SCREAMING_SNAKE_CASE						)						)
         return output_tokens
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       int       =    unicodedata.normalize("""NFD"""       ,				__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       List[Any]       =    []
         for char in text:
           _UpperCAmelCase		:       int       =    unicodedata.category(__SCREAMING_SNAKE_CASE						)
           if cat == "Mn":
             continue
           output.append(__SCREAMING_SNAKE_CASE						)
         return "".join(__SCREAMING_SNAKE_CASE						)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_=None						):
         if not self.do_split_on_punc or (never_split is not None and text in never_split):
           return [text]
         _UpperCAmelCase		:       Optional[int]       =    list(__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       Optional[Any]       =    0
         _UpperCAmelCase		:       Dict       =    True
         _UpperCAmelCase		:       int       =    []
         while i < len(__SCREAMING_SNAKE_CASE						):
           _UpperCAmelCase		:       Optional[int]       =    chars[i]
           if _is_punctuation(__SCREAMING_SNAKE_CASE						):
             output.append([char]						)
             _UpperCAmelCase		:       str       =    True
           else:
             if start_new_word:
               output.append([]						)
             _UpperCAmelCase		:       List[str]       =    False
             output[-1].append(__SCREAMING_SNAKE_CASE						)
           i += 1
  
         return ["".join(__SCREAMING_SNAKE_CASE						) for x in output]
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       Union[str, Any]       =    []
         for char in text:
           _UpperCAmelCase		:       str       =    ord(__SCREAMING_SNAKE_CASE						)
           if self._is_chinese_char(__SCREAMING_SNAKE_CASE						):
             output.append(""" """						)
             output.append(__SCREAMING_SNAKE_CASE						)
             output.append(""" """						)
           else:
             output.append(__SCREAMING_SNAKE_CASE						)
         return "".join(__SCREAMING_SNAKE_CASE						)
  
  
       def 					__snake_case(     self       ,				A_						):
         # This defines a "chinese character" as anything in the CJK Unicode block:
         #   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
         #
         # Note that the CJK Unicode block is NOT all Japanese and Korean characters,
         # despite its name. The modern Korean Hangul alphabet is a different block,
         # as is Japanese Hiragana and Katakana. Those alphabets are used to write
         # space-separated words, so they are not treated specially and handled
         # like the all of the other languages.
         if (
             (cp >= 0X4E_00 and cp <= 0X9F_FF)
             or (cp >= 0X34_00 and cp <= 0X4D_BF)  #
             or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF)  #
             or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F)  #
             or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F)  #
             or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF)  #
             or (cp >= 0XF9_00 and cp <= 0XFA_FF)
             or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F)  #
         ):  #
           return True
  
         return False
  
  
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       Tuple       =    []
         for char in text:
           _UpperCAmelCase		:       List[str]       =    ord(__SCREAMING_SNAKE_CASE						)
           if cp == 0 or cp == 0XFF_FD or _is_control(__SCREAMING_SNAKE_CASE						):
             continue
           if _is_whitespace(__SCREAMING_SNAKE_CASE						):
             output.append(""" """						)
           else:
             output.append(__SCREAMING_SNAKE_CASE						)
         return "".join(__SCREAMING_SNAKE_CASE						)
  
  
  
  
  def       a__						(		snake_case__				:       Union[str, Any]    ):
    _UpperCAmelCase		:       str       =    set()
    _UpperCAmelCase		:       List[Any]       =    word[0]
    for char in word[1:]:
      pairs.add((prev_char, char)    )
      _UpperCAmelCase		:       Optional[Any]       =    char
    return pairs
  
  
  
  
  def       a__						(		snake_case__				:       Tuple    ):
    _UpperCAmelCase		:       Optional[Any]       =    text.replace("""—"""	,      """-"""    )
    _UpperCAmelCase		:       Optional[Any]       =    text.replace("""–"""	,      """-"""    )
    _UpperCAmelCase		:       Dict       =    text.replace("""―"""	,      """-"""    )
    _UpperCAmelCase		:       List[Any]       =    text.replace("""…"""	,      """..."""    )
    _UpperCAmelCase		:       Optional[int]       =    text.replace("""´"""	,      """\'"""    )
    _UpperCAmelCase		:       str       =    re.sub(r"""(-+|~+|!+|\"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)"""	,      r""" \1 """	,      snake_case__    )
    _UpperCAmelCase		:       List[str]       =    re.sub(r"""\s*\n\s*"""	,      """ \n """	,      snake_case__    )
    _UpperCAmelCase		:       Optional[Any]       =    re.sub(r"""[^\S\n]+"""	,      """ """	,      snake_case__    )
    return text.strip()
  
  
  
  
  
  class 		_SCREAMING_SNAKE_CASE    (	UpperCamelCase_    ):
       __SCREAMING_SNAKE_CASE    				=						VOCAB_FILES_NAMES
       __SCREAMING_SNAKE_CASE    				=						PRETRAINED_VOCAB_FILES_MAP
       __SCREAMING_SNAKE_CASE    				=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       __SCREAMING_SNAKE_CASE    				=						["""input_ids""", """attention_mask"""]
  
  
       def __init__(     self       ,				A_       ,				A_       ,				A_="<unk>"       ,				**A_						):
         super().__init__(unk_token=__SCREAMING_SNAKE_CASE       ,				**__SCREAMING_SNAKE_CASE						)
  
         try:
           import ftfy
           from spacy.lang.en import English
  
           _UpperCAmelCase		:       Optional[Any]       =    English()
           _UpperCAmelCase		:       Any       =    _nlp.tokenizer
           _UpperCAmelCase		:       Optional[Any]       =    ftfy.fix_text
         except ImportError:
           logger.warning("""ftfy or spacy is not installed using BERT BasicTokenizer instead of SpaCy & ftfy."""						)
           _UpperCAmelCase		:       Union[str, Any]       =    BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE						)
           _UpperCAmelCase		:       str       =    None
  
         with open(__SCREAMING_SNAKE_CASE       ,				encoding="""utf-8"""						) as vocab_handle:
           _UpperCAmelCase		:       str       =    json.load(__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       Optional[int]       =    {v: k for k, v in self.encoder.items()}
         with open(__SCREAMING_SNAKE_CASE       ,				encoding="""utf-8"""						) as merges_handle:
           _UpperCAmelCase		:       Union[str, Any]       =    merges_handle.read().split("""\n"""						)[1:-1]
         _UpperCAmelCase		:       Optional[Any]       =    [tuple(merge.split()						) for merge in merges]
         _UpperCAmelCase		:       List[str]       =    dict(zip(__SCREAMING_SNAKE_CASE       ,				range(len(__SCREAMING_SNAKE_CASE						)						)						)						)
         _UpperCAmelCase		:       Tuple       =    {}
  
  
       @property
       def 					__snake_case(     self						):
         return True
  
  
       @property
       def 					__snake_case(     self						):
         return len(self.encoder						)
  
  
       def 					__snake_case(     self						):
         return dict(self.encoder       ,				**self.added_tokens_encoder						)
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       str       =    tuple(token[:-1]						) + (token[-1] + """</w>""",)
         if token in self.cache:
           return self.cache[token]
         _UpperCAmelCase		:       Tuple       =    get_pairs(__SCREAMING_SNAKE_CASE						)
  
         if not pairs:
           return token + "</w>"
  
         while True:
           _UpperCAmelCase		:       Optional[int]       =    min(__SCREAMING_SNAKE_CASE       ,				key=lambda A_						: self.bpe_ranks.get(__SCREAMING_SNAKE_CASE       ,				float("""inf"""						)						)						)
           if bigram not in self.bpe_ranks:
             break
           _UpperCAmelCase,_UpperCAmelCase		:       str       =    bigram
           _UpperCAmelCase		:       List[str]       =    []
           _UpperCAmelCase		:       str       =    0
           while i < len(__SCREAMING_SNAKE_CASE						):
             try:
               _UpperCAmelCase		:       Any       =    word.index(__SCREAMING_SNAKE_CASE       ,				__SCREAMING_SNAKE_CASE						)
             except ValueError:
               new_word.extend(word[i:]						)
               break
             else:
               new_word.extend(word[i:j]						)
               _UpperCAmelCase		:       Dict       =    j
  
             if word[i] == first and i < len(__SCREAMING_SNAKE_CASE						) - 1 and word[i + 1] == second:
               new_word.append(first + second						)
               i += 2
             else:
               new_word.append(word[i]						)
               i += 1
           _UpperCAmelCase		:       str       =    tuple(__SCREAMING_SNAKE_CASE						)
           _UpperCAmelCase		:       Any       =    new_word
           if len(__SCREAMING_SNAKE_CASE						) == 1:
             break
           else:
             _UpperCAmelCase		:       List[str]       =    get_pairs(__SCREAMING_SNAKE_CASE						)
         _UpperCAmelCase		:       List[Any]       =    """ """.join(__SCREAMING_SNAKE_CASE						)
         if word == "\n  </w>":
           _UpperCAmelCase		:       List[str]       =    """\n</w>"""
         _UpperCAmelCase		:       Optional[Any]       =    word
         return word
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       List[Any]       =    []
         if self.fix_text is None:
           # Using BERT's BasicTokenizer
           _UpperCAmelCase		:       List[str]       =    self.nlp.tokenize(__SCREAMING_SNAKE_CASE						)
           for token in text:
             split_tokens.extend(list(self.bpe(__SCREAMING_SNAKE_CASE						).split(""" """						)						)						)
         else:
           # Using SpaCy & ftfy (original tokenization process of OpenAI GPT)
           _UpperCAmelCase		:       List[Any]       =    self.nlp(text_standardize(self.fix_text(__SCREAMING_SNAKE_CASE						)						)						)
           for token in text:
             split_tokens.extend(list(self.bpe(token.text.lower()						).split(""" """						)						)						)
         return split_tokens
  
  
       def 					__snake_case(     self       ,				A_						):
         return self.encoder.get(__SCREAMING_SNAKE_CASE       ,				self.encoder.get(self.unk_token						)						)
  
  
       def 					__snake_case(     self       ,				A_						):
         return self.decoder.get(__SCREAMING_SNAKE_CASE       ,				self.unk_token						)
  
  
       def 					__snake_case(     self       ,				A_						):
         _UpperCAmelCase		:       Tuple       =    """""".join(__SCREAMING_SNAKE_CASE						).replace("""</w>"""       ,				""" """						).strip()
         return out_string
  
  
  
  
       def 					__snake_case(     self       ,				A_       ,				A_ = None						):
         if not os.path.isdir(__SCREAMING_SNAKE_CASE						):
           logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''						)
           return
         _UpperCAmelCase		:       int       =    os.path.join(
             __SCREAMING_SNAKE_CASE       ,				(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]						)
         _UpperCAmelCase		:       str       =    os.path.join(
             __SCREAMING_SNAKE_CASE       ,				(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]						)
  
         with open(__SCREAMING_SNAKE_CASE       ,				"""w"""       ,				encoding="""utf-8"""						) as f:
           f.write(json.dumps(self.encoder       ,				indent=2       ,				sort_keys=__SCREAMING_SNAKE_CASE       ,				ensure_ascii=__SCREAMING_SNAKE_CASE						) + """\n"""						)
  
         _UpperCAmelCase		:       List[Any]       =    0
         with open(__SCREAMING_SNAKE_CASE       ,				"""w"""       ,				encoding="""utf-8"""						) as writer:
           writer.write("""#version: 0.2\n"""						)
           for bpe_tokens, token_index in sorted(self.bpe_ranks.items()       ,				key=lambda A_						: kv[1]						):
             if index != token_index:
               logger.warning(
                   F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'''
                   """ Please check that the tokenizer is not corrupted!"""						)
               _UpperCAmelCase		:       List[Any]       =    token_index
             writer.write(""" """.join(__SCREAMING_SNAKE_CASE						) + """\n"""						)
             index += 1
  
         return vocab_file, merge_file
  
  
  
  
  
  
style_context: 
  
  
  from dataclasses import dataclass
  from enum import Enum
  from typing import List, Optional, Union
  
  import numpy as np
  import PIL
  from PIL import Image
  
  from ...utils import BaseOutput, is_torch_available, is_transformers_available
  
  
  @dataclass
  class 		_SCREAMING_SNAKE_CASE    (	A    ):
       __SCREAMING_SNAKE_CASE    				=						{
           '''sld_warmup_steps''': 1_5,
           '''sld_guidance_scale''': 2_0,
           '''sld_threshold''': 0.0,
           '''sld_momentum_scale''': 0.0,
           '''sld_mom_beta''': 0.0,
       }
       __SCREAMING_SNAKE_CASE    				=						{
           '''sld_warmup_steps''': 1_0,
           '''sld_guidance_scale''': 1_0_0_0,
           '''sld_threshold''': 0.01,
           '''sld_momentum_scale''': 0.3,
           '''sld_mom_beta''': 0.4,
       }
       __SCREAMING_SNAKE_CASE    				=						{
           '''sld_warmup_steps''': 7,
           '''sld_guidance_scale''': 2_0_0_0,
           '''sld_threshold''': 0.025,
           '''sld_momentum_scale''': 0.5,
           '''sld_mom_beta''': 0.7,
       }
       __SCREAMING_SNAKE_CASE    				=						{
           '''sld_warmup_steps''': 0,
           '''sld_guidance_scale''': 5_0_0_0,
           '''sld_threshold''': 1.0,
           '''sld_momentum_scale''': 0.5,
           '''sld_mom_beta''': 0.7,
       }
  
  
  @dataclass
  class 		_SCREAMING_SNAKE_CASE    (	A    ):
       __SCREAMING_SNAKE_CASE    				=						42
       __SCREAMING_SNAKE_CASE    				=						42
       __SCREAMING_SNAKE_CASE    				=						42
       __SCREAMING_SNAKE_CASE    				=						42
  
  
  if is_transformers_available() and is_torch_available():
   from .pipeline_stable_diffusion_safe import StableDiffusionPipelineSafe
   from .safety_checker import SafeStableDiffusionSafetyChecker
  
  
  
  
  
  