aligner/app/aligner.py
2026-04-17 19:00:07 +08:00

69 lines
1.6 KiB
Python

from align import AlignEngine
from ahserver.serverenv import ServerEnv
from ahserver.webapp import webapp
from ahserver.filestorage import FileStorage
from appPublic.worker import awaitify
from appPublic.jsonConfig import getConfig
from appPublic.registerfunction import RegisterFunction
from appPublic.log import debug, exception
async def lyric_align(request, params_kw, *params, **kw):
audio_webpath = params_kw.audio_path
text = params_kw.text
debug(f'{params_kw=}')
if audio_webpath is None:
exception(f'{params_kw=}')
return {
"status": "error",
"data": {
"message": "audio_path is None"
}
}
if text is None:
exception(f'{params_kw=}')
return {
"status": "error",
"data": {
"message": "text is None"
}
}
env = ServerEnv()
fs = FileStorage()
audio_path = fs.realPath(audio_webpath)
align = awaitify(env.align_engine.align)
text = text.replace(" ", "").replace('\t', "")
text1 = text.replace("\n", "")
s = await align(audio_path, text1)
debug(f'{s=}, {text1=},{text=}')
lines = text.split('\n')
c_pos = 0
c_max = len(s) - 1
sentences = []
for l in lines:
if l:
segment={
'sentence': l,
'start': s[c_pos]['start'],
'chars':[]
}
for c in l:
if c_pos <= c_max:
segment['chars'].append(s[c_pos])
else:
debug(f'{l=}, {c=}, {c_max=}')
c_pos += 1
segment['end'] = s[c_pos -1]['end']
sentences.append(segment)
return sentences
def init():
rf = RegisterFunction()
rf.register('align', lyric_align)
env = ServerEnv()
config = getConfig()
env.align_engine = AlignEngine(config.align_model)
if __name__ == '__main__':
webapp(init)