import mlx_whisper import numpy as np import sounddevice as sd import queue import sys import torch from silero_vad import load_silero_vad, get_speech_timestamps from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline # Parameters WHISPER_MODEL = "mlx-community/whisper-small.en-mlx" TRANSLATE_MODEL = "facebook/nllb-200-distilled-600M" TARGET_LANG = "spa_Latn" # Spanish (Latin America) - change as needed CHANNELS = 1 SAMPLERATE = 16000 BLOCK_SIZE = 512 VAD_THRESHOLD = 0.5 BUFFER_LIMIT = SAMPLERATE * 30 MIN_SILENCE_DURATION_MS = 500 audio_queue = queue.Queue() def callback(indata, frames, time, status): if status: print(status, file=sys.stderr) audio_queue.put(indata.copy()) def main(): # Set device for translation model (using MPS for Mac M chips) device = "mps" if torch.backends.mps.is_available() else "cpu" print(f"Using device: {device}") print(f"Loading Whisper model '{WHISPER_MODEL}'...") # Whisper MLX runs on its own optimized path print(f"Loading translation model '{TRANSLATE_MODEL}'...") tokenizer = AutoTokenizer.from_pretrained(TRANSLATE_MODEL) model = AutoModelForSeq2SeqLM.from_pretrained(TRANSLATE_MODEL).to(device) translator = pipeline("translation", model=model, tokenizer=tokenizer, src_lang="eng_Latn", tgt_lang=TARGET_LANG, device=device) print("Loading Silero VAD model...") vad_model = load_silero_vad() print("Models loaded.") print(f"\nStarting live transcription & translation to {TARGET_LANG}... (Press Ctrl+C to stop)") audio_buffer = [] speech_started = False try: with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE): while True: while not audio_queue.empty(): data = audio_queue.get() audio_buffer.append(data.flatten()) if len(audio_buffer) > 0: current_audio = np.concatenate(audio_buffer) audio_tensor = torch.from_numpy(current_audio) speech_timestamps = get_speech_timestamps( audio_tensor, vad_model, sampling_rate=SAMPLERATE, threshold=VAD_THRESHOLD, min_silence_duration_ms=MIN_SILENCE_DURATION_MS ) if len(speech_timestamps) > 0: speech_started = True last_end = speech_timestamps[-1]['end'] buffer_len_samples = len(current_audio) if (buffer_len_samples - last_end) > (SAMPLERATE * MIN_SILENCE_DURATION_MS / 1000) or buffer_len_samples > BUFFER_LIMIT: # 1. Transcribe (English) result = mlx_whisper.transcribe(current_audio, path_or_hf_repo=WHISPER_MODEL) original_text = result['text'].strip() if original_text: # 2. Translate translation = translator(original_text) translated_text = translation[0]['translation_text'] print(f"\nEN: {original_text}") print(f"ES: {translated_text}") audio_buffer = [] speech_started = False elif not speech_started and len(current_audio) > SAMPLERATE * 2: audio_buffer = [] except KeyboardInterrupt: print("\nStopped by user.") except Exception as e: print(f"\nError: {e}") if __name__ == "__main__": main()