


'''simple docstring'''





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
    import torch




@is_pipeline_test
class 				UpperCAmelCase			(			unittest.TestCase						):
 UpperCAmelCase				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
 UpperCAmelCase				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	List[str] ):
    UpperCAmelCase__	:Optional[int]      =      TextaTextGenerationPipeline(model=__lowerCamelCase			,       tokenizer=__lowerCamelCase )
    return generator, ["Something to write", "Something else"]


 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any]			,       __lowerCamelCase							:	int			,       __lowerCamelCase							:	str ):
    UpperCAmelCase__	:Any      =      generator('''Something there''' )
    self.assertEqual(__lowerCamelCase			,       [{'''generated_text''': ANY(__lowerCamelCase )}] )
    # These are encoder decoder, they don't just append to incoming string
    self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there''' ) )

    UpperCAmelCase__	:str      =      generator(['''This is great !''', '''Something else''']			,       num_return_sequences=2			,       do_sample=__lowerCamelCase )
    self.assertEqual(
        __lowerCamelCase			,       [
            [{'''generated_text''': ANY(__lowerCamelCase )}, {'''generated_text''': ANY(__lowerCamelCase )}],
            [{'''generated_text''': ANY(__lowerCamelCase )}, {'''generated_text''': ANY(__lowerCamelCase )}],
        ]			,       )

    UpperCAmelCase__	:List[str]      =      generator(
        ['''This is great !''', '''Something else''']			,       num_return_sequences=2			,       batch_size=2			,       do_sample=__lowerCamelCase )
    self.assertEqual(
        __lowerCamelCase			,       [
            [{'''generated_text''': ANY(__lowerCamelCase )}, {'''generated_text''': ANY(__lowerCamelCase )}],
            [{'''generated_text''': ANY(__lowerCamelCase )}, {'''generated_text''': ANY(__lowerCamelCase )}],
        ]			,       )

    with self.assertRaises(__lowerCamelCase ):
       generator(4 )


 @require_torch
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int] ):
    UpperCAmelCase__	:List[str]      =      pipeline('''text2text-generation'''			,       model='''patrickvonplaten/t5-tiny-random'''			,       framework='''pt''' )
    # do_sample=False necessary for reproducibility
    UpperCAmelCase__	:Union[str, Any]      =      generator('''Something there'''			,       do_sample=__lowerCamelCase )
    self.assertEqual(__lowerCamelCase			,       [{'''generated_text''': ''''''}] )

    UpperCAmelCase__	:Tuple      =      3
    UpperCAmelCase__	:int      =      generator(
        '''Something there'''			,       num_return_sequences=__lowerCamelCase			,       num_beams=__lowerCamelCase			,       )
    UpperCAmelCase__	:Union[str, Any]      =      [
        {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''},
        {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''},
        {'''generated_text''': ''''''},
    ]
    self.assertEqual(__lowerCamelCase			,       __lowerCamelCase )

    UpperCAmelCase__	:Union[str, Any]      =      generator('''This is a test'''			,       do_sample=__lowerCamelCase			,       num_return_sequences=2			,       return_tensors=__lowerCamelCase )
    self.assertEqual(
        __lowerCamelCase			,       [
            {'''generated_token_ids''': ANY(torch.Tensor )},
            {'''generated_token_ids''': ANY(torch.Tensor )},
        ]			,       )
    UpperCAmelCase__	:str      =      generator.model.config.eos_token_id
    UpperCAmelCase__	:Tuple      =      '''<pad>'''
    UpperCAmelCase__	:Dict      =      generator(
        ['''This is a test''', '''This is a second test''']			,       do_sample=__lowerCamelCase			,       num_return_sequences=2			,       batch_size=2			,       return_tensors=__lowerCamelCase			,       )
    self.assertEqual(
        __lowerCamelCase			,       [
            [
                {'''generated_token_ids''': ANY(torch.Tensor )},
                {'''generated_token_ids''': ANY(torch.Tensor )},
            ],
            [
                {'''generated_token_ids''': ANY(torch.Tensor )},
                {'''generated_token_ids''': ANY(torch.Tensor )},
            ],
        ]			,       )





 @require_tf
 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[Any] ):
    UpperCAmelCase__	:str      =      pipeline('''text2text-generation'''			,       model='''patrickvonplaten/t5-tiny-random'''			,       framework='''tf''' )
    # do_sample=False necessary for reproducibility
    UpperCAmelCase__	:Dict      =      generator('''Something there'''			,       do_sample=__lowerCamelCase )
    self.assertEqual(__lowerCamelCase			,       [{'''generated_text''': ''''''}] )







'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCamelCase												=						{
    '''configuration_maskformer''': ['''MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MaskFormerConfig'''],
    '''configuration_maskformer_swin''': ['''MaskFormerSwinConfig'''],
}

try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase												=						['''MaskFormerFeatureExtractor''']
    __lowerCamelCase												=						['''MaskFormerImageProcessor''']


try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCamelCase												=						[
        '''MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''MaskFormerForInstanceSegmentation''',
        '''MaskFormerModel''',
        '''MaskFormerPreTrainedModel''',
    ]
    __lowerCamelCase												=						[
        '''MaskFormerSwinBackbone''',
        '''MaskFormerSwinModel''',
        '''MaskFormerSwinPreTrainedModel''',
    ]

if TYPE_CHECKING:
    from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
    from .configuration_maskformer_swin import MaskFormerSwinConfig

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_maskformer import MaskFormerFeatureExtractor
        from .image_processing_maskformer import MaskFormerImageProcessor
    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_maskformer import (
            MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            MaskFormerForInstanceSegmentation,
            MaskFormerModel,
            MaskFormerPreTrainedModel,
        )
        from .modeling_maskformer_swin import (
            MaskFormerSwinBackbone,
            MaskFormerSwinModel,
            MaskFormerSwinPreTrainedModel,
        )


else:
    import sys

    __lowerCamelCase												=						_LazyModule(__name__, globals()['''__file__'''], _import_structure)



