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 lines4964974984995005015023 unmodified lines5065075085095105115125135145155165175185195205215225235245255265275285295303 unmodified lines534535536537538539540436 unmodified lines97797897998098198298369 unmodified lines1053105410551056105710581059464 unmodified lines1524152515261527152815292 unmodified lines153215331534153515361537495 unmodified linesAnswer with ONLY a JSON object with keys: global_metadata, vocal_details, arrangement."""def _llm_json(system, user):import json as _jsonfrom openai import OpenAI3 unmodified lineslast_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.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 = eraise 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.")return data["lyrics"], data["global_metadata"], data["vocal_details"], data["arrangement"]436 unmodified linesimport scipy.io.wavfilesr, wave = scipy.io.wavfile.read(wav_path)mono = wave.astype(np.float32).mean(axis=1) / 32768.0fps, size, bars = 24, 720, 56total_frames = int(len(mono) / sr * fps)69 unmodified linesif 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.log1p(12 * np.nan_to_num(levels))smooth = np.maximum(levels, smooth * 0.85)frame = base.copy()464 unmodified linesf"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.",)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']}",)state.update(global_meta=data["global_metadata"], vocals=data["vocal_details"], arrangement=data["arrangement"])return state, "Structured prompt written — tweak it, or press Generate."495 unmodified lines4964974984995005015023 unmodified lines5065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395403 unmodified lines544545546547548549550551552553554436 unmodified lines9919929939949959969979989991000100169 unmodified lines1071107210731074107510761077107810791080464 unmodified lines15451546154715481549155015512 unmodified lines1554155515561557155815591560495 unmodified linesAnswer with ONLY a JSON object with keys: global_metadata, vocal_details, arrangement."""def _llm_json(system, user, required=()):import json as _jsonfrom openai import OpenAI3 unmodified lineslast_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})")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 dataexcept Exception as e:print(f"composer attempt failed ({model}, pass {attempt + 1}): {type(e).__name__}: {e}", flush=True)last_error = eif 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.",required=("lyrics", "global_metadata", "vocal_details", "arrangement"),)return data["lyrics"], data["global_metadata"], data["vocal_details"], data["arrangement"]436 unmodified linesimport scipy.io.wavfiletitle = " ".join((title or "").split())[:96] or "MiniMax Music 3"try:sr, wave = scipy.io.wavfile.read(wav_path)except (FileNotFoundError, OSError):return gr.skip() # the visitor left and gradio cleaned the cached wavmono = wave.astype(np.float32).mean(axis=1) / 32768.0fps, size, bars = 24, 720, 56total_frames = int(len(mono) / sr * fps)69 unmodified linesif 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[m].mean() if (m := (freqs >= band_edges[b]) & (freqs < band_edges[b + 1])).any() else 0.0for 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 linesf"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