o
    eV7                  	   @  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 ddl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mZ ddlmZmZmZmZ ed e G dd deeeeeZdS )zgr.Chatbot() component.    )annotationsN)Path)CallableLiteral)utils)documentset_documentation_group)JSONSerializable)IOComponent	_Keywords)warn_deprecationwarn_style_method_deprecation)
ChangeableEventListenerMethodLikeable
Selectable	componentc                   @  s   e Zd ZdZ		dAdddddddddddddddddddBd%d&Zd'd( ZeejddddddddddddddfdCd+d,Z	dDd1d2Z
dEd6d7ZdFd9d:ZdGd=d>ZdHdId?d@ZdS )JChatbota.  
    Displays a chatbot output showing both user submitted messages and responses. Supports a subset of Markdown including bold, italics, code, tables. Also supports audio/video/image files, which are displayed in the Chatbot, and other kinds of files which are displayed as links.
    Preprocessing: passes the messages in the Chatbot as a {List[List[str | None | Tuple]]}, i.e. a list of lists. The inner list has 2 elements: the user message and the response message. See `Postprocessing` for the format of these messages.
    Postprocessing: expects function to return a {List[List[str | None | Tuple]]}, i.e. a list of lists. The inner list should have 2 elements: the user message and the response message. The individual messages can be (1) strings in valid Markdown, (2) tuples if sending files: (a filepath or URL to a file, [optional string alt text]) -- if the file is image/video/audio, it is displayed in the Chatbot, or (3) None, in which case the message is not displayed.

    Demos: chatbot_simple, chatbot_multimodal
    Guides: creating-a-chatbot
    NT   F)labelevery
show_label	containerscale	min_widthvisibleelem_idelem_classesheightlatex_delimitersrtlshow_share_buttonshow_copy_buttonavatar_imagessanitize_htmlbubble_full_widthvalueNlist[list[str | tuple[str] | tuple[str | Path, str] | None]] | Callable | None	color_mapdict[str, str] | Noner   
str | Noner   float | Noner   bool | Noner   boolr   
int | Noner   intr   r   r   list[str] | str | Noner   r   "list[dict[str, str | bool]] | Noner    r!   r"   r#   2tuple[str | Path | None, str | Path | None] | Noner$   r%   c                K  s   |durt d |  	 |  	 || _|| _|du rddddg}|| _|p%d| _|du r1t dun|| _|| _|| _	|| _
tj| f|||||||	|
||d
| dS )a  
        Parameters:
            value: Default value to show in chatbot. If callable, the function will be called whenever the app loads to set the initial value of the component.
            color_map: This parameter is deprecated.
            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.
            height: height of the component in pixels.
            latex_delimiters: A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).
            rtl: If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.
            show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
            show_copy_button: If True, will show a copy button for each chatbot message.
            avatar_images: Tuple of two avatar image paths or URLs for user and bot (in that order). Pass None for either the user or bot image to skip. Must be within the working directory of the Gradio app or an external URL.
            sanitize_html: If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.
            bubble_full_width: If False, the chat bubble will fit to the content of the message. If True (default), the chat bubble will be the full width of the component.
        Nz.The 'color_map' parameter has been deprecated.z$$T)leftrightdisplayNN)
r   r   r   r   r   r   r   r   r   r&   )r   r   r    r   r#   r   Z	get_spacer!   r"   r$   r%   r
   __init__)selfr&   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/chatbot.pyr7   %   sF   0

zChatbot.__init__c                 C  s<   | j | j| j| j| j| j| j| j| j| j	| j
dt| S )N)r&   r   
selectablelikeabler   r!   r    r"   r#   r$   r%   )r&   r   r<   r=   r   r!   r    r"   r#   r$   r%   r
   
get_config)r8   r:   r:   r;   r>      s   zChatbot.get_configZlist[list[str | tuple[str] | tuple[str, str] | None]] | Literal[_Keywords.NO_VALUE] | Nonetuple[str | Path | None] | Nonec                 C  sh   i 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%   Z__type__updater:   )r&   r   r   r   r   r   r   r   r    r   r!   r"   r#   r$   r%   Zupdated_configr:   r:   r;   rA      sD   	
zChatbot.updatechat_messagestr | dict | Nonereturn)str | tuple[str] | tuple[str, str] | Nonec                 C  s@   |d u rd S t |tr|d d ur|d |d fS |d fS |S )Nalt_textname)
isinstancedict)r8   rB   r:   r:   r;   _preprocess_chat_messages   s   

z!Chatbot._preprocess_chat_messagesyKlist[list[str | dict | None] | tuple[str | dict | None, str | dict | None]]5list[list[str | tuple[str] | tuple[str, str] | None]]c                 C  sv   |d u r|S g }|D ].}t |ttfsJ d| t|dks'J d| || |d | |d g q
|S )N6Expected a list of lists or list of tuples. Received:    NExpected a list of lists of length 2 or list of tuples of length 2. Received: r      )rH   tuplelistlenappendrJ   r8   rK   Zprocessed_messagesZmessage_pairr:   r:   r;   
preprocess   s$   zChatbot.preprocessstr | tuple | list | Nonec                 C  s   |d u rd S t |ttfr7t|d }t|r|}n| |}t|}||t	|dkr1|d nd d ddS t |trCt
|}|S td| )Nr   rQ   T)rG   	mime_typerF   datais_filez'Invalid message for Chatbot component: )rH   rR   rS   strr   Zvalidate_urlZmake_temp_copy_if_neededclient_utilsZget_mimetyperT   inspectcleandoc
ValueError)r8   rB   Zfile_urifilepathrY   r:   r:   r;   _postprocess_chat_messages   s$   




z"Chatbot._postprocess_chat_messages=list[list[str | tuple[str] | tuple[str, str] | None] | tuple]list[list[str | dict | None]]c                 C  sv   |du rg S g }|D ].}t |ttfsJ d| t|dks'J d| || |d | |d g q
|S )a  
        Parameters:
            y: List of lists representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.  It can also be a tuple whose first element is a string or pathlib.Path filepath or URL to an image/video/audio, and second (optional) element is the alt text, in which case the media file is displayed. It can also be None, in which case that message is not displayed.
        Returns:
            List of lists representing the message and response. Each message and response will be a string of HTML, or a dictionary with media information. Or None if the message is not to be displayed.
        NrN   rO   rP   r   rQ   )rH   rR   rS   rT   rU   rb   rV   r:   r:   r;   postprocess   s$   
zChatbot.postprocessc                 K  s   t   |dur
|| _| S )zc
        This method is deprecated. Please set these arguments in the constructor instead.
        N)r   r   )r8   r   r9   r:   r:   r;   style  s   zChatbot.styler6   )&r&   r'   r(   r)   r   r*   r   r+   r   r,   r   r-   r   r.   r   r/   r   r-   r   r*   r   r0   r   r.   r   r1   r    r-   r!   r,   r"   r-   r#   r2   r$   r-   r%   r-   )r&   r?   r   r*   r   r,   r   r,   r   r.   r   r.   r   r,   r   r.   r    r,   r   r1   r!   r,   r"   r,   r#   r@   r$   r,   r%   r,   )rB   rC   rD   rE   )rK   rL   rD   rM   )rB   rX   rD   rC   )rK   rc   rD   rd   )N)r   r.   )__name__
__module____qualname____doc__r7   r>   staticmethodr   ZNO_VALUErA   rJ   rW   rb   re   rf   r:   r:   r:   r;   r      s\    [
'


r   )rj   
__future__r   r^   pathlibr   typingr   r   Zgradio_clientr   r]   Zgradio_client.documentationr   r   Zgradio_client.serializingr	   ZgradioZgradio.components.baser
   r   Zgradio.deprecationr   r   Zgradio.eventsr   r   r   r   r   r:   r:   r:   r;   <module>   s    