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)
相关推荐
人工智能AI技术几秒前
字节开源 DeerFlow 2.0——登顶 GitHub Trending 1,让 AI 可做任何事情
人工智能
spider'1 分钟前
系统的架构
人工智能
莱歌数字4 分钟前
强化学习如何重构芯片热管理?
人工智能·重构·制造·cae·散热
光仔December4 分钟前
【从0学习Spring AI Alibaba】2、Spring AI Alibaba版本选型及环境搭建
人工智能·大模型·saa·spring ai·ai alibaba
源码之家4 分钟前
计算机毕业设计:基于Python的汽车数据可视化分析系统 Django框架 Scrapy爬虫 可视化 车辆 懂车帝大数据 数据分析 机器学习(建议收藏)✅
python·信息可视化·django·flask·汽车·课程设计·美食
我的xiaodoujiao6 分钟前
API 接口自动化测试详细图文教程学习系列8--测试接口
python·学习·测试工具·pytest
凸头8 分钟前
从“搜了就答”到“智能决策”:拥抱 RAG 2.0 时代的架构演进 ——Java 后端工程师视角下的 AI 应用工程化落地
java·人工智能·架构·rag
蓝色的杯子12 分钟前
免费体验GPT5.4效果
python·chatgpt
逐渐会飞12 分钟前
如何用python在word插入复选框
python·word
float_com13 分钟前
LangChain4j 核心知识体系与 “AI 编程小助手“ 实战解析
人工智能