Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,73 +2,92 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
import scipy.io.wavfile
|
5 |
-
import re
|
6 |
from transformers import VitsModel, AutoTokenizer
|
|
|
7 |
|
8 |
-
#
|
9 |
model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
|
10 |
-
# 2. Load tokenizer for Somali TTS
|
11 |
tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
|
12 |
-
|
13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
model.to(device)
|
15 |
model.eval()
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
24 |
-
def number_to_words(
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
text = text.replace("KH", "qa").replace("
|
|
|
|
|
40 |
return text
|
41 |
|
42 |
-
|
43 |
-
# speaker: "Male" or "Female"
|
44 |
-
def tts(text: str, speaker: str) -> str:
|
45 |
text = normalize_text(text)
|
46 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
47 |
-
spk_id = 0 if speaker == "Male" else 1
|
48 |
-
speaker_ids = torch.tensor([spk_id], device=device)
|
49 |
-
|
50 |
with torch.no_grad():
|
51 |
-
|
52 |
-
wav = out.waveform.squeeze().cpu().numpy()
|
53 |
-
|
54 |
-
# write to WAV
|
55 |
filename = "output.wav"
|
56 |
-
scipy.io.wavfile.write(filename, model.config.sampling_rate,
|
57 |
-
(wav * 32767).astype(np.int16))
|
58 |
return filename
|
59 |
|
60 |
-
|
61 |
-
demo = gr.Interface(
|
62 |
fn=tts,
|
63 |
-
inputs=
|
64 |
-
gr.Textbox(lines=3, label="Geli qoraal Soomaali ah", placeholder="Qor qoraalka halkan…"),
|
65 |
-
gr.Radio(["Male", "Female"], label="Dooro codka")
|
66 |
-
],
|
67 |
outputs=gr.Audio(label="Codka TTS"),
|
68 |
-
title="Somali TTS
|
69 |
-
description="Ku qor qoraal
|
70 |
-
|
71 |
-
)
|
72 |
-
|
73 |
-
if __name__ == "__main__":
|
74 |
-
demo.launch()
|
|
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
import scipy.io.wavfile
|
|
|
5 |
from transformers import VitsModel, AutoTokenizer
|
6 |
+
import re
|
7 |
|
8 |
+
# Load fine-tuned model from Hugging Face Hub or local path
|
9 |
model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
|
|
|
11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
model.to(device)
|
13 |
model.eval()
|
14 |
|
15 |
+
number_words = {
|
16 |
+
0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
|
17 |
+
6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
|
18 |
+
11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
|
19 |
+
14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
|
20 |
+
17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
|
21 |
+
20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
|
22 |
+
60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
|
23 |
+
100: "boqol", 1000: "kun"
|
24 |
}
|
25 |
|
26 |
+
def number_to_words(number):
|
27 |
+
number = int(number)
|
28 |
+
if number < 20:
|
29 |
+
return number_words[number]
|
30 |
+
elif number < 100:
|
31 |
+
tens, unit = divmod(number, 10)
|
32 |
+
return number_words[tens * 10] + (" iyo " + number_words[unit] if unit else "")
|
33 |
+
elif number < 1000:
|
34 |
+
hundreds, remainder = divmod(number, 100)
|
35 |
+
part = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
|
36 |
+
if remainder:
|
37 |
+
part += " iyo " + number_to_words(remainder)
|
38 |
+
return part
|
39 |
+
elif number < 1000000:
|
40 |
+
thousands, remainder = divmod(number, 1000)
|
41 |
+
words = []
|
42 |
+
if thousands == 1:
|
43 |
+
words.append("kun")
|
44 |
+
else:
|
45 |
+
words.append(number_to_words(thousands) + " kun")
|
46 |
+
if remainder >= 100:
|
47 |
+
hundreds, rem2 = divmod(remainder, 100)
|
48 |
+
if hundreds:
|
49 |
+
boqol_text = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
|
50 |
+
words.append(boqol_text)
|
51 |
+
if rem2:
|
52 |
+
words.append("iyo " + number_to_words(rem2))
|
53 |
+
elif remainder:
|
54 |
+
words.append("iyo " + number_to_words(remainder))
|
55 |
+
return " ".join(words)
|
56 |
+
elif number < 1000000000:
|
57 |
+
millions, remainder = divmod(number, 1000000)
|
58 |
+
words = []
|
59 |
+
if millions == 1:
|
60 |
+
words.append("milyan")
|
61 |
+
else:
|
62 |
+
words.append(number_to_words(millions) + " milyan")
|
63 |
+
if remainder:
|
64 |
+
words.append(number_to_words(remainder))
|
65 |
+
return " ".join(words)
|
66 |
+
else:
|
67 |
+
return str(number)
|
68 |
|
69 |
+
def normalize_text(text):
|
70 |
+
numbers = re.findall(r'\d+', text)
|
71 |
+
for num in numbers:
|
72 |
+
text = text.replace(num, number_to_words(num))
|
73 |
+
text = text.replace("KH", "qa").replace("Z", "S")
|
74 |
+
text = text.replace("SH", "SHa'a").replace("DH", "Dha'a")
|
75 |
+
text = text.replace("ZamZam", "SamSam")
|
76 |
return text
|
77 |
|
78 |
+
def tts(text):
|
|
|
|
|
79 |
text = normalize_text(text)
|
80 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
|
|
|
|
|
|
81 |
with torch.no_grad():
|
82 |
+
waveform = model(**inputs).waveform.squeeze().cpu().numpy()
|
|
|
|
|
|
|
83 |
filename = "output.wav"
|
84 |
+
scipy.io.wavfile.write(filename, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
|
|
|
85 |
return filename
|
86 |
|
87 |
+
gr.Interface(
|
|
|
88 |
fn=tts,
|
89 |
+
inputs=gr.Textbox(label="Geli qoraal Soomaali ah"),
|
|
|
|
|
|
|
90 |
outputs=gr.Audio(label="Codka TTS"),
|
91 |
+
title="Somali TTS",
|
92 |
+
description="Ku qor qoraal Soomaaliyeed si aad u maqasho cod dabiici ah.",
|
93 |
+
).launch()
|
|
|
|
|
|
|
|