Scenario
Full pipeline from creative brief to broadcast-ready audio. Generate ad script variations with Mavera, produce audio with ElevenLabs, then assess quality with Mave Agent — iterating voice settings until production standards are met. Flow: MaveraPOST /generations (ad scripts) → ElevenLabs TTS (voice settings) → Mavera POST /mave/chat (quality feedback) → Adjust → Iterate
Code
import os, requests, time, re
EL_KEY = os.environ["ELEVENLABS_API_KEY"]
EL_BASE = "https://api.elevenlabs.io/v1"
MV = os.environ["MAVERA_API_KEY"]
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
os.makedirs("ad_production", exist_ok=True)
VOICE_ID, QUALITY_THRESHOLD = "21m00Tcm4TlvDq8ikWAM", 8
# 1. Generate ad script variations
gen = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"prompt": "Write 3 variations of a 30-second radio ad for 'Beacon' AI marketing analytics. "
"Target: marketing directors. Confident tone. Each: hook, proof point, CTA.\n"
"VARIATION A: problem-focused. VARIATION B: outcome-focused. VARIATION C: story-focused.",
}).json()
script_text = gen.get("output") or gen.get("content") or ""
variations = [v.strip() for v in re.split(r"VARIATION\s+[A-C]", script_text) if v.strip()]
print(f"Generated {len(variations)} variations")
results = []
for vi, script in enumerate(variations[:3]):
label = chr(65 + vi)
settings = {"stability": 0.5 + vi * 0.05, "similarity_boost": 0.75 + vi * 0.05,
"style": 0.3 + vi * 0.1, "use_speaker_boost": True}
best_score, best_path = 0, None
for take in range(1, 4):
# 2. Generate audio
tts = requests.post(f"{EL_BASE}/text-to-speech/{VOICE_ID}",
headers={"xi-api-key": EL_KEY, "Content-Type": "application/json"},
json={"text": script, "model_id": "eleven_multilingual_v2", "voice_settings": settings})
if tts.status_code != 200:
print(f" TTS error {tts.status_code}"); time.sleep(3); continue
path = f"ad_production/var_{label}_take_{take}.mp3"
with open(path, "wb") as f: f.write(tts.content)
print(f"Var {label} Take {take}: {path} ({len(tts.content) // 1024} KB)")
time.sleep(2)
# 3. Quality assessment
quality = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"Audio ad quality assessor. Variation {label}, Take {take}.\n"
f"SCRIPT:\n{script}\nSETTINGS: {settings}\n\n"
"Score 1-10: PACING, HOOK STRENGTH, CLARITY, EMOTIONAL IMPACT, CTA EFFECTIVENESS.\n"
"OVERALL SCORE (1-10). If below 8, suggest adjusted stability/similarity_boost/style values.",
}).json()
feedback = quality.get("content", "")
score = int(m[1]) if (m := re.search(r"OVERALL[^0-9]*(\d+)", feedback, re.I)) else 0
print(f" Score: {score}/10")
if score > best_score: best_score, best_path = score, path
if score >= QUALITY_THRESHOLD: print(" Production-ready!"); break
# 4. Adjust voice settings from feedback
for key, pat in [("stability", r"stability[:\s]*([\d.]+)"),
("similarity_boost", r"similarity[_ ]?boost[:\s]*([\d.]+)"),
("style", r"style[:\s]*([\d.]+)")]:
if m := re.search(pat, feedback, re.I):
settings[key] = min(1.0, max(0.0, float(m[1])))
time.sleep(2)
results.append({"var": label, "score": best_score, "path": best_path})
print(f"\n{'='*60}\nPRODUCTION SUMMARY\n{'='*60}")
for r in results:
status = "APPROVED" if r["score"] >= QUALITY_THRESHOLD else "NEEDS REVIEW"
print(f" Variation {r['var']}: {r['score']}/10 — {status} — {r['path']}")
winner = max(results, key=lambda r: r["score"])
print(f"\nRecommended: Variation {winner['var']} ({winner['score']}/10)")
import fs from "fs";
const EL_KEY = process.env.ELEVENLABS_API_KEY;
const EL_BASE = "https://api.elevenlabs.io/v1";
const MV = process.env.MAVERA_API_KEY;
const MV_BASE = "https://app.mavera.io/api/v1";
const MV_H = { Authorization: `Bearer ${MV}`, "Content-Type": "application/json" };
fs.mkdirSync("ad_production", { recursive: true });
const VOICE_ID = "21m00Tcm4TlvDq8ikWAM";
const gen = await fetch(`${MV_BASE}/generations`, { method: "POST", headers: MV_H,
body: JSON.stringify({ prompt: "Write 3 variations of a 30-second radio ad for 'Beacon' AI analytics. "
+ "Target: marketing directors. Label: VARIATION A (problem), B (outcome), C (story)." }),
}).then(r => r.json());
const variations = (gen.output || gen.content || "").split(/VARIATION\s+[A-C]/).filter(v => v.trim());
const results = [];
for (let vi = 0; vi < Math.min(3, variations.length); vi++) {
const label = String.fromCharCode(65 + vi), script = variations[vi].trim();
let settings = { stability: 0.5 + vi * 0.05, similarity_boost: 0.75 + vi * 0.05,
style: 0.3 + vi * 0.1, use_speaker_boost: true };
let bestScore = 0, bestPath = null;
for (let take = 1; take <= 3; take++) {
const tts = await fetch(`${EL_BASE}/text-to-speech/${VOICE_ID}`, { method: "POST",
headers: { "xi-api-key": EL_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ text: script, model_id: "eleven_multilingual_v2", voice_settings: settings }),
});
if (!tts.ok) { await new Promise(r => setTimeout(r, 3000)); continue; }
const buf = Buffer.from(await tts.arrayBuffer());
const outPath = `ad_production/var_${label}_take_${take}.mp3`;
fs.writeFileSync(outPath, buf);
console.log(`Var ${label} Take ${take}: ${Math.round(buf.length / 1024)} KB`);
await new Promise(r => setTimeout(r, 2000));
const quality = await fetch(`${MV_BASE}/mave/chat`, { method: "POST", headers: MV_H,
body: JSON.stringify({ message: `Ad assessor. Var ${label}, Take ${take}.\nSCRIPT:\n${script}\n`
+ `SETTINGS: ${JSON.stringify(settings)}\nScore 1-10: PACING, HOOK, CLARITY, IMPACT, CTA. `
+ `OVERALL SCORE. If <8, suggest stability/similarity_boost/style values.` }),
}).then(r => r.json());
const feedback = quality.content || "";
const score = parseInt((feedback.match(/OVERALL[^0-9]*(\d+)/i) || [])[1] || "0");
console.log(` Score: ${score}/10`);
if (score > bestScore) { bestScore = score; bestPath = outPath; }
if (score >= 8) { console.log(" Production-ready!"); break; }
const sm = feedback.match(/stability[:\s]*([\d.]+)/i);
const sbm = feedback.match(/similarity[_ ]?boost[:\s]*([\d.]+)/i);
const stm = feedback.match(/style[:\s]*([\d.]+)/i);
if (sm) settings.stability = Math.min(1, Math.max(0, parseFloat(sm[1])));
if (sbm) settings.similarity_boost = Math.min(1, Math.max(0, parseFloat(sbm[1])));
if (stm) settings.style = Math.min(1, Math.max(0, parseFloat(stm[1])));
await new Promise(r => setTimeout(r, 2000));
}
results.push({ var: label, score: bestScore, path: bestPath });
}
results.forEach(r => console.log(`Var ${r.var}: ${r.score}/10 — ${r.score >= 8 ? "APPROVED" : "REVIEW"}`));
Example Output
Var A Take 1: 112 KB — 7/10 (pacing fast) → Take 2: 118 KB — 8/10 ✓
Var B Take 1: 124 KB — 6/10 (CTA rushed) → Take 2: 121 KB — 8/10 ✓
Var C Take 1: 134 KB — 9/10 ✓
PRODUCTION SUMMARY
Variation A: 8/10 — APPROVED — var_A_take_2.mp3
Variation B: 8/10 — APPROVED — var_B_take_2.mp3
Variation C: 9/10 — APPROVED — var_C_take_1.mp3
Recommended: Variation C (9/10)
Error Handling
Voice settings range
Voice settings range
All values accept 0.0–1.0; code clamps parsed feedback. Higher stability = consistent delivery; higher style = more expressive but less predictable.
Iteration cost management
Iteration cost management
3 variations × 3 takes = up to 9 TTS calls at ~400-500 characters each. Threshold 7 for drafts, 8+ for production.
Audio format options
Audio format options
Default is 128kbps MP3. For broadcast quality use
output_format: "mp3_44100_192" or "pcm_44100" for uncompressed PCM.