Spaces:
Runtime error
Runtime error
Update app.py
Browse filesrefactor to gradio
app.py
CHANGED
@@ -1,60 +1,62 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import tempfile
|
3 |
import os
|
4 |
-
|
5 |
-
|
6 |
from TTS.utils.synthesizer import Synthesizer
|
7 |
-
from
|
8 |
|
9 |
# Define constants
|
10 |
-
MAX_TXT_LEN = 800
|
11 |
MODEL_INFO = [
|
12 |
-
# ["Model Name", "Model File", "Config File", "URL"]
|
13 |
-
|
14 |
-
|
15 |
-
# ...
|
16 |
]
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Download models
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
def
|
|
|
28 |
if len(text) > MAX_TXT_LEN:
|
29 |
text = text[:MAX_TXT_LEN]
|
30 |
-
|
31 |
-
|
32 |
-
synthesizer = Synthesizer(f"{model_name}/
|
33 |
if synthesizer is None:
|
34 |
-
|
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 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
main()
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import tempfile
|
3 |
+
import gradio as gr
|
4 |
from TTS.utils.synthesizer import Synthesizer
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
|
7 |
# Define constants
|
|
|
8 |
MODEL_INFO = [
|
9 |
+
# ["Model Name", "Model File", "Config File", "Hub URL"]
|
10 |
+
["vits-espeak-57000", "checkpoint_57000.pth", "config.json", "mhrahmani/persian-tts-vits-0"],
|
11 |
+
# Add other models similarly...
|
|
|
12 |
]
|
13 |
|
14 |
+
# Extract model names from MODEL_INFO
|
15 |
+
MODEL_NAMES = [info[0] for info in MODEL_INFO]
|
16 |
+
|
17 |
+
MAX_TXT_LEN = 400
|
18 |
+
TOKEN = os.environ.get('HUGGING_FACE_HUB_TOKEN') # Replace with the environment variable containing your token, if different
|
19 |
+
|
20 |
# Download models
|
21 |
+
for model_name, model_file, config_file, repo_name in MODEL_INFO:
|
22 |
+
os.makedirs(model_name, exist_ok=True)
|
23 |
+
print(f"|> Downloading: {model_name}")
|
24 |
+
|
25 |
+
# Use hf_hub_download to download models from Hugging Face repositories
|
26 |
+
hf_hub_download(repo_id=repo_name, filename=model_file, cache_dir=model_name, use_auth_token=TOKEN)
|
27 |
+
hf_hub_download(repo_id=repo_name, filename=config_file, cache_dir=model_name, use_auth_token=TOKEN)
|
28 |
+
|
29 |
+
def synthesize(text: str, model_name: str) -> str:
|
30 |
+
"""Synthesize speech using the selected model."""
|
31 |
if len(text) > MAX_TXT_LEN:
|
32 |
text = text[:MAX_TXT_LEN]
|
33 |
+
print(f"Input text was cut off as it exceeded the {MAX_TXT_LEN} character limit.")
|
34 |
+
|
35 |
+
synthesizer = Synthesizer(f"{model_name}/{model_file}", f"{model_name}/{config_file}")
|
36 |
if synthesizer is None:
|
37 |
+
raise NameError("Model not found")
|
|
|
38 |
|
39 |
wavs = synthesizer.tts(text)
|
40 |
+
|
41 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
42 |
synthesizer.save_wav(wavs, fp)
|
43 |
return fp.name
|
44 |
|
45 |
+
# Define Gradio interface
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=synthesize,
|
48 |
+
inputs=[
|
49 |
+
gr.Textbox(label="Enter Text to Synthesize:", value="زین همرهان سست عناصر، دلم گرفت."),
|
50 |
+
gr.Radio(label="Pick a Model", choices=MODEL_NAMES, value=MODEL_NAMES[0]),
|
51 |
+
],
|
52 |
+
outputs=gr.Audio(label="Output", type='filepath'),
|
53 |
+
examples=[["زین همرهان سست عناصر، دلم گرفت.", MODEL_NAMES[0]]],
|
54 |
+
title='persian tts playground',
|
55 |
+
description="Persian text to speech model demo", # Add the required description here.
|
56 |
+
article="",
|
57 |
+
live=False
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the interface
|
61 |
+
iface.launch(share=False)
|
62 |
+
|
|