Switch to dedicated MarianMT for cleaner, non-LLM translation
This commit is contained in:
@@ -5,12 +5,12 @@ import queue
|
||||
import sys
|
||||
import torch
|
||||
from silero_vad import load_silero_vad, get_speech_timestamps
|
||||
from mlx_lm import load, generate
|
||||
from transformers import MarianMTModel, MarianTokenizer
|
||||
|
||||
# Parameters
|
||||
WHISPER_MODEL = "mlx-community/whisper-small.en-mlx"
|
||||
LLM_MODEL = "mlx-community/SmolLM2-135M-Instruct" # Verified public model
|
||||
TARGET_LANG = "Spanish"
|
||||
# Dedicated EN-ES translation model (very fast and accurate)
|
||||
TRANSLATE_MODEL = "Helsinki-NLP/opus-mt-en-es"
|
||||
CHANNELS = 1
|
||||
SAMPLERATE = 16000
|
||||
BLOCK_SIZE = 512
|
||||
@@ -25,26 +25,23 @@ def callback(indata, frames, time, status):
|
||||
print(status, file=sys.stderr)
|
||||
audio_queue.put(indata.copy())
|
||||
|
||||
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)
|
||||
|
||||
response = generate(model, tokenizer, prompt=prompt_text, max_tokens=100, verbose=False)
|
||||
return response.strip()
|
||||
|
||||
def main():
|
||||
# Set device for translation model
|
||||
device = "mps" if torch.backends.mps.is_available() else "cpu"
|
||||
print(f"Using device for translation: {device}")
|
||||
|
||||
print(f"Loading Whisper model '{WHISPER_MODEL}'...")
|
||||
|
||||
print(f"Loading LLM for translation '{LLM_MODEL}'...")
|
||||
llm_model, llm_tokenizer = load(LLM_MODEL)
|
||||
print(f"Loading dedicated translation model '{TRANSLATE_MODEL}'...")
|
||||
tokenizer = MarianTokenizer.from_pretrained(TRANSLATE_MODEL)
|
||||
model = MarianMTModel.from_pretrained(TRANSLATE_MODEL).to(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)")
|
||||
print(f"\nStarting live transcription & translation... (Press Ctrl+C to stop)")
|
||||
|
||||
audio_buffer = []
|
||||
speech_started = False
|
||||
@@ -80,11 +77,14 @@ def main():
|
||||
original_text = result['text'].strip()
|
||||
|
||||
if original_text:
|
||||
# 2. Translate using LLM
|
||||
translated_text = translate_text(llm_model, llm_tokenizer, original_text, TARGET_LANG)
|
||||
# 2. Translate using dedicated MarianMT
|
||||
inputs = tokenizer(original_text, return_tensors="pt", padding=True).to(device)
|
||||
with torch.no_grad():
|
||||
translated_tokens = model.generate(**inputs)
|
||||
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
||||
|
||||
print(f"\nEN: {original_text}")
|
||||
print(f"{TARGET_LANG[:2].upper()}: {translated_text}")
|
||||
print(f"ES: {translated_text}")
|
||||
|
||||
audio_buffer = []
|
||||
speech_started = False
|
||||
|
||||
Reference in New Issue
Block a user