当前位置:   article > 正文

本地部署Whisper实现语言转文字_本地部署语音转文字服务

本地部署语音转文字服务

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

1.前置条件

环境windows10 64位

在这里插入图片描述

2.安装chocolatey

安装chocolatey目的是安装ffpeg
  • 1

以管理员身份运行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'))
  • 1

安装成功打入choco

在这里插入图片描述

安装文件夹路径

C:\ProgramData\chocolatey
  • 1

3.安装ffmpeg

choco install ffmpeg
  • 1

在这里插入图片描述

4.安装whisper

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

安装完成运行

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

安装完成

在这里插入图片描述

5.测试用例

直接命令行

whisper yoump3.mp3
  • 1

在这里插入图片描述

在这里插入图片描述

6.命令行用法

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

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

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

whisper japanese.wav --language Japanese
  • 1

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

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

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

whisper --help
  • 1

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

进入huggingface网址,往下拉

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

在这里插入图片描述

粘贴上述代码

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"])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

修改本地代码,将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"])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号