青岛外贸网站制作公司,网站建设费用能否计入开办费,海口网,网站的建设技术有哪些内容环境配置:
一、安装依赖#xff1a;
pip install -U openai-whisper
或者#xff0c;以下命令会从这个存储库拉取并安装最新的提交#xff0c;以及其Python依赖项#xff1a;
pip install githttps://github.com/openai/whisper.git
二、安装ffmpeg#xff1a;
cd …环境配置:
一、安装依赖
pip install -U openai-whisper
或者以下命令会从这个存储库拉取并安装最新的提交以及其Python依赖项
pip install githttps://github.com/openai/whisper.git
二、安装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 -versionOpenAI Whisper链接https://gitcode.com/gh_mirrors/whisp/whisper/overview?utm_sourcecsdn_github_acceleratorisLogin1
三、下载并运行模板
# 引用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(音频文件绝对路径)
print(result[text])这样模型文件就会被下载到/opt/Whisper/models路径
四、接口封装 # 引用whisper模块
import whisper
from flask import Flask,request
import datetime
import requests
import torch
import os# 语音识别模型
# model whisper.load_model(large, download_root/opt/Whisper/models) # 同时下载模型
#device torch.device(cpu)
device torch.device(cuda:0) # 使用CPU加载模型
model whisper.load_model(/opt/Whisper/models/tiny.pt, devicedevice) # 加载模型
#model whisper.load_model(D:\\whisper\\models\\tiny\\tiny.pt, devicedevice) # 加载本地模型# 实例化一个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] 400# return data# 判断翻译类型lang zhlang_ 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,.mp3,.MP3)) is False:data[data] {text:No wav or mp3!}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#fileName D:\\whisper\\voice\\ datetime.datetime.now().strftime(%Y%m%d_%H%M%S) .wavdownlaod(path, fileName)res toLang(lang, fileName) # model.transcribe(fileName,languageChinese)data[data] resdata[code] 200# 如果是使用本地文件则直接读取elif typ path:res toLang(lang, path) # model.transcribe(path,languageChinese)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#fileName D:\\whisper\\voice\\ datetime.datetime.now().strftime(%Y%m%d_%H%M%S) . file_extensionfile.save(fileName)# 确保文件被写入磁盘with open(fileName, a) as f:f.flush()os.fsync(f.fileno())#语音钻文字 res toLang(lang, fileName) # model.transcribe(fileName,languageChinese)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(urlurl, headersheaders)with open(file_path, wb) as f:f.write(r.content)f.flush()# 识别
def toLang(lang, file_path):print(file_path)if lang en:prompt is Englishtranscription model.transcribe(file_path, languageen,verboseTrue, initial_promptprompt)print(transcription[text])else:prompt 以下是普通话的句子transcription model.transcribe(file_path, languagezh,verboseTrue, initial_promptprompt)print(transcription[text])# 删除临时文件try:os.remove(file_path)print(fDeleted file: {file_path})except Exception as e:print(fFailed to delete file: {file_path}, Error: {str(e)})return transcriptionif __name__ __main__:# 运行服务并确定服务运行的IP和端口app.run(0.0.0.0, 8000)五、后台运行: 1、创建新的conda环境,进行隔离 conda create -n whisper python3.8source activate whispernohup /home/wwwccczzz/anaconda3/envs/whisper/bin/python /opt/Whisper/speech.py /opt/Whisper/speech.log 21 六、开机自启
方法一使用 crontab 1、编辑 crontab 文件 打开终端并输入以下命令来编辑 crontab 文件 crontab -e 2、 添加开机启动任务 在 crontab 文件中添加以下行 reboot nohup /home/wwwccczzz/anaconda3/envs/whisper/bin/python /opt/Whisper/speech.py /opt/Whisper/speech.log 21 这行命令的意思是在系统重启时运行 nohup 命令并将标准输出和标准错误重定向到 /opt/Whisper/speech.log 文件。 3、保存并退出 保存文件并退出编辑器。对于 vi 编辑器可以按 Esc 键然后输入 :wq 并按回车键。
方法二使用 systemd 服务 1、创建 systemd 服务文件 在 /etc/systemd/system/ 目录下创建一个新的服务文件例如 speech.service sudo nano /etc/systemd/system/speech.service 2、编辑服务文件 在文件中添加以下内容
[Unit]
# 描述服务的功能
DescriptionSpeech to Text Service
# 确保网络服务已经启动后再启动此服务
Afternetwork.target[Service]
# 指定运行服务的用户
Userwwwccczzz
# 设置工作目录
WorkingDirectory/opt/Whisper
# 指定启动命令
ExecStart/home/wwwccczzz/anaconda3/envs/whisper/bin/python /opt/Whisper/speech.py
# 如果服务停止则自动重启
Restartalways
# 将标准输出重定向到系统日志
StandardOutputsyslog
# 将标准错误重定向到系统日志
StandardErrorsyslog
# 设置系统日志的标识符便于查找日志
SyslogIdentifierspeech[Install]
# 指定服务在多用户模式下启动
WantedBymulti-user.target3、重新加载 systemd 配置 保存文件并退出编辑器然后重新加载 systemd 配置 sudo systemctl daemon-reload 4、开机自动启用服务 启用服务以使其在系统启动时自动运行 sudo systemctl enable speech.service 5、启动服务/检查服务状态 sudo systemctl start speech.servicesudo systemctl status speech.service