


"""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

a__       :							int        =  logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase)
class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	def __init__(				self    :       Optional[Any]    ,    *UpperCAmelCase__    :       Tuple    ,    **UpperCAmelCase__    :       Optional[int]     ) ->			Tuple:
						super().__init__(*UpperCAmelCase__    ,    **UpperCAmelCase__     )
						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 						UpperCAmelCase_   (				self    :       Optional[Any]    ,    UpperCAmelCase__    :       Union[str, Any]=None    ,    UpperCAmelCase__    :       Union[str, Any]=None    ,    UpperCAmelCase__    :       str=None     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       {}
						__SCREAMING_SNAKE_CASE		     =       {}

						if prompt is not None:
											__SCREAMING_SNAKE_CASE		     =       prompt

						if generate_kwargs is not None:
											__SCREAMING_SNAKE_CASE		     =       generate_kwargs
						if max_new_tokens is not None:
											if "generate_kwargs" not in forward_kwargs:
																__SCREAMING_SNAKE_CASE		     =       {}
											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"     )
											__SCREAMING_SNAKE_CASE		     =       max_new_tokens
						return preprocess_params, forward_kwargs, {}




	def __call__(				self    :       List[str]    ,    UpperCAmelCase__    :       Union[str, List[str], "Image.Image", List["Image.Image"]]    ,    **UpperCAmelCase__    :       str     ) ->			Optional[int]:
						return super().__call__(UpperCAmelCase__    ,    **UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Tuple    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Optional[int]=None     ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       load_image(UpperCAmelCase__     )

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

											__SCREAMING_SNAKE_CASE		     =       self.model.config.model_type

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

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

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

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

						else:
											__SCREAMING_SNAKE_CASE		     =       self.image_processor(images=UpperCAmelCase__    ,    return_tensors=self.framework     )

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

						return model_inputs




	def 						UpperCAmelCase_   (				self    :       Any    ,    UpperCAmelCase__    :       Union[str, Any]    ,    UpperCAmelCase__    :       List[Any]=None     ) ->			Tuple:
						# Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the
						# pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first.
						if (
						    "input_ids" in model_inputs
						    and isinstance(model_inputs["input_ids"]    ,    UpperCAmelCase__     )
						    and all(x is None for x in model_inputs["input_ids"]     )
						):
											__SCREAMING_SNAKE_CASE		     =       None

						if generate_kwargs is None:
											__SCREAMING_SNAKE_CASE		     =       {}
						# 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.
						__SCREAMING_SNAKE_CASE		     =       model_inputs.pop(self.model.main_input_name     )
						__SCREAMING_SNAKE_CASE		     =       self.model.generate(UpperCAmelCase__    ,    **UpperCAmelCase__    ,    **UpperCAmelCase__     )
						return model_outputs




	def 						UpperCAmelCase_   (				self    :       Optional[int]    ,    UpperCAmelCase__    :       Union[str, Any]     ) ->			Optional[int]:
						__SCREAMING_SNAKE_CASE		     =       []
						for output_ids in model_outputs:
											__SCREAMING_SNAKE_CASE		     =       {
											    "generated_text": self.tokenizer.decode(
											        UpperCAmelCase__    ,    skip_special_tokens=UpperCAmelCase__    ,    )
											}
											records.append(UpperCAmelCase__     )
						return records




"""simple docstring"""







from math import asin, atan, cos, radians, sin, sqrt, tan

a__       :							List[str]        =  6_37_81_37.0
a__       :							Tuple        =  6_35_67_52.31_42_45
a__       :							str        =  6_3_7_8_1_3_7




def      UpperCAmelCase__ (lowerCAmelCase_						,		lowerCAmelCase_						,		lowerCAmelCase_						,		lowerCAmelCase_     ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE		     =       (AXIS_A - AXIS_B) / AXIS_A
					__SCREAMING_SNAKE_CASE		     =       atan((1 - flattening) * tan(radians(lowerCAmelCase_     )     )     )
					__SCREAMING_SNAKE_CASE		     =       atan((1 - flattening) * tan(radians(lowerCAmelCase_     )     )     )
					__SCREAMING_SNAKE_CASE		     =       radians(lowerCAmelCase_     )
					__SCREAMING_SNAKE_CASE		     =       radians(lowerCAmelCase_     )
					# Equation
					__SCREAMING_SNAKE_CASE		     =       sin((phi_a - phi_a) / 2     )
					__SCREAMING_SNAKE_CASE		     =       sin((lambda_a - lambda_a) / 2     )
					# Square both values
					sin_sq_phi *= sin_sq_phi
					sin_sq_lambda *= sin_sq_lambda
					__SCREAMING_SNAKE_CASE		     =       sqrt(sin_sq_phi + (cos(lowerCAmelCase_     ) * cos(lowerCAmelCase_     ) * sin_sq_lambda)     )
					return 2 * RADIUS * asin(lowerCAmelCase_     )


if __name__ == "__main__":
		import doctest

		doctest.testmod()
