Upload 2 files
Browse files- app.py +145 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import soundfile as sf
|
4 |
+
import spaces
|
5 |
+
import os
|
6 |
+
import numpy as np
|
7 |
+
import re
|
8 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
9 |
+
from speechbrain.pretrained import EncoderClassifier
|
10 |
+
from datasets import load_dataset
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
def load_models_and_data():
|
15 |
+
model_name = "microsoft/speecht5_tts"
|
16 |
+
processor = SpeechT5Processor.from_pretrained(model_name)
|
17 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("emirhanbilgic/speecht5_finetuned_emirhan_tr").to(device)
|
18 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
19 |
+
|
20 |
+
spk_model_name = "speechbrain/spkrec-xvect-voxceleb"
|
21 |
+
speaker_model = EncoderClassifier.from_hparams(
|
22 |
+
source=spk_model_name,
|
23 |
+
run_opts={"device": device},
|
24 |
+
savedir=os.path.join("/tmp", spk_model_name),
|
25 |
+
)
|
26 |
+
|
27 |
+
# Load a sample from a dataset for default embedding
|
28 |
+
dataset = load_dataset("erenfazlioglu/turkishvoicedataset", split="train")
|
29 |
+
example = dataset[304]
|
30 |
+
|
31 |
+
return model, processor, vocoder, speaker_model, example
|
32 |
+
|
33 |
+
model, processor, vocoder, speaker_model, default_example = load_models_and_data()
|
34 |
+
|
35 |
+
def create_speaker_embedding(waveform):
|
36 |
+
with torch.no_grad():
|
37 |
+
speaker_embeddings = speaker_model.encode_batch(torch.tensor(waveform).unsqueeze(0).to(device))
|
38 |
+
speaker_embeddings = torch.nn.functional.normalize(speaker_embeddings, dim=2)
|
39 |
+
speaker_embeddings = speaker_embeddings.squeeze()
|
40 |
+
return speaker_embeddings
|
41 |
+
|
42 |
+
def prepare_default_embedding(example):
|
43 |
+
audio = example["audio"]
|
44 |
+
return create_speaker_embedding(audio["array"])
|
45 |
+
|
46 |
+
default_embedding = prepare_default_embedding(default_example)
|
47 |
+
|
48 |
+
replacements = [
|
49 |
+
("â", "a"), # Long a
|
50 |
+
("ç", "ch"), # Ch as in "chair"
|
51 |
+
("ğ", "gh"), # Silent g or slight elongation of the preceding vowel
|
52 |
+
("ı", "i"), # Dotless i
|
53 |
+
("î", "i"), # Long i
|
54 |
+
("ö", "oe"), # Similar to German ö
|
55 |
+
("ş", "sh"), # Sh as in "shoe"
|
56 |
+
("ü", "ue"), # Similar to German ü
|
57 |
+
("û", "u"), # Long u
|
58 |
+
]
|
59 |
+
|
60 |
+
number_words = {
|
61 |
+
0: "sıfır", 1: "bir", 2: "iki", 3: "üç", 4: "dört", 5: "beş", 6: "altı", 7: "yedi", 8: "sekiz", 9: "dokuz",
|
62 |
+
10: "on", 11: "on bir", 12: "on iki", 13: "on üç", 14: "on dört", 15: "on beş", 16: "on altı", 17: "on yedi",
|
63 |
+
18: "on sekiz", 19: "on dokuz", 20: "yirmi", 30: "otuz", 40: "kırk", 50: "elli", 60: "altmış", 70: "yetmiş",
|
64 |
+
80: "seksen", 90: "doksan", 100: "yüz", 1000: "bin"
|
65 |
+
}
|
66 |
+
|
67 |
+
def number_to_words(number):
|
68 |
+
if number < 20:
|
69 |
+
return number_words[number]
|
70 |
+
elif number < 100:
|
71 |
+
tens, unit = divmod(number, 10)
|
72 |
+
return number_words[tens * 10] + (" " + number_words[unit] if unit else "")
|
73 |
+
elif number < 1000:
|
74 |
+
hundreds, remainder = divmod(number, 100)
|
75 |
+
return (number_words[hundreds] + " yüz" if hundreds > 1 else "yüz") + (" " + number_to_words(remainder) if remainder else "")
|
76 |
+
elif number < 1000000:
|
77 |
+
thousands, remainder = divmod(number, 1000)
|
78 |
+
return (number_to_words(thousands) + " bin" if thousands > 1 else "bin") + (" " + number_to_words(remainder) if remainder else "")
|
79 |
+
elif number < 1000000000:
|
80 |
+
millions, remainder = divmod(number, 1000000)
|
81 |
+
return number_to_words(millions) + " milyon" + (" " + number_to_words(remainder) if remainder else "")
|
82 |
+
elif number < 1000000000000:
|
83 |
+
billions, remainder = divmod(number, 1000000000)
|
84 |
+
return number_to_words(billions) + " milyar" + (" " + number_to_words(remainder) if remainder else "")
|
85 |
+
else:
|
86 |
+
return str(number)
|
87 |
+
|
88 |
+
def replace_numbers_with_words(text):
|
89 |
+
def replace(match):
|
90 |
+
number = int(match.group())
|
91 |
+
return number_to_words(number)
|
92 |
+
|
93 |
+
# Find the numbers and change with words.
|
94 |
+
result = re.sub(r'\b\d+\b', replace, text)
|
95 |
+
|
96 |
+
return result
|
97 |
+
|
98 |
+
def normalize_text(text):
|
99 |
+
# Convert to lowercase
|
100 |
+
text = text.lower()
|
101 |
+
|
102 |
+
# Replace numbers with words
|
103 |
+
text = replace_numbers_with_words(text)
|
104 |
+
|
105 |
+
# Apply character replacements
|
106 |
+
for old, new in replacements:
|
107 |
+
text = text.replace(old, new)
|
108 |
+
|
109 |
+
# Remove punctuation
|
110 |
+
text = re.sub(r'[^\w\s]', '', text)
|
111 |
+
|
112 |
+
return text
|
113 |
+
|
114 |
+
@spaces.GPU(duration=60)
|
115 |
+
def text_to_speech(text, audio_file=None):
|
116 |
+
# Normalize the input text
|
117 |
+
normalized_text = normalize_text(text)
|
118 |
+
|
119 |
+
# Prepare the input for the model
|
120 |
+
inputs = processor(text=normalized_text, return_tensors="pt").to(device)
|
121 |
+
|
122 |
+
# Use the default speaker embedding
|
123 |
+
speaker_embeddings = default_embedding
|
124 |
+
|
125 |
+
# Generate speech
|
126 |
+
with torch.no_grad():
|
127 |
+
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings.unsqueeze(0), vocoder=vocoder)
|
128 |
+
|
129 |
+
speech_np = speech.cpu().numpy()
|
130 |
+
|
131 |
+
return (16000, speech_np)
|
132 |
+
|
133 |
+
iface = gr.Interface(
|
134 |
+
fn=text_to_speech,
|
135 |
+
inputs=[
|
136 |
+
gr.Textbox(label="Enter Turkish text to convert to speech")
|
137 |
+
],
|
138 |
+
outputs=[
|
139 |
+
gr.Audio(label="Generated Speech", type="numpy")
|
140 |
+
],
|
141 |
+
title="Turkish SpeechT5 Text-to-Speech Demo",
|
142 |
+
description="Enter Turkish text, and listen to the generated speech."
|
143 |
+
)
|
144 |
+
|
145 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.23.5
|
2 |
+
transformers
|
3 |
+
datasets
|
4 |
+
soundfile
|
5 |
+
torch
|
6 |
+
torchaudio
|
7 |
+
sentencepiece
|
8 |
+
speechbrain==0.5.16
|
9 |
+
librosa
|