





"""simple docstring"""
import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
       if is_torch_available():
              import torch
       if is_tf_available():
              import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


__UpperCamelCase    :	Optional[int]						=     logging.get_logger(__name__)

__UpperCamelCase    :	List[str]						=     {
    '''vocab_file''': '''vocab.json''',
    '''merges_file''': '''merges.txt''',
}

__UpperCamelCase    :	Optional[int]						=     {
    '''vocab_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt''',
    },
}

__UpperCamelCase    :	Union[str, Any]						=     {
    '''Salesforce/codegen-350M-mono''': 2048,
}

@lru_cache()
def       _SCREAMING_SNAKE_CASE ():
 lowerCAmelCase													=   (
     list(range(ord('!' )     ,    ord('~' ) + 1 ) ) + list(range(ord('¡' )     ,    ord('¬' ) + 1 ) ) + list(range(ord('®' )     ,    ord('ÿ' ) + 1 ) )
 )
 lowerCAmelCase													=   bs[:]
 lowerCAmelCase													=   0
 for b in range(2**8 ):
  if b not in bs:
   bs.append(__snake_case )
   cs.append(2**8 + n )
   n += 1
 lowerCAmelCase													=   [chr(__snake_case ) for n in cs]
 return dict(zip(__snake_case     ,    __snake_case ) )

def       _SCREAMING_SNAKE_CASE (_UpperCAmelCase				:						Dict ):
 lowerCAmelCase													=   set()
 lowerCAmelCase													=   word[0]
 for char in word[1:]:
  pairs.add((prev_char, char) )
  lowerCAmelCase													=   char
 return pairs







class 			a       (      a__     ):
 snake_case__								=					VOCAB_FILES_NAMES
 snake_case__								=					PRETRAINED_VOCAB_FILES_MAP
 snake_case__								=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 snake_case__								=					['''input_ids''', '''attention_mask''']



 def __init__(    self							,					_snake_case							,					_snake_case							,					_snake_case="replace"							,					_snake_case="<|endoftext|>"							,					_snake_case="<|endoftext|>"							,					_snake_case="<|endoftext|>"							,					_snake_case=None							,					_snake_case=False							,					_snake_case=False							,					**_snake_case							,					):







  """simple docstring"""






  lowerCAmelCase													=   AddedToken(_snake_case							,					lstrip=_snake_case							,					rstrip=_snake_case    ) if isinstance(_snake_case							,					_snake_case    ) else bos_token
  lowerCAmelCase													=   AddedToken(_snake_case							,					lstrip=_snake_case							,					rstrip=_snake_case    ) if isinstance(_snake_case							,					_snake_case    ) else eos_token
  lowerCAmelCase													=   AddedToken(_snake_case							,					lstrip=_snake_case							,					rstrip=_snake_case    ) if isinstance(_snake_case							,					_snake_case    ) else unk_token
  lowerCAmelCase													=   AddedToken(_snake_case							,					lstrip=_snake_case							,					rstrip=_snake_case    ) if isinstance(_snake_case							,					_snake_case    ) else pad_token
  super().__init__(
      errors=_snake_case							,					unk_token=_snake_case							,					bos_token=_snake_case							,					eos_token=_snake_case							,					pad_token=_snake_case							,					add_prefix_space=_snake_case							,					add_bos_token=_snake_case							,					**_snake_case							,					)
  lowerCAmelCase													=   add_bos_token

  with open(_snake_case							,					encoding='utf-8'    ) as vocab_handle:
   lowerCAmelCase													=   json.load(_snake_case    )
  lowerCAmelCase													=   {v: k for k, v in self.encoder.items()}
  lowerCAmelCase													=   errors  # how to handle errors in decoding
  lowerCAmelCase													=   bytes_to_unicode()
  lowerCAmelCase													=   {v: k for k, v in self.byte_encoder.items()}
  with open(_snake_case							,					encoding='utf-8'    ) as merges_handle:
   lowerCAmelCase													=   merges_handle.read().split('\n'    )[1:-1]
  lowerCAmelCase													=   [tuple(merge.split()    ) for merge in bpe_merges]
  lowerCAmelCase													=   dict(zip(_snake_case							,					range(len(_snake_case    )    )    )    )
  lowerCAmelCase													=   {}
  lowerCAmelCase													=   add_prefix_space

  # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
  lowerCAmelCase													=   re.compile(r'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'    )



 @property
 def       UpperCamelCase__     (    self    ):







  """simple docstring"""






  return len(self.encoder    )



 def       UpperCamelCase__     (    self    ):







  """simple docstring"""






  return dict(self.encoder							,					**self.added_tokens_encoder    )



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  if token in self.cache:
   return self.cache[token]
  lowerCAmelCase													=   tuple(_snake_case    )
  lowerCAmelCase													=   get_pairs(_snake_case    )

  if not pairs:
   return token

  while True:
   lowerCAmelCase													=   min(_snake_case							,					key=lambda _snake_case    : self.bpe_ranks.get(_snake_case							,					float('inf'    )    )    )
   if bigram not in self.bpe_ranks:
    break
   lowerCAmelCase							,lowerCAmelCase													=   bigram
   lowerCAmelCase													=   []
   lowerCAmelCase													=   0
   while i < len(_snake_case    ):
    try:
     lowerCAmelCase													=   word.index(_snake_case							,					_snake_case    )
    except ValueError:
     new_word.extend(word[i:]    )
     break
    else:
     new_word.extend(word[i:j]    )
     lowerCAmelCase													=   j

    if word[i] == first and i < len(_snake_case    ) - 1 and word[i + 1] == second:
     new_word.append(first + second    )
     i += 2
    else:
     new_word.append(word[i]    )
     i += 1
   lowerCAmelCase													=   tuple(_snake_case    )
   lowerCAmelCase													=   new_word
   if len(_snake_case    ) == 1:
    break
   else:
    lowerCAmelCase													=   get_pairs(_snake_case    )
  lowerCAmelCase													=   ' '.join(_snake_case    )
  lowerCAmelCase													=   word
  return word



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case=None    ):







  """simple docstring"""






  if self.add_bos_token:
   lowerCAmelCase													=   [self.bos_token_id]
  else:
   lowerCAmelCase													=   []

  lowerCAmelCase													=   bos_token_ids + token_ids_a

  if token_ids_a is None:
   return output

  return output + bos_token_ids + token_ids_a



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  lowerCAmelCase													=   []
  for token in re.findall(self.pat							,					_snake_case    ):
   lowerCAmelCase													=   ''.join(
       self.byte_encoder[b] for b in token.encode('utf-8'    )    )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
   bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_snake_case    ).split(' '    )    )
  return bpe_tokens



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  return self.encoder.get(_snake_case							,					self.encoder.get(self.unk_token    )    )



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  return self.decoder.get(_snake_case    )



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  lowerCAmelCase													=   ''.join(_snake_case    )
  lowerCAmelCase													=   bytearray([self.byte_decoder[c] for c in text]    ).decode('utf-8'							,					errors=self.errors    )
  return text



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case = None    ):







  """simple docstring"""






  if not os.path.isdir(_snake_case    ):
   logger.error(F'Vocabulary path ({save_directory}) should be a directory'    )
   return
  lowerCAmelCase													=   os.path.join(
      _snake_case							,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']    )
  lowerCAmelCase													=   os.path.join(
      _snake_case							,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']    )

  with open(_snake_case							,					'w'							,					encoding='utf-8'    ) as f:
   f.write(json.dumps(self.encoder							,					indent=2							,					sort_keys=_snake_case							,					ensure_ascii=_snake_case    ) + '\n'    )

  lowerCAmelCase													=   0
  with open(_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 _snake_case    : 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!'    )
     lowerCAmelCase													=   token_index
    writer.write(' '.join(_snake_case    ) + '\n'    )
    index += 1

  return vocab_file, merge_file



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case=False							,					**_snake_case    ):







  """simple docstring"""






  lowerCAmelCase													=   kwargs.pop('add_prefix_space'							,					self.add_prefix_space    )
  if is_split_into_words or add_prefix_space:
   lowerCAmelCase													=   ' ' + text
  return (text, kwargs)



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case = False							,					_snake_case = None							,					_snake_case = None							,					**_snake_case							,					):







  """simple docstring"""






  lowerCAmelCase													=   super()._decode(
      token_ids=_snake_case							,					skip_special_tokens=_snake_case							,					clean_up_tokenization_spaces=_snake_case							,					**_snake_case							,					)

  if truncate_before_pattern is not None and len(_snake_case    ) > 0:
   lowerCAmelCase													=   self.truncate(_snake_case							,					_snake_case    )

  return decoded_text



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case    ):







  """simple docstring"""






  def find_re(_snake_case							,					_snake_case							,					_snake_case    ):
   lowerCAmelCase													=   pattern.search(_snake_case							,					_snake_case    )
   return m.start() if m else -1

  lowerCAmelCase													=   [re.compile(_snake_case							,					re.MULTILINE    ) for pattern in truncate_before_pattern]

  lowerCAmelCase													=   list(re.finditer('^print'							,					_snake_case							,					re.MULTILINE    )    )

  if len(_snake_case    ) > 1:
   lowerCAmelCase													=   completion[: prints[1].start()]

  lowerCAmelCase													=   list(re.finditer('^def'							,					_snake_case							,					re.MULTILINE    )    )

  if len(_snake_case    ) > 1:
   lowerCAmelCase													=   completion[: defs[1].start()]

  lowerCAmelCase													=   0

  lowerCAmelCase													=   [
      pos for pos in [find_re(_snake_case							,					_snake_case							,					_snake_case    ) for terminal in terminals] if pos != -1
  ]

  if len(_snake_case    ) > 0:
   return completion[: min(_snake_case    )]
  else:
   return completion












"""simple docstring"""
from typing import Any, Dict, List, Union

import numpy as np

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       from ..models.auto.modeling_auto import (
           MODEL_FOR_IMAGE_SEGMENTATION_MAPPING,
           MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING,
           MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING,
           MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING,
       )


__UpperCamelCase    :	str						=     logging.get_logger(__name__)


__UpperCamelCase    :	Optional[int]						=     Dict[str, Any]
__UpperCamelCase    :	List[str]						=     List[Prediction]







@add_end_docstrings(a__     )
class 			a       (      a__     ):



 def __init__(    self							,					*_snake_case							,					**_snake_case    ):







  """simple docstring"""






  super().__init__(*_snake_case							,					**_snake_case    )

  if self.framework == "tf":
   raise ValueError(F'The {self.__class__} is only available in PyTorch.'    )

  requires_backends(self							,					'vision'    )
  self.check_model_type(
      dict(
          MODEL_FOR_IMAGE_SEGMENTATION_MAPPING.items()
          + MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING.items()
          + MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING.items()
          + MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING.items()    )    )



 def       UpperCamelCase__     (    self							,					**_snake_case    ):







  """simple docstring"""






  lowerCAmelCase													=   {}
  lowerCAmelCase													=   {}
  if "subtask" in kwargs:
   lowerCAmelCase													=   kwargs['subtask']
   lowerCAmelCase													=   kwargs['subtask']
  if "threshold" in kwargs:
   lowerCAmelCase													=   kwargs['threshold']
  if "mask_threshold" in kwargs:
   lowerCAmelCase													=   kwargs['mask_threshold']
  if "overlap_mask_area_threshold" in kwargs:
   lowerCAmelCase													=   kwargs['overlap_mask_area_threshold']

  return preprocess_kwargs, {}, postprocess_kwargs



 def __call__(    self							,					_snake_case							,					**_snake_case    ):







  """simple docstring"""






  return super().__call__(_snake_case							,					**_snake_case    )



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case=None    ):







  """simple docstring"""






  lowerCAmelCase													=   load_image(_snake_case    )
  lowerCAmelCase													=   [(image.height, image.width)]
  if self.model.config.__class__.__name__ == "OneFormerConfig":
   if subtask is None:
    lowerCAmelCase													=   {}
   else:
    lowerCAmelCase													=   {'task_inputs': [subtask]}
   lowerCAmelCase													=   self.image_processor(images=[image]							,					return_tensors='pt'							,					**_snake_case    )
   lowerCAmelCase													=   self.tokenizer(
       inputs['task_inputs']							,					padding='max_length'							,					max_length=self.model.config.task_seq_len							,					return_tensors=self.framework							,					)['input_ids']
  else:
   lowerCAmelCase													=   self.image_processor(images=[image]							,					return_tensors='pt'    )
  lowerCAmelCase													=   target_size
  return inputs



 def       UpperCamelCase__     (    self							,					_snake_case    ):







  """simple docstring"""






  lowerCAmelCase													=   model_inputs.pop('target_size'    )
  lowerCAmelCase													=   self.model(**_snake_case    )
  lowerCAmelCase													=   target_size
  return model_outputs



 def       UpperCamelCase__     (    self							,					_snake_case							,					_snake_case=None							,					_snake_case=0.9							,					_snake_case=0.5							,					_snake_case=0.5    ):







  """simple docstring"""






  lowerCAmelCase													=   None
  if subtask in {"panoptic", None} and hasattr(self.image_processor							,					'post_process_panoptic_segmentation'    ):
   lowerCAmelCase													=   self.image_processor.post_process_panoptic_segmentation
  elif subtask in {"instance", None} and hasattr(self.image_processor							,					'post_process_instance_segmentation'    ):
   lowerCAmelCase													=   self.image_processor.post_process_instance_segmentation

  if fn is not None:
   lowerCAmelCase													=   fn(
       _snake_case							,					threshold=_snake_case							,					mask_threshold=_snake_case							,					overlap_mask_area_threshold=_snake_case							,					target_sizes=model_outputs['target_size']							,					)[0]

   lowerCAmelCase													=   []
   lowerCAmelCase													=   outputs['segmentation']

   for segment in outputs["segments_info"]:
    lowerCAmelCase													=   (segmentation == segment['id']) * 2_55
    lowerCAmelCase													=   Image.fromarray(mask.numpy().astype(np.uinta    )							,					mode='L'    )
    lowerCAmelCase													=   self.model.config.idalabel[segment['label_id']]
    lowerCAmelCase													=   segment['score']
    annotation.append({'score': score, 'label': label, 'mask': mask}    )

  elif subtask in {"semantic", None} and hasattr(self.image_processor							,					'post_process_semantic_segmentation'    ):
   lowerCAmelCase													=   self.image_processor.post_process_semantic_segmentation(
       _snake_case							,					target_sizes=model_outputs['target_size']    )[0]

   lowerCAmelCase													=   []
   lowerCAmelCase													=   outputs.numpy()
   lowerCAmelCase													=   np.unique(_snake_case    )

   for label in labels:
    lowerCAmelCase													=   (segmentation == label) * 2_55
    lowerCAmelCase													=   Image.fromarray(mask.astype(np.uinta    )							,					mode='L'    )
    lowerCAmelCase													=   self.model.config.idalabel[label]
    annotation.append({'score': None, 'label': label, 'mask': mask}    )
  else:
   raise ValueError(F'Subtask {subtask} is not supported for model {type(self.model    )}'    )
  return annotation





