



'''simple docstring'''



from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
							from ..models.auto import AutoTokenizer
class 						lowerCamelCase_				:







						def        __lowercase     (	self :   Optional[int]					,     lowerCAmelCase__ :   Union[str, Any]					):

													"""simple docstring"""
													raise NotImplementedError()







						def        __lowercase     (	self :   Union[str, Any]					):

													"""simple docstring"""
													raise NotImplementedError()


class 						lowerCamelCase_				(       snake_case_      ):







						def __init__(	self :   Dict					,     lowerCAmelCase__ :   "AutoTokenizer"					,     lowerCAmelCase__ :   bool = False					,     **lowerCAmelCase__ :   List[str]					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   List[str]         =      tokenizer
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      skip_prompt
													SCREAMING_SNAKE_CASE       :   int         =      decode_kwargs

													# variables used in the streaming process
													SCREAMING_SNAKE_CASE       :   Tuple         =      []
													SCREAMING_SNAKE_CASE       :   List[str]         =      0
													SCREAMING_SNAKE_CASE       :   Tuple         =      True







						def        __lowercase     (	self :   Tuple					,     lowerCAmelCase__ :   Optional[Any]					):

													"""simple docstring"""
													if len(value.shape					) > 1 and value.shape[0] > 1:
																				raise ValueError('''TextStreamer only supports batch size 1'''					)
													elif len(value.shape					) > 1:
																				SCREAMING_SNAKE_CASE       :   List[str]         =      value[0]

													if self.skip_prompt and self.next_tokens_are_prompt:
																				SCREAMING_SNAKE_CASE       :   str         =      False
																				return

													# Add the new token to the cache and decodes the entire thing.
													self.token_cache.extend(value.tolist()					)
													SCREAMING_SNAKE_CASE       :   Tuple         =      self.tokenizer.decode(self.token_cache					,     **self.decode_kwargs					)

													# After the symbol for a new line, we flush the cache.
													if text.endswith('''\n'''					):
																				SCREAMING_SNAKE_CASE       :   Tuple         =      text[self.print_len :]
																				SCREAMING_SNAKE_CASE       :   List[str]         =      []
																				SCREAMING_SNAKE_CASE       :   List[str]         =      0
													# If the last token is a CJK character, we print the characters.
													elif len(lowerCAmelCase__					) > 0 and self._is_chinese_char(ord(text[-1]					)					):
																				SCREAMING_SNAKE_CASE       :   str         =      text[self.print_len :]
																				self.print_len += len(lowerCAmelCase__					)
													# Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
													# which may change with the subsequent token -- there are probably smarter ways to do this!)
													else:
																				SCREAMING_SNAKE_CASE       :   Optional[Any]         =      text[self.print_len : text.rfind(''' '''					) + 1]
																				self.print_len += len(lowerCAmelCase__					)

													self.on_finalized_text(lowerCAmelCase__					)







						def        __lowercase     (	self :   Optional[int]					):

													"""simple docstring"""
													# Flush the cache, if it exists
													if len(self.token_cache					) > 0:
																				SCREAMING_SNAKE_CASE       :   Tuple         =      self.tokenizer.decode(self.token_cache					,     **self.decode_kwargs					)
																				SCREAMING_SNAKE_CASE       :   Dict         =      text[self.print_len :]
																				SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      []
																				SCREAMING_SNAKE_CASE       :   List[str]         =      0
													else:
																				SCREAMING_SNAKE_CASE       :   Any         =      ''''''

													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      True
													self.on_finalized_text(lowerCAmelCase__					,     stream_end=lowerCAmelCase__					)







						def        __lowercase     (	self :   Optional[int]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   bool = False					):

													"""simple docstring"""
													print(lowerCAmelCase__					,     flush=lowerCAmelCase__					,     end='''''' if not stream_end else None					)







						def        __lowercase     (	self :   List[Any]					,     lowerCAmelCase__ :   Optional[Any]					):

													"""simple docstring"""
													# This defines a "chinese character" as anything in the CJK Unicode block:
													#   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
													#
													# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
													# despite its name. The modern Korean Hangul alphabet is a different block,
													# as is Japanese Hiragana and Katakana. Those alphabets are used to write
													# space-separated words, so they are not treated specially and handled
													# like the all of the other languages.
													if (
													    (cp >= 0X4e_00 and cp <= 0X9f_ff)
													    or (cp >= 0X34_00 and cp <= 0X4d_bf)  #
													    or (cp >= 0X2_00_00 and cp <= 0X2_a6_df)  #
													    or (cp >= 0X2_a7_00 and cp <= 0X2_b7_3f)  #
													    or (cp >= 0X2_b7_40 and cp <= 0X2_b8_1f)  #
													    or (cp >= 0X2_b8_20 and cp <= 0X2_ce_af)  #
													    or (cp >= 0Xf9_00 and cp <= 0Xfa_ff)
													    or (cp >= 0X2_f8_00 and cp <= 0X2_fa_1f)  #
													):  #
																				return True

													return False


class 						lowerCamelCase_				(       snake_case_      ):







						def __init__(	self :   int					,     lowerCAmelCase__ :   "AutoTokenizer"					,     lowerCAmelCase__ :   bool = False					,     lowerCAmelCase__ :   Optional[float] = None					,     **lowerCAmelCase__ :   Any					):

													"""simple docstring"""
													super().__init__(lowerCAmelCase__					,     lowerCAmelCase__					,     **lowerCAmelCase__					)
													SCREAMING_SNAKE_CASE       :   str         =      Queue()
													SCREAMING_SNAKE_CASE       :   List[Any]         =      None
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      timeout







						def        __lowercase     (	self :   List[Any]					,     lowerCAmelCase__ :   str					,     lowerCAmelCase__ :   bool = False					):

													"""simple docstring"""
													self.text_queue.put(lowerCAmelCase__					,     timeout=self.timeout					)
													if stream_end:
																				self.text_queue.put(self.stop_signal					,     timeout=self.timeout					)







						def __iter__(	self :   Any					):

													"""simple docstring"""
													return self







						def        __lowercase     (	self :   Any					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Any         =      self.text_queue.get(timeout=self.timeout					)
													if value == self.stop_signal:
																				raise StopIteration()
													else:
																				return value









'''simple docstring'''



import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import DonutImageProcessor
class 						lowerCamelCase_				(       unittest.TestCase      ):







						def __init__(	self :   Dict					,     lowerCAmelCase__ :   Union[str, Any]					,     lowerCAmelCase__ :   Dict=7					,     lowerCAmelCase__ :   List[str]=3					,     lowerCAmelCase__ :   Union[str, Any]=18					,     lowerCAmelCase__ :   Tuple=30					,     lowerCAmelCase__ :   Any=4_00					,     lowerCAmelCase__ :   Optional[int]=True					,     lowerCAmelCase__ :   List[Any]=None					,     lowerCAmelCase__ :   int=True					,     lowerCAmelCase__ :   Optional[Any]=False					,     lowerCAmelCase__ :   Any=True					,     lowerCAmelCase__ :   Optional[Any]=True					,     lowerCAmelCase__ :   Union[str, Any]=[0.5, 0.5, 0.5]					,     lowerCAmelCase__ :   List[str]=[0.5, 0.5, 0.5]					,     ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      parent
													SCREAMING_SNAKE_CASE       :   int         =      batch_size
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      num_channels
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      image_size
													SCREAMING_SNAKE_CASE       :   Any         =      min_resolution
													SCREAMING_SNAKE_CASE       :   int         =      max_resolution
													SCREAMING_SNAKE_CASE       :   Tuple         =      do_resize
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      size if size is not None else {'''height''': 18, '''width''': 20}
													SCREAMING_SNAKE_CASE       :   List[Any]         =      do_thumbnail
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      do_align_axis
													SCREAMING_SNAKE_CASE       :   Tuple         =      do_pad
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      do_normalize
													SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      image_mean
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      image_std







						def        __lowercase     (	self :   Union[str, Any]					):

													"""simple docstring"""
													return {
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "do_thumbnail": self.do_thumbnail,
													    "do_align_long_axis": self.do_align_axis,
													    "do_pad": self.do_pad,
													    "do_normalize": self.do_normalize,
													    "image_mean": self.image_mean,
													    "image_std": self.image_std,
													}


@require_torch
@require_vision
class 						lowerCamelCase_				(       snake_case_     ,					unittest.TestCase      ):
						_lowerCAmelCase  : Any						  =					DonutImageProcessor if is_vision_available() else None







						def        __lowercase     (	self :   str					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Tuple         =      DonutImageProcessingTester(self					)







						@property
						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													return self.image_processor_tester.prepare_image_processor_dict()







						def        __lowercase     (	self :   Union[str, Any]					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   str         =      self.image_processing_class(**self.image_processor_dict					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_resize'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''size'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_thumbnail'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_align_long_axis'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_pad'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''do_normalize'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''image_mean'''					)					)
													self.assertTrue(hasattr(lowerCAmelCase__					,     '''image_std'''					)					)







						def        __lowercase     (	self :   Any					):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      self.image_processing_class.from_dict(self.image_processor_dict					)
													self.assertEqual(image_processor.size					,     {'''height''': 18, '''width''': 20}					)

													SCREAMING_SNAKE_CASE       :   List[str]         =      self.image_processing_class.from_dict(self.image_processor_dict					,     size=42					)
													self.assertEqual(image_processor.size					,     {'''height''': 42, '''width''': 42}					)

													# Previous config had dimensions in (width, height) order
													SCREAMING_SNAKE_CASE       :   Dict         =      self.image_processing_class.from_dict(self.image_processor_dict					,     size=(42, 84)					)
													self.assertEqual(image_processor.size					,     {'''height''': 84, '''width''': 42}					)







						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													pass







						@is_flaky()
						def        __lowercase     (	self :   int					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[int]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random PIL images
													SCREAMING_SNAKE_CASE       :   List[str]         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     Image.Image					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   Any         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )







						@is_flaky()
						def        __lowercase     (	self :   Union[str, Any]					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random numpy tensors
													SCREAMING_SNAKE_CASE       :   Tuple         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					,     numpify=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     np.ndarray					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   int         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   int         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )







						@is_flaky()
						def        __lowercase     (	self :   List[Any]					):

													"""simple docstring"""
													# Initialize image_processing
													SCREAMING_SNAKE_CASE       :   Optional[Any]         =      self.image_processing_class(**self.image_processor_dict					)
													# create random PyTorch tensors
													SCREAMING_SNAKE_CASE       :   Tuple         =      prepare_image_inputs(self.image_processor_tester					,     equal_resolution=lowerCAmelCase__					,     torchify=lowerCAmelCase__					)
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase__					,     torch.Tensor					)

													# Test not batched input
													SCREAMING_SNAKE_CASE       :   str         =      image_processing(image_inputs[0]					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )

													# Test batched
													SCREAMING_SNAKE_CASE       :   List[str]         =      image_processing(lowerCAmelCase__					,     return_tensors='''pt'''					).pixel_values
													self.assertEqual(
													    encoded_images.shape					,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size['''height'''],
													        self.image_processor_tester.size['''width'''],
													    )					,     )




