diff --git a/pywhispercpp/_logger.py b/pywhispercpp/_logger.py deleted file mode 100644 index 4c0403c..0000000 --- a/pywhispercpp/_logger.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -""" -Internal Logger configuration -""" - -import logging -import sys - -# LEVELS -# logging.DEBUG -# logging.INFO -# logging.WARNING -# logging.ERROR -# logging.CRITICAL -# logging.NOTSET - -formatter = logging.Formatter('[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s') - -logging.basicConfig(stream=sys.stdout, - format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s', - level=logging.INFO) - - -def get_logger(): - return logging.getLogger() - - -def set_log_level(log_level): - logging.getLogger().setLevel(log_level) diff --git a/pywhispercpp/constants.py b/pywhispercpp/constants.py index 58fc9de..2182cdc 100644 --- a/pywhispercpp/constants.py +++ b/pywhispercpp/constants.py @@ -4,7 +4,6 @@ """ Constants """ -import logging from pathlib import Path from typing import Tuple @@ -21,10 +20,10 @@ PACKAGE_NAME = 'pywhispercpp' -LOGGIN_LEVEL = logging.INFO MODELS_DIR = Path(user_data_dir(PACKAGE_NAME)) / 'models' + AVAILABLE_MODELS = [ "base", "base-q5_1", diff --git a/pywhispercpp/model.py b/pywhispercpp/model.py index 57dabd9..d4c655c 100644 --- a/pywhispercpp/model.py +++ b/pywhispercpp/model.py @@ -13,7 +13,6 @@ from typing import Union, Callable, List import _pywhispercpp as pw import numpy as np -from pywhispercpp._logger import set_log_level import pywhispercpp.utils as utils import pywhispercpp.constants as constants import subprocess @@ -27,6 +26,8 @@ __version__ = importlib.metadata.version('pywhispercpp') +logger = logging.getLogger(__name__) + class Segment: """ A small class representing a transcription segment @@ -67,7 +68,6 @@ def __init__(self, model: str = 'tiny', models_dir: str = None, params_sampling_strategy: int = 0, - log_level: int = logging.INFO, **params): """ :param model: The name of the model, one of the [AVAILABLE_MODELS](/pywhispercpp/#pywhispercpp.constants.AVAILABLE_MODELS), @@ -75,13 +75,9 @@ def __init__(self, :param models_dir: The directory where the models are stored, or where they will be downloaded if they don't exist, default to [MODELS_DIR](/pywhispercpp/#pywhispercpp.constants.MODELS_DIR) :param params_sampling_strategy: 0 -> GREEDY, else BEAM_SEARCH - :param log_level: logging level, set to INFO by default :param params: keyword arguments for different whisper.cpp parameters, see [PARAMS_SCHEMA](/pywhispercpp/#pywhispercpp.constants.PARAMS_SCHEMA) """ - # set logging level - set_log_level(log_level) - if Path(model).is_file(): self.model_path = model else: @@ -129,10 +125,10 @@ def transcribe(self, # run inference start_time = time() - logging.info(f"Transcribing ...") + logger.info("Transcribing ...") res = self._transcribe(audio, n_processors=n_processors) end_time = time() - logging.info(f"Inference time: {end_time - start_time:.3f} s") + logger.info(f"Inference time: {end_time - start_time:.3f} s") return res @staticmethod @@ -220,7 +216,7 @@ def _init_model(self) -> None: Private method to initialize the method from the bindings, it will be called automatically from the __init__ :return: """ - logging.info("Initializing the model ...") + logger.info("Initializing the model ...") self._ctx = pw.whisper_init_from_file(self.model_path) self._params = pw.whisper_full_default_params(pw.whisper_sampling_strategy.WHISPER_SAMPLING_GREEDY) diff --git a/pywhispercpp/utils.py b/pywhispercpp/utils.py index d0446c5..c5c9b4b 100644 --- a/pywhispercpp/utils.py +++ b/pywhispercpp/utils.py @@ -12,6 +12,9 @@ from pywhispercpp.constants import MODELS_BASE_URL, MODELS_PREFIX_URL, AVAILABLE_MODELS, MODELS_DIR +logger = logging.getLogger(__name__) + + def _get_model_url(model_name: str) -> str: """ Returns the url of the `ggml` model @@ -31,11 +34,11 @@ def download_model(model_name: str, download_dir=None, chunk_size=1024) -> str: :return: Absolute path of the downloaded model """ if model_name not in AVAILABLE_MODELS: - logging.error(f"Invalid model name `{model_name}`, available models are: {AVAILABLE_MODELS}") + logger.error(f"Invalid model name `{model_name}`, available models are: {AVAILABLE_MODELS}") return if download_dir is None: download_dir = MODELS_DIR - logging.info(f"No download directory was provided, models will be downloaded to {download_dir}") + logger.info(f"No download directory was provided, models will be downloaded to {download_dir}") os.makedirs(download_dir, exist_ok=True) @@ -43,7 +46,7 @@ def download_model(model_name: str, download_dir=None, chunk_size=1024) -> str: file_path = Path(download_dir) / os.path.basename(url) # check if the file is already there if file_path.exists(): - logging.info(f"Model {model_name} already exists in {download_dir}") + logger.info(f"Model {model_name} already exists in {download_dir}") else: # download it from huggingface resp = requests.get(url, stream=True) @@ -60,7 +63,7 @@ def download_model(model_name: str, download_dir=None, chunk_size=1024) -> str: for data in resp.iter_content(chunk_size=chunk_size): size = file.write(data) progress_bar.update(size) - logging.info(f"Model downloaded to {file_path.absolute()}") + logger.info(f"Model downloaded to {file_path.absolute()}") except Exception as e: # error download, just remove the file os.remove(file_path)