






"""simple docstring"""


import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
   from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

   from .test_feature_extraction_speechta import floats_list


_UpperCamelCase							:       str       					=		get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')






@require_torch
class 		snake_case    (    unittest.TestCase			):





    def 							lowerCamelCase__ (				self      :   Optional[Any]     ):


      '''simple docstring'''




      a	:     List[Any]							=  tempfile.mkdtemp()

      a	:     Optional[int]							=  SpeechTaTokenizer(A     )
      tokenizer.save_pretrained(self.tmpdirname     )

      a	:     Dict							=  {
          'feature_size': 1,
          'padding_value': 0.0,
          'sampling_rate': 1_6_0_0_0,
          'do_normalize': False,
          'num_mel_bins': 8_0,
          'hop_length': 1_6,
          'win_length': 6_4,
          'win_function': 'hann_window',
          'fmin': 8_0,
          'fmax': 7_6_0_0,
          'mel_floor': 1E-10,
          'reduction_factor': 2,
          'return_attention_mask': True,
      }

      a	:     Union[str, Any]							=  os.path.join(self.tmpdirname							,						A     )
      with open(self.feature_extraction_file							,						'w'							,						encoding='utf-8'     ) as fp:
        fp.write(json.dumps(A     ) + '\n'     )





    def 							lowerCamelCase__ (				self      :   Optional[Any]							,						**A      :   Dict     ):


      '''simple docstring'''




      return SpeechTaTokenizer.from_pretrained(self.tmpdirname							,						**A     )





    def 							lowerCamelCase__ (				self      :   List[str]							,						**A      :   str     ):


      '''simple docstring'''




      return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname							,						**A     )





    def 							lowerCamelCase__ (				self      :   Any     ):


      '''simple docstring'''




      shutil.rmtree(self.tmpdirname     )





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      a	:     int							=  self.get_tokenizer()
      a	:     Any							=  self.get_feature_extractor()

      a	:     Optional[Any]							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      processor.save_pretrained(self.tmpdirname     )
      a	:     Union[str, Any]							=  SpeechTaProcessor.from_pretrained(self.tmpdirname     )

      self.assertEqual(processor.tokenizer.get_vocab()							,						tokenizer.get_vocab()     )
      self.assertIsInstance(processor.tokenizer							,						A     )

      self.assertEqual(processor.feature_extractor.to_json_string()							,						feature_extractor.to_json_string()     )
      self.assertIsInstance(processor.feature_extractor							,						A     )





    def 							lowerCamelCase__ (				self      :   List[str]     ):


      '''simple docstring'''




      a	:     List[str]							=  SpeechTaProcessor(tokenizer=self.get_tokenizer()							,						feature_extractor=self.get_feature_extractor()     )
      processor.save_pretrained(self.tmpdirname     )

      a	:     Union[str, Any]							=  self.get_tokenizer(bos_token='(BOS)'							,						eos_token='(EOS)'     )
      a	:     Tuple							=  self.get_feature_extractor(do_normalize=A							,						padding_value=1.0     )

      a	:     Optional[Any]							=  SpeechTaProcessor.from_pretrained(
          self.tmpdirname							,						bos_token='(BOS)'							,						eos_token='(EOS)'							,						do_normalize=A							,						padding_value=1.0     )

      self.assertEqual(processor.tokenizer.get_vocab()							,						tokenizer_add_kwargs.get_vocab()     )
      self.assertIsInstance(processor.tokenizer							,						A     )

      self.assertEqual(processor.feature_extractor.to_json_string()							,						feature_extractor_add_kwargs.to_json_string()     )
      self.assertIsInstance(processor.feature_extractor							,						A     )





    def 							lowerCamelCase__ (				self      :   int     ):


      '''simple docstring'''




      a	:     Any							=  self.get_feature_extractor()
      a	:     str							=  self.get_tokenizer()

      a	:     Any							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      a	:     Optional[int]							=  floats_list((3, 1_0_0_0)     )

      a	:     str							=  feature_extractor(audio=A							,						return_tensors='np'     )
      a	:     List[Any]							=  processor(audio=A							,						return_tensors='np'     )

      for key in input_feat_extract.keys():
        self.assertAlmostEqual(input_feat_extract[key].sum()							,						input_processor[key].sum()							,						delta=1E-2     )





    def 							lowerCamelCase__ (				self      :   str     ):


      '''simple docstring'''




      a	:     Optional[Any]							=  self.get_feature_extractor()
      a	:     Optional[int]							=  self.get_tokenizer()

      a	:     Union[str, Any]							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      a	:     Tuple							=  floats_list((3, 1_0_0_0)     )

      a	:     List[str]							=  feature_extractor(audio_target=A							,						return_tensors='np'     )
      a	:     List[str]							=  processor(audio_target=A							,						return_tensors='np'     )

      for key in input_feat_extract.keys():
        self.assertAlmostEqual(input_feat_extract[key].sum()							,						input_processor[key].sum()							,						delta=1E-2     )





    def 							lowerCamelCase__ (				self      :   List[Any]     ):


      '''simple docstring'''




      a	:     Dict							=  self.get_feature_extractor()
      a	:     Union[str, Any]							=  self.get_tokenizer()

      a	:     Union[str, Any]							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      a	:     Union[str, Any]							=  'This is a test string'

      a	:     List[str]							=  processor(text=A     )
      a	:     str							=  tokenizer(A     )

      for key in encoded_tok.keys():
        self.assertListEqual(encoded_tok[key]							,						encoded_processor[key]     )





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      a	:     Dict							=  self.get_feature_extractor()
      a	:     Optional[int]							=  self.get_tokenizer()

      a	:     Tuple							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      a	:     Tuple							=  'This is a test string'

      a	:     int							=  processor(text_target=A     )
      a	:     List[str]							=  tokenizer(A     )

      for key in encoded_tok.keys():
        self.assertListEqual(encoded_tok[key]							,						encoded_processor[key]     )





    def 							lowerCamelCase__ (				self      :   str     ):


      '''simple docstring'''




      a	:     Any							=  self.get_feature_extractor()
      a	:     Union[str, Any]							=  self.get_tokenizer()

      a	:     Optional[int]							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      a	:     str							=  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      a	:     Union[str, Any]							=  processor.batch_decode(A     )
      a	:     Optional[Any]							=  tokenizer.batch_decode(A     )

      self.assertListEqual(A							,						A     )





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


      '''simple docstring'''




      a	:     Dict							=  self.get_feature_extractor()
      a	:     Optional[int]							=  self.get_tokenizer()

      a	:     Union[str, Any]							=  SpeechTaProcessor(tokenizer=A							,						feature_extractor=A     )

      self.assertListEqual(
          processor.model_input_names							,						feature_extractor.model_input_names							,						msg='`processor` and `feature_extractor` model input names do not match'							,						)












"""simple docstring"""


import json

import requests

from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info




def      snake_case    (A_ :Union[str, Any] ):







  '''simple docstring'''

  class 		snake_case    :





      def __init__(				self      :   Tuple							,						A      :   Dict     ):


        '''simple docstring'''




        assert isinstance(A							,						(bytes, str)     )
        a	:     Union[str, Any]							=  content





      def 							lowerCamelCase__ (				self      :   str     ):


        '''simple docstring'''




        return json.loads(self.content     )

  def mock_response(*A_ :Union[str, Any]      ,     **A_ :Tuple ):
    assert args[0] == AUTHENTICATED_USER_ENDPOINT
    assert "Authorization" in kwargs["headers"]
    assert kwargs["headers"]["Authorization"].startswith('token ' )
    assert "Accept" in kwargs["headers"]
    return FakeResponse(B'{"login":"test","id":1}' )

  monkeypatch.setattr(A_      ,     'get'      ,     A_ )
  a	:     List[str]							=  fetch_github_info('token' )
  assert result["login"] == "test"
  assert result["id"] == 1




