
    hh6                     V    d dl mZmZmZ d dlZd dlmZ d dlmZ  G d de          Z	dS )    )IteratorListOptionalN)Document)
BaseLoaderc                      e Zd ZU dZdZeed<   dZeed<   dddddddddd	ee         d
ee         dee         dee         dee         dee	         dee
         dee
         fdZdee         fdZdedefdZdee         fdZdS )HuggingFaceModelLoadera  
    Load model information from `Hugging Face Hub`, including README content.

    This loader interfaces with the Hugging Face Models API to fetch and load
    model metadata and README files.
    The API allows you to search and filter models based on specific criteria
    such as model tags, authors, and more.

    API URL: https://huggingface.co/api/models
    DOC URL: https://huggingface.co/docs/hub/en/api

    Examples:

        .. code-block:: python

            from langchain_community.document_loaders import HuggingFaceModelLoader

            # Initialize the loader with search criteria
            loader = HuggingFaceModelLoader(search="bert", limit=10)

            # Load models
            documents = loader.load()

            # Iterate through the fetched documents
            for doc in documents:
                print(doc.page_content)  # README content of the model
                print(doc.metadata)      # Metadata of the model
    z!https://huggingface.co/api/modelsBASE_URLz4https://huggingface.co/{model_id}/raw/main/README.mdREADME_BASE_URLN   searchauthorfiltersort	directionlimitfullconfigr   r   r   r   r   r   r   r   c          	      &    ||||||||d| _         dS )a  Initialize the HuggingFaceModelLoader.

        Args:
            search: Filter based on substrings for repos and their usernames.
            author: Filter models by an author or organization.
            filter: Filter based on tags.
            sort: Property to use when sorting.
            direction: Direction in which to sort.
            limit: Limit the number of models fetched.
            full: Whether to fetch most model data.
            config: Whether to also fetch the repo config.
        r   Nparams)	selfr   r   r   r   r   r   r   r   s	            t/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/document_loaders/hugging_face_model.py__init__zHuggingFaceModelLoader.__init__*   s.    4 "	
 	
    returnc                     t          j        | j        d | j                                        D                       }|                                 |                                S )z.Fetch model information from Hugging Face Hub.c                     i | ]
\  }}|||S )N ).0kvs      r   
<dictcomp>z7HuggingFaceModelLoader.fetch_models.<locals>.<dictcomp>R   s    JJJTQAMAqMMMr   r   )requestsgetr
   r   itemsraise_for_statusjson)r   responses     r   fetch_modelsz#HuggingFaceModelLoader.fetch_modelsN   s^    <MJJT[%6%6%8%8JJJ
 
 
 	!!###}}r   model_idc                     | j                             |          }	 t          j        |          }|                                 |j        S # t          j        $ r Y dS w xY w)z+Fetch the README content for a given model.)r,   z$README not available for this model.)r   formatr%   r&   r(   textRequestException)r   r,   
readme_urlr*   s       r   fetch_readme_contentz+HuggingFaceModelLoader.fetch_readme_contentW   sp    )00(0CC
	:|J//H%%'''= ( 	: 	: 	:999	:s   .A AAc              #      K   |                                  }|D ]@}|                    dd          }|                     |          }t          ||          V  AdS )z8Load model information lazily, including README content.modelId )page_contentmetadataN)r+   r&   r2   r   )r   modelsmodelr,   readme_contents        r   	lazy_loadz HuggingFaceModelLoader.lazy_loada   s      ""$$ 	 	EyyB//H!66x@@N+      		 	r   )__name__
__module____qualname____doc__r
   str__annotations__r   r   intboolr   r   dictr+   r2   r   r   r;   r    r   r   r	   r	   	   sL         : 8Hc777QOSQQQ
 !% $ $"#' #!%"
 "
 "
 "
 	"

 "
 sm"
 C="
 }"
 tn"
 "
 "
 "
 "
Hd4j    :S :S : : : :8H-      r   r	   )
typingr   r   r   r%   langchain_core.documentsr   )langchain_community.document_loaders.baser   r	   r    r   r   <module>rH      s    + + + + + + + + + +  - - - - - - @ @ @ @ @ @c c c c cZ c c c c cr   