
    g                     p   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	m
Z
 d dlZd dlZd dlmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlmZmZmZ dd	lmZ dd
lm Z  ddl!m"Z"m#Z#m$Z$  e#j%        e&          Z' ee(          j)        dz  dz  Z* ee(          j)        dz  dz  Z+ ej,        d          Z- G d d          Z. G d de.          Z/ G d de.          Z0 G d de.          Z1de2ded         fdZ3de
e2ef         dee         fdZ4de
e2ef         deddfdZ5dd ddddd!d"e2d#e2d$e2d%e2d&e2d'ed(e2d)e2d*ee2         d+e6d,ee2         d-ee2         d.ee2         d/ee2         defd0Z7e$dd ddddd dd1d2e2d3ed4ee2         d5e6d6ee2         d7ee2         d8ee2         d9ee2         d:e6d;ee2         de2fd<            Z8dS )=    N)Path)AnyDictLiteralOptionalTypeUnion)hf_hub_download)upload_file)CardDataDatasetCardData
EvalResultModelCardDataSpaceCardDataeval_results_to_model_indexmodel_index_to_eval_results)get_sessionis_jinja_available	yaml_dump   )	constants)EntryNotFoundError)SoftTemporaryDirectoryloggingvalidate_hf_hub_args	templateszmodelcard_template.mdzdatasetcard_template.mdz1^(\s*---[\r\n]+)([\S\s]*?)([\r\n]+---(\r\n|\n|$))c                      e Zd ZeZeZdZddede	fdZ
ed             Zej        defd            Zd Zd	eeef         fd
Ze	 	 	 ddeeef         dee         dee         de	fd            Zddee         fdZ	 	 	 	 	 	 	 dd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e	 	 d dedee         dee         fd            ZdS )!RepoCardmodelFcontentignore_metadata_errorsc                 "    || _         || _        dS )a  Initialize a RepoCard from string content. The content should be a
        Markdown file with a YAML block at the beginning and a Markdown body.

        Args:
            content (`str`): The content of the Markdown file.

        Example:
            ```python
            >>> from huggingface_hub.repocard import RepoCard
            >>> text = '''
            ... ---
            ... language: en
            ... license: mit
            ... ---
            ...
            ... # My repo
            ... '''
            >>> card = RepoCard(text)
            >>> card.data.to_dict()
            {'language': 'en', 'license': 'mit'}
            >>> card.text
            '\n# My repo\n'

            ```
        <Tip>
        Raises the following error:

            - [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
              when the content of the repo card metadata is not a dictionary.

        </Tip>
        N)r!   r    )selfr    r!   s      T/var/www/html/ai-engine/env/lib/python3.11/site-packages/huggingface_hub/repocard.py__init__zRepoCard.__init__+   s    H '=#    c                     t          | j                  pd}d| | j                            || j                   | d| | j         S )zLThe content of the RepoCard, including the YAML block and the Markdown body.
---)
line_breakoriginal_order)_detect_line_ending_contentdatato_yaml_original_ordertext)r#   r*   s     r$   r    zRepoCard.contentR   s     )77?4
 WZ  W!2!2jY]Ym!2!n!n  Wpz  W  W  @J  W  LP  LU  W  W  	Wr&   c                    || _         t                              |          }|rs|                    d          }||                                d         | _        t          j        |          }|i }t          |t                    st          d          n#t                              d           i }|| _         | j        di |d| j        i| _        t!          |                                          | _        dS )z Set the content of the RepoCard.   N)repo card metadata block should be a dictzBRepo card metadata block was not found. Setting CardData to empty.r!    )r-   REGEX_YAML_BLOCKsearchgroupendr1   yaml	safe_load
isinstancedict
ValueErrorloggerwarningcard_data_classr!   r.   listkeysr0   )r#   r    match
yaml_block	data_dicts        r$   r    zRepoCard.contentX   s       ''00 	 QJ		.DIz22I 	 i.. N !LMMMN NN_```IDI(D(ii9iiTMhiii	#INN$4$455r&   c                     | j         S N)r    )r#   s    r$   __str__zRepoCard.__str__s   s
    |r&   filepathc                     t          |          }|j                            dd           t          |ddd          5 }|                    t          |                      ddd           dS # 1 swxY w Y   dS )a{  Save a RepoCard to a file.

        Args:
            filepath (`Union[Path, str]`): Filepath to the markdown file to save.

        Example:
            ```python
            >>> from huggingface_hub.repocard import RepoCard
            >>> card = RepoCard("---\nlanguage: en\n---\n# This is a test repo card")
            >>> card.save("/tmp/test.md")

            ```
        T)parentsexist_okw utf-8modenewlineencodingN)r   parentmkdiropenwritestr)r#   rJ   fs      r$   savezRepoCard.savev   s     >>dT:::(b7CCC 	qGGCII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   #A//A36A3Nrepo_id_or_path	repo_typetokenc                    t          |                                          rt          |          }nZt          |t                    r2t          t	          |t
          j        |p| j        |                    }nt          d| d          |	                    ddd          5 } | |
                                |          cd	d	d	           S # 1 swxY w Y   d	S )
a  Initialize a RepoCard from a Hugging Face Hub repo's README.md or a local filepath.

        Args:
            repo_id_or_path (`Union[str, Path]`):
                The repo ID associated with a Hugging Face Hub repo or a local filepath.
            repo_type (`str`, *optional*):
                The type of Hugging Face repo to push to. Defaults to None, which will use use "model". Other options
                are "dataset" and "space". Not used when loading from a local filepath. If this is called from a child
                class, the default value will be the child class's `repo_type`.
            token (`str`, *optional*):
                Authentication token, obtained with `huggingface_hub.HfApi.login` method. Will default to the stored token.
            ignore_metadata_errors (`str`):
                If True, errors while parsing the metadata section will be ignored. Some information might be lost during
                the process. Use it at your own risk.

        Returns:
            [`huggingface_hub.repocard.RepoCard`]: The RepoCard (or subclass) initialized from the repo's
                README.md file or filepath.

        Example:
            ```python
            >>> from huggingface_hub.repocard import RepoCard
            >>> card = RepoCard.load("nateraw/food")
            >>> assert card.data.tags == ["generated_from_trainer", "image-classification", "pytorch"]

            ```
        )r]   r^   z.Cannot load RepoCard: path not found on disk (z).rrO   rP   rQ   )r!   N)r   existsr<   rY   r
   r   REPOCARD_NAMEr]   r>   rW   read)clsr\   r]   r^   r!   	card_pathrZ   s          r$   loadzRepoCard.load   sB   H   '')) 	c_--II-- 
	c#+'83=	   II aoaaabbb ^^b7^CC 	Pq3qvvxx8NOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	Ps   $CCCc                 0   |p| j         }|t          |           d}ddi}	 t                                          d||          }|                                 dS # t
          j        j        $ r&}|j        dk    rt          |j
                  |d}~ww xY w)a  Validates card against Hugging Face Hub's card validation logic.
        Using this function requires access to the internet, so it is only called
        internally by [`huggingface_hub.repocard.RepoCard.push_to_hub`].

        Args:
            repo_type (`str`, *optional*, defaults to "model"):
                The type of Hugging Face repo to push to. Options are "model", "dataset", and "space".
                If this function is called from a child class, the default will be the child class's `repo_type`.

        <Tip>
        Raises the following errors:

            - [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
              if the card fails validation checks.
            - [`HTTPError`](https://requests.readthedocs.io/en/latest/api/#requests.HTTPError)
              if the request to the Hub API fails for any other reason.

        </Tip>
        )repoTyper    Acceptz
text/plainz(https://huggingface.co/api/validate-yaml)headersi  N)r]   rY   r   postraise_for_statusrequests
exceptions	HTTPErrorstatus_coder>   r1   )r#   r]   bodyrj   r`   excs         r$   validatezRepoCard.validate   s    , /	 "4yy
 
 \*	""#Mt]d"eeA     ", 	 	 	}## (((			s   8A B/!BBrepo_idcommit_messagecommit_descriptionrevision	create_prparent_commitc	                 l   |p| j         }|                     |           t                      5 }	t          |	          t          j        z  }
|
                    t          |                      t          t          |
          t          j        ||||||||
  
        }ddd           n# 1 swxY w Y   |S )aA  Push a RepoCard to a Hugging Face Hub repo.

        Args:
            repo_id (`str`):
                The repo ID of the Hugging Face Hub repo to push to. Example: "nateraw/food".
            token (`str`, *optional*):
                Authentication token, obtained with `huggingface_hub.HfApi.login` method. Will default to
                the stored token.
            repo_type (`str`, *optional*, defaults to "model"):
                The type of Hugging Face repo to push to. Options are "model", "dataset", and "space". If this
                function is called by a child class, it will default to the child class's `repo_type`.
            commit_message (`str`, *optional*):
                The summary / title / first line of the generated commit.
            commit_description (`str`, *optional*)
                The description of the generated commit.
            revision (`str`, *optional*):
                The git revision to commit from. Defaults to the head of the `"main"` branch.
            create_pr (`bool`, *optional*):
                Whether or not to create a Pull Request with this commit. Defaults to `False`.
            parent_commit (`str`, *optional*):
                The OID / SHA of the parent commit, as a hexadecimal string. Shorthands (7 first characters) are also supported.
                If specified and `create_pr` is `False`, the commit will fail if `revision` does not point to `parent_commit`.
                If specified and `create_pr` is `True`, the pull request will be created from `parent_commit`.
                Specifying `parent_commit` ensures the repo has not changed before committing the changes, and can be
                especially useful if the repo is updated / committed to concurrently.
        Returns:
            `str`: URL of the commit which updated the card metadata.
        )r]   )
path_or_fileobjpath_in_report   r^   r]   ru   rv   rx   rw   ry   N)	r]   rs   r   r   r   rb   
write_textrY   r   )r#   rt   r^   r]   ru   rv   rw   rx   ry   tmpdirtmp_pathurls               r$   push_to_hubzRepoCard.push_to_hub   s    R /	 		***#%% 	F||i&==HD		*** #H&4#-#5#!+  C	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
s   A/B))B-0B-	card_datatemplate_pathtemplate_strc                    t                      rddl}nt          d          |                                                                }|                    |           |!t          |                                          }|&t          | j                                                  }|	                    |          } |j
        dd|                                i|} | |          S )a'  Initialize a RepoCard from a template. By default, it uses the default template.

        Templates are Jinja2 templates that can be customized by passing keyword arguments.

        Args:
            card_data (`huggingface_hub.CardData`):
                A huggingface_hub.CardData instance containing the metadata you want to include in the YAML
                header of the repo card on the Hugging Face Hub.
            template_path (`str`, *optional*):
                A path to a markdown file with optional Jinja template variables that can be filled
                in with `template_kwargs`. Defaults to the default template.

        Returns:
            [`huggingface_hub.repocard.RepoCard`]: A RepoCard instance with the specified card data and content from the
            template.
        r   NzjUsing RepoCard.from_template requires Jinja2 to be installed. Please install it with `pip install Jinja2`.r   r5   )r   jinja2ImportErrorto_dictcopyupdater   	read_textdefault_template_pathTemplaterenderr/   )	rd   r   r   r   template_kwargsr   kwargstemplater    s	            r$   from_templatezRepoCard.from_template&  s    0  	MMMM9  
 ""$$))++o&&&$..88::L 9::DDFFL??<00!(/JJI,=,=,?,?J6JJs7||r&   )F)NNFrH   )NNNNNNNNN)__name__
__module____qualname__r   rA   TEMPLATE_MODELCARD_PATHr   r]   rY   boolr%   propertyr    setterrI   r	   r   r[   classmethodr   rf   rs   r   r   r5   r&   r$   r   r   &   sH       O3I% % %T % % % %N W W XW
 ^6s 6 6 6 ^64  U49-    (  $(#',3P 3PsDy)3P C=3P }	3P
 !%3P 3P 3P [3Pj% %(3- % % % %T  $#'(,,0"&$('+= == }= C=	=
 != %SM= 3-= D>=  }= = = =~  (,&*	( ((  }( sm	( ( ( [( ( (r&   r   c            	       f     e Zd ZeZeZdZe	 	 ddede	e
         de	e
         f fd            Z xZS )	ModelCardr   Nr   r   r   c                 <     t                      j        |||fi |S )a
  Initialize a ModelCard from a template. By default, it uses the default template, which can be found here:
        https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md

        Templates are Jinja2 templates that can be customized by passing keyword arguments.

        Args:
            card_data (`huggingface_hub.ModelCardData`):
                A huggingface_hub.ModelCardData instance containing the metadata you want to include in the YAML
                header of the model card on the Hugging Face Hub.
            template_path (`str`, *optional*):
                A path to a markdown file with optional Jinja template variables that can be filled
                in with `template_kwargs`. Defaults to the default template.

        Returns:
            [`huggingface_hub.ModelCard`]: A ModelCard instance with the specified card data and content from the
            template.

        Example:
            ```python
            >>> from huggingface_hub import ModelCard, ModelCardData, EvalResult

            >>> # Using the Default Template
            >>> card_data = ModelCardData(
            ...     language='en',
            ...     license='mit',
            ...     library_name='timm',
            ...     tags=['image-classification', 'resnet'],
            ...     datasets=['beans'],
            ...     metrics=['accuracy'],
            ... )
            >>> card = ModelCard.from_template(
            ...     card_data,
            ...     model_description='This model does x + y...'
            ... )

            >>> # Including Evaluation Results
            >>> card_data = ModelCardData(
            ...     language='en',
            ...     tags=['image-classification', 'resnet'],
            ...     eval_results=[
            ...         EvalResult(
            ...             task_type='image-classification',
            ...             dataset_type='beans',
            ...             dataset_name='Beans',
            ...             metric_type='accuracy',
            ...             metric_value=0.9,
            ...         ),
            ...     ],
            ...     model_name='my-cool-model',
            ... )
            >>> card = ModelCard.from_template(card_data)

            >>> # Using a Custom Template
            >>> card_data = ModelCardData(
            ...     language='en',
            ...     tags=['image-classification', 'resnet']
            ... )
            >>> card = ModelCard.from_template(
            ...     card_data=card_data,
            ...     template_path='./src/huggingface_hub/templates/modelcard_template.md',
            ...     custom_template_var='custom value',  # will be replaced in template if it exists
            ... )

            ```
        superr   rd   r   r   r   r   	__class__s        r$   r   zModelCard.from_templateW  s)    R %uww$Y|_____r&   r   )r   r   r   r   rA   r   r   r]   r   r   rY   r   __classcell__r   s   @r$   r   r   R  s        #O3I (,&*	H` H` H`  }H` sm	H` H` H` H` H` [H` H` H` H` H`r&   r   c            	       f     e Zd ZeZeZdZe	 	 ddede	e
         de	e
         f fd            Z xZS )DatasetCarddatasetNr   r   r   c                 <     t                      j        |||fi |S )a  Initialize a DatasetCard from a template. By default, it uses the default template, which can be found here:
        https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/datasetcard_template.md

        Templates are Jinja2 templates that can be customized by passing keyword arguments.

        Args:
            card_data (`huggingface_hub.DatasetCardData`):
                A huggingface_hub.DatasetCardData instance containing the metadata you want to include in the YAML
                header of the dataset card on the Hugging Face Hub.
            template_path (`str`, *optional*):
                A path to a markdown file with optional Jinja template variables that can be filled
                in with `template_kwargs`. Defaults to the default template.

        Returns:
            [`huggingface_hub.DatasetCard`]: A DatasetCard instance with the specified card data and content from the
            template.

        Example:
            ```python
            >>> from huggingface_hub import DatasetCard, DatasetCardData

            >>> # Using the Default Template
            >>> card_data = DatasetCardData(
            ...     language='en',
            ...     license='mit',
            ...     annotations_creators='crowdsourced',
            ...     task_categories=['text-classification'],
            ...     task_ids=['sentiment-classification', 'text-scoring'],
            ...     multilinguality='monolingual',
            ...     pretty_name='My Text Classification Dataset',
            ... )
            >>> card = DatasetCard.from_template(
            ...     card_data,
            ...     pretty_name=card_data.pretty_name,
            ... )

            >>> # Using a Custom Template
            >>> card_data = DatasetCardData(
            ...     language='en',
            ...     license='mit',
            ... )
            >>> card = DatasetCard.from_template(
            ...     card_data=card_data,
            ...     template_path='./src/huggingface_hub/templates/datasetcard_template.md',
            ...     custom_template_var='custom value',  # will be replaced in template if it exists
            ... )

            ```
        r   r   s        r$   r   zDatasetCard.from_template  s)    r %uww$Y|_____r&   r   )r   r   r   r   rA   TEMPLATE_DATASETCARD_PATHr   r]   r   r   rY   r   r   r   s   @r$   r   r     s        %O5I (,&*	8` 8`"8`  }8` sm	8` 8` 8` 8` 8` [8` 8` 8` 8` 8`r&   r   c                       e Zd ZeZeZdZdS )	SpaceCardspaceN)r   r   r   r   rA   r   r   r]   r5   r&   r$   r   r     s        #O3IIIr&   r   r    return)r(   
Nc                     |                      d          }|                      d          }|                      d          }||z   dk    rdS ||k    r||k    rdS ||k    rdS dS )zDetect the line ending of a string. Used by RepoCard to avoid making huge diff on newlines.

    Uses same implementation as in Hub server, keep it in sync.

    Returns:
        str: The detected line ending of the string.
    r   r(   r   r   N)count)r    crlfcrlfs       r$   r,   r,     sr     
t		B	t		B==  D	Bw!||trzzdbjjv	Bwwttr&   
local_pathc                 "   t          |                                           }t                              |          }|rQ|                    d          }t          j        |          }|t          |t                    r|S t          d          d S )Nr3   r4   )
r   r   r6   r7   r8   r:   r;   r<   r=   r>   )r   r    rD   rE   r.   s        r$   metadata_loadr     s    :((**G##G,,E [[^^
~j))<:dD11<KDEEEtr&   r.   c                    d}d}t           j                            |           rt          | ddd          5 }|                                }t          |j        t                    r|j        d         }n!t          |j        t                    r|j        }ddd           n# 1 swxY w Y   t          | ddd          5 }t          |d	|
          }t                              |          }|rE|d|                                         d| | d| z   ||                                d         z   }nd| | d| | }|                    |           |                                 ddd           dS # 1 swxY w Y   dS )a&  
    Save the metadata dict in the upper YAML part Trying to preserve newlines as
    in the existing file. Docs about open() with newline="" parameter:
    https://docs.python.org/3/library/functions.html?highlight=open#open Does
    not work with "^M" linebreaks, which are replaced by 

    r(   rO   r`   utf8)rS   rT   r   NrN   F)	sort_keysr*   r)   )ospathra   rW   rc   r<   newlinestuplerY   r   r6   r7   startr9   rX   close)r   r.   r*   r    readme	data_yamlrD   outputs           r$   metadata_saver     s    JG	w~~j!! -*c2??? 	-6kkmmG&/511 -#_Q/

FOS11 -#_
	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 
j#rF	;	;	; 
vde
KKK	 ''00 	K_u{{}}_-0\j0\)0\0\PZ0\0\\_fglgpgpgrgrgtgt_uuFFJ:JyJJZJJJFV
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s%   AB!!B%(B%?B)E55E9<E9F)metrics_configmetrics_verifieddataset_configdataset_splitdataset_revisionmetrics_verification_tokenmodel_pretty_nametask_pretty_nametask_idmetrics_pretty_name
metrics_idmetrics_valuedataset_pretty_name
dataset_idr   r   r   r   r   r   c                 ^    dt          | t          |||||||||	||
||          g          iS )u  
    Creates a metadata dict with the result from a model evaluated on a dataset.

    Args:
        model_pretty_name (`str`):
            The name of the model in natural language.
        task_pretty_name (`str`):
            The name of a task in natural language.
        task_id (`str`):
            Example: automatic-speech-recognition. A task id.
        metrics_pretty_name (`str`):
            A name for the metric in natural language. Example: Test WER.
        metrics_id (`str`):
            Example: wer. A metric id from https://hf.co/metrics.
        metrics_value (`Any`):
            The value from the metric. Example: 20.0 or "20.0 ± 1.2".
        dataset_pretty_name (`str`):
            The name of the dataset in natural language.
        dataset_id (`str`):
            Example: common_voice. A dataset id from https://hf.co/datasets.
        metrics_config (`str`, *optional*):
            The name of the metric configuration used in `load_metric()`.
            Example: bleurt-large-512 in `load_metric("bleurt", "bleurt-large-512")`.
        metrics_verified (`bool`, *optional*, defaults to `False`):
            Indicates whether the metrics originate from Hugging Face's [evaluation service](https://huggingface.co/spaces/autoevaluate/model-evaluator) or not. Automatically computed by Hugging Face, do not set.
        dataset_config (`str`, *optional*):
            Example: fr. The name of the dataset configuration used in `load_dataset()`.
        dataset_split (`str`, *optional*):
            Example: test. The name of the dataset split used in `load_dataset()`.
        dataset_revision (`str`, *optional*):
            Example: 5503434ddd753f426f4b38109466949a1217c2bb. The name of the dataset dataset revision
            used in `load_dataset()`.
        metrics_verification_token (`bool`, *optional*):
            A JSON Web Token that is used to verify whether the metrics originate from Hugging Face's [evaluation service](https://huggingface.co/spaces/autoevaluate/model-evaluator) or not.

    Returns:
        `dict`: a metadata dict with the result from a model evaluated on a dataset.

    Example:
        ```python
        >>> from huggingface_hub import metadata_eval_result
        >>> results = metadata_eval_result(
        ...         model_pretty_name="RoBERTa fine-tuned on ReactionGIF",
        ...         task_pretty_name="Text Classification",
        ...         task_id="text-classification",
        ...         metrics_pretty_name="Accuracy",
        ...         metrics_id="accuracy",
        ...         metrics_value=0.2662102282047272,
        ...         dataset_pretty_name="ReactionJPEG",
        ...         dataset_id="julien-c/reactionjpeg",
        ...         dataset_config="default",
        ...         dataset_split="test",
        ... )
        >>> results == {
        ...     'model-index': [
        ...         {
        ...             'name': 'RoBERTa fine-tuned on ReactionGIF',
        ...             'results': [
        ...                 {
        ...                     'task': {
        ...                         'type': 'text-classification',
        ...                         'name': 'Text Classification'
        ...                     },
        ...                     'dataset': {
        ...                         'name': 'ReactionJPEG',
        ...                         'type': 'julien-c/reactionjpeg',
        ...                         'config': 'default',
        ...                         'split': 'test'
        ...                     },
        ...                     'metrics': [
        ...                         {
        ...                             'type': 'accuracy',
        ...                             'value': 0.2662102282047272,
        ...                             'name': 'Accuracy',
        ...                             'verified': False
        ...                         }
        ...                     ]
        ...                 }
        ...             ]
        ...         }
        ...     ]
        ... }
        True

        ```
    model-index)	task_name	task_typemetric_namemetric_typemetric_valuedataset_namedataset_typemetric_configverifiedverify_tokenr   r   r   )
model_nameeval_results)r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                 r$   metadata_eval_resultr   ,  se    R 	2(.% 3 *!.!4!+"0-!;#1"/%5  
 
 
 r&   )r]   	overwriter^   ru   rv   rw   rx   ry   rt   metadatar]   r   r^   ru   rv   rw   rx   ry   c          
      b   ||nd}||dk    rt           }
n.|dk    rt          }
n |dk    rt          }
nt          d|           	 |
                    | ||          }nF# t
          $ r9 |dk    rt          d          |
                    t                                }Y nw xY w|                                D ]Y\  }}|d	k    rd
|d         vrt          |d|           |d         d
<   t          |          \  }}|j        j        ||j        _        ||j        _        g|j        j        }|D ]}d}|D ]b}|                    |          rK||k    r"|s t          d|j         d|j         d          d}|j        |_        |j        du r|j        |_        c|s|j        j                            |           |j                            |          3|s1|j                            |          |k    rt          d| d          ||j        |<   [|                    | |||||||	          S )a  
    Updates the metadata in the README.md of a repository on the Hugging Face Hub.
    If the README.md file doesn't exist yet, a new one is created with metadata and an
    the default ModelCard or DatasetCard template. For `space` repo, an error is thrown
    as a Space cannot exist without a `README.md` file.

    Args:
        repo_id (`str`):
            The name of the repository.
        metadata (`dict`):
            A dictionary containing the metadata to be updated.
        repo_type (`str`, *optional*):
            Set to `"dataset"` or `"space"` if updating to a dataset or space,
            `None` or `"model"` if updating to a model. Default is `None`.
        overwrite (`bool`, *optional*, defaults to `False`):
            If set to `True` an existing field can be overwritten, otherwise
            attempting to overwrite an existing field will cause an error.
        token (`str`, *optional*):
            The Hugging Face authentication token.
        commit_message (`str`, *optional*):
            The summary / title / first line of the generated commit. Defaults to
            `f"Update metadata with huggingface_hub"`
        commit_description (`str` *optional*)
            The description of the generated commit
        revision (`str`, *optional*):
            The git revision to commit from. Defaults to the head of the
            `"main"` branch.
        create_pr (`boolean`, *optional*):
            Whether or not to create a Pull Request from `revision` with that commit.
            Defaults to `False`.
        parent_commit (`str`, *optional*):
            The OID / SHA of the parent commit, as a hexadecimal string. Shorthands (7 first characters) are also supported.
            If specified and `create_pr` is `False`, the commit will fail if `revision` does not point to `parent_commit`.
            If specified and `create_pr` is `True`, the pull request will be created from `parent_commit`.
            Specifying `parent_commit` ensures the repo has not changed before committing the changes, and can be
            especially useful if the repo is updated / committed to concurrently.
    Returns:
        `str`: URL of the commit which updated the card metadata.

    Example:
        ```python
        >>> from huggingface_hub import metadata_update
        >>> metadata = {'model-index': [{'name': 'RoBERTa fine-tuned on ReactionGIF',
        ...             'results': [{'dataset': {'name': 'ReactionGIF',
        ...                                      'type': 'julien-c/reactiongif'},
        ...                           'metrics': [{'name': 'Recall',
        ...                                        'type': 'recall',
        ...                                        'value': 0.7762102282047272}],
        ...                          'task': {'name': 'Text Classification',
        ...                                   'type': 'text-classification'}}]}]}
        >>> url = metadata_update("hf-internal-testing/reactiongif-roberta-card", metadata)

        ```
    Nz$Update metadata with huggingface_hubr   r   r   zUnknown repo_type: )r^   r]   zJCannot update metadata on a Space that doesn't contain a `README.md` file.r   namer   r   Fz6You passed a new value for the existing metric 'name: z, type: z6'. Set `overwrite=True` to overwrite existing metrics.Tz9You passed a new value for the existing meta data field 'z7'. Set `overwrite=True` to overwrite existing metadata.)r^   r]   ru   rv   rx   rw   ry   )r   r   r   r>   rf   r   r   r   itemsgetattrr   r.   r   r   is_equal_except_valuer   r   r   r   r   appendgetr   )rt   r   r]   r   r^   ru   rv   rw   rx   ry   
card_classcardkeyvaluer   new_resultsexisting_results
new_resultresult_foundexisting_results                       r$   metadata_updater     s   H (6'A^^GmN I00

	i		 

	g		

:y::;;;4weyII 4 4 4ijjj ''

334 nn&& +' +'
U-U1X%%#*4w#G#Ga &A%&H&H#Jy%-)4	&'1	$$#'9#9  #. B BJ#(L+; W W%;;OLL W)_<<Y<&0%F/9/E%F %F'1'=%F %F %F'" '" !" ,0L;E;RO8.74???I?V <' B	.55jAAA!B& y}}S!!-i-DIMMRUDVDVZ_D_D_ LPS L L L  
 "'	#%-#  	 	 	s   A A B"!B")9r   repathlibr   typingr   r   r   r   r   r	   rm   r:   huggingface_hub.file_downloadr
   huggingface_hub.hf_apir   huggingface_hub.repocard_datar   r   r   r   r   r   r   huggingface_hub.utilsr   r   r   rO   r   errorsr   utilsr   r   r   
get_loggerr   r?   __file__rU   r   r   compiler6   r   r   r   r   rY   r,   r   r   r   r   r   r5   r&   r$   <module>r     s   				 				       < < < < < < < < < < < < < < < <   9 9 9 9 9 9 . . . . . .                  M L L L L L L L L L       & & & & & & H H H H H H H H H H 
	H	%	% $x../+=@WW  DNN1K?B[[  2:RSS i i i i i i i iX	N` N` N` N` N` N` N` N`b>` >` >` >` >`( >` >` >`B        1I)J    *
eCI. 
8D> 
 
 
 
eCI. d t    T %)"$(#'&*04} } }} } 	}
 } } } } } SM} } SM} C=} sm} !)}  
!} } } }@ 
  $$((,"#'Q Q QQQ }	Q
 Q C=Q SMQ !Q smQ Q C=Q 	Q Q Q Q Q Qr&   