"""Descarga exactamente los pesos y metadatos utilizados; necesita espacio y red."""
import argparse,hashlib,json
from pathlib import Path
from huggingface_hub import hf_hub_download

MODELS={
 'encodec-24khz':('facebook/encodec_24khz','c1dbe2ae3f1de713481a3b3e7c47f357092ee040',
  ['config.json','model.safetensors'],'model.safetensors','37a7cb100f71a29e6c1d815aca8666a1d7ea8885ebe44c306c751a5103559d57'),
 'f5-tts-v1':('SWivid/F5-TTS','84e5a410d9cead4de2f847e7c9369a6440bdfaca',
  ['F5TTS_v1_Base/model_1250000.safetensors','F5TTS_v1_Base/vocab.txt'],'F5TTS_v1_Base/model_1250000.safetensors','670900fd14e6c458b95da6e9ed317cdb20dbaf7a1c02ac06a05475a9d32b6a38'),
 'vocos-mel-24khz':('charactr/vocos-mel-24khz','0feb3fdd929bcd6649e0e7c5a688cf7dd012ef21',
  ['config.yaml','pytorch_model.bin'],'pytorch_model.bin','97ec976ad1fd67a33ab2682d29c0ac7df85234fae875aefcc5fb215681a91b2a'),
 'hifigan-ljspeech':('speechbrain/tts-hifigan-ljspeech','17fbdc3aae35b81e1554111fa54eab5f2b70cedb',
  ['hyperparams.yaml','generator.ckpt'],'generator.ckpt','1a389f24ffdc3769e0b9c0331f9d803ad0b78185e0da4f2d094e29eedbc11640'),
 'ecapa-voxceleb':('speechbrain/spkrec-ecapa-voxceleb','0f99f2d0ebe89ac095bcc5903c4dd8f72b367286',
  ['hyperparams.yaml','embedding_model.ckpt'],'embedding_model.ckpt','0575cb64845e6b9a10db9bcb74d5ac32b326b8dc90352671d345e2ee3d0126a2'),
 'tacotron2-ljspeech':('speechbrain/tts-tacotron2-ljspeech','d01e530d6d8e1b388c04b882305867addbed4389',
  ['hyperparams.yaml','model.ckpt'],'model.ckpt','02d1aa3fc1cc8fb0981895c765e9d0017416bb78861b5450e458dda92e567856'),
 'whisper-small':('openai/whisper-small','973afd24965f72e36ca33b3055d56a652f456b4d',
  ['config.json','generation_config.json','preprocessor_config.json','tokenizer_config.json','special_tokens_map.json',
   'added_tokens.json','normalizer.json','merges.txt','vocab.json','tokenizer.json','model.safetensors'],
  'model.safetensors','1d7734884874f1a1513ed9aa760a4f8e97aaa02fd6d93a3a85d27b2ae9ca596b')}

def sha(path):
    h=hashlib.sha256()
    with path.open('rb') as f:
        for block in iter(lambda:f.read(1024*1024),b''):h.update(block)
    return h.hexdigest()

def main():
    p=argparse.ArgumentParser();p.add_argument('--out',required=True);p.add_argument('--only',choices=list(MODELS));args=p.parse_args()
    out=Path(args.out);results={}
    for name,(repo,revision,files,weight,expected) in MODELS.items():
        if args.only and name!=args.only:continue
        target=out/name
        for file in files:hf_hub_download(repo_id=repo,revision=revision,filename=file,local_dir=target)
        actual=sha(target/weight)
        if actual!=expected:raise ValueError(f'hash inesperado en {name}')
        results[name]={'repo':repo,'revision':revision,'weight':weight,'sha256':actual}
        print(name,'verificado',actual,flush=True)
    out.mkdir(parents=True,exist_ok=True)
    (out/('manifest-'+args.only+'.json' if args.only else 'manifest.json')).write_text(json.dumps(results,indent=2),encoding='utf8')

if __name__=='__main__':main()
