Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -20,13 +20,20 @@ import gradio as gr
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
try:
|
|
|
30 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
31 |
audio_segments = []
|
32 |
for _, _, audio in generator:
|
@@ -35,38 +42,18 @@ def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0
|
|
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 |
-
|
50 |
-
def search_dad_jokes(term: str) -> str:
|
51 |
-
"""A tool that searches for dad jokes containing a specific term.
|
52 |
-
Args:
|
53 |
-
term: The keyword to search for in dad jokes.
|
54 |
-
"""
|
55 |
-
try:
|
56 |
-
headers = {
|
57 |
-
"Accept": "application/json",
|
58 |
-
"User-Agent": "YourAppName (https://yourappurl.com)"
|
59 |
-
}
|
60 |
-
response = requests.get(f"https://icanhazdadjoke.com/search?term={term}", headers=headers)
|
61 |
-
data = response.json()
|
62 |
-
if data['results']:
|
63 |
-
jokes = [joke['joke'] for joke in data['results']]
|
64 |
-
return f"Found {len(jokes)} jokes:\n" + "\n\n".join(jokes)
|
65 |
-
else:
|
66 |
-
return f"No jokes found for the term '{term}'."
|
67 |
except Exception as e:
|
68 |
-
return f"Error
|
69 |
-
|
70 |
|
71 |
|
72 |
|
@@ -106,21 +93,6 @@ agent = CodeAgent(
|
|
106 |
)
|
107 |
|
108 |
|
109 |
-
|
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()
|
|
|
20 |
# Initialize the Kokoro pipeline
|
21 |
pipeline = KPipeline(lang_code='a') # 'a' stands for American English
|
22 |
|
|
|
|
|
|
|
23 |
@tool
|
24 |
def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0) -> str:
|
25 |
+
"""Convert text to speech using the Kokoro-82M model.
|
26 |
+
|
27 |
+
Args:
|
28 |
+
text (str): The text to be converted to speech.
|
29 |
+
voice (str, optional): The voice to use for speech synthesis. Defaults to 'af_heart'.
|
30 |
+
speed (float, optional): The speed of the speech. Defaults to 1.0.
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
str: The path to the generated audio file.
|
34 |
+
"""
|
35 |
try:
|
36 |
+
# Generate speech audio
|
37 |
generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
|
38 |
audio_segments = []
|
39 |
for _, _, audio in generator:
|
|
|
42 |
if not audio_segments:
|
43 |
raise ValueError("No audio generated.")
|
44 |
|
45 |
+
# Concatenate segments into one audio array
|
46 |
full_audio = np.concatenate(audio_segments)
|
47 |
sample_rate = 24000 # Kokoro outputs at 24 kHz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Ensure the tools folder exists and save the file there
|
50 |
+
os.makedirs("tools", exist_ok=True)
|
51 |
+
filename = os.path.join("tools", "output.wav")
|
52 |
+
sf.write(filename, full_audio, sample_rate)
|
53 |
|
54 |
+
return filename # Return the file path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
+
return f"Error generating speech: {str(e)}"
|
|
|
57 |
|
58 |
|
59 |
|
|
|
93 |
)
|
94 |
|
95 |
|
96 |
+
GradioUI(agent).launch()
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
|