Files
Practical_Training_Assignment/backend/app/api/v1/endpoints/asr.py

61 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from aip import AipSpeech
from fastapi import APIRouter
from starlette.websockets import WebSocket
from app.constants.asr import APP_ID, API_KEY, SECRET_KEY
asr_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
router = APIRouter()
@router.websocket("/asr")
async def chat2(websocket: WebSocket):
# 等待websocket接收数据
await websocket.accept()
temp_buffer = bytes()
# 在此处与百度建立websocket连接
while True:
# 等待websocket接收文本数据
receive_data = await websocket.receive()
buffer = receive_data.get("bytes")
text = receive_data.get("text")
if text == "录音完成":
asr_text = await asr_buffer(temp_buffer)
await websocket.send_text(asr_text)
temp_buffer = bytes()
else:
if buffer:
# 使用websocket API 无须再进行数据的合并每次拿到数据之后直接将内容发送给百度的websocket连接即可
temp_buffer += buffer
# 读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 识别本地文件
async def asr_file(filePath):
result = await asr_client.asr(get_file_content(filePath), 'pcm', 16000, {
'dev_pid': 1537,
})
if result.get('err_msg') == 'success.':
return result.get('result')[0]
else:
return '语音转换失败'
# 识别语音流
# async的意思是定义异步函数当使用await修饰异步函数并执行时如果该异步函数耗时比较长
# python会自动挂起异步函数让其他代码运行等到异步函数完成之后再回头调用函数
async def asr_buffer(buffer_data):
result = asr_client.asr(buffer_data, 'pcm', 16000, {
'dev_pid': 1537,
})
if result.get('err_msg') == 'success.':
return result.get('result')[0]
else:
return '语音转换失败'