from __future__ import annotations import os import sys import threading from pathlib import Path from types import SimpleNamespace from huggingface_hub import snapshot_download os.environ.setdefault("TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD", "1") ROOT = Path(__file__).resolve().parent # The inference path of openvpi/GAME at this revision, vendored in GAME/ with its MIT license SOURCE_REVISION = "4ad815c90dfe2442730f3fdc866fd23e737cbc97" SOURCE_DIR = ROOT / "GAME" # The checkpoint from the v1.0.0 release, mirrored on the Hub MODEL_NAME = "GAME-1.0-small" MODEL_REPO = "teamup-tech/GAME-weights" MODEL_REVISION = "77b6f82a8dc257a37a8870218bc0ca50a6578cf2" _runtime_lock = threading.Lock() _inference_model_lock = threading.Lock() _runtime: SimpleNamespace | None = None _inference_model = None _language_map: dict[str, int] | None = None def _get_model_dir() -> Path: return Path(snapshot_download( MODEL_REPO, revision=MODEL_REVISION, allow_patterns=[f"{MODEL_NAME}/*"] )) / MODEL_NAME def _get_runtime() -> SimpleNamespace: global _runtime if _runtime is not None: return _runtime with _runtime_lock: if _runtime is not None: return _runtime source_path = str(SOURCE_DIR) if source_path not in sys.path: sys.path.insert(0, source_path) from inference.api import infer_model, load_inference_model from inference.callbacks import ( SaveCombinedMidiFileCallback, SaveCombinedTextFileCallback, ) from inference.data import SlicedAudioFileIterableDataset from inference.slicer2 import Slicer from lib.config.schema import ValidationConfig _runtime = SimpleNamespace( infer_model=infer_model, load_inference_model=load_inference_model, MidiCallback=SaveCombinedMidiFileCallback, TextCallback=SaveCombinedTextFileCallback, Dataset=SlicedAudioFileIterableDataset, Slicer=Slicer, ValidationConfig=ValidationConfig, ) return _runtime def _get_model(runtime: SimpleNamespace): global _inference_model, _language_map if _inference_model is not None: return _inference_model, _language_map with _inference_model_lock: if _inference_model is None: _inference_model, _language_map = runtime.load_inference_model( _get_model_dir() / "model.pt" ) return _inference_model, _language_map def _language_id( language_code: str, language_map: dict[str, int] | None, ) -> int: if not language_code: return 0 if language_map is None or language_code not in language_map: supported = ", ".join(language_map or ()) raise ValueError( f"Language '{language_code}' is not supported. " f"Supported languages: {supported}" ) return language_map[language_code] def transcribe( audio_path: Path, output_dir: Path, language_code: str, steps: int, ) -> None: runtime = _get_runtime() model, language_map = _get_model(runtime) sample_rate = model.inference_config.features.audio_sample_rate dataset = runtime.Dataset( filemap={audio_path.stem: audio_path}, samplerate=sample_rate, slicer=runtime.Slicer( sr=sample_rate, threshold=-40.0, min_length=1000, min_interval=200, max_sil_kept=100, ), language=_language_id(language_code, language_map), ) callbacks = [ runtime.MidiCallback(output_dir=output_dir, tempo=120), runtime.TextCallback( output_dir=output_dir, file_format="csv", pitch_format="name", round_pitch=False, ), ] config = runtime.ValidationConfig( d3pm_sample_t0=0.0, d3pm_sample_steps=steps, d3pm_sample_ts=None, boundary_decoding_threshold=0.2, boundary_decoding_radius=round(0.02 / model.timestep), note_presence_threshold=0.2, ) runtime.infer_model( model=model, dataset=dataset, config=config, callbacks=callbacks, batch_size=1, num_workers=0, precision="32-true", ) def load_model() -> None: """Load the model now, so that the first request does not wait on it.""" _get_model(_get_runtime())