
    ;3h-4                    H   d 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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 ddlmZmZ ddlmZ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( G d de      Z) e        G d de             Z*y)z!gr.MultimodalTextbox() component.    )annotations)CallableSequence)Path)TYPE_CHECKINGAnyLiteral	TypedDictcastNhandle_file)document)Field)NotRequired)	ComponentFormComponent)FileDataGradioModel)Events)Error)I18nData)Timerc                  4    e Zd ZU ded<    ee      Zded<   y)MultimodalDatastrtext)default_factoryzlist[FileData]filesN)__name__
__module____qualname____annotations__r   listr        d/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/components/multimodal_textbox.pyr   r      s    
I!$7E>7r%   r   c                  "    e Zd ZU ded<   ded<   y)MultimodalPostprocessr   r   zNotRequired[list[FileData]]r   Nr   r    r!   r"   r$   r%   r&   r(   r(      s    
I&&r%   r(   c                  "    e Zd ZU ded<   ded<   y)MultimodalValuezNotRequired[str]r   zNotRequired[list[str]]r   Nr)   r$   r%   r&   r+   r+   #   s    
!!r%   r+   c                      e Zd ZdZeZej                  ej                  ej                  ej                  ej                  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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 )MultimodalTextboxz
    Creates a textarea for users to enter string input or display string output and also allows for the uploading of multimedia files.

    Demos: chatbot_multimodal
    Guides: creating-a-chatbot
    Nsingle      T   Fvaluei  )sources
file_types
file_countlines	max_linesplaceholderlabelinfoeveryinputs
show_label	containerscale	min_widthinteractivevisibleelem_id	autofocus
autoscrollelem_classesrenderkeypreserved_by_key
text_alignrtl
submit_btnstop_btnmax_plain_text_lengthc               `   ddg}|	dg| _         nCt        |t              r||v r	|g| _         n&t        |t              r|| _         nt	        d|       | j                   D ]  }||vst	        d|        || _        || _        |2t        |t              s"t	        d|j                  j                         || _	        t        ||      | _        || _        || _        || _        || _        || _        || _        t$         | M  ||	|
|||||||||||||       || _        || _        d| _        y)	ao  
        Parameters:
            value: Default value to show in MultimodalTextbox. A string value, or a dictionary of the form {"text": "sample text", "files": [{path: "files/file.jpg", orig_name: "file.jpg", url: "http://image_url.jpg", size: 100}]}. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
            sources: A list of sources permitted. "upload" creates a button where users can click to upload or drop files, "microphone" creates a microphone input. If None, defaults to ["upload"].
            file_count: if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
            file_types: List of file extensions or types of files to be uploaded (e.g. ['image', '.json', '.mp4']). "file" allows any file to be uploaded, "image" allows only image files to be uploaded, "audio" allows only audio files to be uploaded, "video" allows only video files to be uploaded, "text" allows only text files to be uploaded.
            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: the label for this component, displayed above the component if `show_label` is `True` 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 corresponds to.
            info: additional component description, appears below the label in smaller font. Supports markdown / HTML syntax.
            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.
            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. Use this carefully, as it can cause usability issues for sighted and non-sighted users.
            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.
            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.
            autoscroll: If True, will automatically scroll to the bottom of the textbox when the value changes, unless the user scrolls up. If False, will not scroll to the bottom of the textbox when the value changes.
            submit_btn: If False, will not show a submit button. If a string, will use that string as the submit button text.
            stop_btn: If True, will show a stop button (useful for streaming demos). If a string, will use that string as the stop button text.
            max_plain_text_length: Maximum length of plain text in the textbox. If the text exceeds this length, the text will be pasted as a file. Default is 1000.
        upload
microphoneNz3`sources` must be a list consisting of elements in z0`sources` must a list consisting of elements in z.Parameter file_types must be a list. Received )r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rF   rG   rH   rI   r2   zPa dictionary with structure {'text': string, 'files': list of string file paths})r3   
isinstancer   r#   
ValueErrorr4   r5   	__class__r   r6   maxr7   r8   rL   rM   rD   rE   rN   super__init__rK   rJ   _value_description)!selfr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   valid_sourcessourcerT   s!                                   r&   rW   zMultimodalTextbox.__init__=   sv   F AI,?W?$:DL%']*B#9DL&"DLEm_U  ll 	F]* F}oV 	
 %$!*Z*F@AUAUA^A^@_`  
UI.&$ "$%:"!#%-! 	 	
$ $"tr%   c                L   |y| j                   `|j                  D ]Q  }t        j                  |j                  | j                         r.t        d|j                   d| j                           |j                  |j                  D cg c]  }|j                   c}dS c c}w )z
        Parameters:
            payload: the text and list of file(s) entered in the multimodal textbox.
        Returns:
            Passes text value and list of file(s) as a {dict} into the function.
        NzInvalid file type: z5. Please upload a file that is one of these formats: r   r   )r4   r   client_utilsis_valid_filepathr   	mime_typer   )rY   payloadfs      r&   
preprocesszMultimodalTextbox.preprocess   s     ???&]] #11!&&$//J-akk]:optpp  pA  B  LL&-mm4aff4
 	
4s   	B!c           
        |yt        |t        t        f      s"t        d|j                  j
                         t        |t              rt        |g       S |j                  dd      }d|v rt        |d   t              rl|d   D cg c]]  }t        |t        t        z        rt        t        |      n3t        |t        |      j                  t        j                  |            _ }}ng }t        |t              s!t        dt!        |      j
                         t        |t              s!t        d	t!        |      j
                         t        ||      S c c}w )
a  
        Parameters:
            value: Expects a {dict} with "text" and "files", both optional. The files array is a list of file paths or URLs.
        Returns:
            The value to display in the multimodal textbox. Files information as a list of FileData objects.
        NzcMultimodalTextbox expects a string or a dictionary with optional keys 'text' and 'files'. Received r]   r    r   )r`   	orig_namera   z(Expected 'text' to be a string, but got z'Expected 'files' to be a list, but got )rR   dictr   rS   rT   r   r   getr#   r   r   r   namer^   get_mimetype	TypeErrortype)rY   r2   r   filer   s        r&   postprocesszMultimodalTextbox.postprocess   sn    =%$-uv{  wF  wF  wO  wO  vP  Q  eS!!uB77yy$e
5>4 @ "'N	  dHtO4 Xt$"4joo*77=	E 	 E$$:4:;N;N:OP  %&9$u+:N:N9OP  4u55)	s   	A"Ec                     dt        d      gdS NzDescribe this imagezPhttps://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.pngr]   r   rY   s    r&   example_payloadz!MultimodalTextbox.example_payload   s    )f
 	
r%   c                    ddgdS rq   r$   rr   s    r&   example_valuezMultimodalTextbox.example_value   s    )b
 	
r%   )N):r2   z-str | dict[str, str | list] | Callable | Noner3   zNlist[Literal['upload', 'microphone']] | Literal['upload', 'microphone'] | Noner4   zlist[str] | Noner5   z*Literal['single', 'multiple', 'directory']r6   intr7   rv   r8   
str | Noner9   str | I18nData | Noner:   rx   r;   zTimer | float | Noner<   z7Component | Sequence[Component] | set[Component] | Noner=   bool | Noner>   boolr?   z
int | Noner@   rv   rA   ry   rB   rz   rC   rw   rD   rz   rE   rz   rF   list[str] | str | NonerG   rz   rH   z(int | str | tuple[int | str, ...] | NonerI   r{   rJ   zLiteral['left', 'right'] | NonerK   rz   rL   str | bool | NonerM   r|   rN   rv   )rb   MultimodalData | NonereturnzMultimodalValue | None)r2   zMultimodalValue | str | Noner~   r}   )r~   r   )r   r    r!   __doc__r   
data_modelr   changeinputselectsubmitfocusblurstopEVENTSrW   rd   ro   rs   ru   __classcell__)rT   s   @r&   r-   r-   (   s*     J 	F @Dvu '+AI"&'+&*&*JN"& #'"/38<3:6:(,&+%)Cvu<vu	vu %vu ?vu vu vu  vu %vu $vu $vu Hvu   !vu" #vu$ %vu& 'vu( !)vu* +vu, -vu. /vu0 1vu2 -3vu4 5vu6 67vu8 19vu: 4;vu< =vu> &?vu@ $AvuB  #Cvup
(%6N

r%   r-   )+r   
__future__r   collections.abcr   r   pathlibr   typingr   r   r	   r
   r   gradio_client.utilsutilsr^   gradio_clientr   gradio_client.documentationr   pydanticr   typing_extensionsr   gradio.components.baser   r   gradio.data_classesr   r   gradio.eventsr   gradio.exceptionsr   gradio.i18nr   gradio.componentsr   r   r(   r+   r-   r$   r%   r&   <module>r      s    ' " .  ? ? * % 0  ) ; 5   #  '8[ 8
'I '
"i "
 
W
 W
 W
r%   