Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
|
| 9 |
+
# Carga de modelos y recursos
|
| 10 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 11 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 12 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 13 |
+
|
| 14 |
+
# Carga de embeddings de ejemplo (puedes permitir que el usuario elija un speaker tambi茅n si quieres)
|
| 15 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 16 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 17 |
+
|
| 18 |
+
# Funci贸n principal para generar voz
|
| 19 |
+
def tts(text):
|
| 20 |
+
inputs = processor(text=text, return_tensors="pt")
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
| 23 |
+
# Guardar a archivo temporal
|
| 24 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 25 |
+
sf.write(f.name, speech.numpy(), samplerate=16000)
|
| 26 |
+
return f.name
|
| 27 |
+
|
| 28 |
+
# Interfaz con Gradio
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=tts,
|
| 31 |
+
inputs=gr.Textbox(lines=2, placeholder="Introduce un texto..."),
|
| 32 |
+
outputs=gr.Audio(type="filepath"),
|
| 33 |
+
title="SpeechT5 TTS - Hugging Face Space",
|
| 34 |
+
description="Convierte texto a voz con el modelo SpeechT5 de Microsoft"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
interface.launch()
|