Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
mariesig commited on
Commit ·
0e1fd79
1
Parent(s): 32a0164
handle various input types e.g across different gradio versions
Browse files- offline_pipeline.py +24 -3
offline_pipeline.py
CHANGED
|
@@ -19,6 +19,26 @@ from utils import (
|
|
| 19 |
SDK_OFFLINE = SDKWrapper()
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def _safe_progress(progress: gr.Progress, value: float, desc: str) -> None:
|
| 23 |
progress(max(0.0, min(1.0, value)), desc=desc)
|
| 24 |
|
|
@@ -236,15 +256,16 @@ def run_offline_pipeline(
|
|
| 236 |
|
| 237 |
|
| 238 |
def load_local_file(
|
| 239 |
-
sample_path:
|
| 240 |
normalize: bool = True,
|
| 241 |
) -> tuple[np.ndarray | None, str, tuple | None, int | None]:
|
|
|
|
| 242 |
if not sample_path or not os.path.exists(sample_path):
|
| 243 |
return None, "", None, None
|
| 244 |
|
| 245 |
if os.path.getsize(sample_path) > 5 * 1024 * 1024:
|
| 246 |
gr.Warning("File size exceeds 5 MB limit. Please upload a smaller file.")
|
| 247 |
-
|
| 248 |
|
| 249 |
new_sample_stem = os.path.splitext(os.path.basename(sample_path))[0]
|
| 250 |
y, sample_rate = librosa.load(sample_path, sr=None, mono=True)
|
|
@@ -274,4 +295,4 @@ def load_file_from_dataset(
|
|
| 274 |
if y.ndim > 1:
|
| 275 |
y = np.mean(y, axis=0)
|
| 276 |
gradio_audio = to_gradio_audio(y, sample_rate)
|
| 277 |
-
return gradio_audio, y, new_sample_stem, sample_rate
|
|
|
|
| 19 |
SDK_OFFLINE = SDKWrapper()
|
| 20 |
|
| 21 |
|
| 22 |
+
def _extract_uploaded_path(sample_input: Any) -> str | None:
|
| 23 |
+
if sample_input is None:
|
| 24 |
+
return None
|
| 25 |
+
if isinstance(sample_input, str):
|
| 26 |
+
return sample_input
|
| 27 |
+
|
| 28 |
+
for attr in ("path", "name"):
|
| 29 |
+
value = getattr(sample_input, attr, None)
|
| 30 |
+
if isinstance(value, str):
|
| 31 |
+
return value
|
| 32 |
+
|
| 33 |
+
if isinstance(sample_input, dict):
|
| 34 |
+
for key in ("path", "name"):
|
| 35 |
+
value = sample_input.get(key)
|
| 36 |
+
if isinstance(value, str):
|
| 37 |
+
return value
|
| 38 |
+
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
def _safe_progress(progress: gr.Progress, value: float, desc: str) -> None:
|
| 43 |
progress(max(0.0, min(1.0, value)), desc=desc)
|
| 44 |
|
|
|
|
| 256 |
|
| 257 |
|
| 258 |
def load_local_file(
|
| 259 |
+
sample_path: Any,
|
| 260 |
normalize: bool = True,
|
| 261 |
) -> tuple[np.ndarray | None, str, tuple | None, int | None]:
|
| 262 |
+
sample_path = _extract_uploaded_path(sample_path)
|
| 263 |
if not sample_path or not os.path.exists(sample_path):
|
| 264 |
return None, "", None, None
|
| 265 |
|
| 266 |
if os.path.getsize(sample_path) > 5 * 1024 * 1024:
|
| 267 |
gr.Warning("File size exceeds 5 MB limit. Please upload a smaller file.")
|
| 268 |
+
return None, "", None, None
|
| 269 |
|
| 270 |
new_sample_stem = os.path.splitext(os.path.basename(sample_path))[0]
|
| 271 |
y, sample_rate = librosa.load(sample_path, sr=None, mono=True)
|
|
|
|
| 295 |
if y.ndim > 1:
|
| 296 |
y = np.mean(y, axis=0)
|
| 297 |
gradio_audio = to_gradio_audio(y, sample_rate)
|
| 298 |
+
return gradio_audio, y, new_sample_stem, sample_rate
|