Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,11 @@ import scipy.io.wavfile
|
|
| 5 |
from transformers import VitsModel, AutoTokenizer
|
| 6 |
import re
|
| 7 |
|
| 8 |
-
# Load fine-tuned model
|
| 9 |
-
model = VitsModel.from_pretrained("
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
|
|
|
|
|
|
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
number_words = {
|
|
@@ -37,12 +39,10 @@ def number_to_words(number):
|
|
| 37 |
elif number < 1000000:
|
| 38 |
thousands, remainder = divmod(number, 1000)
|
| 39 |
words = []
|
| 40 |
-
|
| 41 |
if thousands == 1:
|
| 42 |
words.append("kun")
|
| 43 |
else:
|
| 44 |
words.append(number_to_words(thousands) + " kun")
|
| 45 |
-
|
| 46 |
if remainder >= 100:
|
| 47 |
hundreds, rem2 = divmod(remainder, 100)
|
| 48 |
if hundreds:
|
|
@@ -52,30 +52,24 @@ def number_to_words(number):
|
|
| 52 |
words.append("iyo " + number_to_words(rem2))
|
| 53 |
elif remainder:
|
| 54 |
words.append("iyo " + number_to_words(remainder))
|
| 55 |
-
|
| 56 |
return " ".join(words)
|
| 57 |
elif number < 1000000000:
|
| 58 |
millions, remainder = divmod(number, 1000000)
|
| 59 |
words = []
|
| 60 |
-
|
| 61 |
if millions == 1:
|
| 62 |
words.append("milyan")
|
| 63 |
else:
|
| 64 |
words.append(number_to_words(millions) + " milyan")
|
| 65 |
-
|
| 66 |
if remainder:
|
| 67 |
words.append(number_to_words(remainder))
|
| 68 |
-
|
| 69 |
return " ".join(words)
|
| 70 |
else:
|
| 71 |
return str(number)
|
| 72 |
|
| 73 |
def normalize_text(text):
|
| 74 |
-
# Convert numbers to Somali words
|
| 75 |
numbers = re.findall(r'\d+', text)
|
| 76 |
for num in numbers:
|
| 77 |
text = text.replace(num, number_to_words(num))
|
| 78 |
-
# Replace foreign characters
|
| 79 |
text = text.replace("KH", "qa").replace("Z", "S")
|
| 80 |
text = text.replace("SH", "SHa'a").replace("DH", "Dha'a")
|
| 81 |
text = text.replace("ZamZam", "SamSam")
|
|
@@ -83,7 +77,7 @@ def normalize_text(text):
|
|
| 83 |
|
| 84 |
def tts(text):
|
| 85 |
text = normalize_text(text)
|
| 86 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 87 |
with torch.no_grad():
|
| 88 |
waveform = model(**inputs).waveform.squeeze().cpu().numpy()
|
| 89 |
filename = "output.wav"
|
|
@@ -96,4 +90,4 @@ gr.Interface(
|
|
| 96 |
outputs=gr.Audio(label="Codka TTS"),
|
| 97 |
title="Somali TTS",
|
| 98 |
description="Ku qor qoraal Soomaaliyeed si aad u maqasho cod dabiici ah.",
|
| 99 |
-
).launch()
|
|
|
|
| 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("HusseinBashir/fine_tuned_vits_som")
|
| 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 = {
|
|
|
|
| 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:
|
|
|
|
| 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")
|
|
|
|
| 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"
|
|
|
|
| 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()
|