Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
feat: add Qwen3-ASR model support
游雁378f022d87ed
diff --git a/funasr/models/qwen3_asr/__init__.py b/funasr/models/qwen3_asr/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/funasr/models/qwen3_asr/model.py b/funasr/models/qwen3_asr/model.pynew file mode 100644index 000000000..c6efc750f--- /dev/null+++ b/funasr/models/qwen3_asr/model.py@@ -0,0 +1,126 @@+import logging+import time+from typing import List, Optional, Union++import numpy as np+import torch+import torch.nn as nn++from funasr.register import tables+from funasr.utils.load_utils import load_audio_text_image_video++[email protected]("model_classes", "Qwen3ASR")+class Qwen3ASR(nn.Module):++ def __init__(+ self,+ model: str = "Qwen/Qwen3-ASR-1.7B",+ model_conf: dict = None,+ **kwargs,+ ):+ super().__init__()+ model_conf = model_conf or {}+ self.model_path = model+ self.qwen3_asr_model = None+ self._device = model_conf.get("device", kwargs.get("device", "cuda:0"))+ self._dtype = model_conf.get("dtype", kwargs.get("dtype", "bf16"))+ self._max_new_tokens = model_conf.get("max_new_tokens", 512)+ self._max_inference_batch_size = model_conf.get("max_inference_batch_size", 32)+ self._forced_aligner = model_conf.get("forced_aligner", None)+ self._forced_aligner_kwargs = model_conf.get("forced_aligner_kwargs", None)+ self._dtype_map = {"bf16": torch.bfloat16, "fp16": torch.float16, "fp32": torch.float32}+ self._init_model()++ def _init_model(self):+ try:+ from qwen_asr import Qwen3ASRModel+ except ImportError:+ raise ImportError(+ "qwen-asr package is required for Qwen3ASR model. "+ "Install with: pip install qwen-asr"+ )++ dtype = self._dtype_map.get(self._dtype, torch.bfloat16)+ fa_kwargs = None+ if self._forced_aligner:+ fa_kwargs = self._forced_aligner_kwargs or {}+ fa_kwargs.setdefault("dtype", dtype)+ fa_kwargs.setdefault("device_map", self._device)++ self.qwen3_asr_model = Qwen3ASRModel.from_pretrained(+ self.model_path,+ dtype=dtype,+ device_map=self._device,+ forced_aligner=self._forced_aligner,+ forced_aligner_kwargs=fa_kwargs,+ max_inference_batch_size=self._max_inference_batch_size,+ max_new_tokens=self._max_new_tokens,+ )+ logging.info(f"Qwen3ASR model loaded from {self.model_path}")++ def forward(self, **kwargs):+ raise NotImplementedError("Qwen3ASR only supports inference mode")++ def inference(+ self,+ data_in,+ data_lengths=None,+ key: list = None,+ tokenizer=None,+ frontend=None,+ **kwargs,+ ):+ language = kwargs.get("language", None)+ return_time_stamps = kwargs.get("return_time_stamps", False)+ context = kwargs.get("context", "")+ fs = kwargs.get("fs", 16000)++ meta_data = {}+ time1 = time.perf_counter()++ if isinstance(data_in, (list, tuple)):+ audio_list = data_in+ elif isinstance(data_in, str):+ audio_list = [data_in]+ elif isinstance(data_in, torch.Tensor):+ audio_np = data_in.cpu().numpy()+ if audio_np.ndim == 1:+ audio_list = [(audio_np, fs)]+ else:+ audio_list = [(audio_np[i], fs) for i in range(audio_np.shape[0])]+ elif isinstance(data_in, np.ndarray):+ if data_in.ndim == 1:+ audio_list = [(data_in, fs)]+ else:+ audio_list = [(data_in[i], fs) for i in range(data_in.shape[0])]+ else:+ audio_list = [data_in]++ time2 = time.perf_counter()+ meta_data["load_data"] = f"{time2 - time1:0.3f}"++ results = self.qwen3_asr_model.transcribe(+ audio=audio_list,+ context=context,+ language=language,+ return_time_stamps=return_time_stamps,+ )++ time3 = time.perf_counter()+ meta_data["inference"] = f"{time3 - time2:0.3f}"++ output = []+ for i, r in enumerate(results):+ k = key[i] if key and i < len(key) else f"sample_{i}"+ result_dict = {"key": k, "text": r.text}+ if r.language:+ result_dict["language"] = r.language+ if return_time_stamps and r.time_stamps is not None:+ result_dict["timestamp"] = [+ [ts.start_time, ts.end_time, ts.text]+ for ts in r.time_stamps.items+ ]+ output.append(result_dict)++ return output, meta_datadiff --git a/tests_models/test_qwen3_asr.py b/tests_models/test_qwen3_asr.pynew file mode 100644index 000000000..5232ec79b--- /dev/null+++ b/tests_models/test_qwen3_asr.py@@ -0,0 +1,82 @@+#!/usr/bin/env python3+"""Test Qwen3-ASR: multi-language speech recognition"""+import sys+import time+++def main():+ from funasr import AutoModel++ model_id = "Qwen/Qwen3-ASR-1.7B"+ url_zh = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR-Repo/asr_zh.wav"+ url_en = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen3-ASR-Repo/asr_en.wav"++ print("[Qwen3-ASR] Loading model...")+ t0 = time.time()+ model = AutoModel(+ model=model_id,+ model_conf={+ "dtype": "bf16",+ "max_new_tokens": 256,+ "max_inference_batch_size": 32,+ },+ device="cuda:0",+ disable_update=True,+ )+ print("[Qwen3-ASR] Model loaded in %.1fs" % (time.time() - t0))++ # Test 1: Chinese audio+ print("[Qwen3-ASR] Test 1: Chinese inference...")+ t0 = time.time()+ res = model.generate(+ input=url_zh,+ language="Chinese",+ )+ print("[Qwen3-ASR] Inference done in %.1fs" % (time.time() - t0))++ if res and len(res) > 0 and "text" in res[0]:+ print("[Qwen3-ASR] Result (zh): %s" % res[0]["text"])+ print("[Qwen3-ASR] Test 1 PASSED")+ else:+ print("[Qwen3-ASR] Test 1 FAILED - no text in result")+ return 1++ # Test 2: English audio+ print("[Qwen3-ASR] Test 2: English inference...")+ t0 = time.time()+ res = model.generate(+ input=url_en,+ language="English",+ )+ print("[Qwen3-ASR] Inference done in %.1fs" % (time.time() - t0))++ if res and len(res) > 0 and "text" in res[0]:+ print("[Qwen3-ASR] Result (en): %s" % res[0]["text"])+ print("[Qwen3-ASR] Test 2 PASSED")+ else:+ print("[Qwen3-ASR] Test 2 FAILED - no text in result")+ return 1++ # Test 3: Auto language detection (no forced language)+ print("[Qwen3-ASR] Test 3: Auto language detection...")+ t0 = time.time()+ res = model.generate(+ input=url_zh,+ )+ print("[Qwen3-ASR] Inference done in %.1fs" % (time.time() - t0))++ if res and len(res) > 0 and "text" in res[0]:+ print("[Qwen3-ASR] Result (auto): %s" % res[0]["text"])+ if "language" in res[0]:+ print("[Qwen3-ASR] Detected language: %s" % res[0]["language"])+ print("[Qwen3-ASR] Test 3 PASSED")+ else:+ print("[Qwen3-ASR] Test 3 FAILED - no text in result")+ return 1++ print("[Qwen3-ASR] All tests PASSED")+ return 0+++if __name__ == "__main__":+ sys.exit(main())