himybro/forcedAlignment
0
1import gradio as gr2from aeneas.executetask import ExecuteTask3from aeneas.task import Task4import tempfile5import os6 7def align_audio_text(audio_file, transcript):8 9 with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_audio:10 tmp_audio.write(audio_file.read())11 audio_path = tmp_audio.name12 13 with tempfile.NamedTemporaryFile(suffix=".txt", mode="w", delete=False) as tmp_text:14 tmp_text.write(transcript)15 text_path = tmp_text.name16 17 output_path = audio_path + ".json"18 19 config_string = u"task_language=eng|os_task_file_format=json|is_text_type=plain"20 21 task = Task(config_string=config_string)22 task.audio_file_path_absolute = audio_path23 task.text_file_path_absolute = text_path24 task.sync_map_file_path_absolute = output_path25 26 ExecuteTask(task).execute()27 task.output_sync_map_file()28 29 with open(output_path, "r") as f:30 result = f.read()31 32 os.remove(audio_path)33 os.remove(text_path)34 os.remove(output_path)35 36 return result37 38demo = gr.Interface(39 fn=align_audio_text,40 inputs=[41 gr.Audio(type="binary", label="Upload Audio (.mp3, .wav)"),42 gr.Textbox(label="Transcript (Text)"),43 ],44 outputs="json",45 title="Forced Alignment with Aeneas",46 description="Upload audio + text, get forced alignment as JSON."47)48 49if __name__ == "__main__":50 demo.launch()51 