2024-08-10 16:49:36 +08:00
|
|
|
import os
|
2024-08-10 16:58:59 +08:00
|
|
|
import time
|
2024-08-04 19:00:15 +08:00
|
|
|
from traceback import print_exc
|
2024-08-04 17:34:16 +08:00
|
|
|
import base64
|
2024-08-09 15:43:28 +08:00
|
|
|
import wave
|
2024-08-03 18:22:53 +08:00
|
|
|
from appPublic.log import info, debug, warning, error, exception, critical
|
2024-08-03 19:06:37 +08:00
|
|
|
from appPublic.dictObject import DictObject
|
2024-08-06 15:56:45 +08:00
|
|
|
from appPublic.folderUtils import temp_file
|
2024-08-03 17:27:14 +08:00
|
|
|
from ahserver.serverenv import ServerEnv
|
|
|
|
from aiohttp.web import StreamResponse
|
|
|
|
|
2024-08-09 15:43:28 +08:00
|
|
|
def save_base64_wav(base64_data, output_file,sample_rate=16000, num_channels=1):
|
|
|
|
# Decode the base64 data
|
|
|
|
wav_data = base64.b64decode(base64_data)
|
2024-08-06 15:56:45 +08:00
|
|
|
|
2024-08-09 15:43:28 +08:00
|
|
|
# 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
|
2024-08-06 15:56:45 +08:00
|
|
|
|
2024-08-09 15:43:28 +08:00
|
|
|
# Write the decoded data to the WAV file
|
|
|
|
wf.writeframes(wav_data)
|
2024-08-06 15:56:45 +08:00
|
|
|
|
2024-08-10 16:42:51 +08:00
|
|
|
async def generate(request, kw):
|
2024-08-03 19:18:31 +08:00
|
|
|
params_kw = kw.get('params_kw', DictObject())
|
2024-08-03 19:16:33 +08:00
|
|
|
model = params_kw.model
|
2024-08-05 17:00:39 +08:00
|
|
|
audio = params_kw.audio
|
|
|
|
if audio is None:
|
2024-08-04 17:34:16 +08:00
|
|
|
return {
|
|
|
|
'status':'error',
|
|
|
|
'message':'audio is null'
|
|
|
|
}
|
2024-08-09 15:43:28 +08:00
|
|
|
fname = temp_file(suffix='.wav')
|
|
|
|
save_base64_wav(audio, fname)
|
2024-08-03 19:16:33 +08:00
|
|
|
engine = None
|
|
|
|
g = ServerEnv()
|
|
|
|
if model=='whisper':
|
|
|
|
engine = g.whisper_engine
|
2024-08-03 17:27:14 +08:00
|
|
|
|
2024-08-04 17:34:16 +08:00
|
|
|
if engine is None:
|
2024-08-04 18:43:17 +08:00
|
|
|
return {
|
2024-08-04 17:34:16 +08:00
|
|
|
'status':'error',
|
|
|
|
'message':f'model={model} is not defined'
|
|
|
|
}
|
|
|
|
try:
|
2024-08-10 16:58:59 +08:00
|
|
|
t1 = time.time()
|
|
|
|
dic = await engine.stt(fname)
|
|
|
|
t2 = time.time()
|
2024-08-06 15:56:45 +08:00
|
|
|
os.remove(fname)
|
2024-08-10 17:00:13 +08:00
|
|
|
info(f'{dic=}')
|
2024-08-04 17:34:16 +08:00
|
|
|
return {
|
2024-08-04 18:42:22 +08:00
|
|
|
"status":"ok",
|
2024-08-10 16:58:59 +08:00
|
|
|
"time_cost":t2-t1,
|
|
|
|
"content":dic['text'],
|
|
|
|
"segments":dic['segments'],
|
|
|
|
"language":dic['language']
|
2024-08-04 17:34:16 +08:00
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
exception(f'{e}')
|
|
|
|
print_exc()
|
|
|
|
return {
|
|
|
|
'status':'error',
|
|
|
|
'message':f'{e}'
|
|
|
|
}
|
|
|
|
|