From af8f0a1a82d9c44723f32cf3fa3a6f2147534889 Mon Sep 17 00:00:00 2001 From: Abhiroop Talasila Date: Wed, 2 Feb 2022 22:37:21 +0530 Subject: [PATCH] Add app-level logging --- autosub/audioProcessing.py | 7 +++++-- autosub/logger.py | 21 +++++++++++++++++++++ autosub/main.py | 18 ++++++++++-------- autosub/segmentAudio.py | 5 ++++- autosub/utils.py | 23 +++++++++++++---------- 5 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 autosub/logger.py diff --git a/autosub/audioProcessing.py b/autosub/audioProcessing.py index 5200e0c..6e6ae54 100644 --- a/autosub/audioProcessing.py +++ b/autosub/audioProcessing.py @@ -2,10 +2,13 @@ # -*- coding: utf-8 -*- import sys +import logger import subprocess import numpy as np from os.path import basename +_logger = logger.setup_applevel_logger(__name__) + def extract_audio(input_file, audio_file_name): """Extract audio from input video file and save to audio/ in root dir @@ -18,9 +21,9 @@ def extract_audio(input_file, audio_file_name): command = ["ffmpeg", "-hide_banner", "-loglevel", "warning", "-i", input_file, "-ac", "1", "-ar", "16000", "-vn", "-f", "wav", audio_file_name] ret = subprocess.run(command).returncode - print("Extracted audio to audio/{}".format(basename(audio_file_name))) + _logger.info(f"Extracted audio to audio/{basename(audio_file_name)}") except Exception as e: - print("Error: ", str(e)) + _logger.error(str(e)) sys.exit(1) diff --git a/autosub/logger.py b/autosub/logger.py new file mode 100644 index 0000000..d6c0739 --- /dev/null +++ b/autosub/logger.py @@ -0,0 +1,21 @@ +import sys +import logging + +APP_NAME = "AutoSub" + +def setup_applevel_logger(logger_name = APP_NAME, file_name=None): + logger = logging.getLogger(logger_name) + logger.setLevel(logging.INFO) + formatter = logging.Formatter("[%(levelname)s] %(message)s") #%(name)s | + sh = logging.StreamHandler(sys.stdout) + sh.setFormatter(formatter) + logger.handlers.clear() + logger.addHandler(sh) + if file_name: + fh = logging.FileHandler(file_name) + fh.setFormatter(formatter) + logger.addHandler(fh) + return logger + +def get_logger(module_name): + return logging.getLogger(APP_NAME).getChild(module_name) \ No newline at end of file diff --git a/autosub/main.py b/autosub/main.py index b9e6562..dbd2a90 100644 --- a/autosub/main.py +++ b/autosub/main.py @@ -5,6 +5,7 @@ import re import sys import wave +import logger import argparse import numpy as np @@ -15,6 +16,7 @@ from audioProcessing import extract_audio from segmentAudio import remove_silent_segments +_logger = logger.setup_applevel_logger(__name__) # Line count for SRT file line_count = 1 @@ -96,7 +98,7 @@ def main(): args = parser.parse_args() #print(sys.argv[0:]) - print("ARGS:", args) + _logger.info(f"ARGS: {args}") ds_model = get_model(args, "model") ds_scorer = get_model(args, "scorer") @@ -105,18 +107,18 @@ def main(): create_model(ds_model, ds_scorer) if args.file is not None: if not os.path.isfile(args.file): - print(f"Invalid file: {args.file}") + _logger.warn(f"Invalid file: {args.file}") sys.exit(0) if args.file is not None: if os.path.isfile(args.file): input_file = args.file - print(f"Input file: {args.file}") + _logger.info(f"Input file: {args.file}") else: - print(f"Invalid file: {args.file}") + _logger.error(f"Invalid file: {args.file}") sys.exit(1) else: - print("Error. One or more of --file or --dry-run are required.") + _logger.error("One or more of --file or --dry-run are required") sys.exit(1) base_directory = os.getcwd() @@ -141,14 +143,14 @@ def main(): clean_folder(audio_directory) extract_audio(input_file, audio_file_name) - print("Splitting on silent parts in audio file") + _logger.info("Splitting on silent parts in audio file") remove_silent_segments(audio_file_name) audiofiles = [file for file in os.listdir(audio_directory) if file.startswith(video_prefix)] audiofiles = sort_alphanumeric(audiofiles) audiofiles.remove(os.path.basename(audio_file_name)) - print("\nRunning inference:") + _logger.info("Running inference...") ds = create_model(ds_model, ds_scorer) for filename in tqdm(audiofiles): @@ -157,7 +159,7 @@ def main(): for format in output_file_handle_dict: file_handle = output_file_handle_dict[format] - print(format.upper(), "file saved to", file_handle.name) + _logger.info(f"{format.upper()}, file saved to, {file_handle.name}") file_handle.close() diff --git a/autosub/segmentAudio.py b/autosub/segmentAudio.py index 55f25f6..556fe12 100644 --- a/autosub/segmentAudio.py +++ b/autosub/segmentAudio.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import os +import logger import numpy as np import trainAudio as TA @@ -9,6 +10,8 @@ import featureExtraction as FE import scipy.io.wavfile as wavfile +_logger = logger.setup_applevel_logger(__name__) + def read_audio_file(input_file): """This function returns a numpy array that stores the audio samples of a @@ -35,7 +38,7 @@ def read_audio_file(input_file): temp_signal.append(data[chn::audiofile.channels]) signal = np.array(temp_signal).T except: - print("Error: file not found or other I/O error. (DECODING FAILED)") + _logger.error("File not found or other I/O error. (DECODING FAILED)") if signal.ndim == 2 and signal.shape[1] == 1: signal = signal.flatten() diff --git a/autosub/utils.py b/autosub/utils.py index 07e1819..2b86801 100644 --- a/autosub/utils.py +++ b/autosub/utils.py @@ -5,8 +5,11 @@ import os import sys import shutil +import logger from deepspeech import Model +_logger = logger.setup_applevel_logger(__name__) + def sort_alphanumeric(data): """Sort function to sort os.listdir() alphanumerically Helps to process audio files sequentially after splitting @@ -35,7 +38,7 @@ def clean_folder(folder): elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: - print(f"Failed to delete {file_path}. Reason: {e}") + _logger.warn(f"Failed to delete {file_path}. Reason: {e}") def get_model(args, arg_name): """Will prioritze supplied arguments but if not, try to find files @@ -55,31 +58,31 @@ def get_model(args, arg_name): if arg is not None: model = os.path.abspath(arg) if not os.path.isfile(model): - print(f"Error. Supplied file {arg} doesn't exist. Please supply a valid {arg_name} file via the --{arg_name} flag.") + _logger.error(f"Supplied file {arg} doesn't exist. Please supply a valid {arg_name} file via the --{arg_name} flag") sys.exit(1) else: models = [file for file in os.listdir() if file.endswith(arg_extension)] num_models = len(models) if num_models == 0: - print(f"Warning no {arg_name}s specified via --{arg_name} and none found in local directory. Please run getmodel.sh to get some.") + _logger.warn(f"No {arg_name}s specified via --{arg_name} and none found in local directory. Please run getmodel.sh to get some") if arg_name == 'model': - print("Error: Must have pbmm model. Exiting") + _logger.error("Must specify pbmm model") sys.exit(1) else: model = '' elif num_models != 1: - print(f"Warning. Detected {num_models} {arg_name} files in local dir") + _logger.warn(f"Detected {num_models} {arg_name} files in local dir") if arg_name == 'model': - print("Must specify pbmm model. Exiting") + _logger.error("Must specify pbmm model") sys.exit(1) else: - print("Please specify scorer using --scorer") + _logger.warn("Please specify scorer using --scorer") model = '' else: model = os.path.abspath(models[0]) - print(f"{arg_name.capitalize()}: {model}") + _logger.info(f"{arg_name.capitalize()}: {model}") return(model) def create_model(model, scorer): @@ -93,11 +96,11 @@ def create_model(model, scorer): try: ds = Model(model) except: - print("Invalid model file. Exiting") + _logger.error("Invalid model file") sys.exit(1) try: ds.enableExternalScorer(scorer) except: - print("Invalid scorer file. Running inference using only model file") + _logger.warn("Invalid scorer file. Running inference using only model file") return(ds) \ No newline at end of file