uasername commited on
Commit
d2943b1
·
verified ·
1 Parent(s): 8a10c10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -10,6 +10,7 @@ from Gradio_UI import GradioUI
10
  from kokoro import KPipeline
11
  import soundfile as sf
12
  import os
 
13
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
  @tool
@@ -36,20 +37,24 @@ def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0
36
  speed: The speed of the speech (default is 1.0).
37
 
38
  Returns:
39
- The filename of the generated audio file.
40
  """
41
  try:
42
  # Generate speech audio
43
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
44
- audio_files = []
45
 
46
- # Save each audio segment to a file
47
- for i, (gs, ps, audio) in enumerate(generator):
48
- filename = f'output_{i}.wav'
49
- sf.write(filename, audio, 24000)
50
- audio_files.append(filename)
51
 
52
- return f"Generated {len(audio_files)} audio file(s): {', '.join(audio_files)}"
 
 
 
 
 
 
53
  except Exception as e:
54
  return f"Error generating speech: {str(e)}"
55
 
 
10
  from kokoro import KPipeline
11
  import soundfile as sf
12
  import os
13
+ import numpy as np
14
 
15
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
16
  @tool
 
37
  speed: The speed of the speech (default is 1.0).
38
 
39
  Returns:
40
+ A tuple containing the sample rate and the generated audio data as a NumPy array.
41
  """
42
  try:
43
  # Generate speech audio
44
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
45
+ audio_segments = []
46
 
47
+ # Collect each audio segment
48
+ for _, _, audio in generator:
49
+ audio_segments.append(audio)
 
 
50
 
51
+ # Concatenate all audio segments into a single array
52
+ if audio_segments:
53
+ full_audio = np.concatenate(audio_segments)
54
+ sample_rate = 24000 # Kokoro-82M outputs audio at 24 kHz
55
+ return sample_rate, full_audio
56
+ else:
57
+ return "No audio generated."
58
  except Exception as e:
59
  return f"Error generating speech: {str(e)}"
60