uasername commited on
Commit
3251fe5
·
verified ·
1 Parent(s): 8702f0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -37,28 +37,24 @@ def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0
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
 
61
-
62
  @tool
63
  def search_dad_jokes(term: str) -> str:
64
  """A tool that searches for dad jokes containing a specific term.
 
37
  speed: The speed of the speech (default is 1.0).
38
 
39
  Returns:
40
+ The filename of the generated audio file.
41
  """
42
  try:
43
  # Generate speech audio
44
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
45
+ audio_files = []
46
 
47
+ # Save each audio segment to a file
48
+ for i, (gs, ps, audio) in enumerate(generator):
49
+ os.makedirs("static", exist_ok=True)
50
+ filename = os.path.join("static", f'output_{i}.wav')
51
+ sf.write(filename, audio, 24000)
52
+ audio_files.append(filename)
53
 
54
+ return f"Generated {len(audio_files)} audio file(s): {', '.join(audio_files)}"
 
 
 
 
 
 
55
  except Exception as e:
56
  return f"Error generating speech: {str(e)}"
57
 
 
58
  @tool
59
  def search_dad_jokes(term: str) -> str:
60
  """A tool that searches for dad jokes containing a specific term.