import os, anthropic, requests
MV = os.environ["MAVERA_API_KEY"]
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
client = anthropic.Anthropic()
SAMPLES = {
"blog_posts": [
"We shipped the dashboard redesign last Tuesday. Not because it was perfect — it wasn't — but because our users had been asking for six months and perfect is the enemy of shipped...",
"Three things we learned from 10,000 support tickets: most problems aren't product problems, they're expectation problems...",
],
"emails": [
"Hi Sarah — quick heads up, we're rolling out new API versioning next week. Nothing breaks on your end, but the docs will look different...",
"Thanks for flagging this. You're right, that error message is confusing. We're rewriting it Thursday. Appreciate you taking the time.",
],
"social_media": [
"Just pushed our 100th integration. No confetti, no press release — just another Tuesday making your stack work better.",
"Hot take: most 'AI-powered' features are just if-statements with better marketing.",
],
"internal_docs": [
"RFC: Moving to event-driven architecture for notifications. Current polling costs $4,200/month in unnecessary compute...",
"Retro: Two things to keep — incremental updates over big bangs, and changelogs humans can read.",
],
}
# 1. Build corpus
corpus = ""
for source_type, texts in SAMPLES.items():
corpus += f"\n\n## {source_type.upper().replace('_', ' ')}\n"
for i, text in enumerate(texts, 1):
corpus += f"\nSample {i}: \"{text}\"\n"
print(f"Corpus: {len(corpus):,} chars, {sum(len(v) for v in SAMPLES.values())} samples")
# 2. Claude voice extraction
voice = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=4096,
input=[{
"role": "user",
"content": "Linguistic analyst specializing in brand voice extraction.\n\n"
"Analyze writing samples from MULTIPLE channels and identify CONSISTENT voice patterns.\n\n"
f"SAMPLES:\n{corpus}\n\n"
"Extract:\n"
"1. **Voice Archetype** — one phrase capturing the personality\n"
"2. **Tone Spectrum** — formal↔casual, serious↔playful, technical↔accessible\n"
"3. **Signature Patterns** — recurring rhetorical devices, sentence structures\n"
"4. **Vocabulary DNA** — words used repeatedly; words conspicuously avoided\n"
"5. **Punctuation & Formatting** — em dashes, paragraph length, list usage\n"
"6. **Channel Adaptation** — how voice shifts across channels\n"
"7. **Do/Don't Rules** — 5 'always do' and 5 'never do' rules\n"
"8. **Example Rewrites** — rewrite a generic sentence in this voice"
}],
)
voice_output = voice.content[0].text
print(f"Voice extraction complete — {voice.usage.output_tokens:,} output tokens")
# 3. Create Mavera Brand Voice
bv = requests.post(f"{MV_BASE}/brand-voices", headers=MV_H, json={
"name": "Extracted Org Voice — Q4 2025",
"description": voice_output[:3000],
}).json()
print(f"\nBrand Voice created: {bv.get('id')}")
print(f"\n{voice_output[:1200]}")