Spaces:
Runtime error
Runtime error
Upload utils.py with huggingface_hub
Browse files
utils.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import librosa
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
def load_audio(wavpath, sr):
|
| 6 |
+
audio, _ = librosa.load(wavpath, sr=sr, mono=True)
|
| 7 |
+
return torch.from_numpy(audio).unsqueeze(0)
|
| 8 |
+
|
| 9 |
+
def normalize_text(text):
|
| 10 |
+
text = text.lower()
|
| 11 |
+
text = re.sub(r'["“”‘’]', ' ', text)
|
| 12 |
+
text = re.sub(r'\s+', ' ', text)
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
def approx_duration_from_text(text, max_duration=30.0):
|
| 16 |
+
EN_DUR_PER_CHAR = 0.082
|
| 17 |
+
ZH_DUR_PER_CHAR = 0.21
|
| 18 |
+
text = re.sub(r"\s+", "", text)
|
| 19 |
+
num_zh = num_en = num_other = 0
|
| 20 |
+
for c in text:
|
| 21 |
+
if "\u4e00" <= c <= "\u9fff":
|
| 22 |
+
num_zh += 1
|
| 23 |
+
elif c.isalpha():
|
| 24 |
+
num_en += 1
|
| 25 |
+
else:
|
| 26 |
+
num_other += 1
|
| 27 |
+
if num_zh > num_en:
|
| 28 |
+
num_zh += num_other
|
| 29 |
+
else:
|
| 30 |
+
num_en += num_other
|
| 31 |
+
return min(max_duration, num_zh * ZH_DUR_PER_CHAR + num_en * EN_DUR_PER_CHAR)
|