o
    }oiL                   	   @  s   d Z ddlmZ ddlZddlmZ z*ddlmZmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZmZmZmZmZ dZW n eefyI   dZY nw G d	d
 d
eZedG dd deeeeZdS )zr

Adopted from https://github.com/gradio-app/gradio/blob/main/gradio/components.py
Fix a markdown render problem.
    )annotationsN)Markdown)AnyCallableDictListLiteralTuple)	
Changeable	ComponentEnumEventListenerMethodIOComponentJSONSerializable
Selectabledocumentprocessing_utilsTFc                   @  s   e Zd ZdZdZdS )	_KeywordsNO_VALUEFINISHED_ITERATINGN)__name__
__module____qualname__r   r    r   r   i/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/nlp/modules/common/chatbot_component.pyr   .   s    r   stylec                   @  sr   e Zd ZdZ		d*dddddddd+ddZdd Zeejdddfd,ddZ	d-dd Z
d.d$d%Zd/d0d(d)ZdS )1Chatbota  
    Displays a chatbot output showing both user submitted messages and responses. Supports a subset of Markdown including bold, italics, code, and images.
    Preprocessing: this component does *not* accept input.
    Postprocessing: expects function to return a {List[Tuple[str | None | Tuple, str | None | Tuple]]}, a list of tuples with user message and response messages. Messages should be strings, tuples, or Nones. If the message is a string, it can include Markdown. If it is a tuple, it should consist of (string filepath to image/video/audio, [optional string alt text]). Messages that are `None` are not displayed.

    Demos: chatbot_simple, chatbot_multimodal
    NT)labelevery
show_labelvisibleelem_idelem_classesvalue5List[Tuple[str | None, str | None]] | Callable | None	color_mapDict[str, str] | Noner   
str | Noner   float | Noner   boolr    r!   r"   List[str] | str | Nonec          
   
   K  sN   |dur	t d tg dd| _|  	 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.
            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.
            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.
        N.The 'color_map' parameter has been deprecated.)zfenced-code-blockstableszbreak-on-newline)extras)r   r   r   r    r!   r"   r#   )warningswarnr   mdr   __init__)
selfr#   r%   r   r   r   r    r!   r"   kwargsr   r   r   r1   ?   s$   
	
zChatbot.__init__c                 C  s   | j | jdt| S )N)r#   
selectable)r#   r4   r   
get_config)r2   r   r   r   r5   m   s
   zChatbot.get_config(Any | Literal[_Keywords.NO_VALUE] | Nonebool | Nonec                 C  s   |||| dd}|S )Nupdate)r   r   r    r#   __type__r   )r#   r   r   r    updated_configr   r   r   r8   t   s   zChatbot.updatechat_message str | Tuple | List | Dict | Nonereturnstr | Dict | Nonec                 C  s   |d u rd S t |ttfr(t|d }|d |t|dkr"|d nd d ddS t |tr/|S t |tr<t| j	|S t
d| )Nr      T)name	mime_typealt_textdatais_filez'Invalid message for Chatbot component: )
isinstancetuplelistr   get_mimetypelendictstrr0   convert
ValueError)r2   r;   rA   r   r   r   _process_chat_messages   s   

zChatbot._process_chat_messagesyOList[Tuple[str | Tuple | List | Dict | None, str | Tuple | List | Dict | None]]1List[Tuple[str | Dict | None, str | Dict | None]]c                 C  sp   |du rg S g }|D ]+}t |ttfsJ d| t|dks'J d| ||d | |d f q
|S )ac  
        Parameters:
            y: List of tuples 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 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 tuples representing the message and response. Each message and response will be a string of HTML, or a dictionary with media information.
        Nz6Expected a list of lists or list of tuples. Received:    zNExpected a list of lists of length 2 or list of tuples of length 2. Received: r   r?   )rE   rF   rG   rI   appendrN   )r2   rO   processed_messagesmessage_pairr   r   r   postprocess   s$   	zChatbot.postprocessheight
int | Nonec                 K  s@   |dur	|| j d< |ddurtd tj| fi | | S )z\
        This method can be used to change the appearance of the Chatbot component.
        NrW   r%   r+   )_stylegetr.   r/   r   r   )r2   rW   r3   r   r   r   r      s   

zChatbot.style)NN)r#   r$   r%   r&   r   r'   r   r(   r   r)   r    r)   r!   r'   r"   r*   )r#   r6   r   r'   r   r7   r    r7   )r;   r<   r=   r>   )rO   rP   r=   rQ   )N)rW   rX   )r   r   r   __doc__r1   r5   staticmethodr   r   r8   rN   rV   r   r   r   r   r   r   5   s,    
.

r   )r[   
__future__r   r.   	markdown2r   typingr   r   r   r   r   r	   gradio.componentsr
   r   r   r   r   r   r   r   r   GRADIO_AVAILABLEImportErrorModuleNotFoundErrorr   r   r   r   r   r   <module>   s    ,