
    hC                     L   S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
JrJrJrJrJrJrJrJr  SSKJr  SSKJr  SSKrS\R0                  ;   a  \	" \R0                  S   S	5      qOLS
\R0                  ;   a  \	" \R0                  S
   SS	5      qO$\	" \R4                  R7                  S5      SSS	5      q " S S5      rS\SS4S jrS\4S jrS\S\\\\4   4S jr S\S\!4S jr"S*S\S\!S\4S jjr#S\S\SS4S jr$   S+S\S\\   S\\\      S\\   4S jjr% S,S\S\S\\&   S\4S  jjr'    S-S\S\S\\&   S!\!S"\!S\\RP                     4S# jjr)    S-S\S\S\\&   S!\!S"\!S\\   4S$ jjr*     S.S%\S&\S'\S(\S\\&   S!\!S"\!S\\RP                     4S) jjr+g)/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)urlopen	ONNX_HOMEhubXDG_CACHE_HOMEonnx~z.cachec                   P    \ rS rSrSrS\\\4   SS4S jrS\4S jr	S\4S jr
S	rg)
	ModelInfo   a*  
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                    [        [        US   5      U l        [        [        US   5      U l        [        [        [        [
        4   US   5      U l        SU l        SU R                  ;   a"  [        [        U R                  S   5      U l        [        5       U l	        SU R                  ;   a2  [        [        [        [           U R                  S   5      5      U l	        [        [        US   5      U l        Xl        g)zH
Parameters:
    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   s     B/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/hub.py__init__ModelInfo.__init__+   s    
 #~g67
sN<$@A(,T#s(^^J=W(X(,$--'!#t}}['ABDN!e	T]]"DcDMM&,ABCDI#~o>?
.<    c           	      p    SU R                    SU R                   SU R                   SU R                   S3	$ )NzModelInfo(model=z, opset=z, path=z, metadata=))r   r#   r   r   r$   s    r%   __str__ModelInfo.__str__?   s:    !$**Xdjj\HYYdeiererdsstuur(   c                 "    U R                  5       $ N)r,   r+   s    r%   __repr__ModelInfo.__repr__B   s    ||~r(   )r   r   r   r   r#   r   r   )__name__
__module____qualname____firstlineno____doc__r   r    r   r&   r,   r0   __static_attributes__ r(   r%   r   r      s<    =tCH~ =$ =(v v# r(   r   new_dirr   c                     U q g)z[
Sets the current ONNX hub cache location

:param new_dir: location of new model hub cache
N_ONNX_HUB_DIR)r9   s    r%   set_dirr=   F   s	     Mr(   c                      [         $ )z]
Gets the current ONNX hub cache location

:return: The location of the ONNX hub model cache
r;   r8   r(   r%   get_dirr?   P   s
     r(   repoc                     U R                  S5      S   R                  S5      S   nU R                  S5      S   R                  S5      S   nSU ;   a  U R                  S5      S   nOSnXU4$ )zE
Gets the repo owner, name and ref from a repo specification string.
:r   /   main)splitr@   
repo_owner	repo_namerepo_refs       r%   _parse_repo_inforK   Y   sq     C#))#.q1J

3"((-a0I
d{::c?1%(**r(   c                 X    [        U 5      u  pnUS:H  =(       a    US:H  =(       a    US:H  $ )zs
Verifies whether the given model repo can be trusted.
A model repo can be trusted if it matches onnx/models:main.
r   modelsrE   rK   rG   s       r%   _verify_repo_refrO   f   s2    
 '7t&<#J8& VyH'<V8vCUVr(   lfsc                 \    [        U 5      u  p#nU(       a  SU SU SU S3$ SU SU SU S3$ )a   
Gets the base github url from a repo specification string

:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param lfs: whether the url is for downloading lfs models
:return: the base github url for downloading
z*https://media.githubusercontent.com/media/rC   z"https://raw.githubusercontent.com/rN   )r@   rP   rH   rI   rJ   s        r%   _get_base_urlrR   o   sQ     '7t&<#J8
;J<qSTU]T^^_``/
|1YKq
RSTTr(   url	file_namec                     Sn[        U 5       n[        US5       n UR                  U5      nU(       d  OUR                  U5        M,  SSS5        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)z
Downloads the file with specifed file_name from the url

:param url: a url of download link
:param file_name: a specified file name for the downloaded file
i @  wbN)r   openreadwrite)rS   rT   
chunk_sizeresponsefchunks         r%   _download_filer^      sY     J	4	4#8AMM*-EGGEN	  $9#8#8s"   A+.A	A+
A(	$A++
A9r   r   c                     [        U 5      nUS-   n [        U5       n[        R                  " [	        [
        [           U5      5       Vs/ s H  n[        U5      PM     nnSSS5        Uc  WO?W V	s/ s H2  oR                  R                  5       UR                  5       :X  d  M0  U	PM4     sn	n
Uc  U
$ U Vs1 s H  oR                  5       iM     nn/ nU
 H]  n	U	R                   Vs1 s H  oR                  5       iM     nn[        UR                  U5      5      S:  d  ML  UR                  U	5        M_     U$ s  snf ! , (       d  f       N= f! [         a  n[        SU 35      UeSnAff = fs  sn	f s  snf s  snf )a  
Gets the list of model info consistent with a given name and tags

:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param model: The name of the model to search for. If `None`, will return all models with matching tags.
:param tags: A list of tags to filter models by. If `None`, will return all models with matching name.
:return: list of ModelInfo
zONNX_HUB_MANIFEST.jsonNzCould not find manifest at r   )rR   r   jsonloadr   r   r    r   r   AssertionErrorr   lowerr   lenintersectionappend)r@   r   r   base_urlmanifest_urlr[   infomanifestemmatching_modelstcanonical_tagsmatching_info_list
model_tagss                  r%   list_modelsrr      s`    T"H66LR\"h,0IId2c7H6M,N),ND	$,N  ) # = 	!FAWW]]_%EaF  |)-.AggiN.*,)*0Aggi
0~**:67!;%%a(  /) #"  R:<.IJPQQR G / 1s^   E .D?D: D?"E 3/E1&E18E6(E;:D??
E	E E 
E.E))E.r#   c                 6   [        X5      nU(       d  [        SU  35      eUc  [        US S9nUS   $ U Vs/ s H  oUR                  U:X  d  M  UPM     nnU(       d.  U Vs/ s H  oUR                  PM     nn[        U  SU SU 35      eUS   $ s  snf s  snf )a  
Gets the model info matching the given name and opset.

:param model: The name of the onnx model in the manifest. This field is case-sensitive
:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param opset: The opset of the model to get. The default of `None` will return the model with largest opset.
:return: ModelInfo
zNo models found with name c                     U R                   * $ r/   )r#   )rl   s    r%   <lambda> get_model_info.<locals>.<lambda>   s
    r(   )keyz has no version with opset z. Valid opsets: r   )rr   rb   sortedr#   )r   r@   r#   rm   selected_modelsrl   valid_opsetss          r%   get_model_infor{      s     "$.O9%ABB} 6HI 1 '6JoE9I1oJ-<=_GG_L= '4UG;KL>Z  1 K=s   BB"Bforce_reloadsilentc           	         [        XU5      nUR                  R                  S5      nUR                  b  UR                   SUS    3US'   [	        [
        [        R                  R	                  U5      5      nU(       d$  [        R                  R                  U5      (       d  [        U5      (       dM  U(       dF  SU S3n[        U[        R                  S9  [        S5        [        5       R                  5       S	:w  a  g[        R                   " [        R                  R#                  U5      S
S9  [%        US
5      n	[        SU  SU 35        ['        XR                  -   U5        O[        SU  SU 35        [)        US5       n
U
R+                  5       nSSS5        UR                  b\  [,        R.                  " W5      R1                  5       nXR                  :X  d)  [3        SUR4                   SU SUR                   S35      e[6        R8                  " [;        [<        [>           [A        W5      5      5      $ ! , (       d  f       N= f)aM  
Downloads a model by name from the onnx model hub

:param model: The name of the onnx model in the manifest. This field is case-sensitive
:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param opset: The opset of the model to download. The default of `None` automatically chooses the largest opset
:param force_reload: Whether to force the model to re-download even if its already found in the cache
:param silent: Whether to suppress the warning message if the repo is not trusted.
:return: ModelProto or None
rC   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.)!r{   r   rF   r   r   r<   osseppathexistsrO   printsysstderrinputrc   makedirsdirnamerR   r^   rW   rX   hashlibsha256	hexdigestrb   r   r   ra   r   r   bytesr   )r   r@   r#   r|   r}   selected_modellocal_model_path_arrlocal_model_pathmsglfs_urlr\   model_bytesdownloaded_shas                r%   ra   ra      s   $ $E7N)44::3?+ ''(*>r*B)CD 		
 M266;;7K+LM277>>*:;;%%f1$  8W  XC#CJJ'"#w}}#%
BGGOO$45Ed+UG?3C2DEFw!:!::<LMeWL1A0BCD		%ffh 
& + 4>>@!9!99 #N$8$8#9nEU V,,:,D,D+E F99  99T"U)W[%9:;; 
&	%s   H<<
I
c           	         [        XU5      nUR                  S   R                  S5      nUR                  S   nUb  U SUS    3US'   [        [        [
        R                  R                  U5      5      nU(       d$  [
        R                  R                  U5      (       d  [        U5      (       dM  U(       dF  SU S3n	[        U	[        R                  S	9  [        S
5        [        5       R                  5       S:w  a  g[
        R                  " [
        R                  R!                  U5      SS9  [#        US5      n
[        SU  SU 35        [%        XR                  S   -   U5        O[        SU  SU 35        ['        US5       nUR)                  5       nSSS5        UbH  [*        R,                  " W5      R/                  5       nX:X  d  [1        SUR2                   SU SU S35      e[4        R&                  " U5       nUS[7        U5      S-
   nUR9                  U5        SSS5        WS-   [
        R:                  " U5      S   -   nU$ ! , (       d  f       N= f! , (       d  f       N@= f)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.

:param model: The name of the onnx model in the manifest. This field is case-sensitive
:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param opset: The opset of the model to download. The default of `None` automatically chooses the largest opset
:param force_reload: Whether to force the model to re-download even if its already found in the cache
:param silent: Whether to suppress the warning message if the repo is not trusted.
:return: str or None
model_with_data_pathrC   model_with_data_shaNr   r   r   r   r   r   r   Tr   r   r   r   r   r   r   r   r   r   r      )r{   r   rF   r   r<   r   r   r   r   rO   r   r   r   r   rc   r   r   rR   r^   rW   rX   r   r   r   rb   r   tarfilerd   
extractalllistdir)r   r@   r#   r|   r}   r   local_model_with_data_path_arrr   local_model_with_data_pathr   r   r\   model_with_data_bytesr   model_with_data_zippedlocal_model_with_data_dir_pathr   s                    r%   download_model_with_test_datar     sj   $ $E7N%3%<%<&eCj # )112GH& ##1%CB%G$HI 	'	
 "&rvv{{#AB" 277>>*DEE%%f1$  8W  XC#CJJ'"#w}}#%
BGGOO$>?$Od+UG?3M2NOP--.DEE&	

 	eWL1K0LMN	($	/1 ! 
0 & (=>HHJ4 #N$8$8#9nEU V,,?+@ A99  
0	15K)C./!3*
& 	))*HI 
2 	'
	
**3
4Q
7	8   3 
0	/ 
2	1s   I#I
I
I,network_modelpreprocessing_modelnetwork_repopreprocessing_repoc                    [        XXEU5      nUc  [        SU 35      e[        XXEU5      nUc  [        SU  35      e[        5       n	0 n
0 nUR                   H@  nUR                  S:X  a  SOUR                  nU	R                  U5        UR                  X'   MB     UR                   H@  nUR                  S:X  a  SOUR                  nU	R                  U5        UR                  X'   MB     SnSnU	 HJ  nUS:X  a
  X   nX   nM  X;   d  M  X;   d  M!  X   X   :w  d  M-  [        SU SU  SU S	X    S
X    S35      e   X:  aP  [        R                  R                  X5      nUR                  Ul        [        R                  R                  U5        OTX:  aO  [        R                  R                  X5      nUR                  Ul        [        R                  R                  U5        [        UR                  R                   UR                  R"                  5       VVs/ s H  u  nnUR$                  UR$                  4PM      nnn[        R&                  R)                  XxUS9nU$ s  snnf )a  
Builds a composite model including data preprocessing by downloading a network and a preprocessing model
and combine it into a single model

:param model: The name of the onnx model in the manifest. This field is case-sensitive
:param repo: The location of the model repo in format "user/repo[:branch]".
    If no branch is found will default to "main"
:param opset: The opset of the model to download. The default of `None` automatically chooses the largest opset
:param force_reload: Whether to force the model to re-download even if its already found in the cache
:param silent: Whether to suppress the warning message if the repo is not trusted.
:return: ModelProto or None
z(Could not load the preprocessing model: z"Could not load the network model:  zai.onnxr   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.)io_map)ra   RuntimeErrorr!   opset_importdomainaddversion
ValueErrorr   version_converterconvert_version
ir_versioncheckercheck_modelzipgraphoutputr   namecomposemerge_models)r   r   r   r   r#   r|   r}   preprocessingnetworkall_domainsdomains_to_version_network domains_to_version_preprocessingopset_import_entryr   preprocessing_opset_versionnetwork_opset_version	out_entryin_entryr   model_with_preprocessings                       r%   load_composite_modelr   c  s   * fM 67J6KL
 	
 =VLG?OPPEK1379$%22+22b8I>P>W>W 	 	-?-G-G"* 3 ,88+22b8I>P>W>W 	 	3E3M3M(0 9 #%Y*J*R'$>$F!6408/78  !4 5U=/ J77=hbAaAi@j k19: ;MM    #:((88
 +55  )		<..>>
 $+#5#5   / $'}':':'A'A7==CVCV#W#WIx 
'#W  
  $||88v  9   $#s   %I')F)onnx/models:mainNN)r   N)r   NFF)r   r   NFF),r6   r   r`   r   r   r   ior   os.pathr   typingr   r   r   r   r	   r
   r   r   urllib.errorr   urllib.requestr   r   environr<   r   
expanduserr   r    r=   r?   rK   boolrO   rR   r^   rr   r"   r{   
ModelProtora   r   r   r8   r(   r%   <module>r      s     	 
    B B B " " "**K0%8M#$45vuEM++C0(FEJM& &RS T  
+3 
+5c3#7 
+W3 W4 WU U$ U3 U    $ # $)
)C=) 49
) 
)_	)Z HL7?}< #7<7<
7< C=7< 	7<
 7< doo7<x #N N 
N  C=N  	N 
 N  c]N h +0Y$Y$Y$ Y$ 	Y$
 C=Y$ Y$ Y$ dooY$r(   