FunAudioChat multimodal plugin for audio, image and video processing.
| 75 | |
| 76 | @dataclass |
| 77 | class FunAudioChatPlugin(BasePlugin): |
| 78 | """ |
| 79 | FunAudioChat multimodal plugin for audio, image and video processing. |
| 80 | """ |
| 81 | audio_reader: str = "read_audio" # Can be "read_audio" or custom reader |
| 82 | |
| 83 | @staticmethod |
| 84 | def load_audio_batch( |
| 85 | paths: list[str], sampling_rate: int, audio_reader: str = "read_audio" |
| 86 | ) -> list[np.ndarray]: |
| 87 | """ |
| 88 | Loads a batch of audio files. |
| 89 | |
| 90 | Args: |
| 91 | paths: List of audio file paths |
| 92 | sampling_rate: Target sampling rate |
| 93 | audio_reader: Audio reader type (default: "read_audio") |
| 94 | |
| 95 | Returns: |
| 96 | List of audio arrays |
| 97 | """ |
| 98 | return load_audio_batch_default(paths, sampling_rate) |
| 99 | |
| 100 | @override |
| 101 | def _get_mm_inputs( |
| 102 | self, |
| 103 | images: list["ImageInput"], |
| 104 | videos: list["VideoInput"], |
| 105 | audios: list["AudioInput"], |
| 106 | processor: "MMProcessor", |
| 107 | imglens: Optional[list[int]] = None, |
| 108 | ) -> dict[str, "torch.Tensor"]: |
| 109 | """Process multimodal inputs including audio.""" |
| 110 | |
| 111 | # Load audio files if they are paths |
| 112 | if len(audios) > 0 and (isinstance(audios[0], str) or isinstance(audios[0], list)): |
| 113 | audios = FunAudioChatPlugin.load_audio_batch( |
| 114 | audios, |
| 115 | sampling_rate=getattr(processor, "audio_sampling_rate", 16000), |
| 116 | audio_reader=self.audio_reader |
| 117 | ) |
| 118 | |
| 119 | mm_inputs = {} |
| 120 | |
| 121 | # Process audios |
| 122 | if len(audios) != 0: |
| 123 | feature_extractor = getattr(processor, "feature_extractor", None) |
| 124 | audios = self._regularize_audios( |
| 125 | audios, |
| 126 | sampling_rate=getattr(processor, "audio_sampling_rate", 16000), |
| 127 | )["audios"] |
| 128 | |
| 129 | with torch.inference_mode(): |
| 130 | mm_inputs.update( |
| 131 | feature_extractor( |
| 132 | audios, |
| 133 | sampling_rate=getattr(processor, "audio_sampling_rate", 16000), |
| 134 | return_attention_mask=True, |
nothing calls this directly
no outgoing calls
no test coverage detected