MOSS-Transcribe-Diarize API使用教程:Python与命令行两种方式详解 MOSS-Transcribe-Diarize API使用教程Python与命令行两种方式详解【免费下载链接】MOSS-Transcribe-DiarizeMOSS-Transcribe-Diarize 是 OpenMOSS 团队推出的开源语音转写与说话人分离模型。它对长音频、多说话人音频进行统一建模支持自动语音识别、带说话人标识的转写、说话人分离、时间戳预测以及简洁转录文本生成。项目地址: https://ai.gitcode.com/OpenMOSS/MOSS-Transcribe-DiarizeMOSS-Transcribe-Diarize是OpenMOSS团队推出的终极开源语音转写与说话人分离模型它能够对长音频、多说话人音频进行统一建模支持自动语音识别、带说话人标识的转写、说话人分离、时间戳预测以及简洁转录文本生成。这款强大的AI工具让语音转写变得简单快速无论是会议录音、播客内容还是视频采访都能一键生成带时间戳和说话人标签的专业转录文本。 为什么选择MOSS-Transcribe-Diarize在众多语音转写工具中MOSS-Transcribe-Diarize凭借其独特优势脱颖而出一体化解决方案将语音识别、说话人分离、时间戳预测整合在一个模型中长音频支持专门优化处理长格式音频内容多说话人识别自动区分不同说话者标记为[S01]、[S02]等高精度时间戳精确到秒级的开始和结束时间标记开源免费Apache 2.0许可证完全免费使用 环境准备与安装系统要求与依赖安装开始使用MOSS-Transcribe-Diarize之前需要准备以下环境Python版本推荐Python 3.12CUDA支持如需GPU加速需要CUDA 12.8及以上版本FFmpeg用于音频/视频文件处理完整的安装步骤如下# 克隆仓库 git clone https://gitcode.com/OpenMOSS/MOSS-Transcribe-Diarize cd MOSS-Transcribe-Diarize # 创建虚拟环境 conda create -n moss-transcribe-diarize python3.12 -y conda activate moss-transcribe-diarize # 安装FFmpeg conda install -c conda-forge ffmpeg7 -y # 安装PyTorch及相关依赖 pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e .[torch-runtime]如需FlashAttention 2加速仅限支持GPUpip install --extra-index-url https://download.pytorch.org/whl/cu128 -e .[torch-runtime,flash-attn]️ 命令行使用方式基本转录命令MOSS-Transcribe-Diarize提供了简单易用的命令行接口只需一行命令即可完成音频转录python infer.py \ --model OpenMOSS-Team/MOSS-Transcribe-Diarize \ --audio /path/to/your_audio.mp3 \ --decoding greedy \ --max-new-tokens 2048支持多种解码策略根据不同的需求可以选择不同的解码策略贪心解码推荐用于准确性python infer.py \ --model OpenMOSS-Team/MOSS-Transcribe-Diarize \ --audio meeting_recording.mp4 \ --decoding greedy \ --max-new-tokens 2048采样解码适合创意性内容python infer.py \ --model OpenMOSS-Team/MOSS-Transcribe-Diarize \ --audio podcast_episode.m4a \ --decoding sample \ --temperature 0.7 \ --max-new-tokens 2048JSON格式输出如需结构化数据可以使用JSON输出格式python infer.py \ --model OpenMOSS-Team/MOSS-Transcribe-Diarize \ --audio interview.wav \ --json自定义提示词您还可以自定义转录提示词让模型按照特定要求生成转录文本python infer.py \ --model OpenMOSS-Team/MOSS-Transcribe-Diarize \ --audio lecture.mp4 \ --prompt 请将这段音频转录成中文包含时间戳和说话人标签。 Python API使用方式基础Python脚本对于开发者Python API提供了更灵活的控制方式。以下是一个完整的Python使用示例import torch from transformers import AutoModelForCausalLM, AutoProcessor from moss_transcribe_diarize.inference_utils import ( build_transcription_messages, generate_transcription, resolve_device, ) # 设置模型和音频路径 model_id OpenMOSS-Team/MOSS-Transcribe-Diarize audio_path /path/to/your_audio_or_video.mp4 # 自动检测设备CPU/GPU device resolve_device(auto) dtype torch.bfloat16 if device.type cuda else torch.float32 # 加载模型注意必须设置trust_remote_codeTrue model AutoModelForCausalLM.from_pretrained( model_id, trust_remote_codeTrue, dtypeauto, ).to(dtypedtype).to(device).eval() # 加载处理器 processor AutoProcessor.from_pretrained( model_id, trust_remote_codeTrue, fix_mistral_regexTrue, ) # 构建转录消息 messages build_transcription_messages(audio_path) # 生成转录结果 result generate_transcription( model, processor, messages, max_new_tokens2048, do_sampleFalse, # 使用贪心解码 devicedevice, dtypedtype, ) # 输出结果 print(转录文本) print(result[text])高级配置选项Python API支持更多高级配置满足复杂场景需求# 自定义提示词 messages build_transcription_messages( audio_path, prompt请为这段会议录音生成详细的转录文本包含精确的时间戳和说话人标签。, ) # 使用采样解码 result generate_transcription( model, processor, messages, max_new_tokens4096, # 支持更长文本 do_sampleTrue, # 启用采样 temperature0.7, # 控制随机性 top_p0.9, # 核采样参数 devicedevice, dtypedtype, ) # 获取完整输出信息 print(f生成token数量{len(result[generated_ids])}) print(f推理时间{result[inference_time]:.2f}秒)批量处理音频文件对于需要处理多个音频文件的场景可以编写批量处理脚本import os from pathlib import Path def batch_transcribe(audio_folder, output_folder): audio_files list(Path(audio_folder).glob(*.mp3)) \ list(Path(audio_folder).glob(*.wav)) \ list(Path(audio_folder).glob(*.mp4)) for audio_file in audio_files: print(f处理文件{audio_file.name}) messages build_transcription_messages(str(audio_file)) result generate_transcription( model, processor, messages, max_new_tokens2048, do_sampleFalse, devicedevice, dtypedtype, ) # 保存结果 output_path Path(output_folder) / f{audio_file.stem}_transcript.txt with open(output_path, w, encodingutf-8) as f: f.write(result[text]) print(f已保存{output_path}) # 使用示例 batch_transcribe(audio_inputs/, transcripts/) 输出格式详解标准输出格式MOSS-Transcribe-Diarize生成的标准转录格式如下[开始时间][说话人标签]转录文本[结束时间]实际示例[0.48][S01]欢迎大家参加今天的会议[2.15][3.22][S02]我们首先回顾一下上周的进展[5.78][6.45][S01]好的我准备了相关的数据报告[9.12]格式说明时间戳精确到秒的小数如[0.48]表示0.48秒说话人标签匿名标签[S01]、[S02]等仅在同一音频内区分不同说话者转录文本识别出的语音内容时间顺序每个说话片段按时间顺序排列 配置文件解析了解项目配置文件有助于更好地使用API模型配置configuration_moss_transcribe_diarize.py处理器配置processing_moss_transcribe_diarize.py模型架构modeling_moss_transcribe_diarize.py处理器配置processor_config.json 性能优化技巧内存优化处理长音频时可以调整以下参数优化内存使用# 减少最大新token数量 result generate_transcription( model, processor, messages, max_new_tokens1024, # 减少内存占用 devicedevice, dtypetorch.float16, # 使用半精度浮点数 ) # 启用CPU模式无GPU时 device resolve_device(cpu) dtype torch.float32速度优化# 启用FlashAttention如已安装 model AutoModelForCausalLM.from_pretrained( model_id, trust_remote_codeTrue, dtypeauto, attn_implementationflash_attention_2, # 启用FlashAttention ).to(device).eval()️ 常见问题解决1. 音频格式支持问题MOSS-Transcribe-Diarize支持多种音频和视频格式音频格式MP3、WAV、FLAC、M4A等视频格式MP4、MOV、MKV、AVI等采样率自动重采样到16kHz2. 内存不足处理如果遇到内存不足错误可以尝试使用CPU模式device resolve_device(cpu)减少max_new_tokens参数值分割长音频为多个片段处理3. 模型加载失败确保正确设置trust_remote_codeTruemodel AutoModelForCausalLM.from_pretrained( model_id, trust_remote_codeTrue, # 必须设置为True dtypeauto, ) 实际应用场景会议记录自动化def transcribe_meeting(audio_path): 自动化会议记录生成 messages build_transcription_messages( audio_path, prompt请转录这次会议讨论清晰标注每个发言人的内容和时间。 ) result generate_transcription(model, processor, messages) return format_meeting_minutes(result[text]) def format_meeting_minutes(transcript): 格式化会议纪要 lines transcript.strip().split(\n) formatted [] for line in lines: if line.startswith([): # 解析时间戳和说话人 parts line.split(]) time parts[0][1:] # 去掉开头的[ speaker parts[1][1:] if len(parts) 1 else text parts[2] if len(parts) 2 else formatted.append(f{time}秒 - {speaker}: {text}) return \n.join(formatted)播客内容索引def create_podcast_index(audio_path): 创建播客时间索引 result generate_transcription(model, processor, messages) transcript result[text] # 提取时间戳和主题 timestamps extract_timestamps(transcript) topics extract_topics(transcript) return { transcript: transcript, timestamps: timestamps, topics: topics, duration: get_audio_duration(audio_path) } 总结MOSS-Transcribe-Diarize提供了Python和命令行两种简单易用的API接口无论是技术开发者还是普通用户都能快速上手。通过本教程您已经掌握了环境配置快速搭建运行环境命令行使用一键式音频转录Python API灵活编程接口高级功能自定义提示、批量处理性能优化内存和速度调优技巧这款强大的开源语音转写工具将极大提升您的音频处理效率无论是会议记录、播客制作还是视频字幕生成都能提供专业级的转录服务。立即开始使用MOSS-Transcribe-Diarize体验高效精准的语音转写体验吧✨【免费下载链接】MOSS-Transcribe-DiarizeMOSS-Transcribe-Diarize 是 OpenMOSS 团队推出的开源语音转写与说话人分离模型。它对长音频、多说话人音频进行统一建模支持自动语音识别、带说话人标识的转写、说话人分离、时间戳预测以及简洁转录文本生成。项目地址: https://ai.gitcode.com/OpenMOSS/MOSS-Transcribe-Diarize创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考