Spaces:
Running on Zero
Running on Zero
Commit ·
929e40e
1
Parent(s): 7409072
Harden composer + share video against the failure modes in production logs (#8)
Browse files- Harden composer + share video against the failure modes in production logs (cd3441a060463372f41524652bd6444e08b32ac4)
495 unmodified lines4964974984994995005015023 unmodified lines5065075085095105115095105115125135145155165175185195205215225235245255265275125135145155165175185195205215225235245255265275285295305315325335345355365375385395403 unmodified lines544545546537547548549550551552553554436 unmodified lines9919929939949959809969979989991000100169 unmodified lines10711072107310561074107510761077107810791080464 unmodified lines15451546154715481549155015512 unmodified lines1554155515561557155815591560495 unmodified linesAnswer with ONLY a JSON object with keys: global_metadata, vocal_details, arrangement."""
def _llm_json(system, user):def _llm_json(system, user, required=()): import json as _json
from openai import OpenAI3 unmodified lines last_error = None # Three DISTINCT providers, all verified enabled for this account (bare/":fastest" can route to # together, which 403s here and killed the fallbacks). Timeouts sized to measured composer latency. # Two passes over the chain: under load every provider can 429 transiently, and a second pass a few # seconds later usually lands. for attempt in range(2): for model, timeout in ( ("deepseek-ai/DeepSeek-V4-Flash-0731:baseten", 45), ("deepseek-ai/DeepSeek-V4-Flash-0731:deepinfra", 75), ("deepseek-ai/DeepSeek-V4-Flash-0731:novita", 100), ): try: completion = client.with_options(timeout=timeout).chat.completions.create( model=model, messages=[{"role": "system", "content": system}, {"role": "user", "content": user}], ) text = completion.choices[0].message.content or "" # Tolerate fences/preambles and reject truncated replies: parse the outermost {...} span. start, end = text.find("{"), text.rfind("}") if start == -1 or end <= start: raise ValueError(f"no JSON object in composer reply (finish_reason={completion.choices[0].finish_reason})") return _json.loads(text[start : end + 1]) except Exception as e: print(f"composer attempt failed ({model}): {type(e).__name__}: {e}", flush=True) last_error = e for model, timeout in ( ("deepseek-ai/DeepSeek-V4-Flash-0731:baseten", 45), ("deepseek-ai/DeepSeek-V4-Flash-0731:deepinfra", 75), ("deepseek-ai/DeepSeek-V4-Flash-0731:novita", 100), ): try: completion = client.with_options(timeout=timeout).chat.completions.create( model=model, messages=[{"role": "system", "content": system}, {"role": "user", "content": user}], ) text = completion.choices[0].message.content or "" # Tolerate fences/preambles and reject truncated replies: parse the outermost {...} span. start, end = text.find("{"), text.rfind("}") if start == -1 or end <= start: raise ValueError(f"no JSON object in composer reply (finish_reason={completion.choices[0].finish_reason})") data = _json.loads(text[start : end + 1]) # Valid JSON with the wrong shape must retry too, not KeyError later. missing = [key for key in required if key not in data] if missing: raise ValueError(f"composer reply missing keys: {missing}") return data except Exception as e: print(f"composer attempt failed ({model}, pass {attempt + 1}): {type(e).__name__}: {e}", flush=True) last_error = e if attempt == 0: time.sleep(3) raise gr.Error( "The composer model is overloaded right now — try again in a moment, " "or write the lyrics and structured prompt directly in the Studio tab."3 unmodified linesdef compose_song(description, duration): if not description.strip(): raise gr.Error("Describe the song you want first.") data = _llm_json(_COMPOSER_SYSTEM, f"Song description: {description}\nTarget duration: {int(duration)} seconds.") data = _llm_json( _COMPOSER_SYSTEM, f"Song description: {description}\nTarget duration: {int(duration)} seconds.", required=("lyrics", "global_metadata", "vocal_details", "arrangement"), ) return data["lyrics"], data["global_metadata"], data["vocal_details"], data["arrangement"]
436 unmodified lines
import scipy.io.wavfile
title = " ".join((title or "").split())[:96] or "MiniMax Music 3" try: sr, wave = scipy.io.wavfile.read(wav_path) sr, wave = scipy.io.wavfile.read(wav_path) except (FileNotFoundError, OSError): return gr.skip() # the visitor left and gradio cleaned the cached wav mono = wave.astype(np.float32).mean(axis=1) / 32768.0 fps, size, bars = 24, 720, 56 total_frames = int(len(mono) / sr * fps)69 unmodified lines if len(chunk) < window: chunk = np.pad(chunk, (0, window - len(chunk))) spectrum = np.abs(np.fft.rfft(chunk * np.hanning(window))) levels = np.array([spectrum[(freqs >= band_edges[b]) & (freqs < band_edges[b + 1])].mean() for b in range(bars)]) levels = np.array([ spectrum[m].mean() if (m := (freqs >= band_edges[b]) & (freqs < band_edges[b + 1])).any() else 0.0 for b in range(bars) ]) levels = np.log1p(12 * np.nan_to_num(levels)) smooth = np.maximum(levels, smooth * 0.85) frame = base.copy()464 unmodified lines f"Current structured prompt, keep the lyrics coherent with it:\n" f"Global metadata: {state['global_meta']}\nVocal details: {state['vocals']}\n" f"Arrangement: {state['arrangement']}\nTarget duration: {int(duration)} seconds.", required=("lyrics",), ) state["lyrics"] = data["lyrics"] return state, "Lyrics written — tweak them, or press Generate."2 unmodified lines _PROMPT_SYSTEM, f"Sound instruction: {instruction or description or '(none — describe a sound that fits the lyrics)'}\n" f"Current lyrics, keep the structured prompt coherent with them:\n{state['lyrics']}", required=("global_metadata", "vocal_details", "arrangement"), ) state.update(global_meta=data["global_metadata"], vocals=data["vocal_details"], arrangement=data["arrangement"]) return state, "Structured prompt written — tweak it, or press Generate."app.py+46-23