ayymen commited on
Commit
5536535
1 Parent(s): ad86f4b

Add num2words

Browse files
Files changed (2) hide show
  1. app.py +26 -11
  2. snfl_imdann.py +61 -0
app.py CHANGED
@@ -3,6 +3,9 @@ import tempfile
3
  from TTS.api import TTS
4
  from huggingface_hub import hf_hub_download
5
  import torch
 
 
 
6
 
7
  CUDA = torch.cuda.is_available()
8
 
@@ -32,8 +35,26 @@ my_examples = [
32
  ["ⵜⴻⵜⵜⵏ ⴰⴳ ⵡⵓⵛⵛⵏ, ⵜⵜⵔⵓⵏ ⵅ ⵓⵎⴽⵙⴰ.", "rif", "idj", False]
33
  ]
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  my_inputs = [
36
- gr.Textbox(lines=5, label="Input Text", placeholder="The only available characters are: ⴰⴱⴳⴷⴹⴻⴼⴽⵀⵃⵄⵅⵇⵉⵊⵍⵎⵏⵓⵔⵕⵖⵙⵚⵛⵜⵟⵡⵢⵣⵥⵯ !,.:?"),
37
  gr.Dropdown(label="Variant", choices=list(VARIANTS.items()), value="shi"),
38
  gr.Dropdown(label="Speaker", choices=SPEAKERS, value="yan"),
39
  gr.Checkbox(label="Split Sentences (each sentence will be generated separately)", value=False),
@@ -43,15 +64,6 @@ my_inputs = [
43
 
44
  my_outputs = gr.Audio(type="filepath", label="Output Audio", autoplay=True)
45
 
46
- best_model_path = hf_hub_download(repo_id=REPO_ID, filename="checkpoint_390000.pth")
47
- config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
48
-
49
- api = TTS(model_path=best_model_path, config_path=config_path).to("cuda" if CUDA else "cpu")
50
-
51
- # pre-download voice conversion models
52
- for model in VOICE_CONVERSION_MODELS.values():
53
- api.load_vc_model_by_name(model, gpu=CUDA)
54
-
55
  def tts(text: str, variant: str = "shi", speaker: str = "yan", split_sentences: bool = False, speaker_wav: str = None, voice_cv_model: str = 'freevc24'):
56
  # replace oov characters
57
  text = text.replace("\n", ". ")
@@ -62,6 +74,9 @@ def tts(text: str, variant: str = "shi", speaker: str = "yan", split_sentences:
62
  text = text.replace(";", ",")
63
  text = text.replace("-", " ")
64
 
 
 
 
65
  with tempfile.NamedTemporaryFile(suffix = ".wav", delete = False) as fp:
66
  if speaker_wav:
67
  api.load_vc_model_by_name(VOICE_CONVERSION_MODELS[voice_cv_model], gpu=CUDA)
@@ -69,7 +84,7 @@ def tts(text: str, variant: str = "shi", speaker: str = "yan", split_sentences:
69
  else:
70
  api.tts_to_file(text, file_path=fp.name, split_sentences=split_sentences, speaker=speaker, language=variant)
71
 
72
- return fp.name
73
 
74
  iface = gr.Interface(
75
  fn=tts,
 
3
  from TTS.api import TTS
4
  from huggingface_hub import hf_hub_download
5
  import torch
6
+ import json
7
+ from snfl_imdann import TifinaghNumberConverter
8
+ import re
9
 
10
  CUDA = torch.cuda.is_available()
11
 
 
35
  ["ⵜⴻⵜⵜⵏ ⴰⴳ ⵡⵓⵛⵛⵏ, ⵜⵜⵔⵓⵏ ⵅ ⵓⵎⴽⵙⴰ.", "rif", "idj", False]
36
  ]
37
 
38
+ best_model_path = hf_hub_download(repo_id=REPO_ID, filename="checkpoint_390000.pth")
39
+ config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
40
+
41
+ api = TTS(model_path=best_model_path, config_path=config_path).to("cuda" if CUDA else "cpu")
42
+
43
+ # pre-download voice conversion models
44
+ for model in VOICE_CONVERSION_MODELS.values():
45
+ api.load_vc_model_by_name(model, gpu=CUDA)
46
+
47
+ with open(config_path, "r") as f:
48
+ config = json.load(f)
49
+ available_chars = config["characters"]["characters"]
50
+ available_punct = config["characters"]["punctuations"]
51
+
52
+ available_chars = available_chars + "".join([str(i) for i in range(10)])
53
+
54
+ placeholder = f"The available characters are: {available_chars} and the available punctuation is: {available_punct}"
55
+
56
  my_inputs = [
57
+ gr.Textbox(lines=5, label="Input Text", placeholder=placeholder),
58
  gr.Dropdown(label="Variant", choices=list(VARIANTS.items()), value="shi"),
59
  gr.Dropdown(label="Speaker", choices=SPEAKERS, value="yan"),
60
  gr.Checkbox(label="Split Sentences (each sentence will be generated separately)", value=False),
 
64
 
65
  my_outputs = gr.Audio(type="filepath", label="Output Audio", autoplay=True)
66
 
 
 
 
 
 
 
 
 
 
67
  def tts(text: str, variant: str = "shi", speaker: str = "yan", split_sentences: bool = False, speaker_wav: str = None, voice_cv_model: str = 'freevc24'):
68
  # replace oov characters
69
  text = text.replace("\n", ". ")
 
74
  text = text.replace(";", ",")
75
  text = text.replace("-", " ")
76
 
77
+ # convert numbers to their spoken form
78
+ text = re.sub(r"\d+", lambda x: TifinaghNumberConverter.convert(int(x.group(0))), text)
79
+
80
  with tempfile.NamedTemporaryFile(suffix = ".wav", delete = False) as fp:
81
  if speaker_wav:
82
  api.load_vc_model_by_name(VOICE_CONVERSION_MODELS[voice_cv_model], gpu=CUDA)
 
84
  else:
85
  api.tts_to_file(text, file_path=fp.name, split_sentences=split_sentences, speaker=speaker, language=variant)
86
 
87
+ return fp.name
88
 
89
  iface = gr.Interface(
90
  fn=tts,
snfl_imdann.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class TifinaghNumberConverter:
2
+ AND = " ⴷ "
3
+ UNITS = ["ⴰⵎⵢⴰ", "ⵢⴰⵏ", "ⵙⵉⵏ", "ⴽⵕⴰⴹ", "ⴽⴽⵓⵥ", "ⵙⵎⵎⵓⵙ", "ⵚⴹⵉⵚ", "ⵙⴰ", "ⵜⴰⵎ", "ⵜⵥⴰ"]
4
+ TENS = ["", "ⵎⵔⴰⵡ", "ⵙⵉⵎⵔⴰⵡ", "ⴽⵕⴰⵎⵔⴰⵡ", "ⴽⴽⵓⵎⵔⴰⵡ", "ⵙⵎⵎⵓⵎⵔⴰⵡ", "ⵚⴹⵉⵎⵔⴰⵡ", "ⵙⴰⵎⵔⴰⵡ", "ⵜⴰⵎⵔⴰⵡ", "ⵜⵥⴰⵎⵔⴰⵡ"]
5
+ HUNDREDS = ["", "ⵜⵉⵎⵉⴹⵉ", "ⵙⵏⴰⵜ ⵜⵎⴰⴹ", "ⴽⵕⴰⴹⵜ ⵜⵎⴰⴹ", "ⴽⴽⵓⵥⵜ ⵜⵎⴰⴹ", "ⵙⵎⵎⵓⵙⵜ ⵜⵎⴰⴹ", "ⵚⴹⵉⵚⵜ ⵜⵎⴰⴹ", "ⵙⴰⵜ ⵜⵎⴰⴹ", "ⵜⴰⵎⵜ ⵜⵎⴰⴹ", "ⵜⵥⴰⵜ ⵜⵎⴰⴹ"]
6
+ ORDERS = ["", "ⵉⴼⴹ", "ⴰⵎⵍⵢⵓⵏ", "ⴰⵎⵍⵢⴰⵕ", "ⴰⵜⵔⵉⵍⵢⵓⵏ", "ⴰⴽⵡⴰⴹⵕⵉⵍⵢⵓⵏ", "ⴰⴽⵡⵉⵏⵜⵔⵉⵍⵢⵓⵏ", "ⴰⵙⵉⴽⵙⵜⵉⵍⵢⵓⵏ", "ⴰⵙⵉⴱⵜⵉⵍⵢⵓⵏ", "ⴰⵡⴽⵜⵉⵍⵢⵓⵏ", "ⴰⵏⵓⵏⵉⵍⵢⵓⵏ"]
7
+ ORDERS_PLURAL = ["", "ⵡⴰⴼⴹⴰⵏ", "ⵉⴷ ⴰⵎⵍⵢⵓⵏ", "ⵉⴷ ⴰⵎⵍⵢⴰⵕ", "ⵉⵜⵔⵉⵍⵢⵓⵏⵏ", "ⵉⴽⵡⴰⴹⵕⵉⵍⵢⵓⵏⵏ", "ⵉⴽⵡⵉⵏⵜⵔⵉⵍⵢⵓⵏⵏ", "ⵉⵙⵉⴽⵙⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵙⵉⴱⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵡⴽⵜⵉⵍⵢⵓⵏⵏ", "ⵉⵏⵓⵏⵉⵍⵢⵓⵏⵏ"]
8
+
9
+ @classmethod
10
+ def convert_number_to_999(cls, n):
11
+ """Convert numbers from 0 to 999 to Tifinagh."""
12
+ if n == 0:
13
+ return ""
14
+ if n < 10:
15
+ return cls.UNITS[n]
16
+ if n < 20:
17
+ if n % 10 == 0:
18
+ return cls.TENS[n // 10]
19
+ return cls.UNITS[n % 10] + cls.AND + cls.TENS[n // 10]
20
+ if n < 100:
21
+ if n % 10 == 0:
22
+ return cls.TENS[n // 10]
23
+ return cls.TENS[n // 10] + cls.AND + cls.UNITS[n % 10]
24
+ if n < 1000:
25
+ if n % 100 == 0:
26
+ return cls.HUNDREDS[n // 100]
27
+ return cls.HUNDREDS[n // 100] + cls.AND + cls.convert_number_to_999(n % 100)
28
+ return ""
29
+
30
+ @classmethod
31
+ def convert_large_number(cls, n):
32
+ """Convert large numbers to Tifinagh."""
33
+ if n < 1000:
34
+ return cls.convert_number_to_999(n)
35
+
36
+ for order in range(len(cls.ORDERS) - 1, 0, -1):
37
+ order_value = 10 ** (order * 3)
38
+ if n >= order_value:
39
+ quotient = n // order_value
40
+ remainder = n % order_value
41
+
42
+ if quotient == 1:
43
+ if remainder:
44
+ return cls.ORDERS[order] + cls.AND + cls.convert_large_number(remainder)
45
+ return cls.ORDERS[order]
46
+ else:
47
+ if remainder:
48
+ return cls.convert_number_to_999(quotient) + " " + \
49
+ cls.ORDERS_PLURAL[order] + cls.AND + cls.convert_large_number(remainder)
50
+ return cls.convert_number_to_999(quotient) + " " + cls.ORDERS_PLURAL[order]
51
+
52
+ return ""
53
+
54
+ @classmethod
55
+ def convert(cls, number):
56
+ """Main conversion method."""
57
+ if number == 0:
58
+ return "ⴰⵎⵢⴰ"
59
+ if number < 0:
60
+ return "- " + cls.convert_large_number(abs(number))
61
+ return cls.convert_large_number(number)