"""Install and select a real Canary model through the isolated server's HTTP API.
Usage: python3 seed.py <server-origin> <bearer-token-file>
"""
import json
import sys
import urllib.request
from pathlib import Path
origin, token_file = sys.argv[1:]
token = Path(token_file).read_text().strip()
for action in ('download', 'select'):
    request = urllib.request.Request(
        origin + '/api/voice/models/' + action,
        data=json.dumps({'modelId': 'handy-computer/canary-180m-flash-gguf'}).encode(),
        headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'},
    )
    result = json.load(urllib.request.urlopen(request, timeout=600))
    print(action, [(m['name'], m['state'], m['active']) for m in result['models']
                   if m['active'] or m['state'] == 'installed'], flush=True)
