bugfix
This commit is contained in:
parent
29de7d4673
commit
b443404708
@ -6,7 +6,7 @@ from appPublic.jsonConfig import getConfig
|
||||
from appPublic.registerfunction import RegisterFunction
|
||||
from ahserver.configuredServer import ConfiguredServer
|
||||
from ahserver.serverenv import ServerEnv
|
||||
from whisper_model import WhisperBase64
|
||||
from whisper_model import WhisperFile
|
||||
from asr_engine import generate
|
||||
|
||||
__version__ = '0.0.1'
|
||||
|
@ -2,9 +2,45 @@ from traceback import print_exc
|
||||
import base64
|
||||
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
|
||||
|
||||
from io import BytesIO
|
||||
import struct
|
||||
|
||||
audio_dic2list(audio):
|
||||
ks = audio.keys()
|
||||
ks.sort()
|
||||
return [audio[k] for k in ks]
|
||||
|
||||
def float32array_to_wav(samples, sample_rate=16000, num_channels=1):
|
||||
# Calculate the total number of samples
|
||||
num_samples = len(samples)
|
||||
|
||||
# Calculate the byte rate
|
||||
byte_rate = sample_rate * num_channels * 4
|
||||
|
||||
# Calculate the block align
|
||||
block_align = num_channels * 4
|
||||
|
||||
# Create the WAV header
|
||||
header = struct.pack(
|
||||
'<4sI4s4sIHHIIHH4sI',
|
||||
b'RIFF', 36 + num_samples * 4, b'WAVE', b'fmt ', 16, 3, num_channels, sample_rate,
|
||||
byte_rate, block_align, 32, b'data', num_samples * 4
|
||||
)
|
||||
|
||||
# Convert the Float32Array to bytes
|
||||
data = struct.pack('f' * num_samples, *samples)
|
||||
|
||||
# Write the header and data to a file
|
||||
tmpfile = temp_file(suffix='.wav')
|
||||
with open(tmpfile, 'w') as f:
|
||||
f.write(header)
|
||||
f.write(data)
|
||||
return tmpfile
|
||||
|
||||
async def generate(request, **kw):
|
||||
params_kw = kw.get('params_kw', DictObject())
|
||||
info(f'{params_kw=}')
|
||||
@ -26,7 +62,10 @@ async def generate(request, **kw):
|
||||
'message':f'model={model} is not defined'
|
||||
}
|
||||
try:
|
||||
txt = await engine.stt(audio)
|
||||
audio = audio_dic2list(audio)
|
||||
fname = float32array_to_wav(audio)
|
||||
txt = await engine.stt(fname)
|
||||
os.remove(fname)
|
||||
info(f'{audio=}, {txt=}')
|
||||
return {
|
||||
"status":"ok",
|
||||
@ -40,20 +79,3 @@ async def generate(request, **kw):
|
||||
'message':f'{e}'
|
||||
}
|
||||
|
||||
async def streaming(request, **kw):
|
||||
resp = StreamResponse()
|
||||
await resp.prepare(request)
|
||||
line = await request.content.readline()
|
||||
content = ''
|
||||
while line:
|
||||
info(f'read from request stream {line=}')
|
||||
txt = await engine.stt(line)
|
||||
content += txt
|
||||
d = json.dumps({'content':content}) + '\n'
|
||||
btxt = d.encode('utf8')
|
||||
await resp.write(btxt)
|
||||
await resp.drain()
|
||||
line = await request.content.readline()
|
||||
info('response finish')
|
||||
return resp
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user