Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -71,25 +71,55 @@ def check_audio_length(audio_path, max_duration=120):
|
|
71 |
return False
|
72 |
|
73 |
def synthesize_and_convert_voice(text, language_iso, voice_audio_path, speed):
|
74 |
-
tts_synthesis = TTS(model_name=f"tts_models/{language_iso}/fairseq/vits"
|
75 |
wav_data = tts_synthesis.tts(text, speed=speed)
|
76 |
tts_conversion = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False)
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
def synthesize_speech(text, speaker_wav_path, language_iso, speed):
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
tts_conversion = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False)
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
def get_language_code(selected_language):
|
95 |
if selected_language in language_options:
|
@@ -99,24 +129,37 @@ def get_language_code(selected_language):
|
|
99 |
else:
|
100 |
return None
|
101 |
|
102 |
-
def process_speech(text,
|
103 |
language_code = get_language_code(selected_language)
|
104 |
|
105 |
if language_code is None:
|
106 |
raise ValueError("Выбранный язык не поддерживается.")
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
error_message = "Длина аудио превышает допустимый лимит в 2 минуты."
|
111 |
error = gr.Error(error_message, duration=5)
|
112 |
raise error
|
113 |
|
114 |
-
|
|
|
115 |
|
116 |
if selected_language in other_language:
|
117 |
-
|
118 |
else:
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
def restart_program():
|
122 |
python = sys.executable
|
|
|
71 |
return False
|
72 |
|
73 |
def synthesize_and_convert_voice(text, language_iso, voice_audio_path, speed):
|
74 |
+
tts_synthesis = TTS(model_name=f"tts_models/{language_iso}/fairseq/vits")
|
75 |
wav_data = tts_synthesis.tts(text, speed=speed)
|
76 |
tts_conversion = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False)
|
77 |
+
|
78 |
+
# Write wav_data to temporary file
|
79 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_tts_wav_file:
|
80 |
+
temp_tts_wav_path = temp_tts_wav_file.name
|
81 |
+
write(temp_tts_wav_path, 22050, wav_data)
|
82 |
+
|
83 |
+
# Prepare output temporary file
|
84 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_output_wav_file:
|
85 |
+
temp_output_wav_path = temp_output_wav_file.name
|
86 |
+
|
87 |
+
tts_conversion.voice_conversion_to_file(temp_tts_wav_path, target_wav=voice_audio_path,
|
88 |
+
file_path=temp_output_wav_path)
|
89 |
+
|
90 |
+
# Read converted audio from temp_output_wav_path
|
91 |
+
output_sample_rate, output_audio_data = read(temp_output_wav_path)
|
92 |
+
|
93 |
+
# Remove temporary files
|
94 |
+
os.remove(temp_tts_wav_path)
|
95 |
+
os.remove(temp_output_wav_path)
|
96 |
+
|
97 |
+
return (output_sample_rate, output_audio_data)
|
98 |
|
99 |
def synthesize_speech(text, speaker_wav_path, language_iso, speed):
|
100 |
+
# Generate speech using tts and save to temporary file
|
101 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_tts_output:
|
102 |
+
temp_tts_output_path = temp_tts_output.name
|
103 |
+
tts.tts_to_file(text=text, file_path=temp_tts_output_path, speed=speed,
|
104 |
+
speaker_wav=speaker_wav_path, language=language_iso)
|
105 |
+
|
106 |
tts_conversion = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False)
|
107 |
+
|
108 |
+
# Prepare output temporary file
|
109 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_output_wav_file:
|
110 |
+
temp_output_wav_path = temp_output_wav_file.name
|
111 |
+
|
112 |
+
tts_conversion.voice_conversion_to_file(temp_tts_output_path, target_wav=speaker_wav_path,
|
113 |
+
file_path=temp_output_wav_path)
|
114 |
+
|
115 |
+
# Read converted audio from temp_output_wav_path
|
116 |
+
output_sample_rate, output_audio_data = read(temp_output_wav_path)
|
117 |
+
|
118 |
+
# Remove temporary files
|
119 |
+
os.remove(temp_tts_output_path)
|
120 |
+
os.remove(temp_output_wav_path)
|
121 |
+
|
122 |
+
return (output_sample_rate, output_audio_data)
|
123 |
|
124 |
def get_language_code(selected_language):
|
125 |
if selected_language in language_options:
|
|
|
129 |
else:
|
130 |
return None
|
131 |
|
132 |
+
def process_speech(text, speaker_wav_path, selected_language, speed):
|
133 |
language_code = get_language_code(selected_language)
|
134 |
|
135 |
if language_code is None:
|
136 |
raise ValueError("Выбранный язык не поддерживается.")
|
137 |
|
138 |
+
if speaker_wav_path is None:
|
139 |
+
error_message = "Пожалуйста, загрузите аудио файл говорящего."
|
140 |
+
error = gr.Error(error_message, duration=5)
|
141 |
+
raise error
|
142 |
+
|
143 |
+
# Check audio length
|
144 |
+
audio = AudioSegment.from_file(speaker_wav_path)
|
145 |
+
duration = audio.duration_seconds
|
146 |
+
if duration > 120:
|
147 |
error_message = "Длина аудио превышает допустимый лимит в 2 минуты."
|
148 |
error = gr.Error(error_message, duration=5)
|
149 |
raise error
|
150 |
|
151 |
+
# Clean audio
|
152 |
+
cleaned_wav_path = clean_audio(speaker_wav_path)
|
153 |
|
154 |
if selected_language in other_language:
|
155 |
+
output_audio_data = synthesize_and_convert_voice(text, language_code, cleaned_wav_path, speed)
|
156 |
else:
|
157 |
+
output_audio_data = synthesize_speech(text, cleaned_wav_path, language_code, speed)
|
158 |
+
|
159 |
+
# Remove temporary files
|
160 |
+
os.remove(cleaned_wav_path)
|
161 |
+
|
162 |
+
return output_audio_data
|
163 |
|
164 |
def restart_program():
|
165 |
python = sys.executable
|