使用 Whisper Tiny 模型实现快速语音转文字:Python 部署与实践指南

原创声明:本文为作者原创作品,采用开放许可,允许自由使用、修改、分发及商业应用,无需额外授权。

1. 环境准备

1.1 系统要求

  • 操作系统:Ubuntu / Debian / WSL / macOS / Windows
  • Python:3.10 或更高版本
  • 系统依赖ffmpeg(Whisper 处理音频文件必需)

1.2 安装系统依赖

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install -y ffmpeg

macOS:

brew install ffmpeg

Windows:ffmpeg.org 下载并安装

1.3 安装 Python 依赖

pip install openai-whisper

或者使用项目 requirements.txt:

pip install -r requirements.txt

2. Whisper 模型选择

Whisper 提供多种模型,可根据需求选择:

模型参数量速度准确度推荐场景
tiny3900万最快较低实时交互、低延迟需求
base7400万较快中等平衡速度和准确度
small2.44亿中等较好一般应用
medium7.69亿较慢较高高准确度需求
large15.5亿最慢最高专业转录

本项目使用 tiny 模型,适合实时语音交互场景。

3. 核心功能实现

3.1 模型管理(单例模式)

import whisper
import warnings

_whisper_model = None

def get_whisper_model():
    """获取或加载Whisper模型(单例模式)"""
    global _whisper_model
    if _whisper_model is None:
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", message="FP16 is not supported on CPU")
            _whisper_model = whisper.load_model("tiny", device="cpu")
    return _whisper_model

特点:

  • 延迟加载:首次使用时才加载模型
  • 单例模式:全局只加载一次,节省内存
  • 自动处理 CPU/GPU:根据设备自动选择

注意:首次运行时会自动下载模型文件(tiny 模型约 75MB),下载完成后会缓存到本地,后续运行会直接使用缓存,无需重新下载。

3.2 音频文件处理

async def process_audio_file(audio_content: bytes, filename: str):
    """处理音频文件并转写"""
    # 1. 验证文件格式
    validate_audio_file(filename, audio_content)
    
    # 2. 保存到临时文件
    temp_path = create_temp_file(audio_content, filename)
    
    # 3. 转写音频
    result = transcribe_audio_file(temp_path, filename)
    
    # 4. 清理临时文件
    cleanup_temp_file(temp_path)
    
    return result

支持的音频格式:

  • .mp3, .wav, .flac, .m4a, .ogg, .webm, .mpeg, .mp4

注意:Whisper 会自动通过 ffmpeg 处理各种音频格式,无需手动转换。确保系统已安装 ffmpeg。

3.3 API 接口

可以使用 FastAPI 等 Web 框架封装 Whisper 功能,提供 HTTP API 接口。典型的接口设计包括:

语音转文字接口示例:

from fastapi import FastAPI, File, UploadFile
import whisper

app = FastAPI()
model = whisper.load_model("tiny")

@app.post("/transcribe")
async def transcribe_audio(audio: UploadFile = File(...)):
    """语音转文字接口"""
    # 保存上传的音频文件
    temp_path = save_temp_file(audio)
    
    # 转写音频
    result = model.transcribe(temp_path, language="zh")
    
    # 清理临时文件
    cleanup_temp_file(temp_path)
    
    return {
        "text": result["text"],
        "language": result["language"]
    }

4. 快速开始

4.1 最小示例

import whisper

# 加载模型(首次会自动下载)
model = whisper.load_model("tiny")

# 转写音频文件
result = model.transcribe("audio.wav", language="zh")
print(result["text"])

4.2 使用 FastAPI 接口

启动服务:

uvicorn main:app --reload

测试接口:

# 转写接口示例
curl -X POST \
  -F "audio=@test.wav" \
  http://localhost:8000/transcribe

4.3 Python 代码调用

import whisper

# 加载模型
model = whisper.load_model("tiny")

# 转写音频文件
result = model.transcribe("audio.wav", language="zh")

# 输出结果
print(f"转写结果: {result['text']}")
print(f"检测语言: {result['language']}")
print(f"处理时间: {result.get('processing_time', 'N/A')}")

5. 配置说明

5.1 修改模型类型

修改模型名称:

# 使用 tiny 模型(推荐,速度快)
_whisper_model = whisper.load_model("tiny", device="cpu")

# 或使用其他模型
_whisper_model = whisper.load_model("base", device="cpu")
_whisper_model = whisper.load_model("small", device="cpu")

注意:如需提高转写准确度,可以:

  • 使用更大的模型(small/medium/large),但速度会变慢
  • 确保音频质量良好,减少背景噪音
  • 指定正确的语言参数,避免自动检测带来的延迟

5.2 设备选择

# CPU 推理(默认)
model = whisper.load_model("tiny", device="cpu")

# GPU 推理(需要 CUDA)
model = whisper.load_model("tiny", device="cuda")

注意:Tiny 模型在 CPU 上运行速度已经很快,适合大多数场景。如需进一步提升速度,可以考虑:

  • 使用 GPU 推理(需要安装 CUDA 版本的 PyTorch)
  • 使用更小的模型(但准确度会降低)

5.3 语言指定

# 自动检测语言(默认)
result = model.transcribe("audio.wav")

# 指定语言(更快)
result = model.transcribe("audio.wav", language="zh")  # 中文
result = model.transcribe("audio.wav", language="en")  # 英文

参考文档