





"""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 		__snake_case (		unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
 _lowerCamelCase				=					TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Tuple									=					TextaTextGenerationPipeline(model=__lowerCamelCase ,						tokenizer=__lowerCamelCase  )
     return generator, ["Something to write", "Something else"]

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						str									=					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'''  )  )

     __A   :						Union[str, Any]									=					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  )}],
         ] ,						)

     __A   :						Any									=					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  UpperCamelCase__(   self  ):



     '''simple docstring'''




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

     __A   :						Optional[int]									=					3
     __A   :						Tuple									=					generator(
         '''Something there''' ,						num_return_sequences=__lowerCamelCase ,						num_beams=__lowerCamelCase ,						)
     __A   :						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  )

     __A   :						int									=					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  )},
         ] ,						)
     __A   :						List[Any]									=					generator.model.config.eos_token_id
     __A   :						List[str]									=					'''<pad>'''
     __A   :						int									=					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  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						int									=					pipeline('''text2text-generation''' ,						model='''patrickvonplaten/t5-tiny-random''' ,						framework='''tf'''  )
     # do_sample=False necessary for reproducibility
     __A   :						List[Any]									=					generator('''Something there''' ,						do_sample=__lowerCamelCase  )
     self.assertEqual(__lowerCamelCase ,						[{'''generated_text''': ''''''}]  )






"""simple docstring"""



from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					""""""
 _lowerCamelCase				=					"""hf-legacy"""  # "hf://"" is reserved for hffs

 def __init__(   self ,						__lowerCamelCase = None ,						__lowerCamelCase = None ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(self ,						**__lowerCamelCase  )
     __A   :						int									=					repo_info
     __A   :						Optional[int]									=					token
     __A   :						int									=					None

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     if self.dir_cache is None:
         __A   :						int									=					{}
         for hf_file in self.repo_info.siblings:
             # TODO(QL): add sizes
             __A   :						Tuple									=					{
                 '''name''': hf_file.rfilename,
                 '''size''': None,
                 '''type''': '''file''',
             }
             self.dir_cache.update(
                 {
                     str(__lowerCamelCase  ): {'''name''': str(__lowerCamelCase  ), '''size''': None, '''type''': '''directory'''}
                     for d in list(PurePosixPath(hf_file.rfilename  ).parents  )[:-1]
                 }  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = "rb" ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     if not isinstance(self.repo_info ,						__lowerCamelCase  ):
         raise NotImplementedError(F"""Open is only implemented for dataset repositories, but got {self.repo_info}"""  )
     __A   :						Union[str, Any]									=					hf_hub_url(self.repo_info.id ,						__lowerCamelCase ,						revision=self.repo_info.sha  )
     return fsspec.open(
         __lowerCamelCase ,						mode=__lowerCamelCase ,						headers=get_authentication_headers_for_url(__lowerCamelCase ,						use_auth_token=self.token  ) ,						client_kwargs={'''trust_env''': True} ,						).open()

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     self._get_dirs()
     __A   :						Optional[Any]									=					self._strip_protocol(__lowerCamelCase  )
     if path in self.dir_cache:
         return self.dir_cache[path]
     else:
         raise FileNotFoundError(__lowerCamelCase  )







 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=False ,						**__lowerCamelCase  ):



     '''simple docstring'''




     self._get_dirs()
     __A   :						Any									=					PurePosixPath(path.strip('''/'''  )  )
     __A   :						Any									=					{}
     for p, f in self.dir_cache.items():
         __A   :						List[Any]									=					PurePosixPath(p.strip('''/'''  )  )
         __A   :						Dict									=					p.parent
         if root == path:
             __A   :						Union[str, Any]									=					f
     __A   :						List[str]									=					list(paths.values()  )
     if detail:
         return out
     else:
         return sorted(f['''name'''] for f in out  )




