a
    hJ                  
   @  s   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZm	Z	 d dl
mZ G dd deZdd	d
dddddddZdd	dd	d
dddddddZdd	d	d
dddddddZd	d	ddddZdS )    )annotationsN)Path)hf_hub_downloadsnapshot_download)tqdmc                      s4   e Zd ZdZ fddZddd fddZ  ZS )	disabled_tqdmz
    Class to override `disable` argument in case progress bars are globally disabled.

    Taken from https://github.com/tqdm/tqdm/issues/619#issuecomment-619639324.
    c                   s   d|d< t  j|i | d S )NTdisable)super__init__)selfargskwargs	__class__ ^/var/www/html/assistant/venv/lib/python3.9/site-packages/sentence_transformers/util/file_io.pyr
      s    zdisabled_tqdm.__init__strNone)attrreturnc                   s2   zt  | W n ty,   |dkr( Y n0 dS )zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)r	   __delattr__AttributeError)r   r   r   r   r   r      s
    zdisabled_tqdm.__delattr__)__name__
__module____qualname____doc__r
   r   __classcell__r   r   r   r   r      s   r   Fr   zbool | str | Nonez
str | Nonebool)model_name_or_pathtokencache_folderrevisionlocal_files_onlyr   c              	   C  s   t t| d||||dS )a}  
    Checks if the given model name or path corresponds to a SentenceTransformer model.

    Args:
        model_name_or_path (str): The name or path of the model.
        token (Optional[Union[bool, str]]): The token to be used for authentication. Defaults to None.
        cache_folder (Optional[str]): The folder to cache the model files. Defaults to None.
        revision (Optional[str]): The revision of the model. Defaults to None.
        local_files_only (bool): Whether to only use local files for the model. Defaults to False.

    Returns:
        bool: True if the model is a SentenceTransformer model, False otherwise.
    zmodules.json)r    r!   r"   r#   )r   load_file_path)r   r    r!   r"   r#   r   r   r   is_sentence_transformer_model    s    r%    z
str | Path)r   filename	subfolderr    r!   r"   r#   r   c              
   C  sb   t | ||}| rt|S t ||}z"t| |j|j |d|||dW S  ty\   Y dS 0 dS )a  
    Loads a file from a local or remote location.

    Args:
        model_name_or_path (str): The model name or path.
        filename (str): The name of the file to load.
        subfolder (str): The subfolder within the model subfolder (if applicable).
        token (Optional[Union[bool, str]]): The token to access the remote file (if applicable).
        cache_folder (Optional[str]): The folder to cache the downloaded file (if applicable).
        revision (Optional[str], optional): The revision of the file (if applicable). Defaults to None.
        local_files_only (bool, optional): Whether to only consider local files. Defaults to False.

    Returns:
        Optional[str]: The path to the loaded file, or None if the file could not be found or loaded.
    sentence-transformers)r'   r(   r"   library_namer    	cache_dirr#   N)r   existsr   r   nameparentas_posix	Exception)r   r'   r(   r    r!   r"   r#   	file_pathr   r   r   r$   @   s"    

r$   )r   r(   r    r!   r"   r#   r   c           	   	   C  s   t |tr| }t| |}| r,t|S | ||dvrB| dndd|||td}ztf i |}W n( ty   d|d< tf i |}Y n0 t||S )af  
    Loads the subfolder path for a given model name or path.

    Args:
        model_name_or_path (str): The name or path of the model.
        subfolder (str): The subfolder to load.
        token (Optional[Union[bool, str]]): The token for authentication.
        cache_folder (Optional[str]): The folder to cache the downloaded files.
        revision (Optional[str], optional): The revision of the model. Defaults to None.
        local_files_only (bool, optional): Whether to only use local files. Defaults to False.

    Returns:
        Optional[str]: The subfolder path if it exists, otherwise None.
    )r&   .z/**Nr)   )Zrepo_idr"   Zallow_patternsr*   r    r+   r#   Z
tqdm_classTr#   )
isinstancer   r/   r,   r   r   r   r0   )	r   r(   r    r!   r"   r#   Zdir_pathZdownload_kwargs	repo_pathr   r   r   load_dir_pathn   s(    

r5   r   )urlpathr   c           	      C  s  t j|dkr&t jt j|dd tj| dd}|jdkrftd|  d|j tj	d |
  d	S |d
 }t|dl}|jd}|d	urt|nd	}td|dd}|jddD ] }|r|t| || qW d	   n1 s0    Y  t || |  d	S )a  
    Downloads a URL to a given path on disk.

    Args:
        url (str): The URL to download.
        path (str): The path to save the downloaded file.

    Raises:
        requests.HTTPError: If the HTTP request returns a non-200 status code.

    Returns:
        None
    r&   T)exist_ok)stream   z"Exception when trying to download z. Response )fileNZ_partwbzContent-LengthB)unittotalZ
unit_scalei   )
chunk_size)osr7   dirnamemakedirsrequestsgetstatus_codeprintsysstderrraise_for_statusopenheadersintr   iter_contentupdatelenwriterenameclose)	r6   r7   reqZdownload_filepathZfile_binarycontent_lengthr?   progresschunkr   r   r   http_get   s$    
*rX   )NNNF)r&   NNNF)NNNF)
__future__r   rA   rH   pathlibr   rD   Zhuggingface_hubr   r   Ztqdm.autonotebookr   r   r%   r$   r5   rX   r   r   r   r   <module>   s0       #     1    2