Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
9 |
def generate_tts(text, speaker):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
output
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
else:
|
25 |
-
return None,
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Gradio
|
28 |
-
with gr.Blocks() as
|
29 |
-
gr.Markdown("
|
30 |
|
31 |
with gr.Row():
|
32 |
-
|
33 |
-
speaker_dropdown = gr.Dropdown(
|
34 |
|
35 |
-
|
36 |
-
generate_button = gr.Button("Generate Speech")
|
37 |
|
38 |
with gr.Row():
|
39 |
-
audio_output = gr.Audio(label="Generated
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
# Launch the app
|
46 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
4 |
+
import soundfile as sf
|
5 |
+
import os
|
6 |
+
import time
|
7 |
|
8 |
+
# Load the Kokoro-TTS model and processor
|
9 |
+
model_name = "hexgrad/Kokoro-TTS"
|
10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_name)
|
11 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
12 |
|
13 |
+
# Define available speakers (update this based on the model's capabilities)
|
14 |
+
speakers = ["Speaker 1", "Speaker 2", "Speaker 3"] # Replace with actual speaker names
|
15 |
+
|
16 |
+
# Function to generate TTS
|
17 |
def generate_tts(text, speaker):
|
18 |
+
try:
|
19 |
+
# Preprocess input text
|
20 |
+
inputs = processor(text, return_tensors="pt", speaker=speaker)
|
21 |
+
|
22 |
+
# Generate speech
|
23 |
+
with torch.no_grad():
|
24 |
+
speech = model.generate(**inputs)
|
25 |
+
|
26 |
+
# Save the output as a temporary file with an auto-generated name
|
27 |
+
timestamp = int(time.time())
|
28 |
+
output_file = f"output_{timestamp}.wav"
|
29 |
+
sf.write(output_file, speech.numpy(), samplerate=22050) # Adjust samplerate if needed
|
30 |
+
|
31 |
+
return output_file
|
32 |
+
except Exception as e:
|
33 |
+
return str(e)
|
34 |
+
|
35 |
+
# Gradio interface
|
36 |
+
def tts_app(text, speaker):
|
37 |
+
output_file = generate_tts(text, speaker)
|
38 |
+
if output_file.endswith(".wav"):
|
39 |
+
return output_file, f"Generated: {output_file}"
|
40 |
else:
|
41 |
+
return None, output_file
|
42 |
+
|
43 |
+
# Auto-naming system for downloads
|
44 |
+
def get_download_name():
|
45 |
+
return f"tts_output_{int(time.time())}.wav"
|
46 |
|
47 |
+
# Create the Gradio app
|
48 |
+
with gr.Blocks() as demo:
|
49 |
+
gr.Markdown("# Kokoro-TTS v1.9: Long Input TTS Generation")
|
50 |
|
51 |
with gr.Row():
|
52 |
+
text_input = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=10)
|
53 |
+
speaker_dropdown = gr.Dropdown(label="Select Speaker", choices=speakers, value=speakers[0])
|
54 |
|
55 |
+
generate_button = gr.Button("Generate TTS")
|
|
|
56 |
|
57 |
with gr.Row():
|
58 |
+
audio_output = gr.Audio(label="Generated Audio")
|
59 |
+
status_output = gr.Textbox(label="Status", placeholder="Generation status will appear here...")
|
60 |
+
|
61 |
+
download_button = gr.Button("Download Audio")
|
62 |
+
download_output = gr.File(label="Download Generated Audio")
|
63 |
+
|
64 |
+
# Link functions to interface
|
65 |
+
generate_button.click(
|
66 |
+
fn=tts_app,
|
67 |
+
inputs=[text_input, speaker_dropdown],
|
68 |
+
outputs=[audio_output, status_output]
|
69 |
+
)
|
70 |
|
71 |
+
download_button.click(
|
72 |
+
fn=get_download_name,
|
73 |
+
outputs=download_output
|
74 |
+
)
|
75 |
|
76 |
# Launch the app
|
77 |
+
demo.launch()
|