asr/app/asr_engine.py
2024-08-10 17:00:13 +08:00

68 lines
1.7 KiB
Python

import os
import time
from traceback import print_exc
import base64
import wave
from appPublic.log import info, debug, warning, error, exception, critical
from appPublic.dictObject import DictObject
from appPublic.folderUtils import temp_file
from ahserver.serverenv import ServerEnv
from aiohttp.web import StreamResponse
def save_base64_wav(base64_data, output_file,sample_rate=16000, num_channels=1):
# Decode the base64 data
wav_data = base64.b64decode(base64_data)
# Open a new WAV file for writing
with wave.open(output_file, 'wb') as wf:
# Set the parameters of the WAV file
wf.setnchannels(num_channels) # Mono channel
wf.setsampwidth(2) # 16-bit sample width
wf.setframerate(sample_rate) # 44.1 kHz sample rate
# Write the decoded data to the WAV file
wf.writeframes(wav_data)
async def generate(request, kw):
params_kw = kw.get('params_kw', DictObject())
model = params_kw.model
audio = params_kw.audio
if audio is None:
return {
'status':'error',
'message':'audio is null'
}
fname = temp_file(suffix='.wav')
save_base64_wav(audio, fname)
engine = None
g = ServerEnv()
if model=='whisper':
engine = g.whisper_engine
if engine is None:
return {
'status':'error',
'message':f'model={model} is not defined'
}
try:
t1 = time.time()
dic = await engine.stt(fname)
t2 = time.time()
os.remove(fname)
info(f'{dic=}')
return {
"status":"ok",
"time_cost":t2-t1,
"content":dic['text'],
"segments":dic['segments'],
"language":dic['language']
}
except Exception as e:
exception(f'{e}')
print_exc()
return {
'status':'error',
'message':f'{e}'
}