Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
# Generate speech audio
|
| 44 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|