本地部署Whisper实现语言转文字

文章目录

本地部署Whisper实现语言转文字

1.前置条件

环境windows10 64位

2.安装chocolatey

安装chocolatey目的是安装ffpeg

以管理员身份运行PowerShell

粘贴命令

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

安装成功打入choco

安装文件夹路径

C:\ProgramData\chocolatey

3.安装ffmpeg

choco install ffmpeg

4.安装whisper

pip install git+https://github.com/openai/whisper.git

安装完成运行

pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git

安装完成

5.测试用例

直接命令行

whisper yoump3.mp3

6.命令行用法

以下命令将使用medium模型转录音频文件中的语音:

whisper audio.flac audio.mp3 audio.wav --model medium

默认设置(选择模型small)非常适合转录英语。要转录包含非英语语音的音频文件,您可以使用以下选项指定语言--language

whisper japanese.wav --language Japanese

添加--task translate后将把演讲翻译成英文:

whisper japanese.wav --language Japanese --task translate

运行以下命令查看所有可用选项:

whisper --help

7.本地硬件受限,借用hugging face资源进行转译

进入huggingface网址,往下拉

https://huggingface.co/openai/whisper-large-v3

粘贴上述代码

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset


device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "openai/whisper-large-v3"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    batch_size=16,
    return_timestamps=True,
    torch_dtype=torch_dtype,
    device=device,
)

dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
sample = dataset[0]["audio"]

result = pipe(sample)
print(result["text"])

修改本地代码,将sample修改为,需要转录的录音,接入代理;

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset

import os
os.environ['CURL_CA_BUNDLE'] = ''
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "openai/whisper-large-v3"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    batch_size=16,
    return_timestamps=True,
    torch_dtype=torch_dtype,
    device=device,
)

dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
sample = dataset[0]["audio"]

result = pipe("myaudio")
print(result["text"])

借用huggingface的速度,速度取决于网速

相关推荐
魔乐社区6 分钟前
DeepSeek在昇腾上的模型部署 - 常见问题及解决方案
人工智能·深度学习·deepseek
夜幕龙19 分钟前
深度生成模型(二)——基本概念与数学建模
人工智能·深度学习·transformer
游王子24 分钟前
OpenCV(11):人脸检测、物体识别
人工智能·opencv·计算机视觉
山海青风25 分钟前
从零开始玩转TensorFlow:小明的机器学习故事 3
人工智能·机器学习·tensorflow
@心都26 分钟前
机器学习数学基础:35.效度
人工智能·机器学习
幻想趾于现实28 分钟前
傅里叶分析
人工智能
春末的南方城市35 分钟前
VidSketch:具有扩散控制的手绘草图驱动视频生成
人工智能·深度学习·计算机视觉·aigc
Toky丶42 分钟前
【文献阅读】A Survey on Model Compression for Large Language Models
人工智能·语言模型·自然语言处理
Williams101 小时前
解锁高效开发新姿势:Trae AI编辑器深度体验
人工智能·编辑器
Francek Chen1 小时前
【大模型科普】AIGC技术发展与应用实践(一文读懂AIGC)
人工智能·深度学习·语言模型·大模型·aigc