diff --git a/tools/accuracy_checker/accuracy_checker/annotation_converters/librispeech.py b/tools/accuracy_checker/accuracy_checker/annotation_converters/librispeech.py index b2e5452e447..b2311bb537e 100644 --- a/tools/accuracy_checker/accuracy_checker/annotation_converters/librispeech.py +++ b/tools/accuracy_checker/accuracy_checker/annotation_converters/librispeech.py @@ -23,6 +23,12 @@ from ..representation import CharacterRecognitionAnnotation from ..config import PathField, NumberField, BoolField from .format_converter import DirectoryBasedAnnotationConverter, ConverterReturn +from ..utils import UnsupportedPackage + +try: + import soundfile as sf +except (ImportError, OSError) as import_error: + sf = UnsupportedPackage('soundfile', str(import_error)) class LibrispeechConverter(DirectoryBasedAnnotationConverter): @@ -74,8 +80,12 @@ def convert(self, check_content=False, **kwargs): continue if self.max_duration > 0 and not self.annotation_file: - with wave.open(str(fname), "rb") as wav: - duration = wav.getnframes() / wav.getframerate() + if self.flac_files: + info = sf.info(str(fname)) + duration = info.frames / info.samplerate + else: + with wave.open(str(fname), "rb") as wav: + duration = wav.getnframes() / wav.getframerate() if duration > self.max_duration: continue diff --git a/tools/accuracy_checker/accuracy_checker/evaluators/custom_evaluators/asr_pipeline_evaluator.py b/tools/accuracy_checker/accuracy_checker/evaluators/custom_evaluators/asr_pipeline_evaluator.py index 3b0680a4575..1c0b82aac11 100644 --- a/tools/accuracy_checker/accuracy_checker/evaluators/custom_evaluators/asr_pipeline_evaluator.py +++ b/tools/accuracy_checker/accuracy_checker/evaluators/custom_evaluators/asr_pipeline_evaluator.py @@ -31,6 +31,7 @@ class ASRPipelineEvaluator(BaseCustomEvaluator): VALID_PIPELINE_CLASSES = [ "GenAIASRPipeline", + "HFASRPipeline", "Qwen3ASROptimumPipeline", ] @@ -225,3 +226,82 @@ def parse_asr_output(raw_text): "language": language_match.group(1) if language_match else None, "text": text_match.group(1).strip() if text_match else raw_text.strip(), } + + +class HFASRPipeline(ASRPipeline): + def __init__(self, config): + self.language = config.get("language") + self.max_new_tokens = config.get("max_new_tokens", 1000) + super().__init__(config) + + def _initialize_pipeline(self, config): + model_id = config.get("model_id") + + if "qwen3" in str(model_id).lower() and "asr" in str(model_id).lower(): + try: + from qwen_asr import Qwen3ASRModel # pylint: disable=C0415 + except ImportError: + Qwen3ASRModel = None + + if Qwen3ASRModel is not None: + return Qwen3ASRModel.from_pretrained( + model_id, + max_inference_batch_size=1, + max_new_tokens=self.max_new_tokens, + ) + + try: + import torch # pylint: disable=C0415 + except ImportError as import_error: + UnsupportedPackage("torch", import_error.msg).raise_error(self.__class__.__name__) + + try: + from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor # pylint: disable=C0415 + from transformers.pipelines.automatic_speech_recognition import ( # pylint: disable=C0415 + AutomaticSpeechRecognitionPipeline, + ) + except ImportError as import_error: + UnsupportedPackage("transformers", import_error.msg).raise_error( + self.__class__.__name__ + ) + + device = config.get("_device", "cpu") + torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 + model = AutoModelForSpeechSeq2Seq.from_pretrained( + model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True + ).to(device.lower()) + + processor = AutoProcessor.from_pretrained(model_id) + + pipeline = AutomaticSpeechRecognitionPipeline( + model=model, + tokenizer=processor.tokenizer, + feature_extractor=processor.feature_extractor, + torch_dtype=torch_dtype, + device=device.lower(), + ) + return pipeline + + def _get_predictions(self, data, identifier, input_meta): + sampling_rate = input_meta[0].get("sample_rate") + if hasattr(self.pipeline, "transcribe"): + result = self.pipeline.transcribe( + (data[0], sampling_rate), + context=identifier, + language=self.language, + return_time_stamps=False, + ) + return result[0].text + + sample = { + "path": identifier, + "array": data[0], + "sampling_rate": sampling_rate, + } + return self.pipeline( + sample, + return_timestamps=True, + generate_kwargs={ + "num_beams": 1, + }, + )["text"] diff --git a/tools/accuracy_checker/requirements-test.in b/tools/accuracy_checker/requirements-test.in index 670b7169839..f4e4019a89e 100644 --- a/tools/accuracy_checker/requirements-test.in +++ b/tools/accuracy_checker/requirements-test.in @@ -11,3 +11,4 @@ opencv-python==4.10.0.84 # So as a workaround, make the atomicwrites dependency unconditional. atomicwrites datasets +soundfile diff --git a/tools/accuracy_checker/tests/test_asr_pipeline_evaluator.py b/tools/accuracy_checker/tests/test_asr_pipeline_evaluator.py index 3dd5b7d4d50..4f0e4d9696c 100644 --- a/tools/accuracy_checker/tests/test_asr_pipeline_evaluator.py +++ b/tools/accuracy_checker/tests/test_asr_pipeline_evaluator.py @@ -20,6 +20,7 @@ from accuracy_checker.evaluators.custom_evaluators.asr_pipeline_evaluator import ( ASRPipelineEvaluator, GenAIASRPipeline, + HFASRPipeline ) from datasets import load_dataset @@ -90,3 +91,19 @@ def test_genai_asr_pipeline(self): result = evaluator.pipe._get_predictions(input_data, identifiers[0], input_meta) assert isinstance(result, str) + + def test_hf_asr_pipeline_qwen3_asr(self): + config = {"_device": "CPU", "model_id": "Qwen/Qwen3-ASR-0.6B"} + pipeline = HFASRPipeline(config) + evaluator = ASRPipelineEvaluator(None, pipeline, None) + + result = evaluator.pipe._get_predictions(input_data, identifiers[0], input_meta) + assert isinstance(result, str) + + def test_hf_asr_pipeline_whisper_tiny(self): + config = {"_device": "CPU", "model_id": "openai/whisper-tiny"} + pipeline = HFASRPipeline(config) + evaluator = ASRPipelineEvaluator(None, pipeline, None) + + result = evaluator.pipe._get_predictions(input_data, identifiers[0], input_meta) + assert isinstance(result, str)