marxus3d commited on
Commit
4aa12d2
·
1 Parent(s): 0157406

Fix list to tensor performance and switch to soundfile to avoid torchcodec issue

Browse files
Files changed (2) hide show
  1. app.py +16 -6
  2. 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 list or tuple instead of a tensor
24
  if isinstance(audio_tensor, tuple):
25
  audio_tensor = audio_tensor[0]
 
 
26
  if isinstance(audio_tensor, list):
27
- audio_tensor = torch.tensor(audio_tensor)
 
 
 
28
 
29
- # torchaudio save expects a 2D tensor (channels, frames)
30
- if audio_tensor.ndim == 1:
31
- audio_tensor = audio_tensor.unsqueeze(0)
 
 
 
32
 
33
- torchaudio.save(output_path, audio_tensor.cpu(), 24000)
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