File size: 1,714 Bytes
acb544e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import logging
import sys
from os import getpid
from typing import Dict, Literal
import psutil
import structlog
from llm_guard.util import configure_logger as configure_llm_guard_logger
LOG_LEVELS = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
EXTERNAL_LOGGERS = {
"transformers",
}
def configure_logger(log_level: LOG_LEVELS = "INFO"):
"""
Configures the logger for the package.
Args:
log_level: The log level to use for the logger. It should be one of the following strings:
"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL".
"""
logging.basicConfig(
format="[%(asctime)s - %(name)s - %(levelname)s] %(message)s",
level=log_level,
stream=sys.stdout,
)
structlog.configure(logger_factory=structlog.stdlib.LoggerFactory())
for log_name in EXTERNAL_LOGGERS:
logging.getLogger(log_name).setLevel(logging.WARNING)
configure_llm_guard_logger(log_level)
def get_resource_utilization() -> Dict:
"""
Returns the current resource utilization of the system.
Returns:
A dictionary containing the current resource utilization of the system.
"""
process = psutil.Process(getpid())
# A float representing the current system-wide CPU utilization as a percentage
cpu_percent = process.cpu_percent()
# A float representing process memory utilization as a percentage
memory_percent = process.memory_percent()
# Total physical memory
total_memory_bytes = psutil.virtual_memory().total
return {
"cpu_utilization_percent": cpu_percent,
"memory_utilization_percent": memory_percent,
"total_memory_available_bytes": total_memory_bytes,
}
|