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)
相关推荐
redreamSo6 分钟前
AI Daily | AI日报:LinkedIn:90% 应用迁移,弃 Kafka 用 Northguard; 谷歌发布免费AI教育功能,重塑课堂!; 小扎143亿请新贵,Meta员工冰火两重天
程序员·aigc·资讯
冰糖猕猴桃13 分钟前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
天水幼麟17 分钟前
python学习笔记(深度学习)
笔记·python·学习
巴里巴气19 分钟前
安装GPU版本的Pytorch
人工智能·pytorch·python
「、皓子~29 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
wt_cs40 分钟前
银行回单ocr api集成解析-图像文字识别-文字识别技术
开发语言·python
降世神童1 小时前
华为云Flexus+DeepSeek征文| 使用华为云CCE容器部署Dify-LLM高可用方案的验证与测试
运维·华为云·aigc
降世神童1 小时前
华为云Flexus+DeepSeek征文| 基于华为云Dify-LLM高可用平台开发运维故障处理智能体
运维·华为云·aigc
_WndProc1 小时前
【Python】Flask网页
开发语言·python·flask
笑衬人心。1 小时前
初学Spring AI 笔记
人工智能·笔记·spring