一、安装依赖
pip install -U openai-whisper
二、安装ffmpeg
cd /opt
# 下载 5.1 版本的 ffmpeg
wget http://www.ffmpeg.org/releases/ffmpeg-5.1.tar.gz
# 解压下载的压缩包
tar -zxvf ffmpeg-5.1.tar.gz
# 进入解压后的文件夹
cd ffmpeg-5.1
# 安装ffplay需要的依赖
sudo apt-get install libx11-dev xorg-dev libsdl2-2.0 libsdl2-dev
sudo apt install clang libfdk-aac-dev libspeex-dev libx264-dev libx265-dev libnuma-dev
sudo apt install yasm pkg-config libopencore-amrnb-dev libopencore-amrwb-dev
# 查看帮助文档确定需要安装的相关参数
./configure --help
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-ffplay --enable-ffprobe --enable-libx264 --enable-libx265 --enable-debug
# 编译ffmpeg
make
# 安装编译
sudo make install
# 设置环境变量
# 查看当前路径
pwd
# 将当前路径添加到环境变量当中
export PATH="$PATH:/opt/ffmpeg-5.1/ffmpeg"
# 测试
# 查看ffmpeg的版本
ffmpeg -version
ffplay -version
ffprobe -version
卸载
cd /opt/ffmpeg-5.1
sudo make uninstall
原文链接
三、下载运行模型
# 引用whisper模块
import whisper
# 下载模型到指定路径
# model = whisper.load_model("base", download_root="/opt/Whisper/models") # 基座模型
model = whisper.load_model("large", download_root="/opt/Whisper/models") # 大模型
# 使用模型
result = model.transcribe("voice.wav")
print(result["text"])
这样模型文件就会被下载到/opt/Whisper/models
路径
四、接口封装
# 引用whisper模块
import whisper
from flask import Flask,request
import datetime
import requests
import torch# 语音识别模型
# model = whisper.load_model("large", download_root="/opt/Whisper/models") # 同时下载模型
device = torch.device("cpu") # 使用CPU加载模型
model = whisper.load_model("/opt/Whisper/models/large-v3.pt", device=device) # 加载模型# 实例化一个web服务对象
app = Flask(__name__)
# 构造一个接受post请求的响应
@app.route('/',methods=['POST'])
def postRequest():data = {}data['data'] = {'text':'Not Found!'}data['code'] = 404# 验证秘钥是否通过key = request.form.get('key')if key != "OpenAI Whisper":data['data'] = {'text':'No permissions!'}data['code'] = 400return data# 判断翻译类型lang = 'zh'lang_ = request.form.get('lang')if lang_ == 'en':lang = 'en'# 验证是否上传文件,未上传文件使用远程地址或本地地址if 'file' not in request.files:# 获取路径path = request.form.get('path')if path.lower().endswith((".wav", ".WAV")) is False:data['data'] = {'text':'No wav!'}data['code'] = 400return datatyp = request.form.get('typ') # url/path# 如果是使用连接,则远程拷贝链接文件到指定地址if typ == 'url':# 指定地址fileName = '/opt/Whisper/voice/' + datetime.datetime.now().strftime('%Y%m%d_%H%M%S') + '.wav'downlaod(path, fileName)res = toLang(lang, fileName) # model.transcribe(fileName,language='Chinese')data['data'] = resdata['code'] = 200# 如果是使用本地文件,则直接读取elif typ == 'path':res = toLang(lang, path) # model.transcribe(path,language='Chinese')data['data'] = resdata['code'] = 200# 如果通过上传文件else:file = request.files['file']if file.filename == '':data['data'] = {'text':'No file!'}data['code'] = 400return data# 指定文件保存路径file_extension = file.filename.split(".")[-1]if file_extension != 'wav' and file_extension!= 'WAV' and file_extension != 'mp3' and file_extension!= 'MP3':data['data'] = {'text':'No Voice!'}data['code'] = 400return data# 指定地址fileName = '/opt/Whisper/voice/' + datetime.datetime.now().strftime('%Y%m%d_%H%M%S') + '.' + file_extension# 保存文件file.save(fileName)res = toLang(lang, fileName) # model.transcribe(fileName,language='Chinese')data['data'] = resdata['code'] = 200return data# 文件下载
def downlaod(url, file_path):headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0"}r = requests.get(url=url, headers=headers)with open(file_path, "wb") as f:f.write(r.content)f.flush()# 识别
def toLang(lang, file_path):if lang == 'en':prompt = 'is English'transcription = model.transcribe(file_path, language='en',verbose=True, initial_prompt=prompt)print(transcription["text"])return transcriptionelse:prompt = '以下是普通话的句子'transcription = model.transcribe(file_path, language='zh',verbose=True, initial_prompt=prompt)print(transcription["text"])return transcriptionif __name__ == '__main__':# 运行服务,并确定服务运行的IP和端口app.run('0.0.0.0', '8000')
五、后台运行
nohup /root/.virtualenvs/glm/bin/python /opt/Whisper/voice2text.py > /opt/Whisper/voice2text.log 2>&1 &
六、开机自启
vi /etc/rc.local
# 写入
nohup /root/.virtualenvs/glm/bin/python /opt/Whisper/voice2text.py > /opt/Whisper/voice2text.log 2>&1 &
# 给rc.local加上权限【已经配置过的不用重复配置】
sudo chmod +x /etc/rc.local
# 启用服务
sudo systemctl enable rc-local
# 启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
# 重启测试
reboot
七、使用说明
接口 | http://127.0.0.1:8000 | |
---|---|---|
类型 | POST | body 中使用 form-data 上传 |
key | OpenAI Whisper | 必填秘钥 |
lang | zh en null | 输出结果语言选择,不传默认输出中文 |
typ | url path null | 路径类型,远程文件传 url 、服务器本地文件传 path 直接上传文件允许不传 |
path | 路径地址,远程文件传网址、服务器本地文件传路径 直接上传文件允许不传 | |
file | 文件,选择文件允许传 wav mp3 否则只允许传 wav |