2024-08-04 17:34:16 +08:00
|
|
|
import base64
|
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-03 17:27:14 +08:00
|
|
|
from ahserver.serverenv import ServerEnv
|
|
|
|
from aiohttp.web import StreamResponse
|
|
|
|
|
|
|
|
async def generate(request, **kw):
|
2024-08-03 19:18:31 +08:00
|
|
|
params_kw = kw.get('params_kw', DictObject())
|
2024-08-04 08:50:43 +08:00
|
|
|
info(f'{params_kw=}')
|
2024-08-03 19:16:33 +08:00
|
|
|
model = params_kw.model
|
2024-08-04 17:34:16 +08:00
|
|
|
audiob64 = params_kw.audiob64
|
|
|
|
if audiob64 is None:
|
|
|
|
return {
|
|
|
|
'status':'error',
|
|
|
|
'message':'audio is null'
|
|
|
|
}
|
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:
|
|
|
|
txt = await engine.stt(audiob64)
|
|
|
|
info(f'{audiob64=}, {txt=}')
|
|
|
|
return {
|
2024-08-04 18:42:22 +08:00
|
|
|
"status":"ok",
|
|
|
|
"content":txt
|
2024-08-04 17:34:16 +08:00
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
exception(f'{e}')
|
|
|
|
print_exc()
|
|
|
|
return {
|
|
|
|
'status':'error',
|
|
|
|
'message':f'{e}'
|
|
|
|
}
|
|
|
|
|
|
|
|
async def streaming(request, **kw):
|
2024-08-03 19:20:42 +08:00
|
|
|
resp = StreamResponse()
|
2024-08-03 19:16:33 +08:00
|
|
|
await resp.prepare(request)
|
|
|
|
line = await request.content.readline()
|
|
|
|
content = ''
|
|
|
|
while line:
|
2024-08-03 18:22:53 +08:00
|
|
|
info(f'read from request stream {line=}')
|
2024-08-03 19:16:33 +08:00
|
|
|
txt = await engine.stt(line)
|
|
|
|
content += txt
|
2024-08-04 09:42:23 +08:00
|
|
|
d = json.dumps({'content':content}) + '\n'
|
2024-08-03 19:16:33 +08:00
|
|
|
btxt = d.encode('utf8')
|
|
|
|
await resp.write(btxt)
|
|
|
|
await resp.drain()
|
|
|
|
line = await request.content.readline()
|
2024-08-03 18:22:53 +08:00
|
|
|
info('response finish')
|
2024-08-03 19:16:33 +08:00
|
|
|
return resp
|
2024-08-03 17:27:14 +08:00
|
|
|
|