speechtotext

[whisper fine tune] AIhub 데이터로 전이학습 - 2

파이썬정복 2024. 9. 22. 20:05

< Argparse를 이용한 audio 처리를 위한 하부 명령어 (sub-command) 등록 및 터미널 arguments 등록 >

subcommand
    - audio
    - file
import argparse
from utils import PrepareDataset

 

오디오 처리

def audio_process(config) -> None:
    print('Start audio processing')
    preprocessor = PrepareDataset()
    preprocessor.process_audio(
        source_dir=config.target_dir,
        remove_original_audio=config.remove_original_audio,
    )

 

**Configuration(구성/설정)**는 시스템이나 소프트웨어의 특정 작동 방식을 정의하거나 설정하는 과정이나 상태를 의미

 

파일 처리

def file_process(config) -> None:
    print('Start file processing')
    preprocessor = PrepareDataset()
    if config.target_file:
        if not (
            config.csv or
            config.pkl or
            config.split_whole_data or
            config.split_train_test
        ):
            print(f'If --target-file (-t) is feed, \
one of --csv, --pkl, --split-train-test (-s) or \
--split-whole-data (-w) must be set.')
            return
        if config.csv:
            preprocessor.save_trn_to_csv(config.target_file)
        if config.pkl:
            preprocessor.save_trn_to_pkl(config.target_file)
        if config.split_whole_data:
            preprocessor.split_whole_data(config.target_file)
        if config.split_train_test:
            preprocessor.split_train_test(
                target_file=config.target_file,
                train_size=config.ratio
            )
    if config.convert_all_to_utf:
        if not config.target_dir:
            print('If --convert-all-to-utf8 (-c) flagged, you must feed --target-dir')
        preprocessor.convert_all_files_to_utf8(config.target_dir)
    if config.remove_all_text_files:
        if not config.target_dir:
            print(f'If --remove-all-text-files (-R) flagged,\
                    you must feed --target-dir')
            return
        preprocessor.remove_all_text_files(
            target_dir=config.target_dir,
            ext=config.remove_file_type
        )

 

오디오 와 파일을 다루기 위한 parser / subparser 정의

: argparse는 Python에서 명령줄 인자를 처리하기 위한 표준 라이브러리로, 다양한 옵션과 인자를 간편하게 정의하고 처리할 수 있습니다. 특히 딥러닝 프로젝트에서 오디오 파일을 처리할 때 argparse를 활용해 명령줄에서 다양한 동작을 관리할 수 있습니다. -> 터미널에서 바로 쓸 수 있음

1. ParserSubparser 정의

  • Parser: 최상위 명령어를 처리하는 객체입니다. 사용자가 실행하는 메인 프로그램의 명령줄 인자를 처리합니다. 예를 들어, 오디오 파일의 경로, 배치 크기, 학습률 등을 최상위 parser로 정의할 수 있습니다.
  • Subparser: 각 기능별로 세분화된 명령어입니다. 이를 통해 한 프로그램 내에서 여러 작업을 처리할 수 있습니다. 예를 들어, 오디오 파일을 전처리하거나 학습하거나 평가하는 명령을 각각 하부 명령어로 등록할 수 있습니다.
def get_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        prog='Korean speech dataset pre-processor',
        description='Process Korean speech dataset'
    )
    sub_parser = parser.add_subparsers(title='sub-command')
    
    ### Parser for sub-command 'audio' ###
    parser_audio = sub_parser.add_parser(
        'audio',
        help='sub-command for audio processing'
    )
    parser_audio.add_argument(
        '--target-dir', '-t',
        required=True,
        help='directory of audio files'
    )
    parser_audio.add_argument(
        '--remove-original-audio', '-r',
        action='store_true',
        help='Remove original audio files'
    )
    parser_audio.set_defaults(func=audio_process)
    
    ### Parser for sub-command 'file' ###
    parser_file = sub_parser.add_parser(
        'file',
        help='handling txt encoding, generate pkl/csv file,\
            or split file (train/test)'
    )
    parser_file.add_argument(
        '--target-file', '-t',
        # required=True,
        help='Target file name for processing'
    )
    parser_file.add_argument(
        '--convert-all-to-utf', '-c',
        action='store_true',
        help='Convert all text files to utf-8 under target_dir'
    )
    parser_file.add_argument(
        '--target-dir', '-d',
        # required=True,
        help='Target directory for converting file encoding to utf-8\
            Use by combining --convert-all-to-utf (-c) flag'
    )
    parser_file.add_argument(
        '--split-whole-data', '-w',
        action='store_true',
        help='Split whole data file int group'
    )
    parser_file.add_argument(
        '--csv',
        action='store_true',
        help='Generate csv file'
    )
    parser_file.add_argument(
        '--pkl',
        action='store_true',
        help='Generate pickle file'
    )
    parser_file.add_argument(
        '--split-train-test', '-s',
        action='store_true',
        help='Flag split train/test set, \
              default: 0.8 (train:test -> 80:20)'
    )
    parser_file.add_argument(
        '--ratio',
        type=float,
        default=0.8,
        help='Split file into .train & .test files'
    )
    parser_file.add_argument(
        '--remove-all-text-files', '-R',
        action='store_true',
        help='Remove all specific type files under target dir'
    )
    parser_file.add_argument(
        '--remove-file-type',
        default='txt',
        help='Set remove file type'
    )
    
    parser_file.set_defaults(func=file_process)
    config = parser.parse_args()
    return config

utils.py에 있는 함수를 불러쓸 수 있음