Spaces:
Runtime error
Runtime error
Tipene Tutaki commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
|
| 6 |
+
# Load model and processor
|
| 7 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 8 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 9 |
+
|
| 10 |
+
# Load a speaker embedding
|
| 11 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 12 |
+
speaker_embedding = torch.tensor(embeddings_dataset[0]["xvector"]).unsqueeze(0)
|
| 13 |
+
|
| 14 |
+
# TTS Function
|
| 15 |
+
def text_to_speech(text):
|
| 16 |
+
inputs = processor(text=text, return_tensors="pt")
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embedding)
|
| 19 |
+
return speech.numpy()
|
| 20 |
+
|
| 21 |
+
# Set up Gradio interface
|
| 22 |
+
iface = gr.Interface(fn=text_to_speech, inputs="text", outputs="audio")
|
| 23 |
+
iface.launch()
|