import ssl
import os
import time

# Disable SSL verification for HuggingFace downloads in this proxied environment
os.environ["HF_HUB_DISABLE_SSL_VERIFY"] = "1"
os.environ["CURL_CA_BUNDLE"] = ""

import urllib.request
ssl._create_default_https_context = ssl._create_unverified_context

from supertonic import TTS

print("Loading model (auto-download on first run)...")
tts = TTS(auto_download=True)

# Demo text — a StageHand-style narration paragraph
text = """
This week on your portfolio: congressional trading signals are ready to go live after a full 
calibration sweep across six thousand trades. The best configuration improves signal precision 
from forty-four to fifty-four percent, with positive median returns versus the S&P five hundred. 
One decision stands between you and shipping it.
""".strip()

print(f"Synthesizing {len(text)} characters...")
start = time.time()
style = tts.get_voice_style(voice_name="M1")
wav, duration = tts.synthesize(
    text=text,
    lang="en",
    voice_style=style,
    total_steps=8,
    speed=1.05,
)
elapsed = time.time() - start

tts.save_audio(wav, "/workspace/agent/supertonic_demo.wav")
print(f"Done: {duration[0]:.2f}s of audio generated in {elapsed:.2f}s wallclock")
print(f"Speed ratio: {duration[0]/elapsed:.1f}x realtime")
