Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tempfile
|
3 |
+
import os
|
4 |
+
from TTS.config import load_config
|
5 |
+
from TTS.utils.manage import ModelManager
|
6 |
+
from TTS.utils.synthesizer import Synthesizer
|
7 |
+
from TTS.utils.download import download_url
|
8 |
+
|
9 |
+
# Define constants
|
10 |
+
MAX_TXT_LEN = 800
|
11 |
+
MODEL_INFO = [
|
12 |
+
# ["Model Name", "Model File", "Config File", "URL"]
|
13 |
+
# Add other models in the same format
|
14 |
+
["vits-espeak-57000", "checkpoint_57000.pth", "config.json", "https://huggingface.co/mhrahmani/persian-tts-vits-0/tree/main"],
|
15 |
+
# ...
|
16 |
+
]
|
17 |
+
|
18 |
+
# Download models
|
19 |
+
def download_models():
|
20 |
+
for model_name, model_file, config_file, url in MODEL_INFO:
|
21 |
+
directory = model_name
|
22 |
+
os.makedirs(directory, exist_ok=True)
|
23 |
+
download_url(f"{url}{model_file}", directory, str(model_file))
|
24 |
+
download_url(f"{url}{config_file}", directory, "config.json")
|
25 |
+
|
26 |
+
# Load a model and perform TTS
|
27 |
+
def synthesize_speech(text, model_name):
|
28 |
+
if len(text) > MAX_TXT_LEN:
|
29 |
+
text = text[:MAX_TXT_LEN]
|
30 |
+
st.warning(f"Input text was truncated to {MAX_TXT_LEN} characters.")
|
31 |
+
|
32 |
+
synthesizer = Synthesizer(f"{model_name}/best_model.pth", f"{model_name}/config.json")
|
33 |
+
if synthesizer is None:
|
34 |
+
st.error("Model not found!")
|
35 |
+
return None
|
36 |
+
|
37 |
+
wavs = synthesizer.tts(text)
|
38 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
39 |
+
synthesizer.save_wav(wavs, fp)
|
40 |
+
return fp.name
|
41 |
+
|
42 |
+
# Streamlit app
|
43 |
+
def main():
|
44 |
+
st.title('persian tts playground')
|
45 |
+
st.markdown("""
|
46 |
+
Persian TTS Demo)
|
47 |
+
""")
|
48 |
+
|
49 |
+
text_input = st.text_area("Enter Text to Synthesize:", "زین همرهان سست عناصر، دلم گرفت.")
|
50 |
+
model_name = st.selectbox("Pick a TTS Model", [info[0] for info in MODEL_INFO], index=1)
|
51 |
+
|
52 |
+
if st.button('Synthesize'):
|
53 |
+
audio_file = synthesize_speech(text_input, model_name)
|
54 |
+
if audio_file:
|
55 |
+
st.audio(audio_file, format='audio/wav')
|
56 |
+
|
57 |
+
# Download models and run the Streamlit app
|
58 |
+
if __name__ == "__main__":
|
59 |
+
download_models()
|
60 |
+
main()
|