forked from neonbjb/tortoise-tts
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
81 lines (61 loc) · 2.34 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
import os
import io
import requests
import base64
import torch
import torchaudio
import pydub
from scipy.io import wavfile
from tortoise.api import TextToSpeech
from tortoise.utils.audio import load_voice
def download_custom_voice(url):
response = requests.get(url)
custom_voice_folder = f"tortoise/voices/custom"
os.makedirs(custom_voice_folder)
with open(os.path.join(custom_voice_folder, 'input.wav'), 'wb') as f:
f.write(response.content)
def base64_encode(buffer: io.BytesIO) -> str:
"""
Encode the given buffer as base64.
"""
return base64.encodebytes(buffer.getvalue()).decode("ascii")
def mp3_bytes_from_wav_bytes(wav_bytes: io.BytesIO) -> io.BytesIO:
mp3_bytes = io.BytesIO()
sound = pydub.AudioSegment.from_wav(wav_bytes)
sound.export(mp3_bytes, format="mp3")
mp3_bytes.seek(0)
return mp3_bytes
# Init is ran on server startup
# Load your model to GPU as a global variable here using the variable name "model"
def init():
global model
device = 0 if torch.cuda.is_available() else -1
model = TextToSpeech()
# Inference is ran for every server call
# Reference your preloaded global model variable here.
def inference(model_inputs:dict) -> dict:
global model
text = model_inputs.get('text', None)
voice = model_inputs.get('voice', 'random')
preset = model_inputs.get('preset', 'fast')
# get custom wav file
if voice == 'custom':
custom_voice_url = model_inputs.get('custom_voice_url', None)
if custom_voice_url == None:
return {'message': "Custom voice requires url of audio sample"}
download_custom_voice(custom_voice_url)
voice_samples, conditioning_latents = load_voice(voice)
# Run the model
gen = model.tts_with_preset(text, voice_samples=voice_samples, conditioning_latents=conditioning_latents,
preset=preset)
if voice == 'custom':
custom_voice_folder = f"tortoise/voices/{voice}"
os.remove(os.path.join(custom_voice_folder, 'input.wav'))
wav_bytes = io.BytesIO()
wavfile.write(wav_bytes, 24000, gen.squeeze().cpu().numpy())
wav_bytes.seek(0)
mp3_bytes = mp3_bytes_from_wav_bytes(wav_bytes)
result = { 'audio': "data:audio/mpeg;base64," + base64_encode(mp3_bytes)}
# Return the results as a dictionary
return result