()
| 7 | |
| 8 | |
| 9 | def main(): |
| 10 | model_dir = "FunAudioLLM/Fun-ASR-Nano-2512" |
| 11 | device = ( |
| 12 | "cuda:0" |
| 13 | if torch.cuda.is_available() |
| 14 | else "mps" |
| 15 | if torch.backends.mps.is_available() |
| 16 | else "cpu" |
| 17 | ) |
| 18 | m, kwargs = FunASRNano.from_pretrained(model=model_dir, device=device) |
| 19 | tokenizer = kwargs.get("tokenizer", None) |
| 20 | m.eval() |
| 21 | |
| 22 | wav_path = f"{kwargs['model_path']}/example/zh.mp3" |
| 23 | res = m.inference(data_in=[wav_path], **kwargs) |
| 24 | text = res[0][0] |
| 25 | print(text) |
| 26 | |
| 27 | chunk_size = 0.72 |
| 28 | duration = sf.info(wav_path).duration |
| 29 | cum_durations = np.arange(chunk_size, duration + chunk_size, chunk_size) |
| 30 | prev_text = "" |
| 31 | for idx, cum_duration in enumerate(cum_durations): |
| 32 | audio, rate = load_audio(wav_path, 16000, duration=round(cum_duration, 3)) |
| 33 | prev_text = m.inference([torch.tensor(audio)], prev_text=prev_text, **kwargs)[0][0]["text"] |
| 34 | if idx != len(cum_durations) - 1: |
| 35 | prev_text = tokenizer.decode(tokenizer.encode(prev_text)[:-5]).replace("�", "") |
| 36 | if prev_text: |
| 37 | print(prev_text) |
| 38 | |
| 39 | |
| 40 | if __name__ == "__main__": |
no test coverage detected