58 lines
1.5 KiB
Python
58 lines
1.5 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
|
|
|
|
async def align(request, *params, params_kw={}):
|
|
audio_webpath = params_kw.audio_path
|
|
text = params_kw.text
|
|
if audio_webpath is None:
|
|
return {
|
|
"status": "error",
|
|
"data": {
|
|
"message": "audio_path is None"
|
|
}
|
|
}
|
|
if text is None:
|
|
return {
|
|
"status": "error",
|
|
"data": {
|
|
"message": "text is None"
|
|
}
|
|
}
|
|
env = ServerEnv()
|
|
fs = FileStorage()
|
|
audio_path = fs.realPath(audio_webpath)
|
|
align = awaitfy(env.align_engine.align)
|
|
s = await align(audio_path, text)
|
|
lines = text.split('\n')
|
|
c_pos = 0
|
|
sentences = []
|
|
for l in lines:
|
|
if l:
|
|
segment={
|
|
'sentence': l,
|
|
'start': s[c_pos]['start']
|
|
'chars':[]
|
|
}
|
|
for c in l:
|
|
c_pos += 1
|
|
segment['chars'].append(s[c_pos])
|
|
|
|
segment['end'] = s[c_pos -1]['end']
|
|
sentences.append(segment)
|
|
return sentences
|
|
|
|
def init():
|
|
rf = RegisterFunction()
|
|
rf.register('align', align)
|
|
env = ServerEnv()
|
|
config = getConfig()
|
|
env.align_engine = AlignEngine(config.align_model)
|
|
|
|
if __name_ == '__main__':
|
|
webapp(init)
|