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