HusseinBashir commited on
Commit
47fa993
·
verified ·
1 Parent(s): 4fb7378

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -67,28 +67,63 @@ def number_to_words(number):
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
-
 
67
  return str(number)
68
 
69
  def normalize_text(text):
70
+ # Remove commas from numbers like 1,000,000
71
+ text = re.sub(r'(\d{1,3})(,\d{3})+', lambda m: m.group(0).replace(",", ""), text)
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
+ '-': 'miinas'
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
+ paragraphs = text.strip().split("\n")
101
+ audio_list = []
102
+
103
+ for i, para in enumerate(paragraphs):
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=(final_audio * 32767).astype(np.int16))
121
  return filename
122
 
123
  gr.Interface(
124
  fn=tts,
125
+ inputs=gr.Textbox(label="Geli qoraal Soomaali ah", lines=10, placeholder="Ku qor 1 ama in ka badan paragraph..."),
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()