Spaces:
Paused
Paused
Upload 6 files
Browse files
app.py
CHANGED
|
@@ -397,19 +397,30 @@ def _post_uvr_multipart(worker_url: str, path: str, input_path: Path, model: str
|
|
| 397 |
raise RuntimeError(f"UVR worker {path} returned HTTP {exc.code}: {detail}") from exc
|
| 398 |
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
def _post_uvr_job(worker_url: str, input_path: Path, model: str, mode: str) -> dict:
|
| 401 |
-
|
| 402 |
-
return json.loads(payload.decode("utf-8", errors="replace"))
|
| 403 |
|
| 404 |
|
| 405 |
-
def
|
| 406 |
-
try:
|
| 407 |
-
payload = _post_uvr_multipart(worker_url, "/separate", input_path, model, mode)
|
| 408 |
-
except RuntimeError as exc:
|
| 409 |
-
if "HTTP 404" in str(exc) or "HTTP 405" in str(exc):
|
| 410 |
-
return None
|
| 411 |
-
raise
|
| 412 |
zip_path = work_dir / "uvr-worker-direct-stems.zip"
|
|
|
|
|
|
|
| 413 |
zip_path.write_bytes(payload)
|
| 414 |
output_dir = work_dir / "uvr-worker-direct-separated"
|
| 415 |
output_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -426,12 +437,26 @@ def _try_uvr_separate(worker_url: str, input_path: Path, work_dir: Path, model:
|
|
| 426 |
}
|
| 427 |
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
def _try_neuralis_uvr_worker(input_path: Path, work_dir: Path, model: str, mode: str, worker_url: str) -> dict | None:
|
| 430 |
try:
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
if not job_id:
|
| 434 |
-
|
| 435 |
deadline = time.time() + 60 * 30
|
| 436 |
status = job
|
| 437 |
while time.time() < deadline:
|
|
|
|
| 397 |
raise RuntimeError(f"UVR worker {path} returned HTTP {exc.code}: {detail}") from exc
|
| 398 |
|
| 399 |
|
| 400 |
+
def _extract_uvr_job_id(value) -> str:
|
| 401 |
+
if not isinstance(value, dict):
|
| 402 |
+
return ""
|
| 403 |
+
for key in ("id", "jobId", "job_id"):
|
| 404 |
+
if value.get(key):
|
| 405 |
+
return str(value.get(key))
|
| 406 |
+
nested = value.get("job")
|
| 407 |
+
if isinstance(nested, dict):
|
| 408 |
+
return _extract_uvr_job_id(nested)
|
| 409 |
+
return ""
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
def _post_uvr_job_payload(worker_url: str, input_path: Path, model: str, mode: str) -> bytes:
|
| 413 |
+
return _post_uvr_multipart(worker_url, "/jobs", input_path, model, mode)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
def _post_uvr_job(worker_url: str, input_path: Path, model: str, mode: str) -> dict:
|
| 417 |
+
return json.loads(_post_uvr_job_payload(worker_url, input_path, model, mode).decode("utf-8", errors="replace"))
|
|
|
|
| 418 |
|
| 419 |
|
| 420 |
+
def _prepare_uvr_zip_payload(payload: bytes, work_dir: Path, name: str = "uvr-worker-stems.zip") -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
zip_path = work_dir / "uvr-worker-direct-stems.zip"
|
| 422 |
+
if name:
|
| 423 |
+
zip_path = work_dir / name
|
| 424 |
zip_path.write_bytes(payload)
|
| 425 |
output_dir = work_dir / "uvr-worker-direct-separated"
|
| 426 |
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 437 |
}
|
| 438 |
|
| 439 |
|
| 440 |
+
def _try_uvr_separate(worker_url: str, input_path: Path, work_dir: Path, model: str, mode: str) -> dict | None:
|
| 441 |
+
try:
|
| 442 |
+
payload = _post_uvr_multipart(worker_url, "/separate", input_path, model, mode)
|
| 443 |
+
except RuntimeError as exc:
|
| 444 |
+
if "HTTP 404" in str(exc) or "HTTP 405" in str(exc):
|
| 445 |
+
return None
|
| 446 |
+
raise
|
| 447 |
+
return _prepare_uvr_zip_payload(payload, work_dir)
|
| 448 |
+
|
| 449 |
+
|
| 450 |
def _try_neuralis_uvr_worker(input_path: Path, work_dir: Path, model: str, mode: str, worker_url: str) -> dict | None:
|
| 451 |
try:
|
| 452 |
+
payload = _post_uvr_job_payload(worker_url, input_path, model, mode)
|
| 453 |
+
try:
|
| 454 |
+
job = json.loads(payload.decode("utf-8", errors="replace"))
|
| 455 |
+
except json.JSONDecodeError:
|
| 456 |
+
return _prepare_uvr_zip_payload(payload, work_dir, "uvr-worker-jobs-response.zip")
|
| 457 |
+
job_id = _extract_uvr_job_id(job)
|
| 458 |
if not job_id:
|
| 459 |
+
raise RuntimeError(f"UVR worker /jobs returned no id: {json.dumps(job, ensure_ascii=True)[:700]}")
|
| 460 |
deadline = time.time() + 60 * 30
|
| 461 |
status = job
|
| 462 |
while time.time() < deadline:
|