HusseinBashir commited on
Commit
c45c533
·
verified ·
1 Parent(s): 246b934

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -16
app.py CHANGED
@@ -1,26 +1,72 @@
 
1
  import torch
 
 
2
  from transformers import VitsModel, AutoTokenizer
3
- import gradio as gr
 
 
 
4
 
5
- # Load the fine-tuned model and tokenizer
6
- model = VitsModel.from_pretrained("HusseinBashir/fine_tuned_vits_som")
7
- tokenizer = AutoTokenizer.from_pretrained("HusseinBashir/fine_tuned_vits_som")
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
- model.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Gradio TTS function
12
  def tts(text):
 
13
  inputs = tokenizer(text, return_tensors="pt").to(device)
14
  with torch.no_grad():
15
- output = model(**inputs).waveform.squeeze(1).cpu().numpy()
16
- return output
 
 
17
 
18
- # Gradio interface for the TTS model
19
- iface = gr.Interface(
20
  fn=tts,
21
- inputs=gr.Textbox(label="Enter text"),
22
- outputs=gr.Audio(label="Generated Speech"),
23
- title="Fine-tuned VITS TTS",
24
- description="Generate speech from text using the fine-tuned VITS model."
25
- )
26
- iface.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
+ model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
9
+ tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
10
 
 
 
 
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device).eval()
13
+
14
+ number_words = {
15
+ 0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
16
+ 6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
17
+ 11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
18
+ 14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
19
+ 17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
20
+ 20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
21
+ 60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
22
+ 100: "boqol", 1000: "kun"
23
+ }
24
+
25
+ def number_to_words(number):
26
+ number = int(number)
27
+ if number < 20:
28
+ return number_words[number]
29
+ elif number < 100:
30
+ tens, unit = divmod(number, 10)
31
+ return number_words[tens * 10] + (" iyo " + number_words[unit] if unit else "")
32
+ elif number < 1000:
33
+ hundreds, remainder = divmod(number, 100)
34
+ part = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
35
+ if remainder:
36
+ part += " iyo " + number_to_words(remainder)
37
+ return part
38
+ elif number < 1000000:
39
+ thousands, remainder = divmod(number, 1000)
40
+ words = []
41
+ if thousands == 1:
42
+ words.append("kun")
43
+ else:
44
+ words.append(number_to_words(thousands) + " kun")
45
+ if remainder:
46
+ words.append("iyo " + number_to_words(remainder))
47
+ return " ".join(words)
48
+ else:
49
+ return str(number)
50
+
51
+ def normalize_text(text):
52
+ numbers = re.findall(r'\d+', text)
53
+ for num in numbers:
54
+ text = text.replace(num, number_to_words(num))
55
+ return text
56
 
 
57
  def tts(text):
58
+ text = normalize_text(text)
59
  inputs = tokenizer(text, return_tensors="pt").to(device)
60
  with torch.no_grad():
61
+ waveform = model(**inputs).waveform.squeeze().cpu().numpy()
62
+ output_path = "output.wav"
63
+ scipy.io.wavfile.write(output_path, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
64
+ return output_path
65
 
66
+ gr.Interface(
 
67
  fn=tts,
68
+ inputs=gr.Textbox(label="Qor qoraalka af-Soomaaliga"),
69
+ outputs=gr.Audio(type="filepath", label="Codka TTS"),
70
+ title="Somali TTS API",
71
+ description="Ku qor qoraal si aad u maqasho codka af-Soomaaliga",
72
+ ).launch()