Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torchaudio
|
4 |
+
import re
|
5 |
+
import os
|
6 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
7 |
+
from speechbrain.pretrained import EncoderClassifier
|
8 |
+
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
|
11 |
+
# Load processor & vocoder
|
12 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
13 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
14 |
+
|
15 |
+
# Load both TTS models
|
16 |
+
model_male = SpeechT5ForTextToSpeech.from_pretrained("Somalitts/5aad").to(device)
|
17 |
+
model_female = SpeechT5ForTextToSpeech.from_pretrained("Somalitts/8aad").to(device)
|
18 |
+
|
19 |
+
# Load speaker encoder model
|
20 |
+
speaker_model = EncoderClassifier.from_hparams(
|
21 |
+
source="speechbrain/spkrec-xvect-voxceleb",
|
22 |
+
run_opts={"device": device},
|
23 |
+
savedir="./spk_model"
|
24 |
+
)
|
25 |
+
|
26 |
+
# Auto-generate embedding
|
27 |
+
def get_embedding(wav_path, pt_path):
|
28 |
+
if os.path.exists(pt_path):
|
29 |
+
return torch.load(pt_path).to(device)
|
30 |
+
else:
|
31 |
+
audio, sr = torchaudio.load(wav_path)
|
32 |
+
audio = torchaudio.functional.resample(audio, sr, 16000).mean(dim=0).unsqueeze(0).to(device)
|
33 |
+
with torch.no_grad():
|
34 |
+
emb = speaker_model.encode_batch(audio)
|
35 |
+
emb = torch.nn.functional.normalize(emb, dim=2).squeeze()
|
36 |
+
torch.save(emb.cpu(), pt_path)
|
37 |
+
return emb
|
38 |
+
|
39 |
+
# Ensure embeddings are created or loaded
|
40 |
+
embedding_male = get_embedding("Hussein.wav", "male_embedding.pt")
|
41 |
+
embedding_female = get_embedding("caasho.wav", "female_embedding.pt")
|
42 |
+
|
43 |
+
# Somali numbers to words
|
44 |
+
number_words = {
|
45 |
+
0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
|
46 |
+
6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
|
47 |
+
11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
|
48 |
+
14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
|
49 |
+
17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
|
50 |
+
20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
|
51 |
+
60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
|
52 |
+
100: "boqol", 1000: "kun",
|
53 |
+
}
|
54 |
+
|
55 |
+
def number_to_words(number):
|
56 |
+
if number < 20:
|
57 |
+
return number_words[number]
|
58 |
+
elif number < 100:
|
59 |
+
tens, unit = divmod(number, 10)
|
60 |
+
return number_words[tens * 10] + (" " + number_words[unit] if unit else "")
|
61 |
+
elif number < 1000:
|
62 |
+
hundreds, remainder = divmod(number, 100)
|
63 |
+
return (number_words[hundreds] + " boqol" if hundreds > 1 else "BOQOL") + (" " + number_to_words(remainder) if remainder else "")
|
64 |
+
elif number < 1000000:
|
65 |
+
thousands, remainder = divmod(number, 1000)
|
66 |
+
return (number_to_words(thousands) + " kun" if thousands > 1 else "KUN") + (" " + number_to_words(remainder) if remainder else "")
|
67 |
+
elif number < 1000000000:
|
68 |
+
millions, remainder = divmod(number, 1000000)
|
69 |
+
return number_to_words(millions) + " malyan" + (" " + number_to_words(remainder) if remainder else "")
|
70 |
+
elif number < 1000000000000:
|
71 |
+
billions, remainder = divmod(number, 1000000000)
|
72 |
+
return number_to_words(billions) + " milyaar" + (" " + number_to_words(remainder) if remainder else "")
|
73 |
+
else:
|
74 |
+
return str(number)
|
75 |
+
|
76 |
+
def replace_numbers_with_words(text):
|
77 |
+
return re.sub(r'\b\d+\b', lambda match: number_to_words(int(match.group())), text)
|
78 |
+
|
79 |
+
def normalize_text(text):
|
80 |
+
text = text.lower()
|
81 |
+
text = replace_numbers_with_words(text)
|
82 |
+
text = re.sub(r'[^\w\s]', '', text)
|
83 |
+
return text
|
84 |
+
|
85 |
+
# Main TTS function
|
86 |
+
def text_to_speech(text, voice):
|
87 |
+
text = normalize_text(text)
|
88 |
+
inputs = processor(text=text, return_tensors="pt").to(device)
|
89 |
+
|
90 |
+
if voice == "Male":
|
91 |
+
model = model_male
|
92 |
+
embedding = embedding_male
|
93 |
+
else:
|
94 |
+
model = model_female
|
95 |
+
embedding = embedding_female
|
96 |
+
|
97 |
+
with torch.no_grad():
|
98 |
+
speech = model.generate_speech(inputs["input_ids"], embedding.unsqueeze(0), vocoder=vocoder)
|
99 |
+
return (16000, speech.cpu().numpy())
|
100 |
+
|
101 |
+
# Gradio Interface
|
102 |
+
iface = gr.Interface(
|
103 |
+
fn=text_to_speech,
|
104 |
+
inputs=[
|
105 |
+
gr.Textbox(label="Geli qoraalka Af-Soomaaliga", placeholder="Tusaale: Baro aqoonta casriga ah..."),
|
106 |
+
gr.Radio(["Male", "Female"], label="Dooro Codka", value="Female")
|
107 |
+
],
|
108 |
+
outputs=gr.Audio(label="Codka la abuuray", type="numpy"),
|
109 |
+
title="Somali TTS (Lab & Dhedig)",
|
110 |
+
description="Dooro codka aad rabto, geli qoraal af-soomaali ah, codka ayaa la abuuri doonaa adigoo isticmaalaya Somali TTS (SpeechT5)."
|
111 |
+
)
|
112 |
+
|
113 |
+
iface.launch()
|