turn on interactive logging when interactive logging is enabled keras displays logs via stdout this provides the best experience when using keras in an interactive environment such as a shell or a notebook turn off interactive logging when interactive logging is disabled keras sends logs to absl logging this is the best option when using keras in a noninteractive way such as running a training or inference job on a server check if interactive logging is enabled to switch between writing logs to stdout and absl logging you may use keras config enableinteractivelogging and keras config disableinteractielogging returns boolean true if interactive logging is enabled and false otherwise sets the verbosity level for logging supported log levels are as follows fatal least verbose error warning info debug most verbose args level a string corresponding to the level of verbosity for logging print the message to absl logging or stdout if isinteractiveloggingenabled if linebreak sys stdout writemessage n else sys stdout writemessage sys stdout flush else logging infomessage def asktoproceedwithoverwritefilepath overwrite inputfwarning filepath already exists overwrite yn strip lower while overwrite not in y n overwrite input enter y overwrite or n cancel strip lower if overwrite n return false printmsgtip next time specify overwritetrue return true turn on interactive logging when interactive logging is enabled keras displays logs via stdout this provides the best experience when using keras in an interactive environment such as a shell or a notebook turn off interactive logging when interactive logging is disabled keras sends logs to absl logging this is the best option when using keras in a non interactive way such as running a training or inference job on a server check if interactive logging is enabled to switch between writing logs to stdout and absl logging you may use keras config enable_interactive_logging and keras config disable_interactie_logging returns boolean true if interactive logging is enabled and false otherwise sets the verbosity level for logging supported log levels are as follows fatal least verbose error warning info debug most verbose args level a string corresponding to the level of verbosity for logging print the message to absl logging or stdout produces a prompt asking about overwriting a file args filepath the path to the file to be overwritten returns true if we can proceed with overwrite false otherwise
import sys

from absl import logging

from keras.api_export import keras_export
from keras.backend.common import global_state


@keras_export(
    [
        "keras.config.enable_interactive_logging",
        "keras.utils.enable_interactive_logging",
    ]
)
def enable_interactive_logging():
    
    global_state.set_global_attribute("interactive_logging", True)


@keras_export(
    [
        "keras.config.disable_interactive_logging",
        "keras.utils.disable_interactive_logging",
    ]
)
def disable_interactive_logging():
    
    global_state.set_global_attribute("interactive_logging", False)


@keras_export(
    [
        "keras.config.is_interactive_logging_enabled",
        "keras.utils.is_interactive_logging_enabled",
    ]
)
def is_interactive_logging_enabled():
    
    return global_state.get_global_attribute("interactive_logging", True)


def set_logging_verbosity(level):
    
    valid_levels = {
        "FATAL": logging.FATAL,
        "ERROR": logging.ERROR,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
    }
    verbosity = valid_levels.get(level)
    if verbosity is None:
        raise ValueError(
            "Please pass a valid level for logging verbosity. "
            f"Expected one of: {set(valid_levels.keys())}. "
            f"Received: {level}"
        )
    logging.set_verbosity(verbosity)


def print_msg(message, line_break=True):
    
    if is_interactive_logging_enabled():
        if line_break:
            sys.stdout.write(message + "\n")
        else:
            sys.stdout.write(message)
        sys.stdout.flush()
    else:
        logging.info(message)


def ask_to_proceed_with_overwrite(filepath):
    
    overwrite = (
        input(f"[WARNING] {filepath} already exists - overwrite? [y/n]")
        .strip()
        .lower()
    )
    while overwrite not in ("y", "n"):
        overwrite = (
            input('Enter "y" (overwrite) or "n" (cancel).').strip().lower()
        )
    if overwrite == "n":
        return False
    print_msg("[TIP] Next time specify overwrite=True!")
    return True