本地部署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的速度,速度取决于网速

相关推荐
Maddie_Mo1 天前
Unity 联动 Trae AI 项目开发基础教学
人工智能·unity·游戏引擎
光锥智能1 天前
Google 与百度同步布局智能体:AI 竞争进入全栈能力比拼阶段
人工智能·百度
一点一木1 天前
深度体验TRAE SOLO移动端7天:作为独立开发者,我把工作流揣进了兜里
前端·人工智能·trae
Lee川1 天前
mini-cursor 揭秘:从 Tool 定义到 Agent 循环的完整实现
前端·人工智能·后端
weelinking1 天前
【产品】00_产品经理用Claude实现产品系列介绍
数据库·人工智能·sql·数据挖掘·github·产品经理
Agent产品评测局1 天前
制造业模具管理AI系统,主流产品能力对比详解:2026年智能制造选型深度洞察
人工智能·ai·chatgpt·制造
研华科技Advantech1 天前
如何用一套实训设备,打通工业AI预测性维护技术全流程?
人工智能
Lab_AI1 天前
AI for Science: MaXFlow AI Agent+ 报告体验双升级,让AI智能体更高效易用!
人工智能·ai for science·ai agent·ai智能体
李坤1 天前
让 Codex 和 Claude 互相 Review:告别手动复制
人工智能·openai·claude
南屹川1 天前
【API设计】GraphQL实战:从REST到GraphQL的演进
人工智能