Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,43 +14,38 @@ import soundfile as sf
|
|
14 |
import os
|
15 |
import numpy as np
|
16 |
|
|
|
|
|
17 |
|
18 |
# Initialize the Kokoro pipeline
|
19 |
pipeline = KPipeline(lang_code='a') # 'a' stands for American English
|
20 |
|
|
|
|
|
|
|
21 |
@tool
|
22 |
def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0) -> str:
|
23 |
-
"""Convert text to speech using
|
24 |
-
|
25 |
-
Args:
|
26 |
-
text: The text to be converted to speech.
|
27 |
-
voice: The voice to use for speech synthesis (default is 'af_heart').
|
28 |
-
speed: The speed of the speech (default is 1.0).
|
29 |
-
|
30 |
-
Returns:
|
31 |
-
An AgentAudio object with the relative URL to the generated audio file.
|
32 |
-
"""
|
33 |
try:
|
34 |
-
# Generate speech audio
|
35 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
36 |
audio_segments = []
|
37 |
for _, _, audio in generator:
|
38 |
audio_segments.append(audio)
|
|
|
39 |
if not audio_segments:
|
40 |
raise ValueError("No audio generated.")
|
41 |
-
|
42 |
full_audio = np.concatenate(audio_segments)
|
43 |
sample_rate = 24000 # Kokoro outputs at 24 kHz
|
44 |
-
|
45 |
os.makedirs("tools", exist_ok=True)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
from smolagents.agent_types import AgentAudio
|
50 |
-
return AgentAudio(f"tools/output.wav")
|
51 |
except Exception as e:
|
52 |
return f"Error generating speech: {str(e)}"
|
53 |
|
|
|
54 |
@tool
|
55 |
def search_dad_jokes(term: str) -> str:
|
56 |
"""A tool that searches for dad jokes containing a specific term.
|
@@ -111,4 +106,21 @@ agent = CodeAgent(
|
|
111 |
)
|
112 |
|
113 |
|
114 |
-
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
import os
|
15 |
import numpy as np
|
16 |
|
17 |
+
import gradio as gr
|
18 |
+
|
19 |
|
20 |
# Initialize the Kokoro pipeline
|
21 |
pipeline = KPipeline(lang_code='a') # 'a' stands for American English
|
22 |
|
23 |
+
# Define audio output path
|
24 |
+
AUDIO_OUTPUT_PATH = "tools/output.wav"
|
25 |
+
|
26 |
@tool
|
27 |
def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0) -> str:
|
28 |
+
"""Convert text to speech using Kokoro-82M model and return audio file path."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
try:
|
|
|
30 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
31 |
audio_segments = []
|
32 |
for _, _, audio in generator:
|
33 |
audio_segments.append(audio)
|
34 |
+
|
35 |
if not audio_segments:
|
36 |
raise ValueError("No audio generated.")
|
37 |
+
|
38 |
full_audio = np.concatenate(audio_segments)
|
39 |
sample_rate = 24000 # Kokoro outputs at 24 kHz
|
40 |
+
|
41 |
os.makedirs("tools", exist_ok=True)
|
42 |
+
sf.write(AUDIO_OUTPUT_PATH, full_audio, sample_rate)
|
43 |
+
|
44 |
+
return AUDIO_OUTPUT_PATH
|
|
|
|
|
45 |
except Exception as e:
|
46 |
return f"Error generating speech: {str(e)}"
|
47 |
|
48 |
+
|
49 |
@tool
|
50 |
def search_dad_jokes(term: str) -> str:
|
51 |
"""A tool that searches for dad jokes containing a specific term.
|
|
|
106 |
)
|
107 |
|
108 |
|
109 |
+
#GradioUI(agent).launch()
|
110 |
+
|
111 |
+
# Gradio wrapper function
|
112 |
+
def gradio_text_to_speech(text):
|
113 |
+
audio_path = text_to_speech_kokoro(text)
|
114 |
+
return audio_path if os.path.exists(audio_path) else "Error: No audio file generated."
|
115 |
+
|
116 |
+
# Define the Gradio UI
|
117 |
+
with gr.Blocks() as demo:
|
118 |
+
gr.Markdown("### Text-to-Speech with Kokoro AI 🎙️")
|
119 |
+
with gr.Row():
|
120 |
+
input_box = gr.Textbox(label="Enter text")
|
121 |
+
output_audio = gr.Audio(label="Generated Speech", type="filepath")
|
122 |
+
|
123 |
+
btn = gr.Button("Generate Speech")
|
124 |
+
btn.click(gradio_text_to_speech, inputs=input_box, outputs=output_audio)
|
125 |
+
|
126 |
+
demo.launch()
|