
    ;3h4(                        d Z ddlmZ ddl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 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.HighlightedText() component.    )annotations)CallableSequence)TYPE_CHECKINGAnyUnion)document)	Component)GradioModelGradioRootModel)Events)I18nData)Timerc                  &    e Zd ZU ded<   dZded<   y)HighlightedTokenstrtokenNzUnion[str, float, None]class_or_confidence)__name__
__module____qualname____annotations__r        b/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/highlighted_text.pyr   r      s    J3707r   r   c                      e Zd ZU ded<   y)HighlightedTextDatazlist[HighlightedToken]rootN)r   r   r   r   r   r   r   r   r      s    
  r   r   c                  
    e 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ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z
dd
ZddZ	 	 	 	 ddZ	 	 	 	 ddZ xZS )HighlightedTextz
    Displays text that contains spans that are highlighted by category or numerical value.

    Demos: diff_texts
    Guides: named-entity-recognition
    NFT    value)	color_mapshow_legendshow_inline_categorycombine_adjacentadjacent_separatorlabeleveryinputs
show_label	containerscale	min_widthvisibleelem_idelem_classesrenderkeypreserved_by_keyinteractivertlc                   || _         || _        || _        || _        || _        || _        t        |   |||	|
|||||||||||       d| _        y)a  
        Parameters:
            value: Default value to show. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
            color_map: A dictionary mapping labels to colors. The colors may be specified as hex codes or by their names. For example: {"person": "red", "location": "#FFEE22"}
            show_legend: whether to show span categories in a separate legend or inline.
            show_inline_category: If False, will not display span category label. Only applies if show_legend=False and interactive=False.
            combine_adjacent: If True, will merge the labels of adjacent tokens belonging to the same category.
            adjacent_separator: Specifies the separator to be used between tokens if combine_adjacent is True.
            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.
            interactive: If True, the component will be editable, and allow user to select spans of text and label them.
            rtl: If True, will display the text in right-to-left direction, and the labels in the legend will also be aligned to the right.
        )r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r#   r6   za list of 2-part tuples, where the first part is a substring of the text and the second part is the category or value of that substring.N)	r$   r%   r&   r'   r(   r7   super__init___value_description)selfr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   	__class__s                         r   r:   zHighlightedText.__init__(   s~    d #&$8! 0"4!%-# 	 	
" #mr   c                    dd ddddgS )NTher   r   quickadjr   r<   s    r   example_payloadzHighlightedText.example_payloads   s    D9e<
 	
r   c                
    g dS )N))r?   N)rA   rB   )brownrB   )foxnounr   rC   s    r   example_valuezHighlightedText.example_valuey   s    SSr   c                (    |y|j                         S )al  
        Parameters:
            payload: An instance of HighlightedTextData
        Returns:
            Passes the value as a list of tuples as a `list[tuple]` into the function. Each `tuple` consists of a `str` substring of the text (so the entire text is included) and `str | float | None` label, which is the category or confidence of that substring.
        N)
model_dump)r<   payloads     r   
preprocesszHighlightedText.preprocess|   s     ?!!##r   c           
     T   |yt        |t              r	 |d   }|d   }t	        |      dk(  r|dfg}ng }d}t        |d       }|D ]`  }|j                  |||d    df       |j                  d	      xs |j                  d
      }|j                  ||d   |d    |f       |d   }b |j                  ||d df       |}| j                  rg }	d\  }
}|D ]>  \  }}|
|}
|}||k(  r|
| j                  |z   z  }
%|s(|	j                  |
|f       |}
|}@ |
|	j                  |
|f       t        |	D cg c]  }t        |d   |d          c}      S t        |D cg c]  }t        |d   |d          c}      S # t        $ r}t        d      |d}~ww xY wc c}w c c}w )aF  
        Parameters:
            value: Expects a list of (word, category) tuples, or a dictionary of two keys: "text", and "entities", which itself is a list of dictionaries, each of which have the keys: "entity" (or "entity_group"), "start", and "end"
        Returns:
            An instance of HighlightedTextData
        NtextentitieszeExpected a dictionary with keys 'text' and 'entities' for the value of the HighlightedText component.r   c                    | d   S )Nstartr   )xs    r   <lambda>z-HighlightedText.postprocess.<locals>.<lambda>   s
    !G* r   )r4   rR   entityentity_groupend)NN   r@   )r   )
isinstancedictKeyError
ValueErrorlensortedappendgetr'   r(   r   r   )r<   r#   rO   rP   kelist_formatindexrU   entity_categoryoutputrunning_textrunning_categorycategoryos                 r   postprocesszHighlightedText.postprocess   s0    =eT"V} , 8}! !(0DE& *F&&UVG_(Et'LM&,jj&:&Xfjj>XO&&fWou>P #5ME* ""DL$#78#  F-7*L*"' 0h'#'L'/$!11 D$;$;d$BBL MM<1A"BC#'L'/$0 '|-=>?& $ %1Q4QqTJ  ' # %1Q4QqTJ ]   F Ps#   
F 6F !F%	FFF)N)*r#   z=list[tuple[str, str | float | None]] | dict | Callable | Noner$   zdict[str, str] | Noner%   boolr&   rk   r'   rk   r(   r   r)   zstr | I18nData | Noner*   zTimer | float | Noner+   z7Component | Sequence[Component] | set[Component] | Noner,   bool | Noner-   rk   r.   z
int | Noner/   intr0   rk   r1   z
str | Noner2   list[str] | str | Noner3   rk   r4   z(int | str | tuple[int | str, ...] | Noner5   rn   r6   rl   r7   rk   )returnr   )rL   HighlightedTextData | Nonero   z+list[tuple[str, str | float | None]] | None)r#   z2list[tuple[str, str | float | None]] | dict | Nonero   rp   )r   r   r   __doc__r   
data_modelr   changeselectEVENTSr:   rD   rI   rM   rj   __classcell__)r=   s   @r   r    r       s    %JmmV]]+F PTIm
 !%)!&"$'+&*JN"& "/38<3:#'1ImLIm	Im Im #Im Im  Im %Im $Im HIm  Im Im Im  !Im" #Im$ %Im& -'Im( )Im* 6+Im, 1-Im. !/Im0 1ImV
T$1$	4$BGB	#Br   r    N)rq   
__future__r   collections.abcr   r   typingr   r   r   gradio_client.documentationr	   gradio.components.baser
   gradio.data_classesr   r   gradio.eventsr   gradio.i18nr   gradio.componentsr   r   r   r    r   r   r   <module>r      sa    % " . , , 0 , <    '8{ 8
!/ ! 
ni n nr   