import os import shutil import logging from pathlib import Path from typing import Optional logger = logging.getLogger(__name__) CUSTOM_MODELS_DIR = Path("models/custom") CUSTOM_MODELS_DIR.mkdir(parents=True, exist_ok=True) def is_gpu_available() -> bool: """Checks if GPU acceleration is available for Lab tasks.""" try: import torch return torch.cuda.is_available() except ImportError: return False def register_custom_model( name: str, file_path: str, engine_type: str, description: str = "" ) -> str: """ Registers a custom model file (.onnx or .pt) into the Lab. Returns the new voice_key. """ ext = Path(file_path).suffix if ext not in [".onnx", ".pt", ".bin", ".npy"]: raise ValueError(f"Unsupported model extension: {ext}") voice_key = f"{'clone' if ext in ['.pt', '.npy'] else 'custom'}_{name.lower().replace(' ', '_')}" target_path = CUSTOM_MODELS_DIR / f"{name}{ext}" shutil.copy(file_path, target_path) logger.info("Registered custom model: %s -> %s", name, target_path) return voice_key def delete_custom_voice(voice_key: str) -> bool: """Removes a custom voice file from the Lab.""" from core.models import get_voice_list voices = get_voice_list() for v in voices: if v["key"] == voice_key and v.get("category") in ["cloned", "fine-tuned"]: path = v.get("path") or v.get("voice") if path and os.path.exists(path): os.remove(path) logger.info("Deleted custom voice file: %s", path) return True return False def extract_style_vector(audio_path: str) -> Optional[str]: """ EXPERIMENTAL: Extracts a Kokoro style vector from audio. Requires torch and the Kokoro encoder model. """ if not is_gpu_available(): logger.warning("GPU/Torch not found. Style extraction is unavailable.") return None # FUTURE: Implementation for style extraction return None