(input_wav_path, input_prompt)
| 52 | |
| 53 | |
| 54 | def do_decode(input_wav_path, input_prompt): |
| 55 | # input_prompt = TASK_PROMPT_MAPPING.get(input_prompt, "未知任务类型") |
| 56 | print(f"wav_path: {input_wav_path}, prompt:{input_prompt}") |
| 57 | timestamp_ms = int(time.time() * 1000) |
| 58 | now_file_tmp_path_resample = f'./.cache/.temp/{timestamp_ms}_resample.wav' |
| 59 | do_resample(input_wav_path, now_file_tmp_path_resample) |
| 60 | input_wav_path = now_file_tmp_path_resample |
| 61 | waveform, sample_rate = torchaudio.load(input_wav_path) |
| 62 | waveform = waveform.squeeze(0) # (channel=1, sample) -> (sample,) |
| 63 | print(f'wavform shape: {waveform.shape}, sample_rate: {sample_rate}') |
| 64 | window = torch.hann_window(400) |
| 65 | stft = torch.stft(waveform, |
| 66 | 400, |
| 67 | 160, |
| 68 | window=window, |
| 69 | return_complex=True) |
| 70 | magnitudes = stft[..., :-1].abs() ** 2 |
| 71 | |
| 72 | filters = torch.from_numpy( |
| 73 | librosa.filters.mel(sr=sample_rate, |
| 74 | n_fft=400, |
| 75 | n_mels=80)) |
| 76 | mel_spec = filters @ magnitudes |
| 77 | |
| 78 | # NOTE(xcsong): https://github.com/openai/whisper/discussions/269 |
| 79 | log_spec = torch.clamp(mel_spec, min=1e-10).log10() |
| 80 | log_spec = torch.maximum(log_spec, log_spec.max() - 8.0) |
| 81 | log_spec = (log_spec + 4.0) / 4.0 |
| 82 | feat = log_spec.transpose(0, 1) |
| 83 | feat_lens = torch.tensor([feat.shape[0]], dtype=torch.int64).to(gpu_id) |
| 84 | feat = feat.unsqueeze(0).to(gpu_id) |
| 85 | # feat = feat.half() |
| 86 | # feat_lens = feat_lens.half() |
| 87 | res_text = model.generate(wavs=feat, wavs_len=feat_lens, prompt=input_prompt)[0] |
| 88 | print("识别结果:", res_text) |
| 89 | return res_text |
| 90 | |
| 91 | |
| 92 | if __name__ == "__main__": |
no test coverage detected