Skip to content

Commit

Permalink
Add app-level logging
Browse files Browse the repository at this point in the history
  • Loading branch information
abhirooptalasila committed Feb 2, 2022
1 parent 542eda0 commit af8f0a1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
7 changes: 5 additions & 2 deletions autosub/audioProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand Down
21 changes: 21 additions & 0 deletions autosub/logger.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 10 additions & 8 deletions autosub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
import wave
import logger
import argparse

import numpy as np
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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()


Expand Down
5 changes: 4 additions & 1 deletion autosub/segmentAudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
# -*- coding: utf-8 -*-

import os
import logger
import numpy as np

import trainAudio as TA
from pydub import AudioSegment
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
Expand All @@ -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()
Expand Down
23 changes: 13 additions & 10 deletions autosub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)

0 comments on commit af8f0a1

Please sign in to comment.