Redis与MySQL缓存一致性:先更新DB再删缓存、延迟双删与兜底
2026/9/18 11:24:10
在日常工作中,我们经常需要处理大量音频文件的转写任务。无论是会议录音整理、客服电话分析,还是多媒体内容生产,手动处理这些音频文件既耗时又容易出错。SenseVoice-small-onnx语音识别模型为我们提供了一个高效的解决方案。
这个基于ONNX量化的多语言语音识别服务具有以下优势:
首先确保系统已安装Python 3.7+,然后安装必要的依赖:
pip install funasr-onnx gradio fastapi uvicorn soundfile jieba模型会自动缓存到指定目录,无需手动下载:
/root/ai-models/danieldong/sensevoice-small-onnx-quant如果需要在其他目录使用模型,可以通过环境变量指定:
export MODEL_PATH="/your/custom/path"下面是一个处理单个目录下所有音频文件的Python脚本:
import os from funasr_onnx import SenseVoiceSmall def transcribe_audio_folder(folder_path, output_file="results.csv"): # 初始化模型 model = SenseVoiceSmall( "/root/ai-models/danieldong/sensevoice-small-onnx-quant", batch_size=10, quantize=True ) # 收集音频文件 audio_files = [ os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.lower().endswith(('.wav', '.mp3', '.m4a', '.flac')) ] # 批量处理 results = model(audio_files, language="auto", use_itn=True) # 保存结果 with open(output_file, "w", encoding="utf-8") as f: f.write("文件名,转写内容\n") for file, text in zip(audio_files, results): f.write(f"{os.path.basename(file)},{text}\n") print(f"处理完成,结果已保存到 {output_file}") if __name__ == "__main__": transcribe_audio_folder("audio_files")对于大量文件,可以使用多线程加速处理:
from concurrent.futures import ThreadPoolExecutor def batch_transcribe(files, model, batch_size=10): batches = [files[i:i+batch_size] for i in range(0, len(files), batch_size)] results = [] with ThreadPoolExecutor() as executor: for batch in batches: results.extend(executor.submit(model, batch, language="auto").result()) return results添加进度条提升用户体验:
from tqdm import tqdm def transcribe_with_progress(model, files): results = [] for i in tqdm(range(0, len(files), 10)): batch = files[i:i+10] results.extend(model(batch, language="auto")) return results对于企业级应用,建议部署为API服务:
python3 app.py --host 0.0.0.0 --port 7860API支持批量上传:
curl -X POST "http://localhost:7860/api/transcribe" \ -F "files=@audio1.wav" \ -F "files=@audio2.wav" \ -F "language=auto"使用Docker可以简化部署:
FROM python:3.9-slim WORKDIR /app COPY . . RUN pip install -r requirements.txt EXPOSE 7860 CMD ["python", "app.py", "--host", "0.0.0.0", "--port", "7860"]构建并运行:
docker build -t sensevoice-api . docker run -p 7860:7860 -v /path/to/models:/root/ai-models sensevoice-api通过实验找到最佳batch_size:
import time def benchmark_batch_sizes(model, test_files): for batch_size in [1, 5, 10, 20, 50]: start = time.time() model(test_files[:100], batch_size=batch_size) duration = time.time() - start print(f"Batch size {batch_size}: {duration:.2f}s")添加资源使用日志:
import psutil import time def log_resources(): while True: cpu = psutil.cpu_percent() mem = psutil.virtual_memory().percent print(f"CPU: {cpu}%, Memory: {mem}%") time.sleep(60)通过本文介绍的方案,您可以轻松实现:
最佳实践建议:
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。