"""Download Piper model via Python API."""
import piper
import os

# Use piper's built-in download
try:
    # Download English voice
    model_path, config_path = piper.download_voices(
        "en_US", 
        "lessac", 
        "medium",
        "/root/.piper/models"
    )
    print(f"Model: {model_path}")
    print(f"Config: {config_path}")
except Exception as e:
    print(f"Error: {e}")
    # Try alternative - manual download
    print("Trying manual download...")
    import urllib.request
    dest = os.path.expanduser("~/.piper/models")
    os.makedirs(dest, exist_ok=True)
    
    base = "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium"
    files = [
        "en_US-lessac-medium.onnx",
        "en_US-lessac-medium.onnx.json"
    ]
    for f in files:
        url = f"{base}/{f}"
        path = os.path.join(dest, f)
        if not os.path.exists(path):
            print(f"Downloading {f}...")
            urllib.request.urlretrieve(url, path)
            print(f"  OK: {os.path.getsize(path)} bytes")
        else:
            print(f"Already: {f} ({os.path.getsize(path)} bytes)")
