"""The hosted adapter uses PyTorch, not the local CTranslate2 CPU engine.""" import os import time # ZeroGPU forks workers. Avoid inheriting CPU thread pools into the child. os.environ['OMP_NUM_THREADS'] = '1' os.environ['MKL_NUM_THREADS'] = '1' os.environ['TOKENIZERS_PARALLELISM'] = 'false' MODEL_ID = 'openai/whisper-medium' REVISION = 'abdf7c39ab9d0397620ccaea8974cc764cd0953e' TEXT_ONLY = os.getenv('READLOOPS_TEXT_ONLY') == '1' def gpu_duration(job): """Reserve less shared GPU time for clips of at most 30 seconds.""" return 30 if len(job['samples']) <= 30 * 16000 else 60 if not TEXT_ONLY: # Import before torch: ZeroGPU patches CUDA allocation during module loading. import spaces import torch torch.set_num_threads(1) from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline processor = AutoProcessor.from_pretrained(MODEL_ID, revision=REVISION, trust_remote_code=False) model = AutoModelForSpeechSeq2Seq.from_pretrained( MODEL_ID, revision=REVISION, dtype=torch.float16, use_safetensors=True, trust_remote_code=False, attn_implementation='sdpa') model.to('cuda') model.eval() recognizer = pipeline('automatic-speech-recognition', model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, device='cuda:0', dtype=torch.float16, chunk_length_s=30, stride_length_s=5) @spaces.GPU(duration=gpu_duration) def transcribe(job): started = time.perf_counter() print('ReadLoops GPU: inference started', flush=True) with torch.inference_mode(): output = recognizer({'raw': job['samples'], 'sampling_rate': 16000}, batch_size=1, num_workers=0, generate_kwargs={'language': job['language'], 'task': 'transcribe', 'num_beams': 5, 'condition_on_prev_tokens': False, 'disable_compile': True}) print('ReadLoops GPU: inference finished', flush=True) text = output['text'].strip() if not text: raise ValueError('No speech was recognised. Try again or type the words you said.') return {'id': job['id'], 'session_id': job['session_id'], 'text': text, 'metrics': {**job['metrics'], 'inference_seconds': round(time.perf_counter() - started, 3), 'model': MODEL_ID, 'revision': REVISION}} else: def transcribe(job): raise ValueError('Speech is disabled in this local text-only check.')