U
    9%e92                     @   s   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 erLddlmZ ded e	e e	e ed dd	d
ZeG dd dZeG dd dZG dd deZG dd deZG dd deZG dd deZdS )zz
Helpful utility functions and classes in relation to exploring API endpoints
with the aim for a user-friendly interface.
    N)	dataclass)TYPE_CHECKINGIterableListOptionalUnion   )	ModelInfor	   )modelsminimum_thresholdmaximum_thresholdreturnc                 c   s   |dkr|dkrt d|dkr$d}|dkr2tj}| D ]}t|dd}|dks6t|tsZq6|dd}t|trx|d }|s~q6tdt	|}|dkrq6t
|d}||kr6||kr6|V  q6dS )	a  Filters a list of models for those that include an emission tag and limit them to between two thresholds

    Args:
        models (Iterable of `ModelInfo`):
            A list of models to filter.
        minimum_threshold (`float`, *optional*):
            A minimum carbon threshold to filter by, such as 1.
        maximum_threshold (`float`, *optional*):
            A maximum carbon threshold to filter by, such as 10.
    NzFBoth `minimum_threshold` and `maximum_threshold` cannot both be `None`ZcardDataZco2_eq_emissionsZ	emissionsz\d+\.\d+|\d+r   )
ValueErrormathinfgetattr
isinstancedictgetresearchstrfloatgroup)r
   r   r   modelZ	card_dataZemissionZmatchedZemission_value r   e/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/huggingface_hub/utils/endpoint_helpers.py_filter_emissions   s*    
r   c                   @   s   e Zd ZU dZdZee ed< dZee	ee
e f  ed< dZee ed< dZee	ee
e f  ed< dZee	ee
e f  ed< dZee	ee
e f  ed< dZee	ee
e f  ed	< dZee	ee
e f  ed
< dZee	ee
e f  ed< dS )DatasetFiltera  
    A class that converts human-readable dataset search parameters into ones
    compatible with the REST API. For all parameters capitalization does not
    matter.

    Args:
        author (`str`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by the original uploader (author or organization), such as
            `facebook` or `huggingface`.
        benchmark (`str` or `List`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by their official benchmark.
        dataset_name (`str`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by its name, such as `SQAC` or `wikineural`
        language_creators (`str` or `List`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub with how the data was curated, such as `crowdsourced` or
            `machine_generated`.
        language (`str` or `List`, *optional*):
            A string or list of strings representing a two-character language to
            filter datasets by on the Hub.
        multilinguality (`str` or `List`, *optional*):
            A string or list of strings representing a filter for datasets that
            contain multiple languages.
        size_categories (`str` or `List`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by the size of the dataset such as `100K<n<1M` or
            `1M<n<10M`.
        task_categories (`str` or `List`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by the designed task, such as `audio_classification` or
            `named_entity_recognition`.
        task_ids (`str` or `List`, *optional*):
            A string or list of strings that can be used to identify datasets on
            the Hub by the specific task such as `speech_emotion_recognition` or
            `paraphrase`.

    Examples:

    ```py
    >>> from huggingface_hub import DatasetFilter

    >>> # Using author
    >>> new_filter = DatasetFilter(author="facebook")

    >>> # Using benchmark
    >>> new_filter = DatasetFilter(benchmark="raft")

    >>> # Using dataset_name
    >>> new_filter = DatasetFilter(dataset_name="wikineural")

    >>> # Using language_creator
    >>> new_filter = DatasetFilter(language_creator="crowdsourced")

    >>> # Using language
    >>> new_filter = DatasetFilter(language="en")

    >>> # Using multilinguality
    >>> new_filter = DatasetFilter(multilinguality="multilingual")

    >>> # Using size_categories
    >>> new_filter = DatasetFilter(size_categories="100K<n<1M")

    >>> # Using task_categories
    >>> new_filter = DatasetFilter(task_categories="audio_classification")

    >>> # Using task_ids
    >>> new_filter = DatasetFilter(task_ids="paraphrase")
    ```
    Nauthor	benchmarkdataset_namelanguage_creatorslanguagemultilingualitysize_categoriestask_categoriestask_ids)__name__
__module____qualname____doc__r    r   r   __annotations__r!   r   r   r"   r#   r$   r%   r&   r'   r(   r   r   r   r   r   F   s   
Ir   c                   @   s   e Zd ZU dZdZee ed< dZee	ee
e f  ed< dZee	ee
e f  ed< dZee ed< dZee	ee
e f  ed< dZee	ee
e f  ed< dZee	ee
e f  ed	< dS )
ModelFilteraB	  
    A class that converts human-readable model search parameters into ones
    compatible with the REST API. For all parameters capitalization does not
    matter.

    Args:
        author (`str`, *optional*):
            A string that can be used to identify models on the Hub by the
            original uploader (author or organization), such as `facebook` or
            `huggingface`.
        library (`str` or `List`, *optional*):
            A string or list of strings of foundational libraries models were
            originally trained from, such as pytorch, tensorflow, or allennlp.
        language (`str` or `List`, *optional*):
            A string or list of strings of languages, both by name and country
            code, such as "en" or "English"
        model_name (`str`, *optional*):
            A string that contain complete or partial names for models on the
            Hub, such as "bert" or "bert-base-cased"
        task (`str` or `List`, *optional*):
            A string or list of strings of tasks models were designed for, such
            as: "fill-mask" or "automatic-speech-recognition"
        tags (`str` or `List`, *optional*):
            A string tag or a list of tags to filter models on the Hub by, such
            as `text-generation` or `spacy`.
        trained_dataset (`str` or `List`, *optional*):
            A string tag or a list of string tags of the trained dataset for a
            model on the Hub.


    ```python
    >>> from huggingface_hub import ModelFilter

    >>> # For the author_or_organization
    >>> new_filter = ModelFilter(author_or_organization="facebook")

    >>> # For the library
    >>> new_filter = ModelFilter(library="pytorch")

    >>> # For the language
    >>> new_filter = ModelFilter(language="french")

    >>> # For the model_name
    >>> new_filter = ModelFilter(model_name="bert")

    >>> # For the task
    >>> new_filter = ModelFilter(task="text-classification")

    >>> # Retrieving tags using the `HfApi.get_model_tags` method
    >>> from huggingface_hub import HfApi

    >>> api = HfApi()
    # To list model tags

    >>> api.get_model_tags()
    # To list dataset tags

    >>> api.get_dataset_tags()
    >>> new_filter = ModelFilter(tags="benchmark:raft")

    >>> # Related to the dataset
    >>> new_filter = ModelFilter(trained_dataset="common_voice")
    ```
    Nr    libraryr$   
model_nametasktrained_datasettags)r)   r*   r+   r,   r    r   r   r-   r/   r   r   r$   r0   r1   r2   r3   r   r   r   r   r.      s   
Ar.   c                       sD   e Zd ZdZdd Z fddZdd Z fdd	Zd
d Z  Z	S )AttributeDictionarya-  
    `dict` subclass that also provides access to keys as attributes

    If a key starts with a number, it will exist in the dictionary but not as an
    attribute

    Example:

    ```python
    >>> d = AttributeDictionary()
    >>> d["test"] = "a"
    >>> print(d.test)  # prints "a"
    ```

    c                 C   s   || kr| | S t |d S NAttributeErrorselfkr   r   r   __getattr__   s    zAttributeDictionary.__getattr__c                    s$   | j t jf|d dk || d S )Nr   _)__setitem__super__setattr__)r9   r:   v	__class__r   r   r?      s    zAttributeDictionary.__setattr__c                 C   s   || kr| |= nt |d S r5   r6   r8   r   r   r   __delattr__  s    zAttributeDictionary.__delattr__c                    s(   t |  }dd |D }t  | S )Nc                 S   s    g | ]}| d d r|qS )r<    )replaceisalpha).0keyr   r   r   
<listcomp>
  s      z/AttributeDictionary.__dir__.<locals>.<listcomp>)sortedkeysr>   __dir__)r9   rK   rA   r   r   rL     s    zAttributeDictionary.__dir__c                 C   sH   d}t |  D ]2}|d| 7 }|dd s:|d7 }|d7 }q|S )NzAvailable Attributes or Keys:
z * r<   rD   z (Key only)
)rJ   rK   rE   rF   )r9   repr_strrH   r   r   r   __repr__  s    
zAttributeDictionary.__repr__)
r)   r*   r+   r,   r;   r?   rC   rL   rO   __classcell__r   r   rA   r   r4      s   r4   c                   @   s4   e Zd ZdZd	eee dddZedddZ	dS )
GeneralTagsa!  
    A namespace object holding all tags, filtered by `keys` If a tag starts with
    a number, it will only exist in the dictionary

    Example:
    ```python
    >>> a.b["1a"]  # will work
    >>> a["b"]["1a"]  # will work
    >>> # a.b.1a # will not work
    ```

    Args:
        tag_dictionary (`dict`):
            A dictionary of tags returned from the /api/***-tags-by-type api
            endpoint
        keys (`list`):
            A list of keys to unpack the `tag_dictionary` with, such as
            `["library","language"]`
    N)tag_dictionaryrK   c                 C   s4   || _ |d krt| j  }|D ]}| | q d S r5   )_tag_dictionarylistrK   _unpack_and_assign_dictionary)r9   rR   rK   rH   r   r   r   __init__,  s
    zGeneralTags.__init__)rH   c                 C   s^   t  }t| || | j|g D ]0}|d dddddd}|d ||< q || |< dS )	zYAssign nested attributes to `self.key` containing information as an `AttributeDictionary`label rD   -r<   .idN)r4   setattrrS   r   rE   )r9   rH   refitemrW   r   r   r   rU   3  s     z)GeneralTags._unpack_and_assign_dictionary)N)
r)   r*   r+   r,   r   r   rT   rV   r   rU   r   r   r   r   rQ     s   rQ   c                       s&   e Zd ZdZed fddZ  ZS )	ModelTagsa  
    A namespace object holding all available model tags If a tag starts with a
    number, it will only exist in the dictionary

    Example:

    ```python
    >>> a.dataset["1_5BArabicCorpus"]  # will work
    >>> a["dataset"]["1_5BArabicCorpus"]  # will work
    >>> # o.dataset.1_5BArabicCorpus # will not work
    ```

    Args:
        model_tag_dictionary (`dict`):
            A dictionary of valid model tags, returned from the
            /api/models-tags-by-type api endpoint
    )model_tag_dictionaryc                    s    dddddg}t  || d S )Nr/   r$   licenseZdatasetZpipeline_tagr>   rV   )r9   r`   rK   rA   r   r   rV   P  s    zModelTags.__init__r)   r*   r+   r,   r   rV   rP   r   r   rA   r   r_   =  s   r_   c                       s&   e Zd ZdZed fddZ  ZS )DatasetTagsa  
    A namespace object holding all available dataset tags If a tag starts with a
    number, it will only exist in the dictionary

    Example

    ```python
    >>> a.size_categories["100K<n<1M"]  # will work
    >>> a["size_categories"]["100K<n<1M"]  # will work
    >>> # o.size_categories.100K<n<1M # will not work
    ```

    Args:
        dataset_tag_dictionary (`dict`):
            A dictionary of valid dataset tags, returned from the
            /api/datasets-tags-by-type api endpoint
    )dataset_tag_dictionaryc                    s&   ddddddddg}t  || d S )	Nr$   r%   r#   r'   r&   r!   r(   ra   rb   )r9   re   rK   rA   r   r   rV   h  s    
zDatasetTags.__init__rc   r   r   rA   r   rd   U  s   rd   )NN)r,   r   r   dataclassesr   typingr   r   r   r   r   Zhf_apir	   r   r   r   r.   r   r4   rQ   r_   rd   r   r   r   r   <module>   s,     ,UK/&