fastwhisper/app/fastwhisper.py
2025-11-06 12:12:46 +08:00

57 lines
1.5 KiB
Python

import asyncio
import json
from appPublic.worker import get_event_loop
from appPublic.log import debug
from fw.init import load_fastwhisper
from ahserver.webapp import webapp
from ahserver.serverenv import ServerEnv
from appPublic.registerfunction import RegisterFunction
async def transcribe(request, *args, **kw):
env = request._run_ns
ret = await env.fastwhisper.submit_task(env.params_kw)
return ret
async def get_status(request, *args, **kw):
env = request._run_ns
debug(f'get_status():{env.params_kw.task_id=}')
data = await env.fastwhisper.get_status(env.params_kw.task_id)
if isinstance(data['result'], str):
data['result'] = json.loads(data['result'])
return ret
async def asr(request, *args, **kw):
env = request._run_ns
ret = await env.fastwhisper.submit_task(env.params_kw)
data = await env.fastwhisper.get_status(ret['task_id'])
if data['status'] == 'SUCCEEDED':
s = data.get('result')
if s:
data['result'] = json.loads(s)
return data
async def start_fw_server(*args, **kw):
debug(f'start fastwhisper engine, {args=}, {kw=}')
env = ServerEnv()
asyncio.create_task(env.fastwhisper.run())
debug('fastwhisper engine started ')
async def ahapp_built(app):
env = ServerEnv()
app.on_startup.append(start_fw_server)
def init():
rf = RegisterFunction()
rf.register('asr', asr)
rf.register('transcribe', transcribe)
rf.register('ahapp_built', ahapp_built)
rf.register('get_status', get_status)
load_fastwhisper()
if __name__ == '__main__':
loop = get_event_loop()
webapp(init)