Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torchaudio
|
4 |
+
|
5 |
+
# Load MMS-TTS components
|
6 |
+
bundle = torchaudio.pipelines.MMS_TTS_ENG
|
7 |
+
text_processor = bundle.get_text_processor()
|
8 |
+
tacotron2 = bundle.get_tacotron2()
|
9 |
+
waveglow = bundle.get_waveglow()
|
10 |
+
|
11 |
+
# Set up device
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
tacotron2 = tacotron2.to(device)
|
14 |
+
waveglow = waveglow.to(device)
|
15 |
+
|
16 |
+
def synthesize_speech(text):
|
17 |
+
try:
|
18 |
+
if not text.strip():
|
19 |
+
raise ValueError("Text input cannot be empty")
|
20 |
+
|
21 |
+
with torch.inference_mode():
|
22 |
+
# Process text input
|
23 |
+
processed, lengths = text_processor(text)
|
24 |
+
processed = processed.to(device)
|
25 |
+
lengths = lengths.to(device)
|
26 |
+
|
27 |
+
# Generate spectrogram
|
28 |
+
spec, spec_lengths = tacotron2(processed, lengths)
|
29 |
+
|
30 |
+
# Generate waveform
|
31 |
+
waveform, lengths = waveglow(spec, spec_lengths)
|
32 |
+
|
33 |
+
# Convert to numpy array for Gradio
|
34 |
+
waveform = waveform.cpu().squeeze().numpy()
|
35 |
+
return (bundle.sample_rate, waveform)
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error: {str(e)}", None
|
39 |
+
|
40 |
+
# Create Gradio interface
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=synthesize_speech,
|
43 |
+
inputs=gr.Textbox(
|
44 |
+
label="Input Text",
|
45 |
+
placeholder="Enter text to synthesize...",
|
46 |
+
lines=3
|
47 |
+
),
|
48 |
+
outputs=gr.Audio(
|
49 |
+
label="Generated Speech",
|
50 |
+
type="numpy"
|
51 |
+
),
|
52 |
+
title="MMS-TTS English Text-to-Speech",
|
53 |
+
description="Convert text to speech using Facebook's MMS-TTS-ENG model",
|
54 |
+
examples=[
|
55 |
+
["Hello! This is a text-to-speech demonstration."],
|
56 |
+
["The quick brown fox jumps over the lazy dog."],
|
57 |
+
["Natural language processing is fascinating!"]
|
58 |
+
]
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the application
|
62 |
+
if __name__ == "__main__":
|
63 |
+
interface.launch(server_name="0.0.0.0" if torch.cuda.is_available() else None)
|