Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import VitsModel, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
print(f"Using device: {device}")
|
| 9 |
+
|
| 10 |
+
@spaces.GPU
|
| 11 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-eng").to(device)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
| 13 |
+
|
| 14 |
+
@spaces.GPU
|
| 15 |
+
def process_text(text):
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 17 |
+
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
output = model(**inputs).waveform
|
| 20 |
+
|
| 21 |
+
audio_numpy = output.cpu().numpy().squeeze()
|
| 22 |
+
sample_rate = model.config.sampling_rate
|
| 23 |
+
return (sample_rate, audio_numpy)
|
| 24 |
+
|
| 25 |
+
examples = [
|
| 26 |
+
["Hello, welcome to text-to-speech system!"],
|
| 27 |
+
["How amazing is artificial intelligence technology?"],
|
| 28 |
+
["The weather is beautiful today, isn't it?"],
|
| 29 |
+
["Learning new things makes life exciting."],
|
| 30 |
+
["This audio was generated by artificial intelligence."]
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("## 🎤 MMS-TTS English Text-to-Speech System")
|
| 35 |
+
gr.Markdown("Enter text below and convert it to natural sounding speech!")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Enter text here...")
|
| 40 |
+
gr.Examples(examples=examples, inputs=input_text, label="Example Texts")
|
| 41 |
+
submit_btn = gr.Button("Generate Speech")
|
| 42 |
+
|
| 43 |
+
with gr.Column():
|
| 44 |
+
audio_output = gr.Audio(label="Generated Speech", type="numpy")
|
| 45 |
+
|
| 46 |
+
submit_btn.click(fn=process_text, inputs=input_text, outputs=audio_output)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|