HusseinBashir commited on
Commit
371beeb
·
verified ·
1 Parent(s): bac9db5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -16
app.py CHANGED
@@ -1,30 +1,94 @@
 
1
  import torch
 
 
2
  from transformers import VitsModel, AutoTokenizer
3
- import gradio as gr
4
 
5
- # Load fine-tuned model and tokenizer
6
  model = VitsModel.from_pretrained("HusseinBashir/codad_tijaabo")
7
- tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
- model.to(device).eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def tts(text):
 
12
  inputs = tokenizer(text, return_tensors="pt").to(device)
13
  with torch.no_grad():
14
- output = model(**inputs)
15
- waveform = output["waveform"].squeeze(1).cpu().numpy()
16
-
17
- # Normalize waveform
18
- waveform = waveform.flatten()
19
- waveform = waveform / max(abs(waveform))
20
-
21
- return (22050, waveform) # 22.05 kHz sample rate typical for VITS
22
 
23
- # Gradio interface
24
  gr.Interface(
25
  fn=tts,
26
  inputs=gr.Textbox(label="Geli qoraal Soomaali ah"),
27
- outputs=gr.Audio(label="Codka la sameeyey"),
28
- title="Codad Tijaabo TTS",
29
- description="Ku qor qoraal Soomaali ah si aad cod u maqasho iyadoo la adeegsanayo VITS."
30
  ).launch()
 
 
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("HusseinBashir/codad_tijaabo")
10
+ tokenizer = AutoTokenizer.from_pretrained("HusseinBashir/codad_tijaabo")
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
+