


'''simple docstring'''



from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    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_tf_available():
	from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
	import torch

	from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

_lowercase    :    int			     =  logging.get_logger(__name__)






@add_end_docstrings(lowerCAmelCase )
class  UpperCamelCase__(     lowerCAmelCase ):




				def __init__(	self    :				Dict   ,					*lowerCAmelCase    :				Union[str, Any]   ,					**lowerCAmelCase    :				Union[str, Any]   )->  str:

										"""simple docstring"""




										super().__init__(*lowerCAmelCase   ,					**lowerCAmelCase   )
										requires_backends(self   ,					'''vision'''   )
										self.check_model_type(
										    TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING   )




				def   a__(	self    :				Any   ,					lowerCAmelCase    :				Any=None   ,					lowerCAmelCase    :				List[Any]=None   ,					lowerCAmelCase    :				List[Any]=None   )->  Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase         =			{}
										UpperCAmelCase         =			{}

										if prompt is not None:
																UpperCAmelCase         =			prompt

										if generate_kwargs is not None:
																UpperCAmelCase         =			generate_kwargs
										if max_new_tokens is not None:
																if "generate_kwargs" not in forward_kwargs:
																						UpperCAmelCase         =			{}
																if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
																						raise ValueError(
																						    '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,'''
																						    ''' please use only one'''   )
																UpperCAmelCase         =			max_new_tokens
										return preprocess_params, forward_kwargs, {}




				def __call__(	self    :				Union[str, Any]   ,					lowerCAmelCase    :				Union[str, List[str], "Image.Image", List["Image.Image"]]   ,					**lowerCAmelCase    :				Optional[Any]   )->  Dict:

										"""simple docstring"""




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




				def   a__(	self    :				Any   ,					lowerCAmelCase    :				Tuple   ,					lowerCAmelCase    :				List[str]=None   )->  int:

										"""simple docstring"""




										UpperCAmelCase         =			load_image(lowerCAmelCase   )

										if prompt is not None:
																if not isinstance(lowerCAmelCase   ,					lowerCAmelCase   ):
																						raise ValueError(
																						    F"""Received an invalid text input, got - {type(lowerCAmelCase   )} - but expected a single string. """
																						    '''Note also that one single text can be provided for conditional image to text generation.'''   )

																UpperCAmelCase         =			self.model.config.model_type

																if model_type == "git":
																						UpperCAmelCase         =			self.image_processor(images=lowerCAmelCase   ,					return_tensors=self.framework   )
																						UpperCAmelCase         =			self.tokenizer(text=lowerCAmelCase   ,					add_special_tokens=lowerCAmelCase   ).input_ids
																						UpperCAmelCase         =			[self.tokenizer.cls_token_id] + input_ids
																						UpperCAmelCase         =			torch.tensor(lowerCAmelCase   ).unsqueeze(0   )
																						model_inputs.update({'''input_ids''': input_ids}   )

																elif model_type == "pix2struct":
																						UpperCAmelCase         =			self.image_processor(images=lowerCAmelCase   ,					header_text=lowerCAmelCase   ,					return_tensors=self.framework   )

																elif model_type != "vision-encoder-decoder":
																						# vision-encoder-decoder does not support conditional generation
																						UpperCAmelCase         =			self.image_processor(images=lowerCAmelCase   ,					return_tensors=self.framework   )
																						UpperCAmelCase         =			self.tokenizer(lowerCAmelCase   ,					return_tensors=self.framework   )
																						model_inputs.update(lowerCAmelCase   )

																else:
																						raise ValueError(F"""Model type {model_type} does not support conditional text generation"""   )

										else:
																UpperCAmelCase         =			self.image_processor(images=lowerCAmelCase   ,					return_tensors=self.framework   )

										if self.model.config.model_type == "git" and prompt is None:
																UpperCAmelCase         =			None

										return model_inputs




				def   a__(	self    :				List[Any]   ,					lowerCAmelCase    :				Dict   ,					lowerCAmelCase    :				str=None   )->  Union[str, Any]:

										"""simple docstring"""




										if (
										    "input_ids" in model_inputs
										    and isinstance(model_inputs['''input_ids''']   ,					lowerCAmelCase   )
										    and all(x is None for x in model_inputs['''input_ids''']   )
										):
																UpperCAmelCase         =			None

										if generate_kwargs is None:
																UpperCAmelCase         =			{}
										# FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
										#  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
										#  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
										#  in the `_prepare_model_inputs` method.
										UpperCAmelCase         =			model_inputs.pop(self.model.main_input_name   )
										UpperCAmelCase         =			self.model.generate(lowerCAmelCase   ,					**lowerCAmelCase   ,					**lowerCAmelCase   )
										return model_outputs





				def   a__(	self    :				Dict   ,					lowerCAmelCase    :				List[Any]   )->  List[Any]:

										"""simple docstring"""




										UpperCAmelCase         =			[]
										for output_ids in model_outputs:
																UpperCAmelCase         =			{
																    '''generated_text''': self.tokenizer.decode(
																        lowerCAmelCase   ,					skip_special_tokens=lowerCAmelCase   ,					)
																}
																records.append(lowerCAmelCase   )
										return records









'''simple docstring'''



# Copyright 2022 The HuggingFace Team and The OpenBMB Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_lowercase    :    Any			     =  {
    """configuration_cpmant""": ["""CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CpmAntConfig"""],
    """tokenization_cpmant""": ["""CpmAntTokenizer"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowercase    :    int			     =  [
	    """CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """CpmAntForCausalLM""",
	    """CpmAntModel""",
	    """CpmAntPreTrainedModel""",
	]


if TYPE_CHECKING:
	from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig
	from .tokenization_cpmant import CpmAntTokenizer

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_cpmant import (
		    CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    CpmAntForCausalLM,
		    CpmAntModel,
		    CpmAntPreTrainedModel,
		)


else:
	import sys

	_lowercase    :    Dict			     =  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





