This commit is contained in:
yumoqing 2024-08-06 15:56:45 +08:00
parent 29de7d4673
commit b443404708
2 changed files with 41 additions and 19 deletions

View File

@ -6,7 +6,7 @@ from appPublic.jsonConfig import getConfig
from appPublic.registerfunction import RegisterFunction from appPublic.registerfunction import RegisterFunction
from ahserver.configuredServer import ConfiguredServer from ahserver.configuredServer import ConfiguredServer
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from whisper_model import WhisperBase64 from whisper_model import WhisperFile
from asr_engine import generate from asr_engine import generate
__version__ = '0.0.1' __version__ = '0.0.1'

View File

@ -2,9 +2,45 @@ from traceback import print_exc
import base64 import base64
from appPublic.log import info, debug, warning, error, exception, critical from appPublic.log import info, debug, warning, error, exception, critical
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.folderUtils import temp_file
from ahserver.serverenv import ServerEnv from ahserver.serverenv import ServerEnv
from aiohttp.web import StreamResponse 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): async def generate(request, **kw):
params_kw = kw.get('params_kw', DictObject()) params_kw = kw.get('params_kw', DictObject())
info(f'{params_kw=}') info(f'{params_kw=}')
@ -26,7 +62,10 @@ async def generate(request, **kw):
'message':f'model={model} is not defined' 'message':f'model={model} is not defined'
} }
try: 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=}') info(f'{audio=}, {txt=}')
return { return {
"status":"ok", "status":"ok",
@ -40,20 +79,3 @@ async def generate(request, **kw):
'message':f'{e}' '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