
    ^j0                        d Z ddlmZ ddlZddl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mZ dd	lmZmZ d
Z ej.                  e      ZddZ G d d      Zy# e$ r dZY *w xY w)zThis module provides mixin functionality for the Albumentations library.
It includes utility functions and classes to enhance the core capabilities.
    )annotationsN)Path)AnyCallable)load)save)HfApihf_hub_download)HfHubHTTPErrorSoftTemporaryDirectoryTFc                D     t        j                         d fd       }|S )a  Decorator to require huggingface_hub.

    This decorator ensures that the `huggingface_hub` package is installed before
    executing the decorated function. If the package is not installed, it raises
    an ImportError with instructions on how to install it.

    Args:
        func (Callable[..., Any]): The function to decorate.

    Returns:
        Callable[..., Any]: The decorated function.

    c                 R    t         st        dj                   d       | i |S )Nz-You need to install `huggingface_hub` to use zJ. Run `pip install huggingface_hub`, or `pip install albumentations[hub]`.)is_huggingface_hub_availableImportError__name__)argskwargsfuncs     h/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/albumentations/core/hub_mixin.pywrapperz(require_huggingface_hub.<locals>.wrapper)   s=    +? O[ [  T$V$$    )r   r   r   r   returnr   )	functoolswraps)r   r   s   ` r   require_huggingface_hubr      s'     __T% % Nr   c            	          e Zd ZdZdZdZddZedd       Zddddd		 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z	edddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z
edddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zy)HubMixina  Mixin class for Hugging Face Hub integration.

    This class provides functionality for saving and loading transforms to/from
    the Hugging Face Hub. It enables serialization, deserialization, and sharing
    of transform configurations.

    Args:
        _CONFIG_KEYS (tuple[str, ...]): Keys used for configuration files.
        _CONFIG_FILE_NAME_TEMPLATE (str): Template for configuration filenames.

    )trainevalzalbumentations_config_{}.jsonc                h    t        |      }|j                  dd       ||z  }t        | |d       |S )aK  Save the transform to a specified directory.

        Args:
            save_directory (Union[str, Path]):
                Directory where the transform will be saved.
            filename (str):
                Name of the file to save the transform.

        Returns:
            Path: Path to the saved transform file.

        T)parentsexist_okjsondata_format)r   mkdirsave_transform)selfsave_directoryfilename	save_paths       r   _save_pretrainedzHubMixin._save_pretrainedE   s?     n-TD9"X-	 	tYF;r   c                8    t        |      |z  }t        |d      S )aK  Load a transform from a specified directory.

        Args:
            save_directory (Union[str, Path]):
                Directory from where the transform will be loaded.
            filename (str):
                Name of the file to load the transform from.

        Returns:
            A.Compose: Loaded transform.

        r#   r$   )r   load_transform)clsr)   r*   r+   s       r   _from_pretrainedzHubMixin._from_pretrained\   s      (83	iV<<r   r   FN)keyallow_custom_keysrepo_idpush_to_hubc               2   |s*|| j                   vrt        d| d| j                    d      | j                  j                  |      }| j	                  ||       |r<|j                         }|t        |      j                  } | j                  d||d|S y)aK  Save the transform and optionally push it to the Huggingface Hub.

        Args:
            save_directory (`str` or `Path`):
                Path to directory in which the transform configuration will be saved.
            key (`str`, *optional*):
                Key to identify the configuration type, one of ["train", "eval"]. Defaults to "eval".
            allow_custom_keys (`bool`, *optional*):
                Allow custom keys for the configuration. Defaults to False.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your transform to the Huggingface Hub after saving it.
            repo_id (`str`, *optional*):
                ID of your repository on the Hub. Used only if `push_to_hub=True`. Will default to the folder name if
                not provided.
            push_to_hub_kwargs (`dict`, *optional*):
                Additional key word arguments passed along to the [`push_to_hub`] method.

        Returns:
            `str` or `None`: url of the commit on the Hub if `push_to_hub=True`, `None` otherwise.

        Invalid key: ``. Please use key from zP keys for upload. If you want to use a custom key, set `allow_custom_keys=True`.N)r3   r1    )	_CONFIG_KEYS
ValueError_CONFIG_FILE_NAME_TEMPLATEformatr,   copyr   namer4   )	r(   r)   r1   r2   r3   r4   push_to_hub_kwargsr*   r   s	            r   save_pretrainedzHubMixin.save_pretrainedm   s    > !S0A0A%A %<T=N=N<O PQ Q  2299#>nh7 ',,.F~.33#4##GGGGGr   )r1   force_downloadproxiestoken	cache_dirlocal_files_onlyrevisionc          
        | j                   j                  |      }	t        |      }d}
|j                         r|	|j	                         D cg c]  }|j
                   c}v r| j                  ||	      }
nat        r3t        j                  |	 dt        |      j                          d       n(t        |	 dt        |      j                          d      |
|
S 	 t        t        |      j                  dd      |	||||||      }t        |      j                  t        |      j
                  }	}| j                  ||	      S c c}w # t         $ r}t!        |	 d	      |d}~ww xY w)
a  Load a transform from the Huggingface Hub or a local directory.

        Args:
            directory_or_repo_id (`str`, `Path`):
                - Either the `repo_id` (string) of a repo with hosted transform on the Hub, e.g. `qubvel-hf/albu`.
                - Or a path to a `directory` containing transform config saved using
                    [`~albumentations.Compose.save_pretrained`], e.g., `../path/to/my_directory/`.
            key (`str`, *optional*):
                Key to identify the configuration type, one of ["train", "eval"]. Defaults to "eval".
            revision (`str`, *optional*):
                Revision of the repo on the Hub. Can be a branch name, a git tag or any commit id.
                Defaults to the latest commit on `main` branch.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether to force (re-)downloading the transform configuration files from the Hub, overriding
                the existing cache.
            proxies (`dict[str, str]`, *optional*):
                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}`. The proxies are used on every request.
            token (`str` or `bool`, *optional*):
                The token to use as HTTP bearer authorization for remote files. By default, it will use the token
                cached when running `huggingface-cli login`.
            cache_dir (`str`, `Path`, *optional*):
                Path to the folder where cached files are stored.
            local_files_only (`bool`, *optional*, defaults to `False`):
                If `True`, avoid downloading the file and return the path to the local cached file if it exists.

        N)r)   r*   z not found in z, trying to load from the Hub.z8. Please install `huggingface_hub` to load from the Hub.\/)r3   r*   rF   rD   rA   rB   rC   rE   z! not found on the HuggingFace Hub)r;   r<   r   is_diriterdirr>   r0   r   loggerinforesolveFileNotFoundErrorr
   strreplaceparentr   )r/   directory_or_repo_idr1   rA   rB   rC   rD   rE   rF   r*   	transformfconfig_file	directoryes                  r   from_pretrainedzHubMixin.from_pretrained   s   P 1188=#$89	  &&(,@,H,H,JKqAFFKK00@T_g0h	-jt4H/I/Q/Q/S.TTrs (jt4H/I/Q/Q/S.T UN N   	X)0199$D!!#-!1	K #'{"3":":D<M<R<RxI''y8'TT7 L:  	X H:-N!OPVWW	Xs   EA(E 	E(E##E(z%Push transform using huggingface_hub.)r1   r2   commit_messageprivaterC   branch	create_prc          	        |s*|| j                   vrt        d| d| j                    d      t        |      }	|	j                  ||d      j                  }t               5 }
t        |
      |z  }| j                  j                  |      }| j                  ||      }|	j                  ||||||      cd	d	d	       S # 1 sw Y   y	xY w)
a  Push the transform to the Huggingface Hub.

        Use `allow_patterns` and `ignore_patterns` to precisely filter which files should be pushed to the hub. Use
        `delete_patterns` to delete existing remote files in the same commit. See [`upload_folder`] reference for more
        details.

        Args:
            repo_id (`str`):
                ID of the repository to push to (example: `"username/my-model"`).
            key (`str`, *optional*):
                Key to identify the configuration type, one of ["train", "eval"]. Defaults to "eval".
            allow_custom_keys (`bool`, *optional*):
                Allow custom keys for the configuration. Defaults to False.
            commit_message (`str`, *optional*):
                Message to commit while pushing.
            private (`bool`, *optional*, defaults to `False`):
                Whether the repository created should be private.
            token (`str`, *optional*):
                The token to use as HTTP bearer authorization for remote files. By default, it will use the token
                cached when running `huggingface-cli login`.
            branch (`str`, *optional*):
                The git branch on which to push the transform. This defaults to `"main"`.
            create_pr (`boolean`, *optional*):
                Whether or not to create a Pull Request from `branch` with that commit. Defaults to `False`.

        Returns:
            str: The url of the commit of your transform in the given repository.

        r6   r7   zV keys for upload. If you still want to use a custom key, set `allow_custom_keys=True`.)rC   T)r3   r[   r"   )r*   )path_or_fileobjpath_in_repor3   rZ   rF   r]   N)r9   r:   r	   create_repor3   r   r   r;   r<   r,   upload_file)r(   r3   r1   r2   rZ   r[   rC   r\   r]   apitmpr)   r*   r+   s                 r   r4   zHubMixin.push_to_hub   s    T !S0A0A%A %<T=N=N<O PW W 
 % //'7T/RZZ $% 	!#Y0N66==cBH--nx-PI?? )%-# # 		 	 	s   !AB>>C)r)   
str | Pathr*   rP   r   r   )r)   re   r*   rP   r   object)r)   re   r1   rP   r2   boolr3   
str | Noner4   rg   r?   r   r   rh   )r/   r   rS   re   r1   rP   rA   rg   rB   zdict[str, str] | NonerC   zstr | bool | NonerD   zstr | Path | NonerE   rg   rF   rh   r   rf   )r3   rP   r1   rP   r2   rg   rZ   rP   r[   rg   rC   rh   r\   rh   r]   zbool | Noner   rP   )r   
__module____qualname____doc__r9   r;   r,   classmethodr0   r@   rY   r   r4   r8   r   r   r   r   5   s   
 %L!@. = =( "'"!/"/ 	/
  / / / "/ 
/b 
 $)-#''+!&#KXKX(KX 	KX
 KX 'KX !KX %KX KX KX 
KX KXZ 
 "'E !!%>> 	>
  > > > > > > 
> >r   r   )r   Callable[..., Any]r   rm   )rk   
__future__r   r   loggingpathlibr   typingr   r   !albumentations.core.serializationr   r.   r   r'   huggingface_hubr	   r
   huggingface_hub.utilsr   r   r   r   	getLoggerr   rL   r   r   r8   r   r   <module>rv      so    #      D D)6L#'  
		8	$6v vC  )#( )s   A A)(A)