




import enum
import warnings

from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING
from ..utils import add_end_docstrings, is_tf_available
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
   import tensorflow as tf







class a (     enum.Enum  ):
      """simple docstring"""

      UpperCamelCase_	:       List[Any]       	=		0
      UpperCamelCase_	:       int       	=		1
      UpperCamelCase_	:       Union[str, Any]       	=		2







@add_end_docstrings(__SCREAMING_SNAKE_CASE  )
class a (     __SCREAMING_SNAKE_CASE  ):
      """simple docstring"""

      UpperCamelCase_	:       List[str]       	=		'\n    In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The\n    voice of Nicholas\'s young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western\n    Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision\n    and denounces one of the men as a horse thief. Although his father initially slaps him for making such an\n    accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of\n    the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop,\n    begging for his blessing. <eod> </s> <eos>\n    '






      def __init__( self :     Union[str, Any]				,							*lowerCamelCase__ :     List[str]				,							**lowerCamelCase__ :     Optional[int]				)    ->						List[Any]:
             """simple docstring"""


             super().__init__(*lowerCamelCase__				,							**lowerCamelCase__				)
             self.check_model_type(
                 TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == '''tf''' else MODEL_FOR_CAUSAL_LM_MAPPING				)
             if "prefix" not in self._preprocess_params:
                    # This is very specific. The logic is quite complex and needs to be done
                    # as a "default".
                    # It also defines both some preprocess_kwargs and generate_kwargs
                    # which is why we cannot put them in their respective methods.
                    __lowercase      =    None
                    if self.model.config.prefix is not None:
                           __lowercase      =    self.model.config.prefix
                    if prefix is None and self.model.__class__.__name__ in [
                        "XLNetLMHeadModel",
                        "TransfoXLLMHeadModel",
                        "TFXLNetLMHeadModel",
                        "TFTransfoXLLMHeadModel",
                    ]:
                           # For XLNet and TransformerXL we add an article to the prompt to give more state to the model.
                           __lowercase      =    self.XL_PREFIX
                    if prefix is not None:
                           # Recalculate some generate_kwargs linked to prefix.
                           __lowercase     ,     __lowercase     ,     __lowercase      =    self._sanitize_parameters(prefix=lowerCamelCase__				,							**self._forward_params				)
                           __lowercase      =    {**self._preprocess_params, **preprocess_params}
                           __lowercase      =    {**self._forward_params, **forward_params}






      def     UpperCAmelCase_      ( self :     Union[str, Any]				,							lowerCamelCase__ :     Union[str, Any]=None				,							lowerCamelCase__ :     Dict=None				,							lowerCamelCase__ :     Tuple=None				,							lowerCamelCase__ :     List[str]=None				,							lowerCamelCase__ :     Tuple=None				,							lowerCamelCase__ :     Optional[Any]=None				,							lowerCamelCase__ :     Optional[int]=None				,							lowerCamelCase__ :     Tuple=None				,							**lowerCamelCase__ :     Dict				,							)    ->						Dict:
             """simple docstring"""


             __lowercase      =    {}
             if prefix is not None:
                    __lowercase      =    prefix
             if prefix:
                    __lowercase      =    self.tokenizer(
                        lowerCamelCase__				,							padding=lowerCamelCase__				,							add_special_tokens=lowerCamelCase__				,							return_tensors=self.framework				)
                    __lowercase      =    prefix_inputs['''input_ids'''].shape[-1]

             if handle_long_generation is not None:
                    if handle_long_generation not in {"hole"}:
                           raise ValueError(
                               f'{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected'
                               ''' [None, \'hole\']'''				)
                    __lowercase      =    handle_long_generation

             preprocess_params.update(lowerCamelCase__				)
             __lowercase      =    generate_kwargs

             __lowercase      =    {}
             if return_full_text is not None and return_type is None:
                    if return_text is not None:
                           raise ValueError('''`return_text` is mutually exclusive with `return_full_text`'''				)
                    if return_tensors is not None:
                           raise ValueError('''`return_full_text` is mutually exclusive with `return_tensors`'''				)
                    __lowercase      =    ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT
             if return_tensors is not None and return_type is None:
                    if return_text is not None:
                           raise ValueError('''`return_text` is mutually exclusive with `return_tensors`'''				)
                    __lowercase      =    ReturnType.TENSORS
             if return_type is not None:
                    __lowercase      =    return_type
             if clean_up_tokenization_spaces is not None:
                    __lowercase      =    clean_up_tokenization_spaces

             if stop_sequence is not None:
                    __lowercase      =    self.tokenizer.encode(lowerCamelCase__				,							add_special_tokens=lowerCamelCase__				)
                    if len(lowerCamelCase__				) > 1:
                           warnings.warn(
                               '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of'''
                               ''' the stop sequence will be used as the stop sequence string in the interim.'''				)
                    __lowercase      =    stop_sequence_ids[0]

             return preprocess_params, forward_params, postprocess_params






      def     UpperCAmelCase_      ( self :     int				,							*lowerCamelCase__ :     Tuple				,							**lowerCamelCase__ :     Any				)    ->						int:
             """simple docstring"""


             if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]:
                    kwargs.update({'''add_space_before_punct_symbol''': True}				)

             return super()._parse_and_tokenize(*lowerCamelCase__				,							**lowerCamelCase__				)






      def __call__( self :     List[Any]				,							lowerCamelCase__ :     List[str]				,							**lowerCamelCase__ :     List[str]				)    ->						List[Any]:
             """simple docstring"""


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






      def     UpperCAmelCase_      ( self :     List[Any]				,							lowerCamelCase__ :     Any				,							lowerCamelCase__ :     Optional[int]=""				,							lowerCamelCase__ :     str=None				,							**lowerCamelCase__ :     Dict				)    ->						int:
             """simple docstring"""


             __lowercase      =    self.tokenizer(
                 prefix + prompt_text				,							padding=lowerCamelCase__				,							add_special_tokens=lowerCamelCase__				,							return_tensors=self.framework				)
             __lowercase      =    prompt_text

             if handle_long_generation == "hole":
                    __lowercase      =    inputs['''input_ids'''].shape[-1]
                    if "max_new_tokens" in generate_kwargs:
                           __lowercase      =    generate_kwargs['''max_new_tokens''']
                    else:
                           __lowercase      =    generate_kwargs.get('''max_length'''				,							self.model.config.max_length				) - cur_len
                           if new_tokens < 0:
                                  raise ValueError('''We cannot infer how many new tokens are expected'''				)
                    if cur_len + new_tokens > self.tokenizer.model_max_length:
                           __lowercase      =    self.tokenizer.model_max_length - new_tokens
                           if keep_length <= 0:
                                  raise ValueError(
                                      '''We cannot use `hole` to handle this generation the number of desired tokens exceeds the'''
                                      ''' models max length'''				)

                           __lowercase      =    inputs['''input_ids'''][:, -keep_length:]
                           if "attention_mask" in inputs:
                                  __lowercase      =    inputs['''attention_mask'''][:, -keep_length:]

             return inputs






      def     UpperCAmelCase_      ( self :     Tuple				,							lowerCamelCase__ :     Any				,							**lowerCamelCase__ :     int				)    ->						Tuple:
             """simple docstring"""


             __lowercase      =    model_inputs['''input_ids''']
             __lowercase      =    model_inputs.get('''attention_mask'''				,							lowerCamelCase__				)
             # Allow empty prompts
             if input_ids.shape[1] == 0:
                    __lowercase      =    None
                    __lowercase      =    None
                    __lowercase      =    1
             else:
                    __lowercase      =    input_ids.shape[0]
             __lowercase      =    model_inputs.pop('''prompt_text'''				)

             # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying
             # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline.
             __lowercase      =    generate_kwargs.pop('''prefix_length'''				,							0				)
             if prefix_length > 0:
                    __lowercase      =    '''max_new_tokens''' in generate_kwargs or (
                        '''generation_config''' in generate_kwargs
                        and generate_kwargs['''generation_config'''].max_new_tokens is not None
                    )
                    if not has_max_new_tokens:
                           __lowercase      =    generate_kwargs.get('''max_length'''				) or self.model.config.max_length
                           generate_kwargs["max_length"] += prefix_length
                    __lowercase      =    '''min_new_tokens''' in generate_kwargs or (
                        '''generation_config''' in generate_kwargs
                        and generate_kwargs['''generation_config'''].min_new_tokens is not None
                    )
                    if not has_min_new_tokens and "min_length" in generate_kwargs:
                           generate_kwargs["min_length"] += prefix_length

        # BS x SL
             __lowercase      =    self.model.generate(input_ids=lowerCamelCase__				,							attention_mask=lowerCamelCase__				,							**lowerCamelCase__				)
             __lowercase      =    generated_sequence.shape[0]
             if self.framework == "pt":
                    __lowercase      =    generated_sequence.reshape(lowerCamelCase__				,							out_b // in_b				,							*generated_sequence.shape[1:]				)
             elif self.framework == "tf":
                    __lowercase      =    tf.reshape(lowerCamelCase__				,							(in_b, out_b // in_b, *generated_sequence.shape[1:])				)
             return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text}






      def     UpperCAmelCase_      ( self :     List[str]				,							lowerCamelCase__ :     Tuple				,							lowerCamelCase__ :     Dict=ReturnType.FULL_TEXT				,							lowerCamelCase__ :     int=True				)    ->						str:
             """simple docstring"""


             __lowercase      =    model_outputs['''generated_sequence'''][0]
             __lowercase      =    model_outputs['''input_ids''']
             __lowercase      =    model_outputs['''prompt_text''']
             __lowercase      =    generated_sequence.numpy().tolist()
             __lowercase      =    []
             for sequence in generated_sequence:
                    if return_type == ReturnType.TENSORS:
                           __lowercase      =    {'''generated_token_ids''': sequence}
                    elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}:
                           # Decode text
                           __lowercase      =    self.tokenizer.decode(
                               lowerCamelCase__				,							skip_special_tokens=lowerCamelCase__				,							clean_up_tokenization_spaces=lowerCamelCase__				,							)

                           # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used
                           if input_ids is None:
                                  __lowercase      =    0
                           else:
                                  __lowercase      =    len(
                                      self.tokenizer.decode(
                                          input_ids[0]				,							skip_special_tokens=lowerCamelCase__				,							clean_up_tokenization_spaces=lowerCamelCase__				,							)				)

                           if return_type == ReturnType.FULL_TEXT:
                                  __lowercase      =    prompt_text + text[prompt_length:]
                           else:
                                  __lowercase      =    text[prompt_length:]

                           __lowercase      =    {'''generated_text''': all_text}
                    records.append(lowerCamelCase__				)

             return records












import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class a (     __SCREAMING_SNAKE_CASE  ):
						"""simple docstring"""

						UpperCamelCase_	:       int       	=		['image_processor', 'tokenizer']
						UpperCamelCase_	:       Dict       	=		'OwlViTImageProcessor'
						UpperCamelCase_	:       List[str]       	=		('CLIPTokenizer', 'CLIPTokenizerFast')






						def __init__( self :     str				,							lowerCamelCase__ :     Any=None				,							lowerCamelCase__ :     List[Any]=None				,							**lowerCamelCase__ :     Union[str, Any]				)    ->						Tuple:
													"""simple docstring"""


													__lowercase      =    None
													if "feature_extractor" in kwargs:
																				warnings.warn(
																				    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																				    ''' instead.'''				,							lowerCamelCase__				,							)
																				__lowercase      =    kwargs.pop('''feature_extractor'''				)

													__lowercase      =    image_processor if image_processor is not None else feature_extractor
													if image_processor is None:
																				raise ValueError('''You need to specify an `image_processor`.'''				)
													if tokenizer is None:
																				raise ValueError('''You need to specify a `tokenizer`.'''				)

													super().__init__(lowerCamelCase__				,							lowerCamelCase__				)






						def __call__( self :     List[Any]				,							lowerCamelCase__ :     List[str]=None				,							lowerCamelCase__ :     int=None				,							lowerCamelCase__ :     Any=None				,							lowerCamelCase__ :     str="max_length"				,							lowerCamelCase__ :     Union[str, Any]="np"				,							**lowerCamelCase__ :     Dict				)    ->						Union[str, Any]:
													"""simple docstring"""


													if text is None and query_images is None and images is None:
																				raise ValueError(
																				    '''You have to specify at least one text or query image or image. All three cannot be none.'''				)

													if text is not None:
																				if isinstance(lowerCamelCase__				,							lowerCamelCase__				) or (isinstance(lowerCamelCase__				,							lowerCamelCase__				) and not isinstance(text[0]				,							lowerCamelCase__				)):
																											__lowercase      =    [self.tokenizer(lowerCamelCase__				,							padding=lowerCamelCase__				,							return_tensors=lowerCamelCase__				,							**lowerCamelCase__				)]

																				elif isinstance(lowerCamelCase__				,							lowerCamelCase__				) and isinstance(text[0]				,							lowerCamelCase__				):
																											__lowercase      =    []

																											# Maximum number of queries across batch
																											__lowercase      =    max([len(lowerCamelCase__				) for t in text]				)

																											# Pad all batch samples to max number of text queries
																											for t in text:
																																		if len(lowerCamelCase__				) != max_num_queries:
																																									__lowercase      =    t + [''' '''] * (max_num_queries - len(lowerCamelCase__				))

																																		__lowercase      =    self.tokenizer(lowerCamelCase__				,							padding=lowerCamelCase__				,							return_tensors=lowerCamelCase__				,							**lowerCamelCase__				)
																																		encodings.append(lowerCamelCase__				)
																				else:
																											raise TypeError('''Input text should be a string, a list of strings or a nested list of strings'''				)

																				if return_tensors == "np":
																											__lowercase      =    np.concatenate([encoding['''input_ids'''] for encoding in encodings]				,							axis=0				)
																											__lowercase      =    np.concatenate([encoding['''attention_mask'''] for encoding in encodings]				,							axis=0				)

																				elif return_tensors == "jax" and is_flax_available():
																											import jax.numpy as jnp

																											__lowercase      =    jnp.concatenate([encoding['''input_ids'''] for encoding in encodings]				,							axis=0				)
																											__lowercase      =    jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings]				,							axis=0				)

																				elif return_tensors == "pt" and is_torch_available():
																											import torch

																											__lowercase      =    torch.cat([encoding['''input_ids'''] for encoding in encodings]				,							dim=0				)
																											__lowercase      =    torch.cat([encoding['''attention_mask'''] for encoding in encodings]				,							dim=0				)

																				elif return_tensors == "tf" and is_tf_available():
																											import tensorflow as tf

																											__lowercase      =    tf.stack([encoding['''input_ids'''] for encoding in encodings]				,							axis=0				)
																											__lowercase      =    tf.stack([encoding['''attention_mask'''] for encoding in encodings]				,							axis=0				)

																				else:
																											raise ValueError('''Target return tensor type could not be returned'''				)

																				__lowercase      =    BatchEncoding()
																				__lowercase      =    input_ids
																				__lowercase      =    attention_mask

													if query_images is not None:
																				__lowercase      =    BatchEncoding()
																				__lowercase      =    self.image_processor(
																				    lowerCamelCase__				,							return_tensors=lowerCamelCase__				,							**lowerCamelCase__				).pixel_values
																				__lowercase      =    query_pixel_values

													if images is not None:
																				__lowercase      =    self.image_processor(lowerCamelCase__				,							return_tensors=lowerCamelCase__				,							**lowerCamelCase__				)

													if text is not None and images is not None:
																				__lowercase      =    image_features.pixel_values
																				return encoding
													elif query_images is not None and images is not None:
																				__lowercase      =    image_features.pixel_values
																				return encoding
													elif text is not None or query_images is not None:
																				return encoding
													else:
																				return BatchEncoding(data=dict(**lowerCamelCase__				)				,							tensor_type=lowerCamelCase__				)






						def     UpperCAmelCase_      ( self :     int				,							*lowerCamelCase__ :     str				,							**lowerCamelCase__ :     Optional[int]				)    ->						Tuple:
													"""simple docstring"""


													return self.image_processor.post_process(*lowerCamelCase__				,							**lowerCamelCase__				)






						def     UpperCAmelCase_      ( self :     Optional[int]				,							*lowerCamelCase__ :     Tuple				,							**lowerCamelCase__ :     Tuple				)    ->						Union[str, Any]:
													"""simple docstring"""


													return self.image_processor.post_process_object_detection(*lowerCamelCase__				,							**lowerCamelCase__				)






						def     UpperCAmelCase_      ( self :     List[str]				,							*lowerCamelCase__ :     List[str]				,							**lowerCamelCase__ :     Optional[Any]				)    ->						List[Any]:
													"""simple docstring"""


													return self.image_processor.post_process_image_guided_detection(*lowerCamelCase__				,							**lowerCamelCase__				)






						def     UpperCAmelCase_      ( self :     Optional[int]				,							*lowerCamelCase__ :     str				,							**lowerCamelCase__ :     Union[str, Any]				)    ->						Optional[Any]:
													"""simple docstring"""


													return self.tokenizer.batch_decode(*lowerCamelCase__				,							**lowerCamelCase__				)






						def     UpperCAmelCase_      ( self :     str				,							*lowerCamelCase__ :     Optional[int]				,							**lowerCamelCase__ :     Union[str, Any]				)    ->						List[str]:
													"""simple docstring"""


													return self.tokenizer.decode(*lowerCamelCase__				,							**lowerCamelCase__				)






						@property
						def     UpperCAmelCase_      ( self :     Tuple				)    ->						List[str]:
													"""simple docstring"""


													warnings.warn(
													    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''				,							lowerCamelCase__				,							)
													return self.image_processor_class






						@property
						def     UpperCAmelCase_      ( self :     str				)    ->						Any:
													"""simple docstring"""


													warnings.warn(
													    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''				,							lowerCamelCase__				,							)
													return self.image_processor






