RaF33 commited on
Commit
c9718e5
1 Parent(s): 1a3caf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -7,9 +7,11 @@ import tempfile
7
 
8
  def text_to_speech(text, language):
9
  if not text:
10
- return np.array([]), 22050 # Return an empty array and a standard sample rate if no text is entered
 
11
  if not language:
12
- return np.array([]), 22050 # Similarly if no language is selected
 
13
 
14
  try:
15
  tts = gTTS(text=text, lang=language)
@@ -17,12 +19,15 @@ def text_to_speech(text, language):
17
  tts.save(fp.name)
18
  sound = AudioSegment.from_file(fp.name, format="mp3")
19
  samples = np.array(sound.get_array_of_samples())
 
 
20
  if sound.channels == 2:
21
  samples = samples.reshape((-1, 2))
22
- sample_rate = sound.frame_rate
23
- return samples.astype(np.float32), sample_rate # Ensure the samples are in the correct float32 format
24
  except Exception as e:
25
- return np.array([]).astype(np.float32), 22050 # Return an empty array in float32 format and sample rate in case of error
 
26
 
27
  interface = gr.Interface(
28
  fn=text_to_speech,
 
7
 
8
  def text_to_speech(text, language):
9
  if not text:
10
+ print("No text provided")
11
+ return np.array([]), 22050
12
  if not language:
13
+ print("No language selected")
14
+ return np.array([]), 22050
15
 
16
  try:
17
  tts = gTTS(text=text, lang=language)
 
19
  tts.save(fp.name)
20
  sound = AudioSegment.from_file(fp.name, format="mp3")
21
  samples = np.array(sound.get_array_of_samples())
22
+ if samples.size == 0:
23
+ print("No audio data generated")
24
  if sound.channels == 2:
25
  samples = samples.reshape((-1, 2))
26
+ print(f"Samples: {samples[:10]}") # Print the first 10 samples to diagnose
27
+ return samples, sound.frame_rate
28
  except Exception as e:
29
+ print(f"Error: {str(e)}")
30
+ return np.array([]), 22050
31
 
32
  interface = gr.Interface(
33
  fn=text_to_speech,