Spaces:
Build error
Build error
File size: 8,011 Bytes
92d8e4f 4e2ccb2 92d8e4f 4e2ccb2 331e8d6 92d8e4f d985d89 92d8e4f a244f0f 0f67e13 4813b88 0f67e13 41e4133 651eb0b 0f67e13 4813b88 15bb08c a244f0f cd2be74 92d8e4f 0f67e13 8110e5d 69c3fea a244f0f 92d8e4f ab9a8c2 4e2ccb2 92d8e4f 7127b4a 92d8e4f 4e2ccb2 a244f0f 4e2ccb2 a244f0f 4e2ccb2 a244f0f 4e2ccb2 7127b4a 92d8e4f a244f0f 4e2ccb2 92d8e4f a244f0f 3befa06 a244f0f 92d8e4f 4e2ccb2 a244f0f 4e2ccb2 a244f0f 92d8e4f 4e2ccb2 a244f0f 3befa06 a244f0f 4e2ccb2 92d8e4f a244f0f 92d8e4f 4e2ccb2 c12e090 92d8e4f a244f0f 92d8e4f 4e2ccb2 a244f0f 3700f4d f41a8b4 4e2ccb2 d985d89 92d8e4f a244f0f |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import os
import tempfile
import gradio as gr
from TTS.api import TTS
from TTS.utils.synthesizer import Synthesizer
from huggingface_hub import hf_hub_download
import json
from zipfile import ZipFile
# Define constants
MODEL_INFO = [
#["vits checkpoint 57000", "checkpoint_57000.pth", "config.json", "mhrahmani/persian-tts-vits-0"],
# ["VITS Grapheme Multispeaker CV15(reduct)(best at 17864)", "best_model_17864.pth", "config.json",
# "saillab/persian-tts-cv15-reduct-grapheme-multispeaker"],
["Single speaker (best)VITS Grapheme Azure (61000)", "checkpoint_61000.pth", "config.json", "saillab/persian-tts-azure-grapheme-60K"],
#["VITS Grapheme ARM24 Fine-Tuned on 1 (66651)", "best_model_66651.pth", "config.json","saillab/persian-tts-grapheme-arm24-finetuned-on1"],
["Single speaker female best VITS Grapheme CV-Azure_male-Azure_female","best_model_15397.pth","config.json","saillab/female_cv_azure_male_azure_female","speakers1.pth"],
#["Multi Speaker Vits Grapheme CV+Azure in one set ","best_model_358320.pth","config.json","saillab/Multi_Speaker_Cv_plus_Azure_female_in_one_set","speakers.pth"],
["Multispeaker VITS Grapheme CV15(reduct)(22000)", "checkpoint_22000.pth", "config.json", "saillab/persian-tts-cv15-reduct-grapheme-multispeaker", "speakers.pth"],
["Multispeaker VITS Grapheme CV15(reduct)(26000)", "checkpoint_25000.pth", "config.json", "saillab/persian-tts-cv15-reduct-grapheme-multispeaker", "speakers.pth"],
["Multispeaker VITS Grapheme CV15(90K)", "best_model_56960.pth", "config.json", "saillab/multi_speaker", "speakers.pth"],
["Single speaker female best VITS Grapheme CV-Azure_male-Azure_female","best_model_15397.pth","config.json","saillab/female_cv_azure_male_azure_female","speakers.pth"],
# ["VITS Grapheme Azure (best at 15934)", "best_model_15934.pth", "config.json",
# "saillab/persian-tts-azure-grapheme-60K"],
["Single speaker VITS Grapheme ARM24 Fine-Tuned on 1 (66651)", "best_model_66651.pth", "config.json","saillab/persian-tts-grapheme-arm24-finetuned-on1"],
["Single speaker VITS Grapheme ARM24 Fine-Tuned on 1 (120000)", "checkpoint_120000.pth", "config.json","saillab/persian-tts-grapheme-arm24-finetuned-on1"],
# ... Add other models similarly
]
# Extract model names from MODEL_INFO
MODEL_NAMES = [info[0] for info in MODEL_INFO]
MAX_TXT_LEN = 400
TOKEN = os.getenv('HUGGING_FACE_HUB_TOKEN')
model_files = {}
config_files = {}
speaker_files = {}
# Create a dictionary to store synthesizer objects for each model
synthesizers = {}
def update_config_speakers_file_recursive(config_dict, speakers_path):
"""Recursively update speakers_file keys in a dictionary."""
if "speakers_file" in config_dict:
config_dict["speakers_file"] = speakers_path
for key, value in config_dict.items():
if isinstance(value, dict):
update_config_speakers_file_recursive(value, speakers_path)
def update_config_speakers_file(config_path, speakers_path):
"""Update the config.json file to point to the correct speakers.pth file."""
# Load the existing config
with open(config_path, 'r') as f:
config = json.load(f)
# Modify the speakers_file entry
update_config_speakers_file_recursive(config, speakers_path)
# Save the modified config
with open(config_path, 'w') as f:
json.dump(config, f, indent=4)
# Download models and initialize synthesizers
for info in MODEL_INFO:
model_name, model_file, config_file, repo_name = info[:4]
speaker_file = info[4] if len(info) == 5 else None # Check if speakers.pth is defined for the model
print(f"|> Downloading: {model_name}")
# Download model and config files
model_files[model_name] = hf_hub_download(repo_id=repo_name, filename=model_file, use_auth_token=TOKEN)
config_files[model_name] = hf_hub_download(repo_id=repo_name, filename=config_file, use_auth_token=TOKEN)
# Download speakers.pth if it exists
if speaker_file:
speaker_files[model_name] = hf_hub_download(repo_id=repo_name, filename=speaker_file, use_auth_token=TOKEN)
update_config_speakers_file(config_files[model_name], speaker_files[model_name]) # Update the config file
print(speaker_files[model_name])
# Initialize synthesizer for the model
synthesizer = Synthesizer(
tts_checkpoint=model_files[model_name],
tts_config_path=config_files[model_name],
tts_speakers_file=speaker_files[model_name], # Pass the speakers.pth file if it exists
use_cuda=False # Assuming you don't want to use GPU, adjust if needed
)
elif speaker_file is None:
# Initialize synthesizer for the model
synthesizer = Synthesizer(
tts_checkpoint=model_files[model_name],
tts_config_path=config_files[model_name],
# tts_speakers_file=speaker_files.get(model_name, None), # Pass the speakers.pth file if it exists
use_cuda=False # Assuming you don't want to use GPU, adjust if needed
)
synthesizers[model_name] = synthesizer
#def synthesize(text: str, model_name: str, speaker_name="speaker-0") -> str:
def synthesize(text: str, model_name: str, speaker_name=None) -> str:
"""Synthesize speech using the selected model."""
if len(text) > MAX_TXT_LEN:
text = text[:MAX_TXT_LEN]
print(f"Input text was cut off as it exceeded the {MAX_TXT_LEN} character limit.")
# Use the synthesizer object for the selected model
synthesizer = synthesizers[model_name]
if synthesizer is None:
raise NameError("Model not found")
if synthesizer.tts_speakers_file is "":
wavs = synthesizer.tts(text)
elif synthesizer.tts_speakers_file is not "":
if speaker_name == "":
#wavs = synthesizer.tts(text, speaker_name="speaker-0") ## should change, better if gradio conditions are figure out.
wavs = synthesizer.tts(text, speaker_name=None)
else:
wavs = synthesizer.tts(text, speaker_name=speaker_name)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
synthesizer.save_wav(wavs, fp)
return fp.name
# Callback function to update UI based on the selected model
def update_options(model_name):
synthesizer = synthesizers[model_name]
# if synthesizer.tts.is_multi_speaker:
if model_name is MODEL_NAMES[1]:
speakers = synthesizer.tts_model.speaker_manager.speaker_names
# return options for the dropdown
return speakers
else:
# return empty options if not multi-speaker
return []
# Create Gradio interface
iface = gr.Interface(
fn=synthesize,
inputs=[
gr.Textbox(label="Enter Text to Synthesize:", value="زین همرهان سست عناصر، دلم گرفت."),
gr.Radio(label="Pick a Model", choices=MODEL_NAMES, value=MODEL_NAMES[0], type="value"),
#gr.Dropdown(label="Select Speaker", choices=update_options(MODEL_NAMES[1]), type="value", default="speaker-0")
gr.Dropdown(label="Select Speaker", choices=update_options(MODEL_NAMES[1]), type="value", default=None)
],
outputs=gr.Audio(label="Output", type='filepath'),
examples=[["زین همرهان سست عناصر، دلم گرفت.", MODEL_NAMES[0], ""]], # Example should include a speaker name for multispeaker models
title='Persian TTS Playground',
description="""
### Persian text to speech model demo.
#### Pick a speaker for MultiSpeaker models. (for single speaker go for speaker-0)
{badges}
""",
badges = """
<div style="display: flex">
<span style="margin-right: 5px">
[ ![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white) ](https://github.com/UNHSAILLab/Persian-TTS)
</span>
</div>
""",
article="",
live=False
)
iface.launch()
|