


import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


SCREAMING_SNAKE_CASE			: int				   =					"hf-internal-testing/tiny-random-bert"
SCREAMING_SNAKE_CASE			: Optional[int]				   =					os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
SCREAMING_SNAKE_CASE			: List[Any]				   =					"9b8c223d42b2188cb49d29af482996f9d0f3e5a6"






class _lowerCamelCase(		unittest.TestCase  ):
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				List[Any] 						=						cached_file(lowerCamelCase, lowerCamelCase)
           # Should have downloaded the file in here
           self.assertTrue(os.path.isdir(lowerCamelCase))
           # Cache should contain at least those three subfolders:
           for subfolder in ["blobs", "refs", "snapshots"]:
               self.assertTrue(os.path.isdir(os.path.join(lowerCamelCase, lowerCamelCase)))
           with open(os.path.join(lowerCamelCase, 'refs', 'main')) as f:
               _lowercase			:				Optional[int] 						=						f.read()
           self.assertEqual(lowerCamelCase, os.path.join(lowerCamelCase, 'snapshots', lowerCamelCase, lowerCamelCase))
           self.assertTrue(os.path.isfile(lowerCamelCase))

           # File is cached at the same place the second time.
           _lowercase			:				Any 						=						cached_file(lowerCamelCase, lowerCamelCase)
           self.assertEqual(lowerCamelCase, lowerCamelCase)

           # Using a specific revision to test the full commit hash.
           _lowercase			:				Dict 						=						cached_file(lowerCamelCase, lowerCamelCase, revision='9b8c223')
           self.assertEqual(lowerCamelCase, os.path.join(lowerCamelCase, 'snapshots', lowerCamelCase, lowerCamelCase))
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           with self.assertRaisesRegex(lowerCamelCase, 'is not a valid model identifier'):
               _lowercase			:				Tuple 						=						cached_file('tiny-random-bert', lowerCamelCase)

           with self.assertRaisesRegex(lowerCamelCase, 'is not a valid git identifier'):
               _lowercase			:				Optional[Any] 						=						cached_file(lowerCamelCase, lowerCamelCase, revision='aaaa')

           with self.assertRaisesRegex(lowerCamelCase, 'does not appear to have a file named'):
               _lowercase			:				List[str] 						=						cached_file(lowerCamelCase, 'conf')
       def      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           with self.assertRaisesRegex(lowerCamelCase, 'does not appear to have a file named'):
               _lowercase			:				str 						=						cached_file(lowerCamelCase, 'conf')

           with open(os.path.join(lowerCamelCase, 'refs', 'main')) as f:
               _lowercase			:				List[Any] 						=						f.read()
           self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase, '.no_exist', lowerCamelCase, 'conf')))

           _lowercase			:				List[Any] 						=						cached_file(lowerCamelCase, 'conf', _raise_exceptions_for_missing_entries=lowerCamelCase)
           self.assertIsNone(lowerCamelCase)

           _lowercase			:				Optional[Any] 						=						cached_file(lowerCamelCase, 'conf', local_files_only=lowerCamelCase, _raise_exceptions_for_missing_entries=lowerCamelCase)
           self.assertIsNone(lowerCamelCase)

           _lowercase			:				Dict 						=						mock.Mock()
           _lowercase			:				Any 						=						5_00
           _lowercase			:				Optional[int] 						=						{}
           _lowercase			:				Optional[int] 						=						HTTPError
           _lowercase			:				List[str] 						=						{}

           # Under the mock environment we get a 500 error when trying to reach the tokenizer.
           with mock.patch('requests.Session.request', return_value=lowerCamelCase) as mock_head:
               _lowercase			:				Dict 						=						cached_file(lowerCamelCase, 'conf', _raise_exceptions_for_connection_errors=lowerCamelCase)
               self.assertIsNone(lowerCamelCase)
               # This check we did call the fake head request
               mock_head.assert_called()
       def      UpperCamelCase	(   self)			->  Dict:




           """simple docstring"""

           self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only', lowerCamelCase))
           self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only', lowerCamelCase))
           self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only', lowerCamelCase))
       def      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

           self.assertIsNone(get_file_from_repo('bert-base-cased', 'ahah.txt'))

           # The function raises if the repository does not exist.
           with self.assertRaisesRegex(lowerCamelCase, 'is not a valid model identifier'):
               get_file_from_repo('bert-base-case', lowerCamelCase)

           # The function raises if the revision does not exist.
           with self.assertRaisesRegex(lowerCamelCase, 'is not a valid git identifier'):
               get_file_from_repo('bert-base-cased', lowerCamelCase, revision='ahaha')

           _lowercase			:				int 						=						get_file_from_repo('bert-base-cased', lowerCamelCase)
           # The name is the cached name which is not very easy to test, so instead we load the content.
           _lowercase			:				Tuple 						=						json.loads(open(lowerCamelCase, 'r').read())
           self.assertEqual(config['hidden_size'], 7_68)




       def      UpperCamelCase	(   self)			->  str:




           """simple docstring"""

           with tempfile.TemporaryDirectory() as tmp_dir:
               _lowercase			:				Tuple 						=						Path(lowerCamelCase) / 'a.txt'
               filename.touch()
               self.assertEqual(get_file_from_repo(lowerCamelCase, 'a.txt'), str(lowerCamelCase))

               self.assertIsNone(get_file_from_repo(lowerCamelCase, 'b.txt'))




from typing import List

import datasets
from datasets.tasks import AudioClassification

from ..folder_based_builder import folder_based_builder


lowerCamelCase__     :		Any        = datasets.utils.logging.get_logger(__name__)



class    _snake_case (						folder_based_builder.FolderBasedBuilderConfig				):
 __lowerCAmelCase	:  bool	   =      None
 __lowerCAmelCase	:  bool	   =      None



class    _snake_case (						folder_based_builder.FolderBasedBuilder				):
 __lowerCAmelCase	:  Optional[Any]	   =      datasets.Audio()
 __lowerCAmelCase	:  Union[str, Any]	   =      'audio'
 __lowerCAmelCase	:  str	   =      AudioFolderConfig
 __lowerCAmelCase	:  List[str]  # definition at the bottom of the script
 __lowerCAmelCase	:  Optional[int]	   =      AudioClassification(audio_column='audio'	,    label_column='label'				)

lowerCamelCase__     :		int        = [
    """.aiff""",
    """.au""",
    """.avr""",
    """.caf""",
    """.flac""",
    """.htk""",
    """.svx""",
    """.mat4""",
    """.mat5""",
    """.mpc2k""",
    """.ogg""",
    """.paf""",
    """.pvf""",
    """.raw""",
    """.rf64""",
    """.sd2""",
    """.sds""",
    """.ircam""",
    """.voc""",
    """.w64""",
    """.wav""",
    """.nist""",
    """.wavex""",
    """.wve""",
    """.xi""",
    """.mp3""",
    """.opus""",
]
lowerCamelCase__     :		int        = AUDIO_EXTENSIONS






