

"""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


SCREAMING_SNAKE_CASE_       :						List[Any]					=    get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')



@require_torch
class        a    (							unittest.TestCase					):



    """simple docstring"""



    def 					UpperCamelCase						(  self:			List[str]							):






         """simple docstring"""


         A__				      =	tempfile.mkdtemp()

         A__				      =	SpeechTaTokenizer(UpperCamelCase							)
         tokenizer.save_pretrained(self.tmpdirname							)

         A__				      =	{
             """feature_size""": 1,
             """padding_value""": 0.0,
             """sampling_rate""": 1_60_00,
             """do_normalize""": False,
             """num_mel_bins""": 80,
             """hop_length""": 16,
             """win_length""": 64,
             """win_function""": """hann_window""",
             """fmin""": 80,
             """fmax""": 76_00,
             """mel_floor""": 1e-1_0,
             """reduction_factor""": 2,
             """return_attention_mask""": True,
         }

         A__				      =	os.path.join(self.tmpdirname     ,    UpperCamelCase							)
         with open(self.feature_extraction_file     ,    """w"""     ,    encoding="""utf-8"""							) as fp:
              fp.write(json.dumps(UpperCamelCase							) + """\n"""							)

    def 					UpperCamelCase						(  self:			Tuple     ,    **UpperCamelCase:			Dict							):






         """simple docstring"""


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

    def 					UpperCamelCase						(  self:			str     ,    **UpperCamelCase:			Dict							):






         """simple docstring"""


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

    def 					UpperCamelCase						(  self:			Optional[int]							):






         """simple docstring"""


         shutil.rmtree(self.tmpdirname							)

    def 					UpperCamelCase						(  self:			List[Any]							):






         """simple docstring"""


         A__				      =	self.get_tokenizer()
         A__				      =	self.get_feature_extractor()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         processor.save_pretrained(self.tmpdirname							)
         A__				      =	SpeechTaProcessor.from_pretrained(self.tmpdirname							)

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

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

    def 					UpperCamelCase						(  self:			List[Any]							):






         """simple docstring"""


         A__				      =	SpeechTaProcessor(tokenizer=self.get_tokenizer()     ,    feature_extractor=self.get_feature_extractor()							)
         processor.save_pretrained(self.tmpdirname							)

         A__				      =	self.get_tokenizer(bos_token="""(BOS)"""     ,    eos_token="""(EOS)"""							)
         A__				      =	self.get_feature_extractor(do_normalize=UpperCamelCase     ,    padding_value=1.0							)

         A__				      =	SpeechTaProcessor.from_pretrained(
             self.tmpdirname     ,    bos_token="""(BOS)"""     ,    eos_token="""(EOS)"""     ,    do_normalize=UpperCamelCase     ,    padding_value=1.0							)

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

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

    def 					UpperCamelCase						(  self:			int							):






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         A__				      =	floats_list((3, 10_00)							)

         A__				      =	feature_extractor(audio=UpperCamelCase     ,    return_tensors="""np"""							)
         A__				      =	processor(audio=UpperCamelCase     ,    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 					UpperCamelCase						(  self:			Optional[Any]							):






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         A__				      =	floats_list((3, 10_00)							)

         A__				      =	feature_extractor(audio_target=UpperCamelCase     ,    return_tensors="""np"""							)
         A__				      =	processor(audio_target=UpperCamelCase     ,    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 					UpperCamelCase						(  self:			Dict							):






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         A__				      =	"""This is a test string"""

         A__				      =	processor(text=UpperCamelCase							)
         A__				      =	tokenizer(UpperCamelCase							)

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

    def 					UpperCamelCase						(  self:			int							):






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         A__				      =	"""This is a test string"""

         A__				      =	processor(text_target=UpperCamelCase							)
         A__				      =	tokenizer(UpperCamelCase							)

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

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






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         A__				      =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         A__				      =	processor.batch_decode(UpperCamelCase							)
         A__				      =	tokenizer.batch_decode(UpperCamelCase							)

         self.assertListEqual(UpperCamelCase     ,    UpperCamelCase							)





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






         """simple docstring"""


         A__				      =	self.get_feature_extractor()
         A__				      =	self.get_tokenizer()

         A__				      =	SpeechTaProcessor(tokenizer=UpperCamelCase     ,    feature_extractor=UpperCamelCase							)

         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 os

from ...utils import logging
from .utils import DataProcessor, InputExample


SCREAMING_SNAKE_CASE_       :						Dict					=    logging.get_logger(__name__)



class        a    (							_lowerCamelCase					):



    """simple docstring"""



    def __init__(  self:			Tuple     ,    UpperCamelCase:			Optional[Any]     ,    UpperCamelCase:			Optional[int]=None							):






         """simple docstring"""


         A__				      =	language
         A__				      =	train_language

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






         """simple docstring"""


         A__				      =	self.language if self.train_language is None else self.train_language
         A__				      =	self._read_tsv(os.path.join(UpperCamelCase     ,    f"""XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv"""							)							)
         A__				      =	[]
         for i, line in enumerate(UpperCamelCase							):
              if i == 0:
                   continue
              A__				      =	f"""train-{i}"""
              A__				      =	line[0]
              A__				      =	line[1]
              A__				      =	"""contradiction""" if line[2] == """contradictory""" else line[2]
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training input {text_a} is not a string"""							)
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training input {text_b} is not a string"""							)
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training label {label} is not a string"""							)
              examples.append(InputExample(guid=UpperCamelCase     ,    text_a=UpperCamelCase     ,    text_b=UpperCamelCase     ,    label=UpperCamelCase							)							)
         return examples

    def 					UpperCamelCase						(  self:			Dict     ,    UpperCamelCase:			Union[str, Any]							):






         """simple docstring"""


         A__				      =	self._read_tsv(os.path.join(UpperCamelCase     ,    """XNLI-1.0/xnli.test.tsv"""							)							)
         A__				      =	[]
         for i, line in enumerate(UpperCamelCase							):
              if i == 0:
                   continue
              A__				      =	line[0]
              if language != self.language:
                   continue
              A__				      =	f"""test-{i}"""
              A__				      =	line[6]
              A__				      =	line[7]
              A__				      =	line[1]
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training input {text_a} is not a string"""							)
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training input {text_b} is not a string"""							)
              if not isinstance(UpperCamelCase     ,    UpperCamelCase							):
                   raise ValueError(f"""Training label {label} is not a string"""							)
              examples.append(InputExample(guid=UpperCamelCase     ,    text_a=UpperCamelCase     ,    text_b=UpperCamelCase     ,    label=UpperCamelCase							)							)
         return examples





    def 					UpperCamelCase						(  self:			Tuple							):






         """simple docstring"""


         return ["contradiction", "entailment", "neutral"]

SCREAMING_SNAKE_CASE_       :						int					=    {
    'xnli': XnliProcessor,
}

SCREAMING_SNAKE_CASE_       :						List[Any]					=    {
    'xnli': 'classification',
}

SCREAMING_SNAKE_CASE_       :						str					=    {
    'xnli': 3,
}






