asr/app/asr_engine.py
2024-08-04 08:50:43 +08:00

32 lines
819 B
Python

from appPublic.log import info, debug, warning, error, exception, critical
from appPublic.dictObject import DictObject
from ahserver.serverenv import ServerEnv
from aiohttp.web import StreamResponse
async def generate(request, **kw):
params_kw = kw.get('params_kw', DictObject())
info(f'{params_kw=}')
model = params_kw.model
engine = None
g = ServerEnv()
if model=='whisper':
engine = g.whisper_engine
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})
btxt = d.encode('utf8')
await resp.write(btxt)
await resp.drain()
line = await request.content.readline()
info('response finish')
return resp