whisper技术部署及简单使用

  • Post author:
  • Post category:其他


whisper是openai开源的语音转文字的技术,可以作为国内收费语音转文字相关软件的替代



安装ffmpeg(解压模式)

查看系统架构

dpkg --print-architecture

下载对应版本的ffmpeg

https://www.johnvansickle.com/ffmpeg/old-releases/

 # 解压
 xz -d ffmpeg-5.0.1-amd64-static.tar.xz 
 tar -xvf ffmpeg-5.0.1-amd64-static.tar	
 
 # 进入解压后的目录
 cd ffmpeg-5.0.1-amd64-static/
 # 查看版本
 ./ffmpeg
 ./ffprobe

配置ffmpeg命令全局可用,可以在bin目录加个链接。比如,分别执行如下命令,即可在:

/usr/bin

目录下创建

ffmpeg



ffprobe

软链接。

cd /usr/bin
ln -s /root/whisper/ffmpeg-5.0.1-amd64-static/ffmpeg ffmpeg
ln -s /data/software/ffmpeg-git-20190424-amd64-static/ffprobe ffprobe

全局查看版本

ffmpeg
ffprobe



安装python(源码编译模式)

https://www.python.org/ftp/python/3.11.4/

卸载python

python3 -V
apt list --installed | grep python
apt-get remove python3.8.5
apt-get remove --auto-remove python3.8.5
apt-get purge python3.8.5
# 刷新包目录
apt update

安装python

# 上传压缩包
Python-3.11.4.tar.xz  
# 安装依赖
apt install build-essential gdb lcov libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev

# 进入解压后的目录
cd Python-3.11.4
./configure --prefix=/usr/local/python3 --enable-shared  --enable-optimizations 
# 编译
make 
# 构建测试
make test	
# 安装
make install
# 清除构建
make clean

cd /usr/local/bin
ln -s /usr/local/python3/bin/python3 /usr/bin/python3

查看python版本
python3 -V



安装pytorch

官网 https://pytorch.org/get-started/locally/

Linux查看显卡信息:
lspci | grep -i vga
00:02.0 VGA compatible controller: Cirrus Logic GD 5446
使用nvidia GPU可以:
lspci | grep -i nvidia

登陆官网下载对应版本torch,这里用cpu模式

pip3 install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cpu



安装whisper

不推荐
pip3 install -U  openai-whisper==20230314

推荐
pip3 install git+https://github.com/openai/whisper.git  



whisper命令行使用

cd /Users/aiksyuan/yxt/python-workspace/whisper/whisper-doc

whisper audio-cn.mp3 --model_dir  /root/whisper/models/whisper --language Chinese --model small -o ./ -f srt  --device cpu --fp16 False --initial_prompt "以下是普通话的句子。"

各个参数含义可以使用whisper --help查看



python调用whisper

基本案例

# 这个脚本可以直接输出音频转出的文字。
import whisper
model = whisper.load_model("base")
result = model.transcribe("I_Have_A_Dream_Speech.mp3",fp16="False")
print(result["text"])

进阶案例

import whisper
from whisper.utils import get_writer
import time

def test_whisper(model_type, file_path, target_path, file_name):
    T1 = time.time()
    model = whisper.load_model(
        model_type, 'cpu', '/Users/aiksyuan/.cache/whisper')
    result = model.transcribe(file_path, fp16=False, initial_prompt='以下是普通话的句子')
    T2 = time.time()
    print(model_type + "模式" + file_name + '解析所需时间:%s秒' % ((T2 - T1)))
    writer = get_writer("srt", target_path)
    writer(result, file_name + '_' + model_type + ".srt",
           {"highlight_words": True, "max_line_count": 3, "max_line_width": 3})
    T3 = time.time()
    print(model_type + "模式" + file_name + '生成srt文件耗时:%s秒' % ((T3 - T2)))
    writer2 = get_writer('txt', target_path)
    writer2(result, file_name + '_' + model_type + '.txt', {})
    T4 = time.time()
    print(model_type + "模式" + file_name + '生成txt文件耗时:%s秒' % ((T4 - T3)))

if __name__ == '__main__':
    models = ['base', 'small', 'medium']
    for model_type in models:
        # test_whisper(model_type, 'audio/audio.mp3', "audio/", "audio")
        # test_whisper(model_type, '踏山河/踏山河.mp3', "踏山河/", "踏山河") 
        test_whisper(model_type, 't1/1.m4a', "t1/", "1") 
        # test_whisper(model_type, '红日/红日.mp3', "红日/", "红日")
        # test_whisper(model_type, 'test001/test001.mp4', "test001/", "test001")
        # test_whisper(model_type, 'test001/2m.mp4', "test001/", "2m")



pip包离线导出安装

pip3 install wheel
mkdir packs

cd packs
导出环境中的所有第三方包
pip3 freeze > requirements.txt

python导出依赖成whl文件
pip3 wheel -r requirements.txt

离线批量安装包
pip3 install --no-index --find-links=/packs/ -r requirements.txt



版权声明:本文为u011831527原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。