o
    eU.                     @  s   d Z ddlmZ ddlmZm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 ddlmZ dd	lmZmZmZmZmZmZ dd
lmZ e
d e	 G dd deeeeeeeeeZdS )zgr.Textbox() component.    )annotations)CallableLiteralN)documentset_documentation_group)StringSerializable)FormComponentIOComponent	_Keywords)warn_style_method_deprecation)
ChangeableEventListenerMethod	Focusable	Inputable
SelectableSubmittable)TokenInterpretable	componentc                   @  s   e Zd ZdZ	dPddddddddddddddddd	dddd
dQd*d+Zd,d- ZeejdddddddddddddddddfdRd0d1Z	dSd4d5Z
dTd7d8Z	dUdVd=d>ZdWd@dAZdXdFdGZ	dYdZdKdLZdddMd[dNdOZdS )\Textboxa  
    Creates a textarea for user to enter string input or display string output.
    Preprocessing: passes textarea value as a {str} into the function.
    Postprocessing: expects a {str} returned from function and sets textarea value to it.
    Examples-format: a {str} representing the textbox input.

    Demos: hello_world, diff_texts, sentence_builder
    Guides: creating-a-chatbot, real-time-speech-recognition
           NT   Ftext)lines	max_linesplaceholderlabelinfoevery
show_label	containerscale	min_widthinteractivevisibleelem_id	autofocus
autoscrollelem_classestype
text_alignrtlshow_copy_buttonvaluestr | Callable | Noner   intr   r   
str | Noner   r   r   float | Noner    bool | Noner!   boolr"   
int | Noner#   r$   r%   r&   r'   r(   r)   list[str] | str | Noner*   $Literal['text', 'password', 'email']r+   Literal['left', 'right'] | Noner,   r-   c                K  s   |dvrt d|| _|dkrt||| _nd| _|| _|| _|| _|  || _	 tj	| f|||||	|
||||||d| t
	|  || _|| _|| _dS )a
  
        Parameters:
            value: default text to provide in textarea. If callable, the function will be called whenever the app loads to set the initial value of the component.
            lines: minimum number of line rows to provide in textarea.
            max_lines: maximum number of line rows to provide in textarea.
            placeholder: placeholder hint to provide behind textarea.
            label: component name in interface.
            info: additional component description.
            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.
            interactive: if True, will be rendered as an editable textbox; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
            visible: If False, component will be hidden.
            autofocus: If True, will focus on the textbox when the page loads.
            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.
            type: The type of textbox. One of: 'text', 'password', 'email', Default is 'text'.
            text_align: How to align the text in the textbox, can be: "left", "right", or None (default). If None, the alignment is left if `rtl` is False, or right if `rtl` is True. Can only be changed if `type` is "text".
            rtl: If True and `type` is "text", sets the direction of the text to right-to-left (cursor appears on the left of the text). Default is False, which renders cursor on the right.
            show_copy_button: If True, includes a copy button to copy the text in the textbox. Only applies if show_label is True.
            autoscroll: If True, will automatically scroll to the bottom of the textbox when the value changes.
        )r   passwordemailz5`type` must be one of "text", "password", or "email".r   r   )r   r   r   r    r!   r"   r#   r$   r%   r&   r)   r.   N)
ValueErrorr   maxr   r   r-   r'   r(   r	   __init__r   r*   r,   r+   )selfr.   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   kwargs r@   ID:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/textbox.pyr=   4   sD   2

zTextbox.__init__c                 C  s<   | j | j| j| j| j| j| j| j| j| j	| j
dt| S )N)r   r   r   r.   r*   r'   r-   r!   r+   r,   r(   )r   r   r   r.   r*   r'   r-   r!   r+   r,   r(   r	   
get_config)r>   r@   r@   rA   rB      s   zTextbox.get_config(str | Literal[_Keywords.NO_VALUE] | None+Literal['text', 'password', 'email'] | Nonec                 C  st   i d|d|d|d|d|d|d|d|d	|	d
|
d| d|d|d|d|d|d||ddS )Nr   r   r   r   r   r    r!   r"   r#   r%   r.   r*   r$   r-   r'   r+   r,   update)r(   Z__type__r@   )r.   r   r   r   r   r   r    r!   r"   r#   r%   r$   r*   r+   r,   r-   r'   r(   r@   r@   rA   rE      sL   	
zTextbox.updatexreturnc                 C     |du rdS t |S )z
        Preprocesses input (converts it to a string) before passing it to the function.
        Parameters:
            x: text
        Returns:
            text
        Nstr)r>   rF   r@   r@   rA   
preprocess      zTextbox.preprocessyc                 C  rH   )z
        Postproccess the function output y by converting it to a str before passing it to the frontend.
        Parameters:
            y: function output to postprocess.
        Returns:
            text
        NrI   )r>   rM   r@   r@   rA   postprocess   rL   zTextbox.postprocess 	separatorrJ   replacementc                 C  s   || _ || _| S )a  
        Calculates interpretation score of characters in input by splitting input into tokens, then using a "leave one out" method to calculate the score of each token by removing each token and measuring the delta of the output value.
        Parameters:
            separator: Separator to use to split input into tokens.
            replacement: In the "leave one out" step, the text that the token should be replaced with. If None, the token is removed altogether.
        )interpretation_separatorinterpretation_replacement)r>   rP   rQ   r@   r@   rA   set_interpret_parameters   s   	z Textbox.set_interpret_parameters!tuple[list[str], list[str], None]c                 C  sf   | | j}g }tt|D ]}t|}| jdu r|| n| j||< || j| q||dfS )zo
        Tokenizes an input string by dividing into "words" delimited by self.interpretation_separator
        N)	splitrR   rangelenlistrS   popappendjoin)r>   rF   tokensZleave_one_out_stringsindexZleave_one_out_setr@   r@   rA   tokenize   s   



zTextbox.tokenizer]   	list[str]binary_mask_matrixlist[list[int]]c                 C  s<   g }|D ]}t |t j|td }|| j| q|S )zO
        Constructs partially-masked sentences for SHAP interpretation
        )Zdtype)nparrayr4   r[   rR   r\   )r>   r]   ra   Zmasked_inputsZbinary_mask_vectorZmasked_inputr@   r@   rA   get_masked_inputs   s
   zTextbox.get_masked_inputsscoreslist[float]list[tuple[str, float]]c           
      K  s:   g }t ||D ]\}}	|||	f || jdf q|S )z
        Returns:
            Each tuple set represents a set of characters and their corresponding interpretation score.
        r   )zipr[   rR   )
r>   rF   Z	neighborsrf   r]   masksr?   resulttokenZscorer@   r@   rA   get_interpretation_scores  s
   z!Textbox.get_interpretation_scores)r-   r!   c                K  s&   t   |dur
|| _|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?   r@   r@   rA   style  s   
zTextbox.style)r   )*r.   r/   r   r0   r   r0   r   r1   r   r1   r   r1   r   r2   r    r3   r!   r4   r"   r5   r#   r0   r$   r3   r%   r4   r&   r1   r'   r4   r(   r4   r)   r6   r*   r7   r+   r8   r,   r4   r-   r4   )$r.   rC   r   r5   r   r5   r   r1   r   r1   r   r1   r    r3   r!   r3   r"   r5   r#   r5   r%   r3   r$   r3   r*   rD   r+   r8   r,   r3   r-   r3   r'   r3   r(   r3   )rF   r1   rG   r1   )rM   r1   rG   r1   )rO   N)rP   rJ   rQ   r1   )rF   rJ   rG   rU   )r]   r`   ra   rb   rG   r`   )N)rf   rg   r]   r`   rG   rh   )r-   r3   r!   r3   )__name__
__module____qualname____doc__r=   rB   staticmethodr
   ZNO_VALUErE   rK   rN   rT   r_   re   rm   rn   r@   r@   r@   rA   r      sr    Y
*



r   )rr   
__future__r   typingr   r   numpyrc   Zgradio_client.documentationr   r   Zgradio_client.serializingr   Zgradio.components.baser   r	   r
   Zgradio.deprecationr   Zgradio.eventsr   r   r   r   r   r   Zgradio.interpretationr   r   r@   r@   r@   rA   <module>   s.     

