Fix variable initialization order (vad_model defined before stream start)
This commit is contained in:
@@ -51,20 +51,23 @@ def main():
|
|||||||
device = "mps" if torch.backends.mps.is_available() else "cpu"
|
device = "mps" if torch.backends.mps.is_available() else "cpu"
|
||||||
print(f"Using device: {device}")
|
print(f"Using device: {device}")
|
||||||
|
|
||||||
|
# 1. Load models first
|
||||||
print(f"Loading Whisper model '{WHISPER_MODEL}'...")
|
print(f"Loading Whisper model '{WHISPER_MODEL}'...")
|
||||||
|
|
||||||
# Dictionary to hold models and tokenizers
|
|
||||||
translation_engines = {}
|
translation_engines = {}
|
||||||
|
|
||||||
for lang_name, model_id in TARGET_LANGS.items():
|
for lang_name, model_id in TARGET_LANGS.items():
|
||||||
print(f"Loading {lang_name} translation model ({model_id})...")
|
print(f"Loading {lang_name} translation model ({model_id})...")
|
||||||
tokenizer = MarianTokenizer.from_pretrained(model_id)
|
tokenizer = MarianTokenizer.from_pretrained(model_id)
|
||||||
model = MarianMTModel.from_pretrained(model_id).to(device)
|
model = MarianMTModel.from_pretrained(model_id).to(device)
|
||||||
translation_engines[lang_name] = (model, tokenizer)
|
translation_engines[lang_name] = (model, tokenizer)
|
||||||
|
|
||||||
|
print("Loading Silero VAD model...")
|
||||||
|
vad_model = load_silero_vad()
|
||||||
|
print("Models loaded.")
|
||||||
|
|
||||||
|
# 2. Select Audio Device
|
||||||
print("\nAvailable Audio Devices:")
|
print("\nAvailable Audio Devices:")
|
||||||
devices = sd.query_devices()
|
print(sd.query_devices())
|
||||||
print(devices)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
device_input = input("\nSelect input device index (or press Enter for default): ")
|
device_input = input("\nSelect input device index (or press Enter for default): ")
|
||||||
@@ -79,6 +82,7 @@ def main():
|
|||||||
speech_started = False
|
speech_started = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 3. Start stream
|
||||||
with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE, device=device_index):
|
with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE, device=device_index):
|
||||||
while True:
|
while True:
|
||||||
while not audio_queue.empty():
|
while not audio_queue.empty():
|
||||||
@@ -104,22 +108,20 @@ def main():
|
|||||||
|
|
||||||
if (buffer_len_samples - last_end) > (SAMPLERATE * MIN_SILENCE_DURATION_MS / 1000) or buffer_len_samples > BUFFER_LIMIT:
|
if (buffer_len_samples - last_end) > (SAMPLERATE * MIN_SILENCE_DURATION_MS / 1000) or buffer_len_samples > BUFFER_LIMIT:
|
||||||
|
|
||||||
# 1. Transcribe once
|
# Transcribe once
|
||||||
result = mlx_whisper.transcribe(current_audio, path_or_hf_repo=WHISPER_MODEL)
|
result = mlx_whisper.transcribe(current_audio, path_or_hf_repo=WHISPER_MODEL)
|
||||||
original_text = result['text'].strip()
|
original_text = result['text'].strip()
|
||||||
|
|
||||||
if original_text:
|
if original_text:
|
||||||
# Limit the input length to avoid memory spikes or model glitches
|
|
||||||
if len(original_text) > 250:
|
if len(original_text) > 250:
|
||||||
original_text = original_text[:247] + "..."
|
original_text = original_text[:247] + "..."
|
||||||
|
|
||||||
print(f"\n[EN]: {original_text}")
|
print(f"\n[EN]: {original_text}")
|
||||||
|
|
||||||
# 2. Translate to all targets
|
# Translate to all targets
|
||||||
for lang_name, (model, tokenizer) in translation_engines.items():
|
for lang_name, (model, tokenizer) in translation_engines.items():
|
||||||
inputs = tokenizer(original_text, return_tensors="pt", padding=True).to(device)
|
inputs = tokenizer(original_text, return_tensors="pt", padding=True).to(device)
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
# Added max_new_tokens to prevent runaway generation
|
|
||||||
translated_tokens = model.generate(**inputs, max_new_tokens=150)
|
translated_tokens = model.generate(**inputs, max_new_tokens=150)
|
||||||
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
||||||
print(f"[{lang_name[:2].upper()}]: {translated_text}")
|
print(f"[{lang_name[:2].upper()}]: {translated_text}")
|
||||||
|
|||||||
Reference in New Issue
Block a user