python3.6 安装protobuf_to_dict报错问题解决

安装:pip3 install protobuf-to-dict==0.0.5

安装后找到安装路径,修改文件python3.6/site-packages/protobuf_to_dict.py

把long改成int

unicode改成str

encode编码删除

运行代码时报错:import os import torch from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks from pydub import AudioSegment def load_asr_model(model_path=“D:/models/iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn”, device=None): “”" 加载ASR模型 - 修复版本 参数: model_path: 本地模型路径 device: 指定设备 (None=自动选择) 返回: 初始化的ASR管道 “”" if device is None: device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’ print(f"🔄 正在加载模型: {model_path} (设备: {device})...") return pipeline( task=Tasks.auto_speech_recognition, model=model_path, device=device, model_revision='v2.0.4' # 指定模型版本确保兼容性 ) def convert_to_wav(input_path, output_dir=“wav_output”, sample_rate=16000, channels=1): “”“将任意音频格式转换为标准WAV格式”“” os.makedirs(output_dir, exist_ok=True) filename = os.path.basename(input_path) name, _ = os.path.splitext(filename) output_path = os.path.join(output_dir, f"{name}.wav") try: audio = AudioSegment.from_file(input_path) audio = audio.set_frame_rate(sample_rate) audio = audio.set_channels(channels) audio.export(output_path, format="wav") print(f"✅ 转换完成: {input_path} -> {output_path}") return output_path except Exception as e: print(f"❌ 转换失败: {e}") return None def run_asr(asr_pipeline, wav_path): “”" 执行语音识别 - 修复版本 参数: asr_pipeline: 加载的模型管道 wav_path: WAV文件路径 返回: 识别结果字典 “”" print(f"🔊 开始识别: {wav_path}") # 修复:处理长音频的VAD配置 param_dict = { "vad_model": "fsmn-vad", # 显式指定VAD模型 "punc_model": "ct-punc", # 显式指定标点模型 "max_length_s": 60, # 最大处理长度(秒) "batch_size_token": 5, # 批处理大小 } result = asr_pipeline(audio_in=wav_path, param_dict=param_dict) # 解析结果 print("\n📝 识别结果:") if "text" in result: print(f"完整文本: {result['text']}") if "sentences" in result: print("\n分段结果:") for i, sent in enumerate(result["sentences"]): print(f"{i+1}. {sent['text']} (时间: {sent['start']:.2f}s-{sent['end']:.2f}s)") if "speaker" in result: print("\n👥 说话人分离结果:") for spk in result["speaker"]: print(f" 说话人{spk['spk_id']}: {spk['text']} (时间: {spk['start']:.2f}s-{spk['end']:.2f}s)") return result if name == “main”: # 配置参数 MODEL_PATH = “D:/models/iic/speech_paraformer-large-vad-punc-spk_asr_nat-zh-cn” INPUT_AUDIO = “test_audio.mp3” # 支持MP3, WAV, FLAC等格式 # 1. 音频转换 wav_file = convert_to_wav( input_path=INPUT_AUDIO, sample_rate=16000, channels=1 ) if not wav_file: exit(1) # 2. 模型加载 asr_pipeline = load_asr_model(MODEL_PATH) # 3. 执行识别 result = run_asr(asr_pipeline, wav_file) # 4. 保存结果 result_file = f"{os.path.splitext(wav_file)[0]}_result.txt" with open(result_file, "w", encoding="utf-8") as f: f.write(result["text"]) print(f"💾 结果已保存至: {result_file}") 报错信息:RuntimeError: Failed to import transformers.modeling_utils because of the following error (look up to see its traceback): Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: Downgrade the protobuf package to 3.20.x or lower. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower). More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值