Fix list to tensor performance and switch to soundfile to avoid torchcodec issue
Browse files- app.py +16 -6
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -18,19 +18,29 @@ def generate_amharic_speech(text: str, ref_audio_path: str = "ref_amharic.wav"):
|
|
| 18 |
language="am"
|
| 19 |
)
|
| 20 |
|
|
|
|
|
|
|
| 21 |
output_path = "output.wav"
|
| 22 |
|
| 23 |
-
# Handle if generate() returns a
|
| 24 |
if isinstance(audio_tensor, tuple):
|
| 25 |
audio_tensor = audio_tensor[0]
|
|
|
|
|
|
|
| 26 |
if isinstance(audio_tensor, list):
|
| 27 |
-
audio_tensor =
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
return output_path
|
| 35 |
|
| 36 |
# Expose Gradio Interface
|
|
|
|
| 18 |
language="am"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
import numpy as np
|
| 22 |
+
import soundfile as sf
|
| 23 |
output_path = "output.wav"
|
| 24 |
|
| 25 |
+
# Handle if generate() returns a tuple
|
| 26 |
if isinstance(audio_tensor, tuple):
|
| 27 |
audio_tensor = audio_tensor[0]
|
| 28 |
+
|
| 29 |
+
# Convert to numpy array safely and handle lists of ndarrays
|
| 30 |
if isinstance(audio_tensor, list):
|
| 31 |
+
audio_tensor = np.array(audio_tensor)
|
| 32 |
+
|
| 33 |
+
if isinstance(audio_tensor, torch.Tensor):
|
| 34 |
+
audio_tensor = audio_tensor.cpu().numpy()
|
| 35 |
|
| 36 |
+
# soundfile expects shape (frames, channels) or (frames,)
|
| 37 |
+
# If the output is (channels, frames) like (1, N), we transpose it or squeeze it
|
| 38 |
+
if audio_tensor.ndim == 2 and audio_tensor.shape[0] < audio_tensor.shape[1]:
|
| 39 |
+
audio_tensor = audio_tensor.T
|
| 40 |
+
elif audio_tensor.ndim == 2 and audio_tensor.shape[0] == 1:
|
| 41 |
+
audio_tensor = audio_tensor.squeeze()
|
| 42 |
|
| 43 |
+
sf.write(output_path, audio_tensor, 24000)
|
| 44 |
return output_path
|
| 45 |
|
| 46 |
# Expose Gradio Interface
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|