File size: 1,277 Bytes
440e0b5
1a4e740
 
 
440e0b5
1a4e740
 
 
 
 
 
440e0b5
 
1a4e740
 
 
20bee1f
 
 
 
 
 
 
 
 
 
 
 
 
 
1a4e740
 
440e0b5
 
1a4e740
 
440e0b5
 
fd48142
1a4e740
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
import librosa
import numpy as np
import torch
from datasets import load_dataset

# Carga el modelo de clasificaci贸n de tetxo a audio speech
checkpoint = "microsoft/speecht5_tts"
processor = SpeechT5Processor.from_pretrained(checkpoint)
model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint)
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)

device = "cuda" if torch.cuda.is_available() else "cpu"

replacements = [
    ("谩", "a"),
    ("铆", "i"),
    ("帽", "n"),
    ("贸", "o"),
    ("煤", "u"),
    ("眉", "u"),
]

def cleanup_text(text):
    for src, dst in replacements:
        text = text.replace(src, dst)
    return text
    
### TEXT TO AUDIO SPEECH  MODEL 2    
# Define la funci贸n que convierte texto en voz
def synthesize_speech(text):
    text = cleanup_text(text)
    inputs = processor(text=text, return_tensors="pt")

    speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
    
    return speech
### END TEXT TO AUDIO SPEECH  MODEL 2