fix: don't crash the UI on output files with custom names

create_text2music_ui() sorted the saved *_input_params.json files with
int(name.split('_')[1]), which assumes the generated
output_<timestamp>_<idx>_ shape. Output names are user-controlled — via
infer.py --output_path or a save_path from the UI — so any other name
raised ValueError while the Blocks were being built and took the whole
interface down before it could start:

    ValueError: invalid literal for int() with base 10: 'base'

Sort by mtime instead. That is what "previous generated input params"
means anyway (newest first) and it works for any filename; a file that
disappears between listdir() and getmtime() sorts last rather than
raising.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Leonid Pershin
2026-09-08 20:30:58 +03:00
co-authored by Claude Opus 5
parent 0584397884
commit c7953dc4e0
+11 -1
View File
@@ -101,7 +101,17 @@ def create_text2music_ui(
if not os.path.isdir(output_file_dir): if not os.path.isdir(output_file_dir):
os.makedirs(output_file_dir, exist_ok=True) os.makedirs(output_file_dir, exist_ok=True)
json_files = [f for f in os.listdir(output_file_dir) if f.endswith('.json')] json_files = [f for f in os.listdir(output_file_dir) if f.endswith('.json')]
json_files.sort(reverse=True, key=lambda x: int(x.split('_')[1]))
def _mtime(name):
# Output filenames are user-controlled (infer.py --output_path, or the
# save_path passed from the UI), so a timestamp cannot be parsed out of
# them: doing so used to raise ValueError and take the whole UI down.
try:
return os.path.getmtime(os.path.join(output_file_dir, name))
except OSError:
return 0.0
json_files.sort(key=_mtime, reverse=True)
output_files = gr.Dropdown(choices=json_files, label="Select previous generated input params", scale=9, interactive=True) output_files = gr.Dropdown(choices=json_files, label="Select previous generated input params", scale=9, interactive=True)
load_bnt = gr.Button("Load", variant="primary", scale=1) load_bnt = gr.Button("Load", variant="primary", scale=1)