
    ;3h"                        d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ erddlmZ  G d de      Z G d de      Z e        G d de             Zy)zgr.Label() component.    )annotationsN)CallableSequence)Path)TYPE_CHECKINGAnyOptionalUnion)document)	Component)GradioModel)Events)I18nData)Timerc                  *    e Zd ZU dZded<   dZded<   y)LabelConfidenceN Optional[Union[str, int, float]]labelzOptional[float]
confidence)__name__
__module____qualname__r   __annotations__r        W/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/label.pyr   r      s    .2E+2"&J&r   r   c                  *    e Zd ZU dZded<   dZded<   y)	LabelDataNr   r   zOptional[list[LabelConfidence]]confidences)r   r   r   r   r   r   r   r   r   r   r      s    .2E+237K07r   r   c                       e Zd ZdZdZeZej                  ej                  gZ
	 dddddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 dd	Z	 	 	 	 dd
ZddZddZ xZS )LabelaB  
    Displays a classification label, along with confidence scores of top categories, if provided. As this component does not
    accept user input, it is rarely used as an input component.

    Guides: image-classification-in-pytorch, image-classification-in-tensorflow, image-classification-with-vision-transformers
    r   NT   value)num_top_classesr   everyinputs
show_label	containerscale	min_widthvisibleelem_idelem_classesrenderkeypreserved_by_keycolorshow_headingc               x    || _         || _        || _        t        |   |||||||	|
||||||       d| _        y)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 a function is provided, the function will be called each time the app loads to set the initial value of this component.
            num_top_classes: number of most confident classes to show.
            label: the label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
            every: Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
            inputs: Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
            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 size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
            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.
            render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
            key: in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.
            preserved_by_key: A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.
            color: The background color of the label (either a valid css color name or hexadecimal string).
            show_heading: If False, the heading will not be displayed if a dictionary of labels and confidences is provided. The heading will still be visible if the value is a string or number.
        )r   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r#   z\a dictionary mapping string categories to float values that represent confidence from 0 - 1.N)r$   r1   r2   super__init___value_description)selfr#   r$   r   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   	__class__s                     r   r5   zLabel.__init__-   sd    R  /
(!%- 	 	
  #Ar   c                    |y|j                   |j                  S |j                         d   D ci c]  }|d   |d    c}S c c}w )a
  
        Parameters:
            payload: An instance of `LabelData` containing the label and confidences.
        Returns:
            Depending on the value, passes the label as a `str | int | float`, or the labels and confidences as a `dict[str, float]`.
        Nr   r   r   )r   r   
model_dump)r7   payloadds      r   
preprocesszLabel.preprocessk   sZ     ?&== 181C1C1Em1T
,-AgJ,'
 	
 
s   Ac                   ||i k(  ri S t        |t              r_|j                  d      rNt        |      j	                         r5t        di t        j                  t        |      j                               S t        |t        t        t        f      rt        t        |            S t        |t              rd|v r.t        |d   t              r|d   }|D ci c]  }|d   |d    }}t        |j                         t        j                  d      d	      }| j                   |d| j                    }t        |d
   d
   |D cg c]  }t#        |d
   |d          c}      S t%        dt'        |             c c}w c c}w )a  
        Parameters:
            value: 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 one of the preceding formats.
        Returns:
            Returns a `LabelData` object with the label and confidences, or a `dict` of the same format, or a `str` or `int` or `float` if the input was a single label.
        Nz.json)r   r   r   r      T)r/   reverser   r   r   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 r   )
isinstancestrendswithr   existsr   jsonloads	read_textfloatintdictsorteditemsoperator
itemgetterr$   r   
ValueErrortype)r7   r#   csorted_predpreds        r   postprocesszLabel.postprocess|   sv    =ERKIeS!ennW&=$u+BTBTBVCtzz$u+*?*?*ABCCec5#./3u:..eT"%*U=5I4*Pm,>CD7Q|_4DD 8#6#6q#94K ##/)*@D,@,@A!!nQ' !, $$q'd1gF  "5k],
 	
 Es   E>F
c                    dddddddgdS )NCatcat?rA   dog皙?rB   r   r7   s    r   example_payloadzLabel.example_payload   s$    s3s3
 	
r   c                    dddS )NrZ   r\   )rY   r[   r   r]   s    r   example_valuezLabel.example_value   s    3''r   )N)"r#   z0dict[str, float] | str | float | Callable | Noner$   
int | Noner   zstr | I18nData | Noner%   zTimer | float | Noner&   z7Component | Sequence[Component] | set[Component] | Noner'   zbool | Noner(   boolr)   ra   r*   rK   r+   rb   r,   
str | Noner-   list[str] | str | Noner.   rb   r/   z(int | str | tuple[int | str, ...] | Noner0   rd   r1   rc   r2   rb   )r;   zLabelData | Nonereturnz+dict[str, float] | str | int | float | None)r#   z3dict[str | float, float] | str | int | float | Nonere   zLabelData | dict | None)re   r   )r   r   r   __doc__CONFIDENCES_KEYr   
data_modelr   changeselectEVENTSr5   r=   rV   r^   r`   __classcell__)r8   s   @r   r!   r!       s[    $OJmmV]]+F CG<A '+'+&*JN"& "/38<3: !'<A?<A $	<A
 %<A $<A H<A  <A <A <A <A <A <A -<A <A  6!<A" 1#<A$ %<A& '<A|
'
	4
"#
H#
	 #
J
(r   r!   )rf   
__future__r   rG   rO   collections.abcr   r   pathlibr   typingr   r   r	   r
   gradio_client.documentationr   gradio.components.baser   gradio.data_classesr   gradio.eventsr   gradio.i18nr   gradio.componentsr   r   r   r!   r   r   r   <module>rw      sj     "   .  6 6 0 , +    ''k '
8 8
 
J(I J( J(r   