|
| 1 | +import argparse |
| 2 | +import datasets |
| 3 | +import evaluate |
| 4 | +import io |
| 5 | +import json |
| 6 | +import soundfile as sf |
| 7 | +import tempfile |
| 8 | +import time |
| 9 | +from tqdm import tqdm |
| 10 | +import openai |
| 11 | +from normalizer import data_utils # must provide .normalizer() and .write_manifest() |
| 12 | + |
| 13 | +def transcribe_dataset( |
| 14 | + dataset_path, dataset, split, |
| 15 | + model_name="whisper-1", |
| 16 | +): |
| 17 | + # Load dataset |
| 18 | + ds = datasets.load_dataset(dataset_path, dataset, split=split, streaming=False) |
| 19 | + |
| 20 | + # Track results |
| 21 | + all_results = { |
| 22 | + "references": [], |
| 23 | + "predictions": [], |
| 24 | + "audio_length_s": [], |
| 25 | + "transcription_time_s": [], |
| 26 | + } |
| 27 | + |
| 28 | + print(f"Transcribing with OpenAI model: {model_name}") |
| 29 | + |
| 30 | + for i, sample in tqdm(enumerate(ds), total=len(ds), desc="Transcribing"): |
| 31 | + # Get reference text, use empty string if not present |
| 32 | + reference = sample.get("text", "").strip() |
| 33 | + |
| 34 | + # Write temp .wav file |
| 35 | + with tempfile.NamedTemporaryFile(suffix=".wav") as tmpfile: |
| 36 | + sf.write(tmpfile.name, sample["audio"]["array"], sample["audio"]["sampling_rate"], format="WAV") |
| 37 | + |
| 38 | + start = time.time() |
| 39 | + response = openai.Audio.transcribe( |
| 40 | + model=model_name, |
| 41 | + file=tmpfile, |
| 42 | + response_format="text" |
| 43 | + ) |
| 44 | + end = time.time() |
| 45 | + |
| 46 | + transcription = response.strip() |
| 47 | + reference = sample["text"] |
| 48 | + audio_duration = sample["audio_length_s"] |
| 49 | + transcription_time = end - start |
| 50 | + |
| 51 | + transcription = data_utils.normalizer(transcription) |
| 52 | + reference = data_utils.normalizer(reference) |
| 53 | + # Store |
| 54 | + all_results["predictions"].append(transcription) |
| 55 | + all_results["references"].append(reference) |
| 56 | + all_results["audio_length_s"].append(audio_duration) |
| 57 | + all_results["transcription_time_s"].append(transcription_time) |
| 58 | + |
| 59 | + # Save results to manifest |
| 60 | + manifest_path = data_utils.write_manifest( |
| 61 | + all_results["references"], |
| 62 | + all_results["predictions"], |
| 63 | + model_name, |
| 64 | + dataset_path, |
| 65 | + dataset, |
| 66 | + split, |
| 67 | + audio_length=all_results["audio_length_s"], |
| 68 | + transcription_time=all_results["transcription_time_s"], |
| 69 | + ) |
| 70 | + print("Results saved at path:", manifest_path) |
| 71 | + |
| 72 | + # Evaluate |
| 73 | + wer_metric = evaluate.load("wer") |
| 74 | + wer = wer_metric.compute( |
| 75 | + references=all_results["references"], |
| 76 | + predictions=all_results["predictions"] |
| 77 | + ) |
| 78 | + wer = round(100 * wer, 2) |
| 79 | + rtfx = round( |
| 80 | + sum(all_results["audio_length_s"]) / sum(all_results["transcription_time_s"]), |
| 81 | + 2 |
| 82 | + ) |
| 83 | + |
| 84 | + print("WER:", wer, "%", "RTFx:", rtfx) |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + parser = argparse.ArgumentParser(description="Transcribe using OpenAI Whisper API") |
| 88 | + |
| 89 | + parser.add_argument("--dataset_path", required=True, help="Dataset path or name") |
| 90 | + parser.add_argument("--dataset", required=True, help="Subset name of the dataset") |
| 91 | + parser.add_argument("--split", default="test", help="Dataset split") |
| 92 | + parser.add_argument("--model_name", default="whisper-1", help="OpenAI model name") |
| 93 | + |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + transcribe_dataset( |
| 97 | + dataset_path=args.dataset_path, |
| 98 | + dataset=args.dataset, |
| 99 | + split=args.split, |
| 100 | + model_name=args.model_name, |
| 101 | + ) |
0 commit comments