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

@@ -104,23 +104,24 @@ class ProfilePictureManager:
try:
if debug:
logger.info("Starting face detector container...")
# Use Docker socket API to start container
import aiofiles
import json as json_lib
# Docker socket path
# Prefer using the face_detector_manager helper (robust compose/docker logic)
try:
from .face_detector_manager import face_detector_manager
started = await face_detector_manager.start_container(debug=debug)
return started
except Exception as e:
if debug:
logger.debug(f"face_detector_manager not available or failed to create/start container: {e}; falling back to Docker socket API")
# Fallback: Use Docker socket API to start container
socket_path = "/var/run/docker.sock"
# Check if socket exists
if not os.path.exists(socket_path):
if debug:
logger.error("Docker socket not available")
return False
# Use aiohttp UnixConnector to communicate with Docker socket
from aiohttp import UnixConnector
async with aiohttp.ClientSession(
connector=UnixConnector(path=socket_path)
) as session:
@@ -132,9 +133,9 @@ class ProfilePictureManager:
error_text = await response.text()
logger.error(f"Failed to start container: {response.status} - {error_text}")
return False
# Wait for API to be ready
for i in range(30): # 30 second timeout
# Wait for API to be ready (fallback timeout)
for i in range(60): # 60 second timeout (fallback)
try:
async with aiohttp.ClientSession() as session:
async with session.get(
@@ -148,9 +149,9 @@ class ProfilePictureManager:
except:
pass
await asyncio.sleep(1)
if debug:
logger.warning("Face detector didn't become ready in time")
logger.warning("Face detector didn't become ready in time (fallback)")
return False
except Exception as e:
@@ -163,16 +164,24 @@ class ProfilePictureManager:
try:
if debug:
logger.info("Stopping face detector to free VRAM...")
# Prefer using face_detector_manager if available
try:
from .face_detector_manager import face_detector_manager
await face_detector_manager.stop_container(debug=debug)
return
except Exception as e:
if debug:
logger.debug(f"face_detector_manager not available or failed to stop container: {e}; falling back to Docker socket API")
socket_path = "/var/run/docker.sock"
if not os.path.exists(socket_path):
if debug:
logger.error("Docker socket not available")
return
from aiohttp import UnixConnector
async with aiohttp.ClientSession(
connector=UnixConnector(path=socket_path)
) as session:
@@ -186,7 +195,7 @@ class ProfilePictureManager:
if debug:
error_text = await response.text()
logger.warning(f"Failed to stop container: {response.status} - {error_text}")
except Exception as e:
if debug:
logger.error(f"Error stopping face detector: {e}")