import os, ssl, time
os.environ["HF_HUB_DISABLE_SSL_VERIFY"] = "1"
os.environ["CURL_CA_BUNDLE"] = ""
ssl._create_default_https_context = ssl._create_unverified_context

import torchaudio
from chatterbox.tts import ChatterboxTTS

REFERENCE = "/workspace/extra/projects/stagehand/input/voice_samples/richard_fowler_voice_45s_75s.wav"
OUTPUT = "/workspace/agent/richard_chatterbox_demo.wav"

# Listing walkthrough narration — StageHand use case
TEXT = (
    "Welcome to 2847 Broadbay Drive, a stunning four-bedroom home nestled in one of "
    "Cedar Park's most sought-after neighborhoods. From the moment you step through the door, "
    "you'll notice the soaring ceilings and natural light that make this home feel both "
    "grand and inviting. The open-concept kitchen flows seamlessly into the living area — "
    "perfect for entertaining. Schedule your private tour today."
)

print("Loading Chatterbox model (downloading on first run)...")
model = ChatterboxTTS.from_pretrained(device="cpu")

print(f"Generating voice clone from: {REFERENCE}")
print(f"Text ({len(TEXT)} chars): {TEXT[:60]}...")

start = time.time()
wav = model.generate(
    text=TEXT,
    audio_prompt_path=REFERENCE,
    exaggeration=0.5,
    cfg_weight=0.5,
)
elapsed = time.time() - start

torchaudio.save(OUTPUT, wav, model.sr)

# Report duration
duration = wav.shape[-1] / model.sr
print(f"\nDone: {duration:.1f}s of audio in {elapsed:.1f}s ({duration/elapsed:.2f}x realtime)")
print(f"Saved to: {OUTPUT}")
