-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpredict.py
38 lines (28 loc) · 1.22 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import tempfile
from spleeter.separator import Separator
from spleeter.audio.adapter import AudioAdapter
from cog import BasePredictor, Input, Path, BaseModel
class ModelOutput(BaseModel):
vocals: Path
accompaniment: Path
class Predictor(BasePredictor):
def setup(self):
"""Loads Spleeter 2 stems model into memory from disk"""
self.separator = Separator('spleeter:2stems')
self.audio_loader = AudioAdapter.default()
def predict(
self,
audio: Path = Input(description="Audio file")
) -> ModelOutput:
"""Separate the vocals from the accompaniment of an audio file"""
waveform, sample_rate = self.audio_loader.load(str(audio))
prediction = self.separator.separate(waveform)
out_path = Path(tempfile.mkdtemp())
out_path_vocals = out_path / "vocals.wav"
out_path_accompaniment = out_path / "accompaniment.wav"
self.audio_loader.save(str(out_path_vocals), prediction['vocals'], sample_rate)
self.audio_loader.save(str(out_path_accompaniment), prediction['accompaniment'], sample_rate)
return ModelOutput(
vocals=out_path_vocals,
accompaniment=out_path_accompaniment
)