Spaces:
Runtime error
Runtime error
Update predict.py
Browse files- predict.py +59 -62
predict.py
CHANGED
@@ -1,62 +1,59 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
-
import random
|
4 |
-
import librosa
|
5 |
-
import numpy as np
|
6 |
-
import gradio as gr
|
7 |
-
from typing import Any, List, Dict, Tuple
|
8 |
-
|
9 |
-
from utils import meow_stretch, get_word_lengths
|
10 |
-
from config import config, BaseConfig
|
11 |
-
|
12 |
-
COUNTER = 0
|
13 |
-
|
14 |
-
''' Gradio Input/Output Configurations '''
|
15 |
-
inputs: str = 'text'
|
16 |
-
outputs: gr.Audio = gr.Audio()
|
17 |
-
|
18 |
-
def load_meows(cfg: BaseConfig) -> List[Dict[str, Any]]:
|
19 |
-
|
20 |
-
meow_dir = os.path.dirname(cfg.manifest_path)
|
21 |
-
|
22 |
-
with open(cfg.manifest_path, mode='r') as fr:
|
23 |
-
lines = fr.readlines()
|
24 |
-
|
25 |
-
items = []
|
26 |
-
for line in lines:
|
27 |
-
item = json.loads(line)
|
28 |
-
item['audio'], item['rate'] = librosa.load(os.path.join(meow_dir, item['audio_filepath']), sr=None)
|
29 |
-
items.append(item)
|
30 |
-
|
31 |
-
return items
|
32 |
-
|
33 |
-
def extract_meows_weights(items: List[Dict[str, Any]]) -> Tuple[List[np.ndarray], List[float]]:
|
34 |
-
meows = [item['audio'] for item in items]
|
35 |
-
weights = [item['weight'] for item in items]
|
36 |
-
return meows, weights
|
37 |
-
|
38 |
-
''' Load meows '''
|
39 |
-
meow_items = load_meows(config)
|
40 |
-
meows, weights = extract_meows_weights(meow_items)
|
41 |
-
|
42 |
-
def predict(text: str) -> str:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
return (config.sample_rate, result_meows)
|
61 |
-
|
62 |
-
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
import librosa
|
5 |
+
import numpy as np
|
6 |
+
import gradio as gr
|
7 |
+
from typing import Any, List, Dict, Tuple
|
8 |
+
|
9 |
+
from utils import meow_stretch, get_word_lengths
|
10 |
+
from config import config, BaseConfig
|
11 |
+
|
12 |
+
COUNTER = 0
|
13 |
+
|
14 |
+
''' Gradio Input/Output Configurations '''
|
15 |
+
inputs: str = 'text'
|
16 |
+
outputs: gr.Audio = gr.Audio()
|
17 |
+
|
18 |
+
def load_meows(cfg: BaseConfig) -> List[Dict[str, Any]]:
|
19 |
+
|
20 |
+
meow_dir = os.path.dirname(cfg.manifest_path)
|
21 |
+
|
22 |
+
with open(cfg.manifest_path, mode='r') as fr:
|
23 |
+
lines = fr.readlines()
|
24 |
+
|
25 |
+
items = []
|
26 |
+
for line in lines:
|
27 |
+
item = json.loads(line)
|
28 |
+
item['audio'], item['rate'] = librosa.load(os.path.join(meow_dir, item['audio_filepath']), sr=None)
|
29 |
+
items.append(item)
|
30 |
+
|
31 |
+
return items
|
32 |
+
|
33 |
+
def extract_meows_weights(items: List[Dict[str, Any]]) -> Tuple[List[np.ndarray], List[float]]:
|
34 |
+
meows = [item['audio'] for item in items]
|
35 |
+
weights = [item['weight'] for item in items]
|
36 |
+
return meows, weights
|
37 |
+
|
38 |
+
''' Load meows '''
|
39 |
+
meow_items = load_meows(config)
|
40 |
+
meows, weights = extract_meows_weights(meow_items)
|
41 |
+
|
42 |
+
def predict(text: str) -> str:
|
43 |
+
|
44 |
+
word_lengths = get_word_lengths(text)
|
45 |
+
selected_meows = random.choices(meows, weights=weights, k=len(word_lengths))
|
46 |
+
transformed_meows = [
|
47 |
+
meow_stretch(
|
48 |
+
meow, wl,
|
49 |
+
init_factor=config.init_factor,
|
50 |
+
add_factor=config.add_factor,
|
51 |
+
power_factor=config.power_factor
|
52 |
+
) for meow, wl in zip(selected_meows, word_lengths)
|
53 |
+
]
|
54 |
+
|
55 |
+
result_meows = np.concatenate(transformed_meows, axis=0)
|
56 |
+
|
57 |
+
return (config.sample_rate, result_meows)
|
58 |
+
|
59 |
+
|
|
|
|
|
|