Face Detector container now able to be created, started and stopped from within miku-bot container

This commit is contained in:
2026-01-11 02:01:41 +02:00
parent 2d3b9d0e08
commit 353c9c9583
14 changed files with 2275 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
Y# face_detector_manager.py
# face_detector_manager.py
"""
Manages on-demand starting/stopping of anime-face-detector container
to free up VRAM when not needed.
@@ -20,14 +20,72 @@ class FaceDetectorManager:
FACE_DETECTOR_API = "http://anime-face-detector:6078/detect"
HEALTH_ENDPOINT = "http://anime-face-detector:6078/health"
CONTAINER_NAME = "anime-face-detector"
STARTUP_TIMEOUT = 30 # seconds
STARTUP_TIMEOUT = 60 # seconds - increased to allow for model loading
def __init__(self):
self.is_running = False
def _container_exists(self) -> bool:
"""Check if the anime-face-detector container exists (created but may not be running)"""
try:
result = subprocess.run(
["docker", "ps", "-a", "--filter", f"name=^/{self.CONTAINER_NAME}$", "--format", "{{.Names}}"],
capture_output=True,
text=True,
timeout=5
)
return self.CONTAINER_NAME in result.stdout
except Exception as e:
logger.error(f"Error checking if container exists: {e}")
return False
def _create_container(self, debug: bool = False) -> bool:
"""Create the anime-face-detector container using docker run"""
try:
if debug:
logger.info("Creating anime-face-detector container...")
# Run docker run command to create the container (without starting it)
# This replicates the docker-compose configuration for anime-face-detector
cmd = [
"docker", "create",
"--name", self.CONTAINER_NAME,
"--network", "miku-discord_default", # Use the same network as miku-bot
"--runtime", "nvidia",
"-e", "NVIDIA_VISIBLE_DEVICES=all",
"-e", "NVIDIA_DRIVER_CAPABILITIES=compute,utility",
"-p", "7860:7860",
"-p", "6078:6078",
"--restart", "no",
"--gpus", "all",
"miku-discord-anime-face-detector:latest"
]
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=30
)
if result.returncode != 0:
if debug:
logger.error(f"Failed to create container: {result.stderr}")
return False
if debug:
logger.info("Container created successfully")
return True
except Exception as e:
if debug:
logger.error(f"Error creating container: {e}")
return False
async def start_container(self, debug: bool = False) -> bool:
"""
Start the anime-face-detector container.
Creates the container if it doesn't exist, then starts it.
Returns:
True if started successfully, False otherwise
@@ -36,10 +94,14 @@ class FaceDetectorManager:
if debug:
logger.debug("Starting anime-face-detector container...")
# Start container using docker compose
# Step 1: Check if container exists, create if it doesn't
if not self._container_exists():
if not self._create_container(debug=debug):
return False
# Step 2: Start the container
result = subprocess.run(
["docker", "compose", "up", "-d", self.CONTAINER_NAME],
cwd="/app", # Assumes we're in the bot container, adjust path as needed
["docker", "start", self.CONTAINER_NAME],
capture_output=True,
text=True,
timeout=30
@@ -81,8 +143,7 @@ class FaceDetectorManager:
logger.debug("Stopping anime-face-detector container...")
result = subprocess.run(
["docker", "compose", "stop", self.CONTAINER_NAME],
cwd="/app",
["docker", "stop", self.CONTAINER_NAME],
capture_output=True,
text=True,
timeout=15