Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import scipy.io.wavfile
|
5 |
+
from transformers import VitsModel, AutoTokenizer
|
6 |
+
import re
|
7 |
+
|
8 |
+
model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
|
10 |
+
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model.to(device).eval()
|
13 |
+
|
14 |
+
number_words = {
|
15 |
+
0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
|
16 |
+
6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
|
17 |
+
11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
|
18 |
+
14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
|
19 |
+
17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
|
20 |
+
20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
|
21 |
+
60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
|
22 |
+
100: "boqol", 1000: "kun"
|
23 |
+
}
|
24 |
+
|
25 |
+
def number_to_words(number):
|
26 |
+
number = int(number)
|
27 |
+
if number < 20:
|
28 |
+
return number_words[number]
|
29 |
+
elif number < 100:
|
30 |
+
tens, unit = divmod(number, 10)
|
31 |
+
return number_words[tens * 10] + (" iyo " + number_words[unit] if unit else "")
|
32 |
+
elif number < 1000:
|
33 |
+
hundreds, remainder = divmod(number, 100)
|
34 |
+
part = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
|
35 |
+
if remainder:
|
36 |
+
part += " iyo " + number_to_words(remainder)
|
37 |
+
return part
|
38 |
+
elif number < 1000000:
|
39 |
+
thousands, remainder = divmod(number, 1000)
|
40 |
+
words = []
|
41 |
+
if thousands == 1:
|
42 |
+
words.append("kun")
|
43 |
+
else:
|
44 |
+
words.append(number_to_words(thousands) + " kun")
|
45 |
+
if remainder:
|
46 |
+
words.append("iyo " + number_to_words(remainder))
|
47 |
+
return " ".join(words)
|
48 |
+
else:
|
49 |
+
return str(number)
|
50 |
+
|
51 |
+
def normalize_text(text):
|
52 |
+
numbers = re.findall(r'\d+', text)
|
53 |
+
for num in numbers:
|
54 |
+
text = text.replace(num, number_to_words(num))
|
55 |
+
return text
|
56 |
+
|
57 |
+
def tts(text):
|
58 |
+
text = normalize_text(text)
|
59 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
60 |
+
with torch.no_grad():
|
61 |
+
waveform = model(**inputs).waveform.squeeze().cpu().numpy()
|
62 |
+
output_path = "output.wav"
|
63 |
+
scipy.io.wavfile.write(output_path, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
|
64 |
+
return output_path
|
65 |
+
|
66 |
+
gr.Interface(
|
67 |
+
fn=tts,
|
68 |
+
inputs=gr.Textbox(label="Qor qoraalka af-Soomaaliga"),
|
69 |
+
outputs=gr.Audio(type="filepath", label="Codka TTS"),
|
70 |
+
title="Somali TTS API",
|
71 |
+
description="Ku qor qoraal si aad u maqasho codka af-Soomaaliga",
|
72 |
+
).launch()
|