diff --git a/transcribe.py b/transcribe.py index 49c85db..b6db2b8 100644 --- a/transcribe.py +++ b/transcribe.py @@ -5,12 +5,12 @@ import queue import sys import torch from silero_vad import load_silero_vad, get_speech_timestamps -from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline +from mlx_lm import load, generate # 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 +LLM_MODEL = "mlx-community/SmolLM2-135M-Instruct-4bit" # Tiny but fast LLM for translation +TARGET_LANG = "Spanish" CHANNELS = 1 SAMPLERATE = 16000 BLOCK_SIZE = 512 @@ -25,19 +25,19 @@ def callback(indata, frames, time, 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 +def translate_text(model, tokenizer, text, target_lang): + prompt = f"Translate the following English text to {target_lang}. Only provide the translation, no extra text.\n\nEnglish: {text}\n\n{target_lang}:" + messages = [{"role": "user", "content": prompt}] + prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) - 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) + response = generate(model, tokenizer, prompt=prompt_text, max_tokens=100, verbose=False) + return response.strip() + +def main(): + print(f"Loading Whisper model '{WHISPER_MODEL}'...") + + print(f"Loading LLM for translation '{LLM_MODEL}'...") + llm_model, llm_tokenizer = load(LLM_MODEL) print("Loading Silero VAD model...") vad_model = load_silero_vad() @@ -80,12 +80,11 @@ def main(): original_text = result['text'].strip() if original_text: - # 2. Translate - translation = translator(original_text) - translated_text = translation[0]['translation_text'] + # 2. Translate using LLM + translated_text = translate_text(llm_model, llm_tokenizer, original_text, TARGET_LANG) print(f"\nEN: {original_text}") - print(f"ES: {translated_text}") + print(f"{TARGET_LANG[:2].upper()}: {translated_text}") audio_buffer = [] speech_started = False