Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
-
|
|
|
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
model = model.to(device)
|
| 13 |
|
| 14 |
def text_to_speech(text):
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
with torch.no_grad():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 25 |
-
write(temp_file.name, sample_rate,
|
| 26 |
-
|
| 27 |
return temp_file.name
|
| 28 |
|
| 29 |
-
# Gradio Interface
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=text_to_speech,
|
| 32 |
inputs=gr.Textbox(label="Enter Text"),
|
| 33 |
outputs=gr.Audio(label="Generated Speech"),
|
| 34 |
-
title="
|
| 35 |
-
description="
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import VitsModel, AutoTokenizer
|
| 4 |
+
import numpy as np
|
| 5 |
+
import scipy.io.wavfile
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
# Load lightweight MMS TTS model
|
| 9 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-eng")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
| 11 |
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
model = model.to(device)
|
| 14 |
|
| 15 |
def text_to_speech(text):
|
| 16 |
+
if text.strip() == "":
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 20 |
+
|
| 21 |
with torch.no_grad():
|
| 22 |
+
output = model(**inputs).waveform
|
| 23 |
+
|
| 24 |
+
audio = output.squeeze().cpu().numpy()
|
| 25 |
|
| 26 |
+
# Normalize audio
|
| 27 |
+
audio = audio / np.max(np.abs(audio))
|
| 28 |
+
|
| 29 |
+
# Save to temporary WAV file
|
| 30 |
+
sample_rate = model.config.sampling_rate
|
| 31 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 32 |
+
scipy.io.wavfile.write(temp_file.name, sample_rate, audio.astype(np.float32))
|
| 33 |
+
|
| 34 |
return temp_file.name
|
| 35 |
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=text_to_speech,
|
| 38 |
inputs=gr.Textbox(label="Enter Text"),
|
| 39 |
outputs=gr.Audio(label="Generated Speech"),
|
| 40 |
+
title="Fast Text-to-Speech (Lightweight)",
|
| 41 |
+
description="Fast and accurate TTS using facebook/mms-tts-eng"
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|