o
    Uôj¸E  ã                   @   sh  d Z 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mZmZmZmZmZmZmZ ddlmZ ddlmZ ddlZdejv rTe	ejd d	ƒand
ejv rce	ejd
 dd	ƒane	ej d¡ddd	ƒaG dd„ dƒZdeddfdd„Zdefdd„Zdedeeeef fdd„Z dede!fdd„Z"d7dede!defdd„Z#ded eddfd!d"„Z$	#		d8ded$ee d%eee  dee fd&d'„Z%	d9d$eded(ee& defd)d*„Z'	#			d:d$eded(ee& d+e!d,e!deej( fd-d.„Z)	#			d:d$eded(ee& d+e!d,e!dee fd/d0„Z*	#	#			d;d1ed2ed3ed4ed(ee& d+e!d,e!deej( fd5d6„Z+dS )<zJONNX Model Hub

This implements the python client for the ONNX model hub.
é    N)ÚBytesIO)Újoin)ÚIOÚAnyÚDictÚListÚOptionalÚSetÚTupleÚcast)Ú	HTTPError)ÚurlopenZ	ONNX_HOMEÚhubÚXDG_CACHE_HOMEÚonnxÚ~z.cachec                   @   sF   e Zd ZdZdeeef ddfdd„Zdefdd„Zdefd	d
„Z	dS )Ú	ModelInfoaM  A class to represent a model's property and metadata in the ONNX Hub.
    It extracts model name, path, sha, tags, etc. from the passed in raw_model_info dict.

    Attributes:
        model: The name of the model.
        model_path: The path to the model, relative to the model zoo (https://github.com/onnx/models/) repo root.
        metadata: Additional metadata of the model, such as the size of the model, IO ports, etc.
        model_sha: The SHA256 digest of the model file.
        tags: A set of tags associated with the model.
        opset: The opset version of the model.
    Úraw_model_infoÚreturnNc                 C   s    t t|d ƒ| _t t|d ƒ| _t tttf |d ƒ| _d| _d| jv r-t t| jd ƒ| _tƒ | _	d| jv rCtt t
t | jd ƒƒ| _	t t|d ƒ| _|| _dS )zgInitializer.

        Args:
            raw_model_info: A JSON dict containing the model info.
        ÚmodelÚ
model_pathÚmetadataNÚ	model_shaÚtagsÚopset_version)r   Ústrr   r   r   r   r   r   Úsetr   r   ÚintÚopsetr   )Úselfr   © r    ú[/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/onnx/hub.pyÚ__init__*   s   


zModelInfo.__init__c              	   C   s&   d| j › d| j› d| j› d| j› d�	S )NzModelInfo(model=z, opset=z, path=z, metadata=ú))r   r   r   r   ©r   r    r    r!   Ú__str__?   s   &zModelInfo.__str__c                 C   s   |   ¡ S ©N)r%   r$   r    r    r!   Ú__repr__B   s   zModelInfo.__repr__)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r%   r'   r    r    r    r!   r      s
    r   Únew_dirr   c                 C   s   | a dS )zkSets the current ONNX hub cache location.

    Args:
        new_dir: Location of new model hub cache.
    N©Ú_ONNX_HUB_DIR)r,   r    r    r!   Úset_dirF   s   r/   c                   C   s   t S )znGets the current ONNX hub cache location.

    Returns:
        The location of the ONNX hub model cache.
    r-   r    r    r    r!   Úget_dirP   s   r0   Úrepoc                 C   sV   |   d¡d   d¡d }|   d¡d   d¡d }d| v r$|   d¡d }nd}|||fS )zCGets the repo owner, name and ref from a repo specification string.ú:r   ú/é   Úmain)Úsplit©r1   Ú
repo_ownerÚ	repo_nameÚrepo_refr    r    r!   Ú_parse_repo_infoY   s   
r;   c                 C   s&   t | ƒ\}}}|dko|dko|dkS )zzVerifies whether the given model repo can be trusted.
    A model repo can be trusted if it matches onnx/models:main.
    r   Úmodelsr5   ©r;   r7   r    r    r!   Ú_verify_repo_refd   s   r>   FÚlfsc                 C   sB   t | ƒ\}}}|rd|› d|› d|› d�S d|› d|› d|› d�S )aT  Gets the base github url from a repo specification string.

    Args:
        repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main".
        lfs: Whether the url is for downloading lfs models.

    Returns:
        The base github url for downloading.
    z*https://media.githubusercontent.com/media/r3   z"https://raw.githubusercontent.com/r=   )r1   r?   r8   r9   r:   r    r    r!   Ú_get_base_urll   s   r@   ÚurlÚ	file_namec              	   C   sŠ   d}t | ƒ�5}t|dƒ�}	 | |¡}|sn| |¡ qW d  ƒ n1 s&w   Y  W d  ƒ dS W d  ƒ dS 1 s>w   Y  dS )z°Downloads the file with specified file_name from the url.

    Args:
        url: A url of download link.
        file_name: A specified file name for the downloaded file.
    i @  ÚwbTN)r   ÚopenÚreadÚwrite)rA   rB   Ú
chunk_sizeÚresponseÚfÚchunkr    r    r!   Ú_download_file   s   

üPûrK   úonnx/models:mainr   r   c              
      sú   t | ƒ}|d }z%t|ƒ�}dd„ t ttt |ƒ¡D ƒ}W d  ƒ n1 s'w   Y  W n tyA } ztd|› �ƒ|‚d}~ww ˆ du rH|n‡ fdd„|D ƒ}|du rW|S dd„ |D ƒ}	g }
|D ]}d	d„ |j	D ƒ}t
|	 |¡ƒd
krz|
 |¡ qb|
S )aó  Gets the list of model info consistent with a given name and tags

    Args:
        repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        model: The name of the model to search for. If `None`, will
            return all models with matching tags.
        tags: A list of tags to filter models by. If `None`, will return
            all models with matching name.

    Returns:
        ``ModelInfo``s.
    zONNX_HUB_MANIFEST.jsonc                 S   s   g | ]}t |ƒ‘qS r    )r   )Ú.0Úinfor    r    r!   Ú
<listcomp>§   s    ÿzlist_models.<locals>.<listcomp>NzCould not find manifest at c                    s"   g | ]}|j  ¡ ˆ  ¡ kr|‘qS r    )r   Úlower©rM   Úm©r   r    r!   rO   ±   s   " c                 S   ó   h | ]}|  ¡ ’qS r    ©rP   ©rM   Útr    r    r!   Ú	<setcomp>¸   ó    zlist_models.<locals>.<setcomp>c                 S   rT   r    rU   rV   r    r    r!   rX   »   rY   r   )r@   r   ÚjsonÚloadr   r   r   r   ÚAssertionErrorr   ÚlenÚintersectionÚappend)r1   r   r   Úbase_urlZmanifest_urlrH   ZmanifestÚeÚmatching_modelsZcanonical_tagsZmatching_info_listrR   Ú
model_tagsr    rS   r!   Úlist_models�   s6   
ÿÿ€€ÿÿý
€rd   r   c                    s€   t || ƒ}|std| › �ƒ‚ˆ du rt|dd„ d�}|d S ‡ fdd„|D ƒ}|s<dd„ |D ƒ}t| › d	ˆ › d
|› �ƒ‚|d S )aÛ  Gets the model info matching the given name and opset.

    Args:
        model: The name of the onnx model in the manifest. This field is
            case-sensitive
        repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        opset: The opset of the model to get. The default of `None` will
            return the model with largest opset.

    Returns:
        ``ModelInfo``.
    zNo models found with name Nc                 S   s   | j  S r&   ©r   )rR   r    r    r!   Ú<lambda>×   s    z get_model_info.<locals>.<lambda>)Úkeyc                    s   g | ]	}|j ˆ kr|‘qS r    re   rQ   re   r    r!   rO   Ù   s    z"get_model_info.<locals>.<listcomp>c                 S   s   g | ]}|j ‘qS r    re   rQ   r    r    r!   rO   Û   s    z has no version with opset z. Valid opsets: r   )rd   r\   Úsorted)r   r1   r   rb   Zselected_modelsZvalid_opsetsr    re   r!   Úget_model_infoÁ   s   
úÿri   Úforce_reloadÚsilentc                 C   s‚  t | ||ƒ}|j d¡}|jdur|j› d|d › �|d< tttj |¡ƒ}|s.tj 	|¡sqt
|ƒsN|sNd|› d�}t|tjd� tdƒ tƒ  ¡ d	krNdS tjtj |¡d
d� t|d
ƒ}	td| › d|› �ƒ t|	|j |ƒ n
td| › d|› �ƒ t|dƒ�}
|
 ¡ }W d  ƒ n1 s�w   Y  |jdurµt |¡ ¡ }||jksµtd|j› d|› d|j› d�ƒ‚t ttt t |ƒƒ¡S )a±  Downloads a model by name from the onnx model hub.

    Args:
        model: The name of the onnx model in the manifest. This field is
            case-sensitive
        repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        opset: The opset of the model to download. The default of `None`
            automatically chooses the largest opset
        force_reload: Whether to force the model to re-download even if
            its already found in the cache
        silent: Whether to suppress the warning message if the repo is
            not trusted.

    Returns:
        ModelProto or None
    r3   NÚ_éÿÿÿÿúThe model repo specification ú_ is not trusted and may contain security vulnerabilities. Only continue if you trust this repo.©ÚfileúContinue?[y/n]ÚyT©Úexist_okúDownloading ú to local path úUsing cached ú model from ÚrbúThe cached model ú has SHA256 ú while checksum should be úh. The model in the hub may have been updated. Use force_reload to download the model from the model hub.)!ri   r   r6   r   r   r.   ÚosÚsepÚpathÚexistsr>   ÚprintÚsysÚstderrÚinputrP   ÚmakedirsÚdirnamer@   rK   rD   rE   ÚhashlibÚsha256Ú	hexdigestr\   r   r   r[   r   r   Úbytesr   )r   r1   r   rj   rk   Úselected_modelZlocal_model_path_arrZlocal_model_pathÚmsgÚlfs_urlrI   Úmodel_bytesÚdownloaded_shar    r    r!   r[   â   s@   
þÿ

ÿ

ÿÿr[   c                 C   sÔ  t | ||ƒ}|jd  d¡}|jd }|dur"|› d|d › �|d< tttj |¡ƒ}|s3tj |¡sxt	|ƒsS|sSd|› d�}	t
|	tjd	� t
d
ƒ tƒ  ¡ dkrSdS tjtj |¡dd� t|dƒ}
t
d| › d|› �ƒ t|
|jd  |ƒ n
t
d| › d|› �ƒ t|dƒ�}| ¡ }W d  ƒ n1 s–w   Y  |dur¹t |¡ ¡ }||ks¹td|j› d|› d|› d�ƒ‚t |¡�}|dt|ƒd … }| |¡ W d  ƒ n1 sØw   Y  |d t |¡d  }|S )a   Downloads a model along with test data by name from the onnx model hub and returns the directory to which the files have been extracted.

    Args:
        model: The name of the onnx model in the manifest. This field is
            case-sensitive
        repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        opset: The opset of the model to download. The default of `None`
            automatically chooses the largest opset
        force_reload: Whether to force the model to re-download even if
            its already found in the cache
        silent: Whether to suppress the warning message if the repo is
            not trusted.

    Returns:
        str or None
    Úmodel_with_data_pathr3   Úmodel_with_data_shaNrl   rm   rn   ro   rp   rr   rs   Trt   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   é   )ri   r   r6   r   r.   r   r€   r�   r‚   r>   rƒ   r„   r…   r†   rP   r‡   rˆ   r@   rK   rD   rE   r‰   rŠ   r‹   r\   r   Útarfiler]   Ú
extractallÚlistdir)r   r1   r   rj   rk   r�   Zlocal_model_with_data_path_arrr“   Zlocal_model_with_data_pathrŽ   r�   rI   Zmodel_with_data_bytesr‘   Zmodel_with_data_zippedZlocal_model_with_data_dir_pathr’   r    r    r!   Údownload_model_with_test_data#  sl   ÿþ
þÿÿ
þ
ÿÿÿÿûÿþÿr˜   Únetwork_modelÚpreprocessing_modelÚnetwork_repoÚpreprocessing_repoc                 C   sÆ  t |||||ƒ}|du rtd|› �ƒ‚t | ||||ƒ}|du r&td| › �ƒ‚tƒ }	i }
i }|jD ]}|jdkr9dn|j}|	 |¡ |j|
|< q0|jD ]}|jdkrSdn|j}|	 |¡ |j||< qJd}d}|	D ]7}|dkrv|| }|
| }qg||v rž||
v rž|| || kržtd|› d| › d	|› d
|| › d|
| › d�ƒ‚qg||krµtj	 
||¡}|j|_tj |¡ n||krÊtj	 
||¡}|j|_tj |¡ dd„ t|jj|jjƒD ƒ}tjj|||d�}|S )aå  Builds a composite model including data preprocessing by downloading a network and a preprocessing model
    and combine it into a single model

    Args:
        network_model: The name of the onnx model in the manifest.
        preprocessing_model: The name of the preprocessing model.
        network_repo: The location of the model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        preprocessing_repo: The location of the proprocessing model repo in format
            "user/repo[:branch]". If no branch is found will default to
            "main"
        opset: The opset of the model to download. The default of `None`
            automatically chooses the largest opset
        force_reload: Whether to force the model to re-download even if
            its already found in the cache
        silent: Whether to suppress the warning message if the repo is
            not trusted.

    Returns:
        ModelProto or None
    Nz(Could not load the preprocessing model: z"Could not load the network model: Ú zai.onnxrm   zCan not merge z and z: because they contain different opset versions for domain z (z) and z\). Only the default domain can be automatically converted to the highest version of the two.c                 S   s   g | ]
\}}|j |j f‘qS r    )Úname)rM   Z	out_entryZin_entryr    r    r!   rO   Ö  s    
ÿÿz(load_composite_model.<locals>.<listcomp>)Úio_map)r[   ÚRuntimeErrorr   Úopset_importÚdomainÚaddÚversionÚ
ValueErrorr   Úversion_converterÚconvert_versionÚ
ir_versionÚcheckerÚcheck_modelÚzipÚgraphÚoutputr†   ÚcomposeÚmerge_models)r™   rš   r›   rœ   r   rj   rk   ÚpreprocessingÚnetworkZall_domainsZdomains_to_version_networkZ domains_to_version_preprocessingZopset_import_entryr¢   Zpreprocessing_opset_versionZnetwork_opset_versionrŸ   Zmodel_with_preprocessingr    r    r!   Úload_composite_model{  s~   
ÿÿ
ÿ

ÿ

ÿÿÿþÿ€ÿÿþÿr²   )F)rL   NN)rL   N)rL   NFF)rL   rL   NFF),r+   r‰   rZ   r   r„   r•   Úior   Úos.pathr   Útypingr   r   r   r   r   r	   r
   r   Úurllib.errorr   Úurllib.requestr   r   Úenvironr.   r�   Ú
expanduserr   r   r/   r0   r;   Úboolr>   r@   rK   rd   r   ri   Ú
ModelProtor[   r˜   r²   r    r    r    r!   Ú<module>   sÌ   (

)
	ýÿþ
ý
ü2ÿÿÿÿ
þ#ûÿþýüû
úCûÿþýüû
ú[ùÿþýüûúùø