Spaces:
Sleeping
Sleeping
| #!/usr/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| import argparse | |
| import logging | |
| from pathlib import Path | |
| import platform | |
| import re | |
| from project_settings import project_path, log_directory | |
| import log | |
| log.setup(log_directory=log_directory) | |
| import gradio as gr | |
| from toolbox.os.command import Command | |
| main_logger = logging.getLogger("main") | |
| def get_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--example_wav_dir", | |
| default=(project_path / "data/examples").as_posix(), | |
| type=str | |
| ) | |
| args = parser.parse_args() | |
| return args | |
| def process_uploaded_file(filename: str) -> str: | |
| filename = Path(filename).as_posix() | |
| main_logger.info("asr recognize: {}".format(filename)) | |
| cmd = "build/asr_id --filename {}".format( | |
| filename | |
| ) | |
| asr_result = Command.popen(cmd) | |
| pattern = "text: (.*)textSize: (.*)wordSize: (.*)timeCost: (.+)" | |
| match = re.search(pattern, asr_result, flags=re.IGNORECASE | re.DOTALL) | |
| if match is None: | |
| raise AssertionError("run asr recognize failed: \n{}".format(asr_result)) | |
| text = match.group(1) | |
| return text | |
| def shell(cmd: str): | |
| return Command.popen(cmd) | |
| def main(): | |
| args = get_args() | |
| title = "## 针对电话场景的印尼语ASR." | |
| # examples | |
| example_wav_dir = Path(args.example_wav_dir) | |
| examples = list() | |
| for filename in example_wav_dir.glob("*.wav"): | |
| examples.append( | |
| [ | |
| filename.as_posix() | |
| ] | |
| ) | |
| # blocks | |
| with gr.Blocks() as blocks: | |
| gr.Markdown(value=title) | |
| with gr.Tabs(): | |
| with gr.TabItem("Upload from disk"): | |
| uploaded_file = gr.Audio( | |
| sources=["upload"], | |
| type="filepath", | |
| label="Upload from disk", | |
| ) | |
| upload_button = gr.Button("Submit for recognition") | |
| uploaded_output = gr.Textbox(label="Recognized speech from uploaded file") | |
| gr.Examples( | |
| examples=examples, | |
| inputs=[ | |
| uploaded_file, | |
| ], | |
| outputs=[ | |
| uploaded_output | |
| ], | |
| fn=process_uploaded_file | |
| ) | |
| upload_button.click( | |
| process_uploaded_file, | |
| inputs=[ | |
| uploaded_file, | |
| ], | |
| outputs=[ | |
| uploaded_output | |
| ], | |
| ) | |
| with gr.TabItem("shell"): | |
| shell_text = gr.Textbox(label="cmd") | |
| shell_button = gr.Button("run") | |
| shell_output = gr.Textbox(label="output") | |
| shell_button.click( | |
| shell, | |
| inputs=[ | |
| shell_text, | |
| ], | |
| outputs=[ | |
| shell_output | |
| ], | |
| ) | |
| blocks.queue().launch( | |
| share=False if platform.system() == "Windows" else False, | |
| server_name="127.0.0.1" if platform.system() == "Windows" else "0.0.0.0", | |
| server_port=7860 | |
| ) | |
| return | |
| if __name__ == "__main__": | |
| main() | |