Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
backend/spike_corpus/*.wav filter=lfs diff=lfs merge=lfs -text
backend/runtime-locks/*.txt text eol=lf
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ jobs:
run: >-
uv run --frozen --only-group ci python -m pytest
tests/test_sa3.py
tests/test_sa3_audio.py
tests/test_sa3_manifest.py
tests/test_models.py::test_readiness_classifies_a_checkout
tests/test_models.py::test_readiness_missing_when_no_checkout

Expand Down
57 changes: 26 additions & 31 deletions backend/lsdj/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import argparse
import asyncio
import contextlib
import io
import json
import logging
import math
import multiprocessing as mp
import os
import queue
import time
import wave

import uvicorn
from fastapi import FastAPI, HTTPException, Request
Expand Down Expand Up @@ -234,34 +232,11 @@ def _generation_number(
return float(value)


def _validate_init_wav(data: bytes) -> None:
def _normalize_init_wav(data: bytes) -> bytes:
try:
with wave.open(io.BytesIO(data), "rb") as source:
channels = source.getnchannels()
sample_width = source.getsampwidth()
sample_rate = source.getframerate()
frames = source.getnframes()
compression = source.getcomptype()
pcm_bytes = source.readframes(frames)
except (EOFError, wave.Error):
raise HTTPException(
status_code=422, detail="'init_audio' must be a valid WAV file"
) from None
if (
compression != "NONE"
or channels not in (1, 2)
or sample_width != 2
or sample_rate != 44_100
or frames == 0
or len(pcm_bytes) != frames * channels * sample_width
):
raise HTTPException(
status_code=422,
detail=(
"'init_audio' must be non-empty 44.1 kHz 16-bit PCM WAV "
"with one or two channels"
),
)
return sa3.normalize_wav(data).wav
except sa3.AudioFormatError as error:
raise HTTPException(status_code=422, detail=f"'init_audio' {error}") from None


async def _read_init_audio(upload: UploadFile) -> bytes:
Expand All @@ -278,8 +253,7 @@ async def _read_init_audio(upload: UploadFile) -> bytes:
)
chunks.append(chunk)
data = b"".join(chunks)
_validate_init_wav(data)
return data
return _normalize_init_wav(data)


async def _read_capped_body(request: Request, limit: int, detail: str) -> bytes:
Expand Down Expand Up @@ -446,6 +420,19 @@ def _validate_generate_request(
)
options["apg"] = apg

if "steps" in parsed:
steps = parsed["steps"]
if (
isinstance(steps, bool)
or not isinstance(steps, int)
or not sa3.MIN_STEPS <= steps <= sa3.MAX_STEPS
):
raise HTTPException(
status_code=422,
detail=f"'steps' must be an integer from {sa3.MIN_STEPS}-{sa3.MAX_STEPS}",
)
options["steps"] = steps

if "negative_prompt" in parsed:
negative_prompt = parsed["negative_prompt"]
if not isinstance(negative_prompt, str):
Expand Down Expand Up @@ -675,9 +662,17 @@ async def generate_audio(request: Request) -> Response:
except sa3.GenerationFailed as error:
logger.warning("generation failed: %s", error)
raise HTTPException(status_code=502, detail=str(error)) from None
except sa3.GenerationCancelled as error:
raise HTTPException(status_code=499, detail=str(error)) from None
return Response(content=wav, media_type="audio/wav")


@app.get("/api/sa3/status")
def stable_audio_status() -> dict:
"""Selected runtime, feature matrix, limitations, and active generation."""
return sa3.status()


@app.get("/api/models")
def list_models() -> dict:
"""The downloaded models + RAM info for the deck UI's model picker and the
Expand Down
Loading
Loading