4.2、ipex-llm(原bigdl-llm)进行语音识别

ipex-llm环境配置及模型下载

由于需要处理音频文件,还需要安装用于音频分析的 librosa 软件包。

python 复制代码
pip install librosa

下载音频文件

python 复制代码
!wget -O audio_en.mp3 https://datasets-server.huggingface.co/assets/common_voice/--/en/train/5/audio/audio.mp3
!wget -O audio_zh.mp3 https://datasets-server.huggingface.co/assets/common_voice/--/zh-CN/train/2/audio/audio.mp3

播放下载完成的音频:

python 复制代码
import IPython

IPython.display.display(IPython.display.Audio("audio_en.mp3"))
IPython.display.display(IPython.display.Audio("audio_zh.mp3"))

1、加载预训练好的 Whisper 模型

加载一个经过预训练的 Whisper 模型,例如 whisper-medium 。OpenAI 发布了各种尺寸的预训练 Whisper 模型(包括 whisper-small、whisper-tiny 等),您可以选择最符合您要求的模型。

只需在 ipex-llm 中使用单行 transformers-style API,即可加载具有 INT4 优化功能的 whisper-medium(通过指定 load_in_4bit=True),如下所示。请注意,对于 Whisper,我们使用了 AutoModelForSpeechSeq2Seq 类。

python 复制代码
from ipex_llm.transformers import AutoModelForSpeechSeq2Seq

model = AutoModelForSpeechSeq2Seq.from_pretrained(pretrained_model_name_or_path="openai/whisper-medium",
                                                  load_in_4bit=True,
                                                  trust_remote_code=True)

2、加载 Whisper Processor

无论是音频预处理还是将模型输出从标记转换为文本的后处理,我们都需要 Whisper Processor。您只需使用官方的 transformers API 加载 WhisperProcessor 即可:

python 复制代码
from transformers import WhisperProcessor

processor = WhisperProcessor.from_pretrained(pretrained_model_name_or_path="openai/whisper-medium")

3、转录英文音频

使用带有 INT4 优化功能的 IPEX-LLM 优化 Whisper 模型并加载 Whisper Processor 后,就可以开始通过模型推理转录音频了。

让我们从英语音频文件 audio_en.mp3 开始。在将其输入 Whisper Processor 之前,我们需要从原始语音波形中提取序列数据:

python 复制代码
import librosa

data_en, sample_rate_en = librosa.load("audio_en.mp3", sr=16000)

对于 whisper-medium,其 WhisperFeatureExtractor(WhisperProcessor 的一部分)默认使用

16,000Hz 采样率从音频中提取特征。关键的是要用模型的 WhisperFeatureExtractor

以采样率加载音频文件,以便精确识别。

然后,我们就可以根据序列数据转录音频文件,使用的方法与使用官方的 transformers API 完全相同:

python 复制代码
import torch
import time

# 定义任务类型
forced_decoder_ids = processor.get_decoder_prompt_ids(language="english", task="transcribe")

with torch.inference_mode():
    # 为 Whisper 模型提取输入特征
    input_features = processor(data_en, sampling_rate=sample_rate_en, return_tensors="pt").input_features

    # 为转录预测 token id
    st = time.time()
    predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
    end = time.time()

    # 将 token id 解码为文本
    transcribe_str = processor.batch_decode(predicted_ids, skip_special_tokens=True)

    print(f'Inference time: {end-st} s')
    print('-'*20, 'English Transcription', '-'*20)
    print(transcribe_str)

forced_decoder_ids 为不同语言和任务(转录或翻译)定义上下文 token 。如果设置为 None,Whisper 将自动预测它们。

4、转录中文音频并翻译成英文

现在把目光转向中文音频 audio_zh.mp3。Whisper 可以转录多语言音频,并将其翻译成英文。这里唯一的区别是通过 forced_decoder_ids 来定义特定的上下文 token:

python 复制代码
# 提取序列数据
data_zh, sample_rate_zh = librosa.load("audio_zh.mp3", sr=16000)

# 定义中文转录任务
forced_decoder_ids = processor.get_decoder_prompt_ids(language="chinese", task="transcribe")

with torch.inference_mode():
    input_features = processor(data_zh, sampling_rate=sample_rate_zh, return_tensors="pt").input_features
    st = time.time()
    predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
    end = time.time()
    transcribe_str = processor.batch_decode(predicted_ids, skip_special_tokens=True)

    print(f'Inference time: {end-st} s')
    print('-'*20, 'Chinese Transcription', '-'*20)
    print(transcribe_str)

# 定义中文转录以及翻译任务
forced_decoder_ids = processor.get_decoder_prompt_ids(language="chinese", task="translate")

with torch.inference_mode():
    input_features = processor(data_zh, sampling_rate=sample_rate_zh, return_tensors="pt").input_features
    st = time.time()
    predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
    end = time.time()
    translate_str = processor.batch_decode(predicted_ids, skip_special_tokens=True)

    print(f'Inference time: {end-st} s')
    print('-'*20, 'Chinese to English Translation', '-'*20)
    print(translate_str)
相关推荐
菜鸟小芯15 分钟前
DAY1 从 “会聊天” 到 “能做事”:OpenClaw 开源 AI 智能体全解析
人工智能·开源·华为云
~央千澈~16 分钟前
抖音弹幕游戏开发之第9集:pyautogui进阶 - 模拟鼠标操作·优雅草云桧·卓伊凡
开发语言·python·游戏
Jouham16 分钟前
全链路 AI 获客 vs 传统拓客:瞬维智能如何用效率与成本重构中小企获客逻辑
人工智能·重构
DisonTangor18 分钟前
MiniMax AI 开源 MiniMax-M2.5
人工智能·语言模型·自然语言处理·开源·aigc
啊阿狸不会拉杆21 分钟前
《机器学习导论》第 19 章 - 机器学习实验的设计与分析
人工智能·python·算法·决策树·机器学习·统计检验·评估方法
路人与大师22 分钟前
大庆油田全链路智能体化设计草案
网络·人工智能
格林威25 分钟前
Baumer相机薄膜厚度均匀性评估:基于光学干涉条纹的 6 个核心方法,附 OpenCV+Halcon 实战代码!
人工智能·opencv·计算机视觉·视觉检测·工业相机·智能相机·堡盟相机
张3蜂28 分钟前
python知识点点亮
开发语言·python
好学且牛逼的马28 分钟前
【Hot100|26-LeetCode 21. 合并两个有序链表 - 完整解法详解】
开发语言·python
Katecat9966329 分钟前
矿井地雷检测与识别:Yolo11-HAFB-1模型应用详解
python