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