"""Offline GPU transcription. Decode with FFmpeg, avoiding torchaudio FFmpeg ABI coupling.""" import json,sys from pathlib import Path import numpy as np,soundfile as sf,torch from transformers import AutoModel if not torch.cuda.is_available():raise RuntimeError('CUDA fehlt; kein CPU-Fallback.') models,input_file,output,melody=sys.argv[1:5] audio,sr=sf.read(input_file,dtype='float32',always_2d=True) model=AutoModel.from_pretrained(str(Path(models)/'SheetSage2'),base_model_path=str(Path(models)/'MERT-v2-FullSong'),trust_remote_code=True,local_files_only=True).eval().to('cuda') waveform=audio.mean(axis=1) with torch.inference_mode(): try:result=model.transcribe(waveform,sampling_rate=sr,output_dir=output,melody_only=melody=='true') except RuntimeError as exc: # SheetSage2 can quantize a note at a hard file boundary to an unrepresentable # tiny duration. Retry that specific notation failure with analysis-only silence. if 'cannot be represented on the decoded subbeat grid' not in str(exc):raise result=model.transcribe(np.pad(waveform,(0,2*sr)),sampling_rate=sr,output_dir=output,melody_only=melody=='true') abc=result['abc'] if not isinstance(abc,str) or not abc.strip():raise RuntimeError('Keine Musiknotation erzeugt.') (Path(output)/'score.abc').write_text(abc)