File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change 44
55import dashscope
66from dashscope .audio .asr import (Recognition )
7+ from pydub import AudioSegment
78
89from setting .models_provider .base_model_provider import MaxKBBaseModel
910from setting .models_provider .impl .base_stt import BaseSpeechToText
@@ -40,7 +41,6 @@ def speech_to_text(self, audio_file):
4041 dashscope .api_key = self .api_key
4142 recognition = Recognition (model = self .model ,
4243 format = 'mp3' ,
43- sample_rate = 16000 ,
4444 callback = None )
4545 with tempfile .NamedTemporaryFile (delete = False ) as temp_file :
4646 # 将上传的文件保存到临时文件中
@@ -49,6 +49,13 @@ def speech_to_text(self, audio_file):
4949 temp_file_path = temp_file .name
5050
5151 try :
52+ audio = AudioSegment .from_file (temp_file_path )
53+ if audio .channels != 1 :
54+ audio = audio .set_channels (1 )
55+ audio = audio .set_frame_rate (16000 )
56+
57+ # 将转换后的音频文件保存到临时文件中
58+ audio .export (temp_file_path , format = 'mp3' )
5259 # 识别临时文件
5360 result = recognition .call (temp_file_path )
5461 text = ''
You can’t perform that action at this time.
0 commit comments