Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,63 +67,28 @@ def number_to_words(number):
|
|
| 67 |
return str(number)
|
| 68 |
|
| 69 |
def normalize_text(text):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Remove decimals (e.g., .00)
|
| 74 |
-
text = re.sub(r'\.\d+', '', text)
|
| 75 |
-
|
| 76 |
-
# Replace numbers with Somali words
|
| 77 |
-
def replace_num(match):
|
| 78 |
-
return number_to_words(match.group())
|
| 79 |
-
|
| 80 |
-
text = re.sub(r'\d+', replace_num, text)
|
| 81 |
-
|
| 82 |
-
# Replace special symbols
|
| 83 |
-
symbol_map = {
|
| 84 |
-
'$': 'doolar',
|
| 85 |
-
'=': 'egwal',
|
| 86 |
-
'+': 'balaas',
|
| 87 |
-
'#': 'haash'
|
| 88 |
-
}
|
| 89 |
-
for sym, word in symbol_map.items():
|
| 90 |
-
text = text.replace(sym, ' ' + word + ' ')
|
| 91 |
-
|
| 92 |
-
# Optional character normalization
|
| 93 |
text = text.replace("KH", "qa").replace("Z", "S")
|
| 94 |
text = text.replace("SH", "SHa'a").replace("DH", "Dha'a")
|
| 95 |
text = text.replace("ZamZam", "SamSam")
|
| 96 |
-
|
| 97 |
return text
|
| 98 |
|
| 99 |
def tts(text):
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if not para.strip():
|
| 105 |
-
continue
|
| 106 |
-
norm_para = normalize_text(para)
|
| 107 |
-
inputs = tokenizer(norm_para, return_tensors="pt").to(device)
|
| 108 |
-
with torch.no_grad():
|
| 109 |
-
waveform = model(**inputs).waveform.squeeze().cpu().numpy()
|
| 110 |
-
|
| 111 |
-
# Add pause between paragraphs (only if it's not the last one)
|
| 112 |
-
if i < len(paragraphs) - 1:
|
| 113 |
-
pause = np.zeros(int(model.config.sampling_rate * 0.8)) # 0.8 seconds pause
|
| 114 |
-
audio_list.append(np.concatenate((waveform, pause)))
|
| 115 |
-
else:
|
| 116 |
-
audio_list.append(waveform)
|
| 117 |
-
|
| 118 |
-
final_audio = np.concatenate(audio_list)
|
| 119 |
filename = "output.wav"
|
| 120 |
-
scipy.io.wavfile.write(filename, rate=model.config.sampling_rate, data=(
|
| 121 |
return filename
|
| 122 |
|
| 123 |
gr.Interface(
|
| 124 |
fn=tts,
|
| 125 |
-
inputs=gr.Textbox(label="Geli qoraal Soomaali ah"
|
| 126 |
outputs=gr.Audio(label="Codka TTS"),
|
| 127 |
title="Somali TTS",
|
| 128 |
-
description="Ku qor qoraal Soomaaliyeed si aad u maqasho cod dabiici ah."
|
| 129 |
).launch()
|
|
|
|
|
|
| 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 |
+
|