_ui.py import torchaudio safety check (#9234)

* Added safety around torchaudio import in _ui.py

* Trusted cursor too much, fixed torchaudio bool
This commit is contained in:
Jedrzej Kosinski 2025-08-07 14:54:09 -07:00 committed by GitHub
parent 05df2df489
commit 42974a448c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,11 @@ from typing import Type
import av
import numpy as np
import torch
import torchaudio
try:
import torchaudio
TORCH_AUDIO_AVAILABLE = True
except ImportError:
TORCH_AUDIO_AVAILABLE = False
from PIL import Image as PILImage
from PIL.PngImagePlugin import PngInfo
@ -302,6 +306,8 @@ class AudioSaveHelper:
# Resample if necessary
if sample_rate != audio["sample_rate"]:
if not TORCH_AUDIO_AVAILABLE:
raise Exception("torchaudio is not available; cannot resample audio.")
waveform = torchaudio.functional.resample(waveform, audio["sample_rate"], sample_rate)
# Create output with specified format