File size: 952 Bytes
8a3847d
 
bb39e00
8a3847d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242ae95
 
 
 
 
 
 
 
2d5bd1e
8a3847d
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import kuznetsov_av.text_to_speech_converter as t2s
import base64

class Request(BaseModel):
    """
    Input text.
    """
    text: str

class Response(BaseModel):
    """
    Result of text-to-audio generation.
    audio - base64 string
    """
    audio: str
    sampling_rate: int


app = FastAPI()

@app.get('/')
async def root() -> str:
    """
    Root method of API.
    """
    return '{"message": "Converter method: /text-to-speech/convert/"}'

@app.post('/text-to-speech/convert/')
async def text_to_speech(entity: Request) -> Response:
    """
    Text-to-audio generation method using text_to_speech_converter.
    """
    synthesiser = t2s.load_model()
    embeddings_dataset = t2s.load_speaker_dataset()
    audio, sampling_rate = t2s.text_to_speech(entity.text, synthesiser, embeddings_dataset)
    return Response(audio=base64.b32encode(audio), sampling_rate=sampling_rate)