o
    Ďi                     @   s   d 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ZddlZdddZ	ddd	Z
dddZdddZdddZdddZdddZdS )z
Download utils
    N)PathTc              	   C   sf   z$t | } tj| }t|j|jgsJ |r"tj| 	 dkW S dW S  t
tjjfy2   Y dS w )N   TF)strurllibparseurlparseallschemenetlocrequesturlopengetcodeAssertionError	HTTPError)urlcheckresult r   R/home/jeff/fluffinator/venv/lib/python3.10/site-packages/yolov5/utils/downloads.pyis_url   s    r    c                 C   s8   t jd|  ddd}t|rt|dd S dS )Nz
gsutil du T)shellzutf-8 r   )
subprocesscheck_outputdecodelenevalsplit)r   sr   r   r   gsutil_getsize   s   r    &https://ultralytics.com/images/bus.jpgc                 C   s    t j| dd}t|jddS )NT)allow_redirectszcontent-length)requestsheadintheadersget)r   responser   r   r   url_getsize!   s   r*         ?c                 C   s  ddl m} t| } d|  d| }zz,|d| d|  d tjj|t| |jt	j
kd |  r:|  j|ks>J |W n9 tyx } z-|  rO|   |d	| d
|pX| d|  d td|pf| d|  d W Y d }~nd }~ww W |  r|  j|k r|  r|   |d	| d|  |d d S |  r|  j|k r|  r|   |d	| d|  |d w )Nr   LOGGERzDownloaded file 'z(' does not exist or size is < min_bytes=zDownloading z to z...)progresszERROR: z
Re-attempting zcurl -# -L 'z' -o 'z' --retry 3 -C -
r   )yolov5.utils.generalr-   r   infotorchhubdownload_url_to_filer   levelloggingINFOexistsstatst_size	Exceptionunlinkossystem)filer   url2	min_bytes	error_msgr-   
assert_msger   r   r   safe_download'   s2   ""(rE   ultralytics/yolov5v7.0c                 C   s  ddl m} d!dd}t| |d}|d ur|} tt|  dd} |  sttj	
t| j}t| d	rdt| d
d}|dd } t|  r[|d| d|   | S t| |dd | S dd dD }	z	|||\}
}	W n8 ty   z||\}
}	W n& ty   ztjddtjd  d }
W n ty   |}
Y nw Y nw Y nw | jjddd ||	v rd}t| d| d|
 d| d|  d| d|
 d| d  t| S )"Nr   r,   latestc                 S   sF   |dkr	d| }t d|  d|  }|d dd |d D fS )	NrH   ztags/zhttps://api.github.com/repos/
/releases/tag_namec                 S   s   g | ]}|d  qS )namer   ).0xr   r   r   
<listcomp>G   s    z;attempt_download.<locals>.github_assets.<locals>.<listcomp>assets)r$   r(   json)
repositoryversionr)   r   r   r   github_assetsB   s   
z'attempt_download.<locals>.github_assets)hf_token'r   )zhttp:/zhttps:/z:/z://?zFound z locally at g     j@)r?   r   rA   c                 S   s&   g | ]}d D ]
}d| | dqqS ))r   6z-clsz-segyolov5.ptr   )rL   sizesuffixr   r   r   rN   \   s   & z$attempt_download.<locals>.<listcomp>nsmlxzgit tagT)r   stderrr#   )parentsexist_okzHhttps://drive.google.com/drive/folders/1EFQTEUeXWSFww0luse2jB9M1QNZQGwNlzhttps://github.com/z/releases/download//z2 missing, try downloading from https://github.com/rI   z or )r   rA   rB   )rH   )r0   r-   attempt_download_from_hubr   r   stripreplacer8   r   r   unquoterK   
startswithr   is_filer1   rE   r;   r   r   STDOUTr   parentmkdir)r?   reporeleaserT   r-   rS   r   rK   r   rO   tagurl3r   r   r   attempt_download>   sT   
"	rn   c              	   C   sd   ddl m} ddlm} ddlm} z|| d|d}dd |D d }|W S  ||fy1   Y d S w )	Nr   )list_repo_filesRepositoryNotFoundErrorHFValidationErrormodelrepo_id	repo_typetokenc                 S      g | ]	}| d r|qS rY   endswithrL   fr   r   r   rN   {       z1get_model_filename_from_hfhub.<locals>.<listcomp>)huggingface_hubro   huggingface_hub.utils._errorsrq   !huggingface_hub.utils._validatorsrs   )rv   rT   ro   rq   rs   
repo_filesmodel_filenamer   r   r   get_model_filename_from_hfhubt   s   r   c              	   C   s   ddl m}m} ddlm} ddlm} z)|| d|d}d}||v r*|| |d|d}d	d
 |D d }	|| |	d|d}
|
W S  ||fyI   Y d S w )Nr   )hf_hub_downloadro   rp   rr   rt   ru   zconfig.json)rv   filenamerw   rx   c                 S   ry   rz   r{   r}   r   r   r   rN      r   z-attempt_download_from_hub.<locals>.<listcomp>)r   r   ro   r   rq   r   rs   )rv   rT   r   ro   rq   rs   r   config_file_
model_filer?   r   r   r   ra      s0   ra   )T)r   )r!   )Nr+   r   )rF   rG   N)N)__doc__r6   r=   r   r   pathlibr   r$   r2   r   r    r*   rE   rn   r   ra   r   r   r   r   <module>   s   





6