Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# 1. Choose a TTS model from Hugging Face
|
| 6 |
+
# You can later change this to another model, e.g. "suno/bark-small" if supported
|
| 7 |
+
TTS_MODEL_ID = "facebook/mms-tts-eng" # English TTS
|
| 8 |
+
|
| 9 |
+
# 2. Create the TTS pipeline
|
| 10 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
+
tts = pipeline("text-to-speech", model=TTS_MODEL_ID, device=device)
|
| 12 |
+
|
| 13 |
+
def synthesize_tts(text):
|
| 14 |
+
if not text or text.strip() == "":
|
| 15 |
+
raise gr.Error("Please enter some text to synthesize.")
|
| 16 |
+
|
| 17 |
+
# 3. Run the pipeline
|
| 18 |
+
out = tts(text)
|
| 19 |
+
# out["audio"] is a numpy array; out["sampling_rate"] is the sample rate
|
| 20 |
+
audio = (out["sampling_rate"], out["audio"])
|
| 21 |
+
return audio
|
| 22 |
+
|
| 23 |
+
title = "Simple Text-to-Speech (TTS) Space"
|
| 24 |
+
description = (
|
| 25 |
+
"Enter some English text and generate speech using a Hugging Face TTS model. "
|
| 26 |
+
"You can later replace the model with F5-TTS for voice cloning."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown(f"# {title}")
|
| 31 |
+
gr.Markdown(description)
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
text_in = gr.Textbox(
|
| 36 |
+
lines=4,
|
| 37 |
+
label="Text to synthesize",
|
| 38 |
+
placeholder="Type some English text here..."
|
| 39 |
+
)
|
| 40 |
+
btn = gr.Button("Generate Speech")
|
| 41 |
+
with gr.Column():
|
| 42 |
+
audio_out = gr.Audio(label="Generated audio", type="numpy")
|
| 43 |
+
|
| 44 |
+
btn.click(fn=synthesize_tts, inputs=text_in, outputs=audio_out)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|