-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.py
More file actions
68 lines (55 loc) · 2.17 KB
/
Copy pathapi.py
File metadata and controls
68 lines (55 loc) · 2.17 KB
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
from pathlib import Path
from uuid import uuid4
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from starlette.background import BackgroundTasks
from inference_pipeline import DEVICE, inference_text_to_speech
from src.constants import SupportedEmotions, SupportedLanguages
EMOTTS_API_ROUTE = "/tts/emo/v1"
TEST_AUDIO_PATH = "data/testaudio-gs-16b-1c-44100hz.wav"
ARG_TO_LANGUAGE = {
"ru": SupportedLanguages.russian,
"en": SupportedLanguages.english,
}
ARG_TO_EMOTION = {
SupportedEmotions.angry.api_name: SupportedEmotions.angry,
SupportedEmotions.happy.api_name: SupportedEmotions.happy,
SupportedEmotions.neutral.api_name: SupportedEmotions.neutral,
SupportedEmotions.sad.api_name: SupportedEmotions.sad,
SupportedEmotions.surprise.api_name: SupportedEmotions.surprise,
SupportedEmotions.very_angry.api_name: SupportedEmotions.very_angry,
SupportedEmotions.very_happy.api_name: SupportedEmotions.very_happy,
}
app = FastAPI()
def remove_file(path: Path):
path.unlink(missing_ok=True)
@app.get("/")
async def root():
return {"message": "EmoTTS Project"}
# To read this from response:
# r = requests.get("http://<host>:<port>/testaudio", stream=True)
# if r.status_code == 200:
# with open(path, 'wb') as f:
# for chunk in r:
# f.write(chunk)
@app.get("/testaudio", response_class=FileResponse)
async def audio():
filepath = Path(TEST_AUDIO_PATH)
return filepath
@app.get(EMOTTS_API_ROUTE, response_class=FileResponse)
async def tts(lang: str, emo: str, speaker: str, text: str, bg_tasks: BackgroundTasks):
language = ARG_TO_LANGUAGE.get(lang)
emotion = ARG_TO_EMOTION.get(emo)
if not language or not emotion:
raise HTTPException(status_code=500, detail=f"OOPS!\nlang:{lang}, emo:{emo}, text:{text}")
generated_audio_path = Path(f"predictions/generated-{str(uuid4())}.wav")
inference_text_to_speech(
language=language,
input_text=text,
speaker=speaker,
emotion=emotion,
audio_output_path=generated_audio_path,
device=DEVICE,
)
bg_tasks.add_task(remove_file, generated_audio_path)
return generated_audio_path