uasername commited on
Commit
6da24fc
·
verified ·
1 Parent(s): 4744263

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -37,21 +37,26 @@ 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
- 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
 
 
37
  speed: The speed of the speech (default is 1.0).
38
 
39
  Returns:
40
+ An AgentAudio object with the relative URL to 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_segments = []
46
+ for _, _, audio in generator:
47
+ audio_segments.append(audio)
48
+ if not audio_segments:
49
+ raise ValueError("No audio generated.")
50
+ # Concatenate segments into one audio array
51
+ full_audio = np.concatenate(audio_segments)
52
+ sample_rate = 24000 # Kokoro outputs at 24 kHz
53
+ # Ensure the static folder exists and save the file there
54
+ os.makedirs("static", exist_ok=True)
55
+ filename = os.path.join("static", "output.wav")
56
+ sf.write(filename, full_audio, sample_rate)
57
+ # Return an AgentAudio object pointing to the relative URL of the audio file
58
+ from smolagents.agent_types import AgentAudio
59
+ return AgentAudio(f"/static/output.wav")
60
  except Exception as e:
61
  return f"Error generating speech: {str(e)}"
62