(
tokenizer,
spt,
prompt,
text,
device,
silence_duration,
audio_data=None,
max_channels=8,
pad_token=1024,
)
| 270 | |
| 271 | |
| 272 | def process_inputs( |
| 273 | tokenizer, |
| 274 | spt, |
| 275 | prompt, |
| 276 | text, |
| 277 | device, |
| 278 | silence_duration, |
| 279 | audio_data=None, |
| 280 | max_channels=8, |
| 281 | pad_token=1024, |
| 282 | ): |
| 283 | seq = f"<|begin_of_style|>{prompt}<|end_of_style|>\n<|begin_of_text|>{text}<|end_of_text|>\n<|begin_of_speech|>" |
| 284 | inputs1 = np.array(tokenizer.encode(seq)) |
| 285 | input_ids = np.full((inputs1.shape[0], max_channels), pad_token) |
| 286 | input_ids[:, 0] = inputs1 |
| 287 | |
| 288 | if audio_data is not None: |
| 289 | try: |
| 290 | # audio_data should now be a processed audio tensor |
| 291 | wav = audio_data |
| 292 | |
| 293 | # Add fixed 5-second silence at the end of audio (using 16k sample rate) |
| 294 | silence_samples = int(silence_duration * 16000) |
| 295 | silence = torch.zeros(wav.shape[0], silence_samples) |
| 296 | wav = torch.cat([wav, silence], dim=1) |
| 297 | |
| 298 | with torch.no_grad(): |
| 299 | # Use SPT encoding |
| 300 | encode_result = spt.encode([wav.squeeze().to(device)]) |
| 301 | audio_token = ( |
| 302 | encode_result["codes_list"][0].permute(1, 0).cpu().numpy() |
| 303 | ) # Adjust dimension order |
| 304 | |
| 305 | # similar to DAC encoding adjustment |
| 306 | audio_token[:, 0] = ( |
| 307 | audio_token[:, 0] + 151665 |
| 308 | ) # Keep this line if offset is needed, otherwise delete |
| 309 | input_ids = np.concatenate([input_ids, audio_token]) |
| 310 | except Exception as e: |
| 311 | print(f"Error processing audio data: {e}") |
| 312 | raise |
| 313 | |
| 314 | return input_ids |
| 315 | |
| 316 | |
| 317 | def shifting_inputs(input_ids, tokenizer, pad_token=1024, max_channels=8): |
no test coverage detected