o
    ̳i.1                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ G dd deZdS )    N)
HTTPStatus)Path)warn)snapshot_download)GatedRepoErrorRepositoryNotFoundError)model_download)set_kaggle_credentials)KaggleApiHTTPError)parse_model_handle)
Subcommand)REPO_ID_FNAMEc                       s   e Zd ZdZdejf fddZdddZd	ejddfd
dZ	d	ejddfddZ
d	ejddfddZdeddfddZd	ejfddZ  ZS )Downloadz7Holds all the logic for the `tune download` subcommand.
subparsersc              	      sH   t    |jdddddtdtjd| _|   | jj	| j
d d S )Ndownloadztune downloadz!tune download <repo-id> [OPTIONS]z?Download a model from the Hugging Face Hub or Kaggle Model Hub.a              examples:
                # Download a model from the Hugging Face Hub with a Hugging Face API token
                $ tune download meta-llama/Llama-2-7b-hf --hf-token <TOKEN>
                Successfully downloaded model repo and wrote to the following locations:
                /tmp/Llama-2-7b-hf/config.json
                /tmp/Llama-2-7b-hf/README.md
                /tmp/Llama-2-7b-hf/consolidated.00.pth
                ...

                # Download an ungated model from the Hugging Face Hub
                $ tune download mistralai/Mistral-7B-Instruct-v0.2 --output-dir /tmp/model
                Successfully downloaded model repo and wrote to the following locations:
                /tmp/model/config.json
                /tmp/model/README.md
                /tmp/model/model-00001-of-00002.bin
                ...

                # Download a model from the Kaggle Model Hub
                $ tune download metaresearch/llama-3.2/pytorch/1b --source kaggle
                Successfully downloaded model repo and wrote to the following locations:
                /tmp/llama-3.2/pytorch/1b/tokenizer.model
                /tmp/llama-3.2/pytorch/1b/params.json
                /tmp/llama-3.2/pytorch/1b/consolidated.00.pth
                ...

            For a list of all models, visit the Hugging Face Hub
            https://huggingface.co/models or Kaggle Model Hub https://kaggle.com/models.
            )progusagehelpdescriptionepilogformatter_class)func)super__init__
add_parsertextwrapdedentargparseRawTextHelpFormatter_parser_add_argumentsset_defaults_download_cmd)selfr   	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/_cli/download.pyr       s   
'zDownload.__init__returnNc                 C   s   | j jdtdd | j jdtdddd | j jd	tdtd
ddd | j jdtddd | j jdtddddgdd | j jdtddd | j jdtddd dS )zAdd arguments to the parser.repo_idzOName of the repository on Hugging Face Hub or model handle on Kaggle Model Hub.)typer   z--output-dirFNzFDirectory in which to save the model. Defaults to `/tmp/<model_name>`.)r*   requireddefaultr   z
--hf-tokenHF_TOKENz<Hugging Face API token. Needed for gated models like Llama2.z--ignore-patternszIf provided, files matching any of the patterns are not downloaded. Example: '*.safetensors'. Only supported for Hugging Face Hub models.)r*   r+   r   z--sourcehuggingfacekagglez\If provided, downloads model weights from the provided repo_id on the designated source hub.)r*   r+   r,   choicesr   z--kaggle-usernamezZKaggle username for authentication. Needed for private models or gated models like Llama2.z--kaggle-api-keyzKaggle API key. Needed for private models or gated models like Llama2. You can find your API key at https://kaggle.com/settings.)r   add_argumentstrr   osgetenv)r#   r&   r&   r'   r    L   sZ   

zDownload._add_argumentsargsc                 C   s,   |j dkr
| |S |j dkr| |S d S )Nr.   r/   )source_download_from_huggingface_download_from_kaggle)r#   r5   r&   r&   r'   r"   ~   s
   



zDownload._download_cmdc           
   
   C   st  |j }|du r|jdd }td| }td|j  zt|j||j|jd}W nS tyC   |jr;| j	
d n| j	
d Y n; tyV   | j	
d	|j d
 Y n( ty} } zt }d|j d| d| }| j	
| W Y d}~nd}~ww tj|td }t|d}	tjd|ji|	dd W d   n1 sw   Y  tdgtt| R ddi dS )z,Downloads a model from the Hugging Face Hub.N/z/tmpz0Ignoring files matching the following patterns: )	local_dirignore_patternstokenzkIt looks like you are trying to access a gated repository. Please ensure you have access to the repository.a-  It looks like you are trying to access a gated repository. Please ensure you have access to the repository and have provided the proper Hugging Face API token using the option `--hf-token` or by running `huggingface-cli login`.You can find your token by visiting https://huggingface.co/settings/tokenszRepository 'z$' not found on the Hugging Face Hub.Failed to download  with error: '' and traceback: z.jsonwr)      )indentHSuccessfully downloaded model repo and wrote to the following locations:sep
)
output_dirr)   splitr   printr<   r   hf_tokenr   r   errorr   	Exception	traceback
format_excr3   pathjoinr   openjsondumplistiterdir)
r#   r5   rG   
model_nametrue_output_diretbmsg	file_path	json_filer&   r&   r'   r7      sT   
	
z#Download._download_from_huggingfacec              
   C   sJ  |j }| | | | |jrtd |jrtd zt|}tdgtt	|
 R ddi W dS  ty} } z>|jjtjtjhv rL| jd n|jjtjkr]| jd| d t }d	| d
| d| }| j| W Y d}~dS d}~w ty } zt }d	| d
| d| }| j| W Y d}~dS d}~ww )z,Downloads a model from the Kaggle Model Hub.z]--output-dir flag is not supported for Kaggle model downloads. This argument will be ignored.zb--ignore-patterns flag is not supported for Kaggle model downloads. This argument will be ignored.rD   rE   rF   aS  It looks like you are trying to access a gated model. Please ensure you have access to the model and have provided the proper Kaggle credentials using the options `--kaggle-username` and `--kaggle-api-key`. You can also set these to environment variables as detailed in https://github.com/Kaggle/kagglehub/blob/main/README.md#authenticate.'z$' not found on the Kaggle Model Hub.r>   r?   r@   N)r)   _validate_kaggle_model_handle_set_kaggle_credentialsrG   r   r<   r   rI   rT   r   rU   r
   responsestatus_coder   UNAUTHORIZED	FORBIDDENr   rK   	NOT_FOUNDrM   rN   rL   )r#   r5   model_handlerG   rX   rY   rZ   r&   r&   r'   r8      sT   


zDownload._download_from_kagglehandlec              
   C   s   z)t |}|jdkr|jdkrtd| d |jdvr'td| d W d S W d S  tyK } zd| d	| d
}| j| W Y d }~d S d }~ww )NpytorchmetaresearchzRequested PyTorch model zQ was not published from Meta, and therefore may not be compatible with torchtune.>   rg   transformerszRequested model zc is neither a PyTorch nor a Transformers model, and therefore may not be compatible with torchtune.zFailed to validate z with error .)r   	frameworkownerr   rL   r   rK   )r#   rf   parsed_handlerX   rZ   r&   r&   r'   r^      s"   





z&Download._validate_kaggle_model_handlec              
   C   s   z-|j s|jr+td |j r|j ntjd}|jr|jntjd}t|| W d S W d S  tyJ } zd| d}t| W Y d }~d S d }~ww )Na  TIP: you can avoid passing in the --kaggle-username and --kaggle-api-key arguments by storing them as the environment variables KAGGLE_USERNAME and KAGGLE_KEY, respectively. For more details, see https://github.com/Kaggle/kagglehub/blob/main/README.md#authenticateKAGGLE_USERNAME
KAGGLE_KEYz.Failed to set Kaggle credentials with error: 'r]   )	kaggle_usernamekaggle_api_keyrI   r3   environgetr	   rL   r   )r#   r5   rp   rq   rX   rZ   r&   r&   r'   r_     s(   

z Download._set_kaggle_credentials)r(   N)__name__
__module____qualname____doc__r   _SubParsersActionr   r    	Namespacer"   r7   r8   r2   r^   r_   __classcell__r&   r&   r$   r'   r      s    
,264r   )r   rR   r3   r   rM   httpr   pathlibr   warningsr   huggingface_hubr   huggingface_hub.utilsr   r   	kagglehubr   kagglehub.authr	   kagglehub.exceptionsr
   kagglehub.handler   torchtune._cli.subcommandr   'torchtune.training.checkpointing._utilsr   r   r&   r&   r&   r'   <module>   s"   