Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,94 @@
|
|
| 1 |
-
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from transformers import VitsModel, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
-
import
|
| 6 |
-
import io
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
app = FastAPI()
|
| 10 |
-
|
| 11 |
-
# Load model & tokenizer hal mar marka server-ka bilaabmo
|
| 12 |
-
model_name = "facebook/mms-tts-som"
|
| 13 |
-
model = VitsModel.from_pretrained(model_name)
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
model.to(device)
|
| 18 |
model.eval()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# 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()
|
| 94 |
+
|