feat: 调用百炼服务合成语音

This commit is contained in:
martsforever
2025-11-16 19:49:24 +08:00
parent 3110d6d229
commit dca009ed26
4 changed files with 234 additions and 3 deletions
@@ -0,0 +1,31 @@
import sys
import threading
from io import BufferedWriter
from fastapi import FastAPI, Query
from starlette.websockets import WebSocket
import dashscope
from dashscope.audio.tts import SpeechSynthesizer
from app.config.env import env
dashscope.api_key = env.llm_key_bailian
def add_websocket_voice_generate_route(app: FastAPI):
@app.post("/ws_voice_generate")
async def ws_voice_generate():
# dashscope.api_key = "apiKey"
result = SpeechSynthesizer.call(model='sambert-zhichu-v1',
text='今天天气怎么样',
sample_rate=16000,
format='mp3')
if result.get_audio_data() is not None:
with open('output.mp3', 'wb') as f:
f.write(result.get_audio_data())
print('SUCCESS: get audio data: %dbytes in output.mp3' %
(sys.getsizeof(result.get_audio_data())))
else:
print('ERROR: response is %s' % (result.get_response()))