Spaces:
Sleeping
Sleeping
Download core/lab_engine.py from ssamreshan/BizPiper: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/ssamreshan/BizPiper/resolve/main/core/lab_engine.py
- Command line
-
hf download hf://spaces/ssamreshan/BizPiper/core/lab_engine.py
-
curl -L -o lab_engine.py https://huggingface.co/spaces/ssamreshan/BizPiper/resolve/main/core/lab_engine.py
2.03 kB
| 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 | |