chendl/compositional_test
1
1# Copyright 2023 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import unittest16 17from datasets import load_dataset18 19from transformers.pipelines import pipeline20from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow21 22 23@is_pipeline_test24@require_torch25class ZeroShotAudioClassificationPipelineTests(unittest.TestCase):26 # Deactivating auto tests since we don't have a good MODEL_FOR_XX mapping,27 # and only CLAP would be there for now.28 # model_mapping = {CLAPConfig: CLAPModel}29 30 @require_torch31 def test_small_model_pt(self):32 audio_classifier = pipeline(33 task="zero-shot-audio-classification", model="hf-internal-testing/tiny-clap-htsat-unfused"34 )35 dataset = load_dataset("ashraq/esc50")36 audio = dataset["train"]["audio"][-1]["array"]37 output = audio_classifier(audio, candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"])38 self.assertEqual(39 nested_simplify(output),40 [{"score": 0.501, "label": "Sound of a dog"}, {"score": 0.499, "label": "Sound of vaccum cleaner"}],41 )42 43 @unittest.skip("No models are available in TF")44 def test_small_model_tf(self):45 pass46 47 @slow48 @require_torch49 def test_large_model_pt(self):50 audio_classifier = pipeline(51 task="zero-shot-audio-classification",52 model="laion/clap-htsat-unfused",53 )54 # This is an audio of a dog55 dataset = load_dataset("ashraq/esc50")56 audio = dataset["train"]["audio"][-1]["array"]57 output = audio_classifier(audio, candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"])58 59 self.assertEqual(60 nested_simplify(output),61 [62 {"score": 0.999, "label": "Sound of a dog"},63 {"score": 0.001, "label": "Sound of vaccum cleaner"},64 ],65 )66 67 output = audio_classifier([audio] * 5, candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"])68 self.assertEqual(69 nested_simplify(output),70 [71 [72 {"score": 0.999, "label": "Sound of a dog"},73 {"score": 0.001, "label": "Sound of vaccum cleaner"},74 ],75 ]76 * 5,77 )78 output = audio_classifier(79 [audio] * 5, candidate_labels=["Sound of a dog", "Sound of vaccum cleaner"], batch_size=580 )81 self.assertEqual(82 nested_simplify(output),83 [84 [85 {"score": 0.999, "label": "Sound of a dog"},86 {"score": 0.001, "label": "Sound of vaccum cleaner"},87 ],88 ]89 * 5,90 )91 92 @unittest.skip("No models are available in TF")93 def test_large_model_tf(self):94 pass95 