32 lines
988 B
Python
32 lines
988 B
Python
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()))
|