-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
143 lines (121 loc) Β· 4.71 KB
/
app.py
File metadata and controls
143 lines (121 loc) Β· 4.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# flake8: noqa
import random
import uuid
from dataclasses import dataclass
from pathlib import Path
import requests
import streamlit as st
from api import EMOTTS_API_ROUTE
from inference_pipeline import (
CleanedTextIsEmptyStringError, SpeakerNotFoundError,
inference_text_to_speech,
)
from src.constants import Emotion, Language, SupportedLanguages
from src.web.streamlit_utils import (
hide_hamburger_menu, st_empty_block, st_header_centered,
)
BROKEN_SPEAKERS = ["0019"]
SELECTOR_TO_LANG = {
"π·πΊ Russian (ru-RU)": SupportedLanguages.russian,
"πΊπΈ English (en-EN)": SupportedLanguages.english,
}
LOADING_PHRASES = [
"π·ββοΈ Building language corpuses...",
"π Dreaming about free GPU hosting...",
"π« Permuting tensors...",
]
@dataclass
class AppModes:
api_connector: str = "api"
standalone: str = "standalone"
APP_MODE = AppModes.api_connector
HOST = "localhost"
PORT = 8080
# EMOTTS_API_ENDPOINT = f"http://{HOST}:{PORT}{EMOTTS_API_ROUTE}"
EMOTTS_API_ENDPOINT = "https://api-emotts.appspot.com/tts/emo/v1" #GCP
def run_in_api_connector_mode(language: Language, emotion: Emotion, input_text: str, speaker: str, audio_output_path: Path) -> None:
loading_phrase = random.choice(LOADING_PHRASES)
with st.spinner(loading_phrase):
query = f"{EMOTTS_API_ENDPOINT}?lang={language.api_name}&emo={emotion.api_name}&speaker={speaker}&text={input_text}"
r = requests.get(query, stream=True)
if r.status_code == 200:
with open(audio_output_path, 'wb') as f:
for chunk in r:
f.write(chunk)
else:
st.warning(f"Status: {r.status_code}")
st.warning(f"Message: {r.text}")
def run_in_standalone_mode(language: Language, emotion: Emotion, input_text: str, speaker: str, audio_output_path: Path) -> None:
loading_phrase = random.choice(LOADING_PHRASES)
with st.spinner(loading_phrase):
inference_text_to_speech(
language=language,
input_text=input_text,
speaker=speaker,
emotion=emotion,
audio_output_path=audio_output_path,
)
def layout_app() -> None:
# header_text = "π₯ EMOtts dEMO π"
header_text = "EmoTTS Project"
st_header_centered(header_text)
st_empty_block(2)
language_name = st.selectbox(label="π
Language", options=SELECTOR_TO_LANG.keys())
language = SELECTOR_TO_LANG.get(language_name)
with st.form(key="input_form"):
col1, col2 = st.columns(2)
with col1:
speaker_selector = language.speaker_selector
for broken_speaker in BROKEN_SPEAKERS:
speaker_selector.pop(broken_speaker, None)
speaker = st.selectbox(label="π£ Speaker", options=speaker_selector.keys())
with col2:
emotion_name = st.select_slider(label="π¨ Emotion", options=language.emo_selector.keys())
emotion = language.emo_selector.get(emotion_name)
st_empty_block(2)
input_text = st.text_area(label="π What should I say?", value=language.test_phrase, max_chars=140)
st_empty_block(2)
form_submit = st.form_submit_button("Synthesize speech")
if form_submit:
audio_output_path = Path(f"generated-{uuid.uuid4()}.wav")
try:
# Run inference pipeline
if APP_MODE == AppModes.standalone:
run_in_standalone_mode(
language=language,
input_text=input_text,
speaker=speaker,
emotion=emotion,
audio_output_path=audio_output_path,
)
elif APP_MODE == AppModes.api_connector:
run_in_api_connector_mode(
language=language,
input_text=input_text,
speaker=speaker,
emotion=emotion,
audio_output_path=audio_output_path,
)
with open(audio_output_path, "rb") as audio_file:
st.audio(audio_file.read())
audio_output_path.unlink()
except CleanedTextIsEmptyStringError:
st.warning("π Looks like input text can not be pronounced")
st.stop()
except SpeakerNotFoundError:
st.warning("π This combination of speaker, language and emotion is not supported")
st.stop()
# except Exception:
# st.error("Oops! Forget about it and hit F5 π")
# st.stop()
def main() -> None:
hide_hamburger_menu()
layout_app()
if __name__ == "__main__":
st.set_page_config(
page_title="EmoTTS Project",
page_icon="π¬",
layout="centered",
initial_sidebar_state="auto",
)
main()