| import os
|
| import torch
|
| from nemo.collections.asr.models import EncDecHybridRNNTCTCBPEModel
|
| from nemo.collections.asr.metrics.wer import word_error_rate
|
|
|
|
|
|
|
|
|
| MODEL_PATH = "output_finetuned/finetuned_model_best.nemo"
|
| SAMPLE_AUDIO = "arabic_recording.wav"
|
| EXPECTED_TEXT = "زيرو واحد واحد واحد واحد واحد واحد اتنين اربعة ستة"
|
|
|
|
|
|
|
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu"
|
| print(f"Loading model on: {device}")
|
|
|
| try:
|
| model = EncDecHybridRNNTCTCBPEModel.restore_from(restore_path=MODEL_PATH, map_location=device)
|
| model.eval()
|
| print("✅ Model loaded successfully.")
|
| except Exception as e:
|
| print(f"❌ Failed to load model: {e}")
|
| exit()
|
|
|
|
|
|
|
|
|
| def test_model(model, sample_audio, expected_text):
|
| if not os.path.exists(sample_audio):
|
| print(f"❌ Audio file not found: {sample_audio}")
|
| return
|
|
|
| print(f"\n🔍 Testing on: {sample_audio}")
|
|
|
|
|
| with torch.no_grad():
|
| output = model.transcribe([sample_audio])
|
|
|
|
|
| if isinstance(output, tuple):
|
|
|
| prediction_list = output[0]
|
| else:
|
| prediction_list = output
|
|
|
|
|
| prediction = prediction_list[0] if isinstance(prediction_list, list) else prediction_list
|
|
|
|
|
| print(f"\nPredicted: {prediction}")
|
| print(f"Expected : {expected_text}")
|
|
|
|
|
| wer = word_error_rate([expected_text], [prediction])
|
| print(f"\n📊 Word Error Rate (WER): {wer:.3f}")
|
| return prediction, wer
|
|
|
|
|
|
|
|
|
| if __name__ == "__main__":
|
| prediction, wer = test_model(model, SAMPLE_AUDIO, EXPECTED_TEXT)
|
|
|