Skip to content

Commit d3b9e4d

Browse files
committed
fix: handle exceptions directly in UI scripts
- inference - tts Initially attempted to move error handling to core.py, but overlooked that the existing code already handles exception propagation. Relocating capture to inference.py and tts.py for proper UI feedback.
1 parent 464c135 commit d3b9e4d

4 files changed

Lines changed: 54 additions & 48 deletions

File tree

core.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import os
2-
import sys
3-
import json
41
import argparse
2+
import json
3+
import os
54
import subprocess
6-
from functools import lru_cache
5+
import sys
76
import traceback
87
from distutils.util import strtobool
8+
from functools import lru_cache
99

1010
now_dir = os.getcwd()
1111
sys.path.append(now_dir)
1212

1313
current_script_directory = os.path.dirname(os.path.realpath(__file__))
1414
logs_path = os.path.join(current_script_directory, "logs")
1515

16-
from rvc.lib.tools.prerequisites_download import prequisites_download_pipeline
17-
from rvc.train.process.model_blender import model_blender
18-
from rvc.train.process.model_information import model_information
1916
from rvc.lib.tools.analyzer import analyze_audio
2017
from rvc.lib.tools.launch_tensorboard import launch_tensorboard_pipeline
2118
from rvc.lib.tools.model_download import model_download_pipeline
19+
from rvc.lib.tools.prerequisites_download import prequisites_download_pipeline
20+
from rvc.train.process.model_blender import model_blender
21+
from rvc.train.process.model_information import model_information
2222

2323
python = sys.executable
2424

@@ -122,8 +122,6 @@ def run_infer_script(
122122
"index_rate": index_rate,
123123
"protect": protect,
124124
"f0_method": f0_method,
125-
"pth_path": pth_path,
126-
"index_path": index_path,
127125
"split_audio": split_audio,
128126
"f0_autotune": f0_autotune,
129127
"f0_autotune_strength": f0_autotune_strength,
@@ -175,18 +173,11 @@ def run_infer_script(
175173
"delay_mix": delay_mix,
176174
"sid": sid,
177175
}
178-
try:
179-
infer_pipeline = import_voice_converter()
180-
infer_pipeline.convert_audio(**kwargs)
181-
return f"File {input_path} inferred successfully.", output_path.replace(
182-
".wav", f".{export_format.lower()}"
183-
)
184-
except Exception:
185-
traceback.print_exc()
186-
return (
187-
"An error occurred during audio conversion. Check the console for details.",
188-
None,
189-
)
176+
infer_pipeline = import_voice_converter()
177+
infer_pipeline.convert_audio(**kwargs)
178+
return f"File {input_path} inferred successfully.", output_path.replace(
179+
".wav", f".{export_format.lower()}"
180+
)
190181

191182

192183
# Batch infer
@@ -261,8 +252,6 @@ def run_batch_infer_script(
261252
"volume_envelope": volume_envelope,
262253
"protect": protect,
263254
"f0_method": f0_method,
264-
"pth_path": pth_path,
265-
"index_path": index_path,
266255
"split_audio": split_audio,
267256
"f0_autotune": f0_autotune,
268257
"f0_autotune_strength": f0_autotune_strength,
@@ -314,13 +303,9 @@ def run_batch_infer_script(
314303
"delay_mix": delay_mix,
315304
"sid": sid,
316305
}
317-
try:
318-
infer_pipeline = import_voice_converter()
319-
infer_pipeline.convert_audio_batch(**kwargs)
320-
return f"Files from {input_folder} inferred successfully."
321-
except Exception:
322-
traceback.print_exc()
323-
return "An error occurred during audio batch conversion. Check the console for details."
306+
infer_pipeline = import_voice_converter()
307+
infer_pipeline.convert_audio_batch(**kwargs)
308+
return f"Files from {input_folder} inferred successfully."
324309

325310

326311
# TTS
@@ -371,7 +356,7 @@ def run_tts_script(
371356
],
372357
),
373358
]
374-
subprocess.run(command_tts)
359+
subprocess.run(command_tts, check=True)
375360
infer_pipeline = import_voice_converter()
376361
infer_pipeline.convert_audio(
377362
pitch=pitch,
@@ -397,7 +382,7 @@ def run_tts_script(
397382
formant_shifting=None,
398383
formant_qfrency=None,
399384
formant_timbre=None,
400-
post_process=None,
385+
post_process=False,
401386
reverb=None,
402387
pitch_shift=None,
403388
limiter=None,

rvc/lib/tools/tts.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
import sys
21
import asyncio
3-
import edge_tts
42
import os
5-
3+
import sys
4+
import edge_tts
65

76
async def main():
8-
# Parse command line arguments
97
tts_file = str(sys.argv[1])
108
text = str(sys.argv[2])
119
voice = str(sys.argv[3])
1210
rate = int(sys.argv[4])
1311
output_file = str(sys.argv[5])
1412

1513
rates = f"+{rate}%" if rate >= 0 else f"{rate}%"
14+
1615
if tts_file and os.path.exists(tts_file):
17-
text = ""
1816
try:
1917
with open(tts_file, "r", encoding="utf-8") as file:
2018
text = file.read()
21-
except UnicodeDecodeError:
19+
except:
2220
with open(tts_file, "r") as file:
2321
text = file.read()
24-
await edge_tts.Communicate(text, voice, rate=rates).save(output_file)
25-
# print(f"TTS with {voice} completed. Output TTS file: '{output_file}'")
22+
23+
try:
24+
await edge_tts.Communicate(text, voice, rate=rates).save(output_file)
25+
except Exception:
26+
sys.exit(1)
2627

2728

2829
if __name__ == "__main__":

tabs/inference/inference.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import shutil
55
import sys
6+
import traceback
67

78
import gradio as gr
89
import regex as re
@@ -474,7 +475,7 @@ def get_speakers_id(model):
474475
return list(range(speakers_id))
475476
else:
476477
return [0]
477-
except Exception as e:
478+
except Exception:
478479
return [0]
479480
else:
480481
return [0]
@@ -1174,14 +1175,25 @@ def enforce_terms(terms_accepted, *args):
11741175
message = "You must agree to the Terms of Use to proceed."
11751176
gr.Info(message)
11761177
return message, None
1177-
return run_infer_script(*args)
1178+
try:
1179+
return run_infer_script(*args)
1180+
except Exception:
1181+
traceback.print_exc()
1182+
return (
1183+
"An error occurred during audio conversion. Check the console for details.",
1184+
None,
1185+
)
11781186

11791187
def enforce_terms_batch(terms_accepted, *args):
11801188
if not terms_accepted:
11811189
message = "You must agree to the Terms of Use to proceed."
11821190
gr.Info(message)
11831191
return message
1184-
return run_batch_infer_script(*args)
1192+
try:
1193+
return run_batch_infer_script(*args)
1194+
except Exception:
1195+
traceback.print_exc()
1196+
return "An error occurred during audio batch conversion. Check the console for details."
11851197

11861198
terms_checkbox = gr.Checkbox(
11871199
label=i18n("I agree to the terms of use"),

tabs/tts/tts.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import random
44
import sys
5+
import traceback
56

67
import gradio as gr
78

@@ -10,19 +11,19 @@
1011

1112
from assets.i18n.i18n import I18nAuto
1213
from core import run_tts_script
13-
from tabs.settings.sections.filter import get_filter_trigger, load_config_filter
1414
from tabs.inference.inference import (
1515
change_choices,
1616
create_folder_and_move_files,
17+
default_weight,
18+
extract_model_and_epoch,
19+
filter_dropdowns,
1720
get_files,
1821
get_speakers_id,
1922
match_index,
2023
refresh_embedders_folders,
21-
extract_model_and_epoch,
22-
default_weight,
23-
filter_dropdowns,
2424
update_filter_visibility,
2525
)
26+
from tabs.settings.sections.filter import get_filter_trigger, load_config_filter
2627

2728
i18n = I18nAuto()
2829

@@ -357,7 +358,14 @@ def enforce_terms(terms_accepted, *args):
357358
message = "You must agree to the Terms of Use to proceed."
358359
gr.Info(message)
359360
return message, None
360-
return run_tts_script(*args)
361+
try:
362+
return run_tts_script(*args)
363+
except Exception:
364+
traceback.print_exc()
365+
return (
366+
"An error occurred during TTS conversion. Check the console for details.",
367+
None,
368+
)
361369

362370
terms_checkbox = gr.Checkbox(
363371
label=i18n("I agree to the terms of use"),

0 commit comments

Comments
 (0)