-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
49 lines (41 loc) · 1.27 KB
/
service.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
39
40
41
42
43
44
45
46
47
48
49
from __future__ import annotations
import os
import typing as t
from pathlib import Path
import bentoml
MODEL_ID = "tts_models/multilingual/multi-dataset/xtts_v2"
sample_input_data = {
'text': 'It took me quite a long time to develop a voice and now that I have it I am not going to be silent.',
'language': 'en',
}
@bentoml.service(
resources={
"gpu": 1,
"memory": "8Gi",
},
traffic={"timeout": 300},
)
class XTTS:
def __init__(self) -> None:
import torch
from TTS.api import TTS
self.tts = TTS(MODEL_ID, gpu=torch.cuda.is_available())
@bentoml.api
def synthesize(
self,
context: bentoml.Context,
text: str = sample_input_data["text"],
lang: str = sample_input_data["language"],
) -> t.Annotated[Path, bentoml.validators.ContentType('audio/*')]:
output_path = os.path.join(context.temp_dir, "output.wav")
sample_path = "./female.wav"
if not os.path.exists(sample_path):
sample_path = "./src/female.wav"
self.tts.tts_to_file(
text,
file_path=output_path,
speaker_wav=sample_path,
language=lang,
split_sentences=True,
)
return Path(output_path)