Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
3 |
+
from transformers import AutoProcessor, AutoModelForTextToSpectrogram
|
4 |
+
from datasets import load_dataset
|
5 |
+
import torch
|
6 |
+
import soundfile as sf
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Load models and processors
|
10 |
+
processor = AutoProcessor.from_pretrained("ayush2607/speecht5_tts_technical_data")
|
11 |
+
model = AutoModelForTextToSpectrogram.from_pretrained("ayush2607/speecht5_tts_technical_data")
|
12 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
13 |
+
|
14 |
+
# Load xvector containing speaker's voice characteristics from a dataset
|
15 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
16 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
17 |
+
|
18 |
+
def text_to_speech(text):
|
19 |
+
inputs = processor(text=text, return_tensors="pt")
|
20 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
21 |
+
|
22 |
+
output_path = "output.wav"
|
23 |
+
sf.write(output_path, speech.numpy(), samplerate=16000)
|
24 |
+
|
25 |
+
return output_path
|
26 |
+
|
27 |
+
# Create Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=text_to_speech,
|
30 |
+
inputs=gr.Textbox(label="Enter text to convert to speech"),
|
31 |
+
outputs=gr.Audio(label="Generated Speech"),
|
32 |
+
title="Text-to-Speech Converter",
|
33 |
+
description="Convert text to speech using the SpeechT5 model."
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the app
|
37 |
+
iface.launch()
|