import base64 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 audiob64 = params_kw.audiob64 if audiob64 is None: return { 'status':'error', 'message':'audio is null' } engine = None g = ServerEnv() if model=='whisper': engine = g.whisper_engine if engine is None: retrun { 'status':'error', 'message':f'model={model} is not defined' } try: txt = await engine.stt(audiob64) info(f'{audiob64=}, {txt=}') return { "status':'ok', "content':txt } except Exception as e: exception(f'{e}') print_exc() return { 'status':'error', '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