


'''simple docstring'''







import io
import math
from typing import Dict, Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    get_image_size,
    infer_channel_dimension_format,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_vision_available, logging
from ...utils.import_utils import requires_backends


if is_vision_available():
   import textwrap

   from PIL import Image, ImageDraw, ImageFont

if is_torch_available():
   import torch

   from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
   A__:    List[str]					    =       False


A__:    str					    =       logging.get_logger(__name__)
A__:    Optional[Any]					    =       '''ybelkada/fonts'''


def 	SCREAMING_SNAKE_CASE_					(  )  ->			Optional[Any]:






       if is_torch_available() and not is_torch_greater_or_equal_than_1_11:
              raise ImportError(
                  F"You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use "
                  """Pix2StructImageProcessor. Please upgrade torch."""  )


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	int    ,_UpperCAmelCase	:	Union[str, Any]    ,_UpperCAmelCase	:	List[str]  )  ->			Optional[int]:
       requires_backends(__a    ,["""torch"""]  )
       _check_torch_version()

       _a    : Tuple     					=image_tensor.unsqueeze(0  )
       _a    : Optional[int]     					=torch.nn.functional.unfold(__a    ,(patch_height, patch_width)    ,stride=(patch_height, patch_width)  )
       _a    : Union[str, Any]     					=patches.reshape(image_tensor.size(0  )    ,image_tensor.size(1  )    ,__a    ,__a    ,-1  )
       _a    : List[Any]     					=patches.permute(0    ,4    ,2    ,3    ,1  ).reshape(
           image_tensor.size(2  ) // patch_height    ,image_tensor.size(3  ) // patch_width    ,image_tensor.size(1  ) * patch_height * patch_width    ,)
       return patches.unsqueeze(0  )


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	Tuple    ,_UpperCAmelCase	:	Union[str, Any] = 36    ,_UpperCAmelCase	:	Tuple = "black"    ,_UpperCAmelCase	:	Optional[int] = "white"    ,_UpperCAmelCase	:	str = 5    ,_UpperCAmelCase	:	str = 5    ,_UpperCAmelCase	:	Any = 5    ,_UpperCAmelCase	:	int = 5    ,_UpperCAmelCase	:	Tuple = None    ,_UpperCAmelCase	:	Dict = None    ,)  ->			Image.Image:
       requires_backends(__a    ,"""vision"""  )
       # Add new lines so that each line is no more than 80 characters.

       _a    : Union[str, Any]     					=textwrap.TextWrapper(width=80  )
       _a    : int     					=wrapper.wrap(text=__a  )
       _a    : Union[str, Any]     					='''\n'''.join(__a  )

       if font_bytes is not None and font_path is None:
              _a    : str     					=io.BytesIO(__a  )
       elif font_path is not None:
              _a    : int     					=font_path
       else:
              _a    : int     					=hf_hub_download(__a    ,"""Arial.TTF"""  )
       _a    : List[str]     					=ImageFont.truetype(__a    ,encoding="""UTF-8"""    ,size=__a  )

       # Use a temporary canvas to determine the width and height in pixels when
       # rendering the text.
       _a    : Optional[int]     					=ImageDraw.Draw(Image.new("""RGB"""    ,(1, 1)    ,__a  )  )
       _a    : List[str]     					=temp_draw.textbbox((0, 0)    ,__a    ,__a  )

       # Create the actual image with a bit of padding around the text.
       _a    : List[Any]     					=text_width + left_padding + right_padding
       _a    : int     					=text_height + top_padding + bottom_padding
       _a    : str     					=Image.new("""RGB"""    ,(image_width, image_height)    ,__a  )
       _a    : Tuple     					=ImageDraw.Draw(__a  )
       draw.text(xy=(left_padding, top_padding)    ,text=__a    ,fill=__a    ,font=__a  )
       return image


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	Any    ,_UpperCAmelCase	:	Any    ,**_UpperCAmelCase	:	int  )  ->			Optional[Any]:
       requires_backends(__a    ,"""vision"""  )

       # Convert to PIL image if necessary
       _a    : Optional[int]     					=to_pil_image(__a  )

       _a    : List[str]     					=render_text(__a    ,**__a  )
       _a    : List[str]     					=max(header_image.width    ,image.width  )

       _a    : List[str]     					=int(image.height * (new_width / image.width)  )
       _a    : Dict     					=int(header_image.height * (new_width / header_image.width)  )

       _a    : Dict     					=Image.new("""RGB"""    ,(new_width, new_height + new_header_height)    ,"""white"""  )
       new_image.paste(header_image.resize((new_width, new_header_height)  )    ,(0, 0)  )
       new_image.paste(image.resize((new_width, new_height)  )    ,(0, new_header_height)  )

       # Convert back to the original framework if necessary
       _a    : Optional[int]     					=to_numpy_array(__a  )

       if infer_channel_dimension_format(__a  ) == ChannelDimension.LAST:
              _a    : int     					=to_channel_dimension_format(__a    ,ChannelDimension.LAST  )

       return new_image







class A__       (						A__      ):
      __UpperCamelCase       :  Tuple	       =							["flattened_patches"]






      def __init__( self			:Dict     ,						SCREAMING_SNAKE_CASE			:Optional[Any] = True     ,						SCREAMING_SNAKE_CASE			:Dict = True     ,						SCREAMING_SNAKE_CASE			:Tuple = None     ,						SCREAMING_SNAKE_CASE			:List[Any] = 2_0_4_8     ,						SCREAMING_SNAKE_CASE			:List[Any] = False     ,						**SCREAMING_SNAKE_CASE			:Union[str, Any]     ,						)       ->					List[str]:




             '''simple docstring'''
             super().__init__(**UpperCamelCase_	)
             _a    : List[Any]     					=patch_size if patch_size is not None else {'''height''': 1_6, '''width''': 1_6}
             _a    : Optional[int]     					=do_normalize
             _a    : List[str]     					=do_convert_rgb
             _a    : Union[str, Any]     					=max_patches
             _a    : Optional[int]     					=is_vqa






      def __UpperCAmelCase       ( self			:int     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:Any     ,						SCREAMING_SNAKE_CASE			:List[str]     ,						**SCREAMING_SNAKE_CASE			:Any	)       ->					int:




             '''simple docstring'''
             requires_backends(self.extract_flattened_patches     ,						"""torch"""	)
             _check_torch_version()

             # convert to torch
             _a    : Union[str, Any]     					=to_channel_dimension_format(UpperCamelCase_     ,						ChannelDimension.FIRST	)
             _a    : Dict     					=torch.from_numpy(UpperCamelCase_	)

             _a    : Tuple     					=patch_size['''height'''], patch_size['''width''']
             _a    : Any     					=get_image_size(UpperCamelCase_	)

             # maximize scale s.t.
             _a    : Tuple     					=math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width)	)
             _a    : Tuple     					=max(min(math.floor(scale * image_height / patch_height	)     ,						UpperCamelCase_	)     ,						1	)
             _a    : Optional[Any]     					=max(min(math.floor(scale * image_width / patch_width	)     ,						UpperCamelCase_	)     ,						1	)
             _a    : str     					=max(num_feasible_rows * patch_height     ,						1	)
             _a    : Optional[int]     					=max(num_feasible_cols * patch_width     ,						1	)

             _a    : Optional[int]     					=torch.nn.functional.interpolate(
                 image.unsqueeze(0	)     ,						size=(resized_height, resized_width)     ,						mode="""bilinear"""     ,						align_corners=UpperCamelCase_     ,						antialias=UpperCamelCase_     ,						).squeeze(0	)

             # [1, rows, columns, patch_height * patch_width * image_channels]
             _a    : Optional[int]     					=torch_extract_patches(UpperCamelCase_     ,						UpperCamelCase_     ,						UpperCamelCase_	)

             _a    : List[Any]     					=patches.shape
             _a    : Dict     					=patches_shape[1]
             _a    : int     					=patches_shape[2]
             _a    : List[str]     					=patches_shape[3]

             # [rows * columns, patch_height * patch_width * image_channels]
             _a    : int     					=patches.reshape([rows * columns, depth]	)

             # [rows * columns, 1]
             _a    : Union[str, Any]     					=torch.arange(UpperCamelCase_	).reshape([rows, 1]	).repeat(1     ,						UpperCamelCase_	).reshape([rows * columns, 1]	)
             _a    : str     					=torch.arange(UpperCamelCase_	).reshape([1, columns]	).repeat(UpperCamelCase_     ,						1	).reshape([rows * columns, 1]	)

             # Offset by 1 so the ids do not contain zeros, which represent padding.
             row_ids += 1
             col_ids += 1

             # Prepare additional patch features.
             # [rows * columns, 1]
             _a    : List[Any]     					=row_ids.to(torch.floataa	)
             _a    : int     					=col_ids.to(torch.floataa	)

             # [rows * columns, 2 + patch_height * patch_width * image_channels]
             _a    : Any     					=torch.cat([row_ids, col_ids, patches]     ,						-1	)

             # [max_patches, 2 + patch_height * patch_width * image_channels]
             _a    : Any     					=torch.nn.functional.pad(UpperCamelCase_     ,						[0, 0, 0, max_patches - (rows * columns)]	).float()

             _a    : Union[str, Any]     					=to_numpy_array(UpperCamelCase_	)

             return result






      def __UpperCAmelCase       ( self			:List[str]     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:Dict = None     ,						**SCREAMING_SNAKE_CASE			:Tuple	)       ->					Any:




             '''simple docstring'''


             if image.dtype == np.uinta:
                    _a    : Dict     					=image.astype(np.floataa	)

             # take mean across the whole `image`
             _a    : Optional[int]     					=np.mean(UpperCamelCase_	)
             _a    : str     					=np.std(UpperCamelCase_	)
             _a    : int     					=max(UpperCamelCase_     ,						1.0 / math.sqrt(np.prod(image.shape	)	)	)

             return normalize(UpperCamelCase_     ,						mean=UpperCamelCase_     ,						std=UpperCamelCase_     ,						**UpperCamelCase_	)






      def __UpperCAmelCase       ( self			:str     ,						SCREAMING_SNAKE_CASE			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Optional[Any] = None     ,						SCREAMING_SNAKE_CASE			:List[Any] = None     ,						SCREAMING_SNAKE_CASE			:Dict = None     ,						SCREAMING_SNAKE_CASE			:List[str] = None     ,						SCREAMING_SNAKE_CASE			:Tuple = None     ,						SCREAMING_SNAKE_CASE			:List[str] = None     ,						SCREAMING_SNAKE_CASE			:Tuple = ChannelDimension.FIRST     ,						**SCREAMING_SNAKE_CASE			:str     ,						)       ->					str:




             '''simple docstring'''
             _a    : List[str]     					=do_normalize if do_normalize is not None else self.do_normalize
             _a    : Tuple     					=do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb
             _a    : List[Any]     					=patch_size if patch_size is not None else self.patch_size
             _a    : Any     					=max_patches if max_patches is not None else self.max_patches
             _a    : Optional[int]     					=self.is_vqa

             if kwargs.get("""data_format"""     ,						UpperCamelCase_	) is not None:
                    raise ValueError("""data_format is not an accepted input as the outputs are """	)

             _a    : Dict     					=make_list_of_images(UpperCamelCase_	)

             if not valid_images(UpperCamelCase_	):
                    raise ValueError(
                        """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                        """torch.Tensor, tf.Tensor or jax.ndarray."""	)

             # PIL RGBA images are converted to RGB
             if do_convert_rgb:
                    _a    : List[str]     					=[convert_to_rgb(UpperCamelCase_	) for image in images]

             # All transformations expect numpy arrays.
             _a    : Optional[int]     					=[to_numpy_array(UpperCamelCase_	) for image in images]

             if is_vqa:
                    if header_text is None:
                           raise ValueError("""A header text must be provided for VQA models."""	)
                    _a    : Tuple     					=kwargs.pop("""font_bytes"""     ,						UpperCamelCase_	)
                    _a    : Any     					=kwargs.pop("""font_path"""     ,						UpperCamelCase_	)

                    if isinstance(UpperCamelCase_     ,						UpperCamelCase_	):
                           _a    : Any     					=[header_text] * len(UpperCamelCase_	)

                    _a    : str     					=[
                        render_header(UpperCamelCase_     ,						header_text[i]     ,						font_bytes=UpperCamelCase_     ,						font_path=UpperCamelCase_	)
                        for i, image in enumerate(UpperCamelCase_	)
                    ]

             if do_normalize:
                    _a    : Optional[int]     					=[self.normalize(image=UpperCamelCase_	) for image in images]

             # convert to torch tensor and permute
             _a    : Optional[Any]     					=[
                 self.extract_flattened_patches(image=UpperCamelCase_     ,						max_patches=UpperCamelCase_     ,						patch_size=UpperCamelCase_	)
                 for image in images
             ]

             # create attention mask in numpy
             _a    : Optional[int]     					=[(image.sum(axis=-1	) != 0).astype(np.floataa	) for image in images]

             _a    : List[str]     					=BatchFeature(
                 data={"""flattened_patches""": images, """attention_mask""": attention_masks}     ,						tensor_type=UpperCamelCase_	)

             return encoded_outputs






'''simple docstring'''
from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream
class    lowercase	(							A__					):

		"""simple docstring"""

		def __init__(		self    ,   UpperCamelCase_    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = False    ,   UpperCamelCase_ = False    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = None    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							super().__init__(
							    features=UpperCamelCase_    ,   cache_dir=UpperCamelCase_    ,   keep_in_memory=UpperCamelCase_    ,   streaming=UpperCamelCase_    ,   num_proc=UpperCamelCase_    ,   **UpperCamelCase_    ,   )
							UpperCamelCase__   :Any												= Generator(
							    cache_dir=UpperCamelCase_    ,   features=UpperCamelCase_    ,   generator=UpperCamelCase_    ,   gen_kwargs=UpperCamelCase_    ,   **UpperCamelCase_    ,   )




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							if self.streaming:
												UpperCamelCase__   :Optional[Any]												= self.builder.as_streaming_dataset(split='''train'''							)
							# Build regular (map-style) dataset
							else:
												UpperCamelCase__   :Optional[int]												= None
												UpperCamelCase__   :int												= None
												UpperCamelCase__   :Any												= None
												UpperCamelCase__   :Any												= None

												self.builder.download_and_prepare(
												    download_config=UpperCamelCase_    ,   download_mode=UpperCamelCase_    ,   verification_mode=UpperCamelCase_    ,   base_path=UpperCamelCase_    ,   num_proc=self.num_proc    ,   )
												UpperCamelCase__   :List[Any]												= self.builder.as_dataset(
												    split='''train'''    ,   verification_mode=UpperCamelCase_    ,   in_memory=self.keep_in_memory							)
							return dataset