
    rhJ                        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e      Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd
ZddZy)    )annotationsN)Path)hf_hub_downloadsnapshot_download)tqdmc                  .     e Zd ZdZ fdZd fdZ x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                .    d|d<   t        |   |i | y )NTdisable)super__init__)selfargskwargs	__class__s      u/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/util/file_io.pyr   zdisabled_tqdm.__init__   s     y$)&)    c                P    	 t         |   |       y# t        $ r	 |dk7  r Y yw xY w)zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)r   __delattr__AttributeError)r   attrr   s     r   r   zdisabled_tqdm.__delattr__   s3    	G% 	w 	s    %%)r   strreturnNone)__name__
__module____qualname____doc__r   r   __classcell__)r   s   @r   r	   r	      s    * r   r	   c           
     6    t        t        | 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)tokencache_folderrevisionlocal_files_only)boolload_file_path)model_name_or_pathr"   r#   r$   r%   s        r   is_sentence_transformer_modelr)       s+    ( %-	
	 	r   c           
         t        | ||      }|j                         rt        |      S t        ||      }	 t        | |j                  |j
                  j                         |d|||      S # t        $ r Y yw xY w)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)filename	subfolderr$   library_namer"   	cache_dirr%   N)r   existsr   r   nameparentas_posix	Exception)r(   r,   r-   r"   r#   r$   r%   	file_paths           r   r'   r'   @   s    2 'H=I9~ Y)I^^&&//10"-	
 		
  s   4A+ +	A76A7c           	     .   t        |t              r|j                         }t        | |      }|j                         rt	        |      S | ||dvr| dndd|||t
        d}	 t        di |}t        ||      S # t        $ r d|d<   t        di |}Y 'w xY w)	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.
    ) .z/**Nr+   )repo_idr$   allow_patternsr.   r"   r/   r%   
tqdm_classTr%    )
isinstancer   r3   r0   r   r	   r   r4   )	r(   r-   r"   r#   r$   r%   dir_pathdownload_kwargs	repo_paths	            r   load_dir_pathrA   n   s    , )T"&&(	 &	2H8} &/8	/IYKs+t/!,#	O9%88	
 	9%%	  9.2*+%88	9s   !A8 8BBc                   t         j                  j                  |      dk7  r4t        j                  t         j                  j                  |      d       t	        j
                  | d      }|j                  dk7  r<t        d|  d|j                   t        j                         |j                          y	|d
z   }t        |d      5 }|j                  j                  d      }|t        |      nd	}t        d|d      }|j                  d      D ]0  }|s|j!                  t#        |             |j%                  |       2 	 d	d	d	       t        j&                  ||       j)                          y	# 1 sw Y   0xY w)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
    r7   T)exist_ok)stream   z"Exception when trying to download z. Response )fileN_partwbzContent-LengthB)unittotal
unit_scalei   )
chunk_size)ospathdirnamemakedirsrequestsgetstatus_codeprintsysstderrraise_for_statusopenheadersintr   iter_contentupdatelenwriterenameclose)	urlrO   reqdownload_filepathfile_binarycontent_lengthrK   progresschunks	            r   http_getri      s:    
wwt"
BGGOOD)D9
,,s4
(C
#23%{3??BST[^[e[efw		& )+)9:'5'AN#tS$?%%%6 	)EE
+!!%(	)	) II&NN) )s   
AE8-E88F)NNNF)r(   r   r"   bool | str | Noner#   
str | Noner$   rk   r%   r&   r   r&   )r7   NNNF)r(   r   r,   z
str | Pathr-   r   r"   rj   r#   rk   r$   rk   r%   r&   r   rk   )r(   r   r-   r   r"   rj   r#   rk   r$   rk   r%   r&   r   rk   )rb   r   rO   r   r   r   )
__future__r   rN   rV   pathlibr   rR   huggingface_hubr   r   tqdm.autonotebookr   r	   r)   r'   rA   ri   r<   r   r   <module>rp      s0   " 	 
   > "D ,  $#"  	
  
F ##"+++ + 	+
 + + + +b  $#"/&/&/& /& 	/&
 /& /& /&d"r   