





'''simple docstring'''
import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ASTConfig
from transformers.testing_utils import require_torch, require_torchaudio, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_torchaudio_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import ASTForAudioClassification, ASTModel
    from transformers.models.audio_spectrogram_transformer.modeling_audio_spectrogram_transformer import (
        AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
    )


if is_torchaudio_available():
    import torchaudio

    from transformers import ASTFeatureExtractor

class      snake_case     :




 """simple docstring"""



 def __init__(       self       :							List[Any]							,	__A       :							List[Any]							,	__A       :							Any=1_3							,	__A       :							Any=2							,	__A       :							Tuple=2_4							,	__A       :							Dict=1_6							,	__A       :							List[str]=True							,	__A       :							List[Any]=True							,	__A       :							Any=3_2							,	__A       :							List[str]=5							,	__A       :							Optional[int]=4							,	__A       :							List[str]=3_7							,	__A       :							Optional[Any]="gelu"							,	__A       :							Optional[int]=0.1							,	__A       :							Any=0.1							,	__A       :							Union[str, Any]=1_0							,	__A       :							List[Any]=0.02							,	__A       :							Union[str, Any]=None							,	__A       :							Tuple=2							,	__A       :							Any=2							,	):
        __UpperCamelCase       =  parent
        __UpperCamelCase       =  batch_size
        __UpperCamelCase       =  patch_size
        __UpperCamelCase       =  max_length
        __UpperCamelCase       =  num_mel_bins
        __UpperCamelCase       =  is_training
        __UpperCamelCase       =  use_labels
        __UpperCamelCase       =  hidden_size
        __UpperCamelCase       =  num_hidden_layers
        __UpperCamelCase       =  num_attention_heads
        __UpperCamelCase       =  intermediate_size
        __UpperCamelCase       =  hidden_act
        __UpperCamelCase       =  hidden_dropout_prob
        __UpperCamelCase       =  attention_probs_dropout_prob
        __UpperCamelCase       =  type_sequence_label_size
        __UpperCamelCase       =  initializer_range
        __UpperCamelCase       =  scope
        __UpperCamelCase       =  frequency_stride
        __UpperCamelCase       =  time_stride

        # in AST, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
        __UpperCamelCase       =  (self.num_mel_bins - self.patch_size) // self.frequency_stride + 1
        __UpperCamelCase       =  (self.max_length - self.patch_size) // self.time_stride + 1
        __UpperCamelCase       =  frequency_out_dimension * time_out_dimension
        __UpperCamelCase       =  num_patches + 2






 def        _lowerCamelCase					(       self       :							Optional[int]	):
        __UpperCamelCase       =  floats_tensor([self.batch_size, self.max_length, self.num_mel_bins]	)

        __UpperCamelCase       =  None
        if self.use_labels:
               __UpperCamelCase       =  ids_tensor([self.batch_size]							,	self.type_sequence_label_size	)

        __UpperCamelCase       =  self.get_config()

        return config, input_values, labels






 def        _lowerCamelCase					(       self       :							Dict	):
        return ASTConfig(
            patch_size=self.patch_size							,	max_length=self.max_length							,	num_mel_bins=self.num_mel_bins							,	hidden_size=self.hidden_size							,	num_hidden_layers=self.num_hidden_layers							,	num_attention_heads=self.num_attention_heads							,	intermediate_size=self.intermediate_size							,	hidden_act=self.hidden_act							,	hidden_dropout_prob=self.hidden_dropout_prob							,	attention_probs_dropout_prob=self.attention_probs_dropout_prob							,	is_decoder=__A							,	initializer_range=self.initializer_range							,	frequency_stride=self.frequency_stride							,	time_stride=self.time_stride							,	)






 def        _lowerCamelCase					(       self       :							Optional[Any]							,	__A       :							Any							,	__A       :							str							,	__A       :							str	):
        __UpperCamelCase       =  ASTModel(config=__A	)
        model.to(__A	)
        model.eval()
        __UpperCamelCase       =  model(__A	)
        self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, self.seq_length, self.hidden_size)	)






 def        _lowerCamelCase					(       self       :							Any	):
        __UpperCamelCase       =  self.prepare_config_and_inputs()
        (
            (
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						(
            __UpperCamelCase
        )       ,						
        )       =  config_and_inputs
        __UpperCamelCase       =  {'input_values': input_values}
        return config, inputs_dict






@require_torch
class      snake_case     (    __lowerCamelCase			,       __lowerCamelCase			,       unittest.TestCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					List[str]					=(
     (
         ASTModel,
         ASTForAudioClassification,
     )
     if is_torch_available()
     else ()
 )
 SCREAMING_SNAKE_CASE_	:					int					=(
     {"audio-classification": ASTForAudioClassification, "feature-extraction": ASTModel}
     if is_torch_available()
     else {}
 )
 SCREAMING_SNAKE_CASE_	:					Optional[Any]					=False
 SCREAMING_SNAKE_CASE_	:					str					=False
 SCREAMING_SNAKE_CASE_	:					str					=False
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=False






 def        _lowerCamelCase					(       self       :							Tuple							,	__A       :							int							,	__A       :							Optional[Any]							,	__A       :							List[str]							,	__A       :							Optional[Any]							,	__A       :							List[Any]	):
        if pipeline_test_casse_name == "AudioClassificationPipelineTests":
               return True

        return False






 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        __UpperCamelCase       =  ASTModelTester(self	)
        __UpperCamelCase       =  ConfigTester(self							,	config_class=__A							,	has_text_modality=__A							,	hidden_size=3_7	)






 def        _lowerCamelCase					(       self       :							Union[str, Any]	):
        self.config_tester.run_common_tests()






 @unittest.skip(reason='AST does not use inputs_embeds'	)
 def        _lowerCamelCase					(       self       :							Tuple	):
        pass






 def        _lowerCamelCase					(       self       :							str	):
        __UpperCamelCase       ,						__UpperCamelCase       =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               __UpperCamelCase       =  model_class(__A	)
               self.assertIsInstance(model.get_input_embeddings()							,	(nn.Module)	)
               __UpperCamelCase       =  model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(__A							,	nn.Linear	)	)






 def        _lowerCamelCase					(       self       :							Optional[int]	):
        __UpperCamelCase       ,						__UpperCamelCase       =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               __UpperCamelCase       =  model_class(__A	)
               __UpperCamelCase       =  inspect.signature(model.forward	)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               __UpperCamelCase       =  [*signature.parameters.keys()]

               __UpperCamelCase       =  ['input_values']
               self.assertListEqual(arg_names[:1]							,	__A	)






 def        _lowerCamelCase					(       self       :							Dict	):
        __UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__A	)






 @slow
 def        _lowerCamelCase					(       self       :							Dict	):
        for model_name in AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCamelCase       =  ASTModel.from_pretrained(__A	)
               self.assertIsNotNone(__A	)






def 				lowercase__   (   ) ->      Optional[int]:



       """simple docstring"""






       __UpperCamelCase       =  hf_hub_download(
           repo_id='nielsr/audio-spectogram-transformer-checkpoint'				,  filename='sample_audio.flac'				,  repo_type='dataset'     )

       __UpperCamelCase       ,						__UpperCamelCase       =  torchaudio.load(__lowercase     )

       return audio, sampling_rate


@require_torch
@require_torchaudio
class      snake_case     (    unittest.TestCase       ):




 """simple docstring"""



 @cached_property
 def        _lowerCamelCase					(       self       :							int	):
        return (
            ASTFeatureExtractor.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'	)
            if is_torchaudio_available()
            else None
        )






 @slow
 def        _lowerCamelCase					(       self       :							Optional[int]	):
        __UpperCamelCase       =  self.default_feature_extractor
        __UpperCamelCase       =  ASTForAudioClassification.from_pretrained('MIT/ast-finetuned-audioset-10-10-0.4593'	).to(__A	)

        __UpperCamelCase       =  self.default_feature_extractor
        __UpperCamelCase       ,						__UpperCamelCase       =  prepare_audio()
        __UpperCamelCase       =  audio.squeeze().numpy()
        __UpperCamelCase       =  feature_extractor(__A							,	sampling_rate=__A							,	return_tensors='pt'	).to(__A	)

        # forward pass
        with torch.no_grad():
               __UpperCamelCase       =  model(**__A	)

        # verify the logits
        __UpperCamelCase       =  torch.Size((1, 5_2_7)	)
        self.assertEqual(outputs.logits.shape							,	__A	)

        __UpperCamelCase       =  torch.tensor([-0.8760, -7.0042, -8.6602]	).to(__A	)

        self.assertTrue(torch.allclose(outputs.logits[0, :3]							,	__A							,	atol=1e-4	)	)







'''simple docstring'''
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
    from PIL import Image

class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					Tuple					="naver-clova-ix/donut-base-finetuned-docvqa"
 SCREAMING_SNAKE_CASE_	:					Dict					=(
     "This is a tool that answers a question about an document (pdf). It takes an input named `document` which "
     "should be the document containing the information, as well as a `question` that is the question about the "
     "document. It returns a text that contains the answer to the question."
 )
 SCREAMING_SNAKE_CASE_	:					List[str]					="document_qa"
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=AutoProcessor
 SCREAMING_SNAKE_CASE_	:					Union[str, Any]					=VisionEncoderDecoderModel

 SCREAMING_SNAKE_CASE_	:					List[Any]					=["image", "text"]
 SCREAMING_SNAKE_CASE_	:					Any					=["text"]






 def __init__(       self       :							Optional[int]							,	*__A       :							List[str]							,	**__A       :							List[Any]	):
        if not is_vision_available():
               raise ValueError('Pillow must be installed to use the DocumentQuestionAnsweringTool.'	)

        super().__init__(*__A							,	**__A	)






 def        _lowerCamelCase					(       self       :							Any							,	__A       :							"Image"							,	__A       :							str	):
        __UpperCamelCase       =  '<s_docvqa><s_question>{user_input}</s_question><s_answer>'
        __UpperCamelCase       =  task_prompt.replace('{user_input}'							,	__A	)
        __UpperCamelCase       =  self.pre_processor.tokenizer(
            __A							,	add_special_tokens=__A							,	return_tensors='pt'	).input_ids
        __UpperCamelCase       =  self.pre_processor(__A							,	return_tensors='pt'	).pixel_values

        return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}






 def        _lowerCamelCase					(       self       :							Union[str, Any]							,	__A       :							Optional[Any]	):
        return self.model.generate(
            inputs['pixel_values'].to(self.device	)							,	decoder_input_ids=inputs['decoder_input_ids'].to(self.device	)							,	max_length=self.model.decoder.config.max_position_embeddings							,	early_stopping=__A							,	pad_token_id=self.pre_processor.tokenizer.pad_token_id							,	eos_token_id=self.pre_processor.tokenizer.eos_token_id							,	use_cache=__A							,	num_beams=1							,	bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]							,	return_dict_in_generate=__A							,	).sequences






 def        _lowerCamelCase					(       self       :							Tuple							,	__A       :							List[Any]	):
        __UpperCamelCase       =  self.pre_processor.batch_decode(__A	)[0]
        __UpperCamelCase       =  sequence.replace(self.pre_processor.tokenizer.eos_token							,	''	)
        __UpperCamelCase       =  sequence.replace(self.pre_processor.tokenizer.pad_token							,	''	)
        __UpperCamelCase       =  re.sub(R'<.*?>'							,	''							,	__A							,	count=1	).strip()  # remove first task start token
        __UpperCamelCase       =  self.pre_processor.tokenajson(__A	)

        return sequence["answer"]
