Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,49 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import soundfile as sf
|
| 3 |
-
import uuid
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
buf_len += len(word) + 1
|
| 40 |
-
if buf_len > max_len:
|
| 41 |
-
refined.append(' '.join(temp))
|
| 42 |
-
temp = []
|
| 43 |
-
buf_len = 0
|
| 44 |
-
if temp:
|
| 45 |
-
refined.append(' '.join(temp))
|
| 46 |
-
return refined
|
| 47 |
-
|
| 48 |
-
# Core TTS function
|
| 49 |
-
def synthesize(language, text, gender, emotion, speed):
|
| 50 |
-
description = (
|
| 51 |
-
f"A native {language.lower()} female speaker with an expressive tone."
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
audio_chunks = []
|
| 55 |
-
text_chunks = split_text(text)
|
| 56 |
-
|
| 57 |
-
for chunk in text_chunks:
|
| 58 |
-
# New tokenization for each chunk
|
| 59 |
-
desc_input = desc_tokenizer(description, return_tensors="pt").to(device)
|
| 60 |
-
prompt_input = tokenizer(chunk, return_tensors="pt").to(device)
|
| 61 |
-
|
| 62 |
-
with torch.no_grad():
|
| 63 |
-
output = quantized_model.generate(
|
| 64 |
-
input_ids=desc_input.input_ids,
|
| 65 |
-
attention_mask=desc_input.attention_mask,
|
| 66 |
-
prompt_input_ids=prompt_input.input_ids,
|
| 67 |
-
prompt_attention_mask=torch.ones_like(prompt_input.input_ids).to(device)
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
audio = output.cpu().numpy().squeeze()
|
| 71 |
-
audio_chunks.append(audio)
|
| 72 |
-
|
| 73 |
-
full_audio = np.concatenate(audio_chunks)
|
| 74 |
-
filename = f"{uuid.uuid4().hex}.wav"
|
| 75 |
-
sf.write(filename, full_audio, quantized_model.config.sampling_rate)
|
| 76 |
-
return filename
|
| 77 |
|
| 78 |
-
# Gradio UI
|
| 79 |
iface = gr.Interface(
|
| 80 |
fn=synthesize,
|
| 81 |
inputs=[
|
| 82 |
-
gr.Dropdown(
|
| 83 |
-
gr.Textbox(label="Text
|
| 84 |
-
# gr.Radio(["Male", "Female"], label="Speaker Gender"),
|
| 85 |
-
# gr.Dropdown(["Neutral", "Happy", "Sad", "Angry"], label="Emotion"),
|
| 86 |
-
# gr.Dropdown(["Slow", "Moderate", "Fast"], label="Speaking Rate"),
|
| 87 |
-
#gr.Dropdown(["Low", "Normal", "High"], label="Pitch"),
|
| 88 |
-
#gr.Dropdown(["Basic", "Refined"], label="Voice Quality"),
|
| 89 |
],
|
| 90 |
-
outputs=gr.Audio(
|
| 91 |
-
title="Multilingual
|
| 92 |
-
description="
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 3 |
+
import torch
|
| 4 |
+
import torchaudio
|
| 5 |
+
|
| 6 |
+
LANG_MODEL_MAP = {
|
| 7 |
+
"English": "facebook/mms-tts-eng",
|
| 8 |
+
"Hindi": "facebook/mms-tts-hin",
|
| 9 |
+
"Tamil": "facebook/mms-tts-tam",
|
| 10 |
+
"Malayalam": "facebook/mms-tts-mal",
|
| 11 |
+
"Kannada": "facebook/mms-tts-kan"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
cache = {}
|
| 16 |
+
|
| 17 |
+
def load_model_and_processor(language):
|
| 18 |
+
model_name = LANG_MODEL_MAP[language]
|
| 19 |
+
if model_name not in cache:
|
| 20 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 21 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_name).to(device)
|
| 22 |
+
cache[model_name] = (processor, model)
|
| 23 |
+
return cache[model_name]
|
| 24 |
+
|
| 25 |
+
def synthesize(language, text):
|
| 26 |
+
processor, model = load_model_and_processor(language)
|
| 27 |
+
|
| 28 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
generated_ids = model.generate(**inputs)
|
| 31 |
+
audio = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 32 |
+
|
| 33 |
+
# Decode and return waveform
|
| 34 |
+
waveform, sr = torchaudio.load(audio)
|
| 35 |
+
return sr, waveform.squeeze().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=synthesize,
|
| 39 |
inputs=[
|
| 40 |
+
gr.Dropdown(choices=list(LANG_MODEL_MAP.keys()), label="Select Language"),
|
| 41 |
+
gr.Textbox(label="Enter Text", placeholder="Type something...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
],
|
| 43 |
+
outputs=gr.Audio(label="Synthesized Speech", type="numpy"),
|
| 44 |
+
title="Multilingual TTS - MMS Facebook",
|
| 45 |
+
description="A Gradio demo for multilingual TTS using Meta's MMS models. Supports English, Hindi, Tamil, Malayalam, and Kannada."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
iface.launch()
|