o
    e                     @  s   d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ed e
 G dd deeeeZdS )zgr.Label() component.    )annotationsN)Path)CallableLiteral)documentset_documentation_group)JSONSerializable)IOComponent	_Keywords)warn_style_method_deprecation)
ChangeableEventListenerMethod
Selectable	componentc                   @  s   e Zd ZdZdZ	d,ddddddddddddd-ddZdd Zd.d#d$Zee	j
dddddde	j
fd/d'd(Zdd)d0d*d+ZdS )1Labelai  
    Displays a classification label, along with confidence scores of top categories, if provided.
    Preprocessing: this component does *not* accept input.
    Postprocessing: expects a {Dict[str, float]} of classes and confidences, or {str} with just the class or an {int}/{float} for regression outputs, or a {str} path to a .json file containing a json dictionary in the structure produced by Label.postprocess().

    Demos: main_note, titanic_survival
    Guides: image-classification-in-pytorch, image-classification-in-tensorflow, image-classification-with-vision-transformers, building-a-pictionary-app
    confidencesNT   )num_top_classeslabelevery
show_label	containerscale	min_widthvisibleelem_idelem_classescolorvalue0dict[str, float] | str | float | Callable | Noner   
int | Noner   
str | Noner   float | Noner   bool | Noner   boolr   r   intr   r   r   list[str] | str | Noner   c                K  s>   || _ || _|  	 tj| f|||||||	|
||d
| dS )a]  
        Parameters:
            value: Default value to show in the component. If a str or number is provided, simply displays the string or number. If a {Dict[str, float]} of classes and confidences is provided, displays the top class on top and the `num_top_classes` below, along with their confidence bars. If callable, the function will be called whenever the app loads to set the initial value of the component.
            num_top_classes: number of most confident classes to show.
            label: component name in interface.
            every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
            show_label: if True, will display label.
            container: If True, will place the component in a container - providing some extra padding around the border.
            scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
            min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
            visible: If False, component will be hidden.
            elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
            elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
            color: The background color of the label (either a valid css color name or hexadecimal string).
        )
r   r   r   r   r   r   r   r   r   r   N)r   r   r	   __init__)selfr   r   r   r   r   r   r   r   r   r   r   r   kwargs r*   GD:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/label.pyr'   &   s(    
zLabel.__init__c                 C  s    | j | j| j| jdt| S )N)r   r   r   
selectable)r   r   r   r,   r	   
get_config)r(   r*   r*   r+   r-   ]   s   zLabel.get_configy%dict[str, float] | str | float | Nonereturndict | Nonec                 C  s   |du s|i kr
i S t |tr|drt| r| |S t |tttfr-dt|iS t |trnd|v rHt |d trH|d }dd |D }t	|
 tddd	}| jdur`|d| j }|d
 d
 dd |D dS tdt| )a  
        Parameters:
            y: a dictionary mapping labels to confidence value, or just a string/numerical label by itself
        Returns:
            Object with key 'label' representing primary label, and key 'confidences' representing a list of label-confidence pairs
        Nz.jsonr   r   c                 S  s   i | ]	}|d  |d qS r   
confidencer*   ).0cr*   r*   r+   
<dictcomp>v   s    z%Label.postprocess.<locals>.<dictcomp>   T)keyreverser   c                 S  s   g | ]}|d  |d dqS )r   r7   r2   r*   )r4   predr*   r*   r+   
<listcomp>|   s    z%Label.postprocess.<locals>.<listcomp>)r   r   zThe `Label` output interface expects one of: a string label, or an int label, a float label, or a dictionary whose keys are labels and values are confidences. Instead, got a )
isinstancestrendswithr   exists	serializefloatr%   dictsorteditemsoperator
itemgetterr   
ValueErrortype)r(   r.   Zsorted_predr*   r*   r+   postprocessf   s.    



zLabel.postprocessCdict[str, float] | str | float | Literal[_Keywords.NO_VALUE] | None(str | Literal[_Keywords.NO_VALUE] | Nonec              
   C  s4   |t ju rd }n|d u rd}||||||| |dd	S )Ntransparentupdate)	r   r   r   r   r   r   r   r   Z__type__)r
   NO_VALUE)r   r   r   r   r   r   r   r   r*   r*   r+   rM      s   
zLabel.update)r   c                C  s   t   |dur
|| _| S )zc
        This method is deprecated. Please set these arguments in the constructor instead.
        N)r   r   )r(   r   r*   r*   r+   style   s   zLabel.style)N)r   r   r   r    r   r!   r   r"   r   r#   r   r$   r   r    r   r%   r   r$   r   r!   r   r&   r   r!   )r.   r/   r0   r1   )r   rJ   r   r!   r   r#   r   r#   r   r    r   r    r   r#   r   rK   )r   r#   )__name__
__module____qualname____doc__ZCONFIDENCES_KEYr'   r-   rI   staticmethodr
   rN   rM   rO   r*   r*   r*   r+   r      s>    	7
	 'r   )rS   
__future__r   rE   pathlibr   typingr   r   Zgradio_client.documentationr   r   Zgradio_client.serializingr   Zgradio.components.baser	   r
   Zgradio.deprecationr   Zgradio.eventsr   r   r   r   r*   r*   r*   r+   <module>   s    