
    AF[i\3                        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 d dlmZmZ d dlmZ d d	lmZ  G d
 de      Z G d de      Z e        G d de	             Zy)    )annotations)Callable)Literal)document)	Componentserver)Button)GradioModelGradioRootModel)Events)set_default_buttonsc                  "    e Zd ZU ded<   ded<   y)DialogueLinestrspeakertextN__name__
__module____qualname____annotations__     c/var/www/html/chatbot/moweb-chatbot/venv/lib/python3.12/site-packages/gradio/components/dialogue.pyr   r      s    L
Ir   r   c                      e Zd ZU ded<   y)DialogueModelzlist[DialogueLine] | strrootNr   r   r   r   r   r      s    
""r   r   c                      e Zd ZdZej
                  ej                  ej                  gZe	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 fdZddZddZedd       Zedd       Zedd       Zedd       Z	 	 	 	 ddZd Zd Zd Z xZS )Dialoguea  
    Creates a Dialogue component for displaying or collecting multi-speaker conversations. This component can be used as input to allow users to enter dialogue involving multiple speakers, or as output to display diarized speech, such as the result of a transcription or speaker identification model. Each message can be associated with a specific speaker, making it suitable for use cases like conversations, interviews, or meetings.

    Demos: dia_dialogue_demo
    Nr   
z=Type colon (:) in the dialogue line to see the available tagsT   Fboth)typespeakers	formatterunformattertags	separator	color_maplabelinfoplaceholder
show_label	containerscale	min_widthinteractivevisibleelem_id	autofocus
autoscrollelem_classesrenderkey	max_linesbuttons
submit_btnui_modec               R   t         |   ||	|
||||||||||       |dk(  rt        d      || _        || _        || _        || _        || _        || _        || _	        |xs g | _
        || _        || _        || _        || _        t        |d      | _        || _        |sd| _        yy)aa  
        Parameters:
            value: Value of the dialogue. It is a list of dictionaries, each containing a 'speaker' key and a 'text' key. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
            type: The type of the component, either "list" for a multi-speaker dialogue consisting of dictionaries with 'speaker' and 'text' keys or "text" for a single text input. Defaults to "text".
            speakers: The different speakers allowed in the dialogue. If `None` or an empty list, no speakers will be displayed. Instead, the component will be a standard textarea that optionally supports `tags` autocompletion.
            formatter: A function that formats the dialogue line dictionary, e.g. {"speaker": "Speaker 1", "text": "Hello, how are you?"} into a string, e.g. "Speaker 1: Hello, how are you?". This function is run on user input and the resulting string is passed into the prediction function.
            unformatter: A function that parses a formatted dialogue string back into a dialogue line dictionary. Should take a single string line and return a dictionary with 'speaker' and 'text' keys. If not provided, the default unformatter will attempt to parse the default formatter pattern.
            tags: The different tags allowed in the dialogue. Tags are displayed in an autocomplete menu below the input textbox when the user starts typing `:`. Use the exact tag name expected by the AI model or inference function.
            separator: The separator between the different dialogue lines used to join the formatted dialogue lines into a single string. It should be unambiguous. For example, a newline character or tab character.
            color_map: A dictionary mapping speaker names to colors. The colors may be specified as hex codes or by their names. For example: {"Speaker 1": "red", "Speaker 2": "#FFEE22"}. If not provided, default colors will be assigned to speakers. This is only used if `interactive` is False.
            max_lines: maximum number of lines allowed in the dialogue.
            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.
            show_label: if True, will display the label. If False, the copy button is hidden as well as well as the 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. If "hidden", component will be visually hidden and not take up space in the layout but still exist in the DOM
            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: if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
            buttons: A list of buttons to show for the component. Valid options are "copy" or a gr.Button() instance. The "copy" button allows the user to copy the text in the textbox. Custom gr.Button() instances will appear in the toolbar with their configured icon and/or label, and clicking them will trigger any .click() events registered on the button. By default, no buttons are shown.
            submit_btn: If False, will not show a submit button. If True, will show a submit button with an icon. If a string, will use that string as the submit button text.
            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.
            ui_mode: Determines the user interface mode of the component. Can be "dialogue" (displays dialogue lines), "text" (displays a single text input), or "both" (displays both dialogue lines and a text input). Defaults to "both".
        )valuer*   r+   r-   r.   r/   r0   r1   r2   r3   r6   r7   r8    z$Separator cannot be an empty string.N)super__init__
ValueErrorr<   r#   r,   r4   r5   r9   r$   r'   r%   r&   r(   r)   r   r:   r;   r+   )selfr>   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   	__class__s                               r   rA   zDialogue.__init__(   s    z 	!#% 	 	
 CDD	&"$" JB	"&""*7D9$DI r   c                b    | j                   dk(  r|j                         S | j                  |      S )z
        Parameters:
            payload: Expects a `DialogueModel` object or string.
        Returns:
            Returns the dialogue as a string or list of dictionaries.
        list)r#   
model_dump_format)rC   payloads     r   
preprocesszDialogue.preprocess   s.     99%%''||G$$r   c           	        t        |j                  t              r|j                  dk(  sNt        |j                  t              r5t	        |j                        dk(  r|j                  d   j
                  dk(  ry| j                  }|s| j                  }t        |j                  t              r|j                  S | j                  j                  |j                  D cg c]  } ||j                  |j
                        ! c}      S c c}w )N    r   )
isinstancer   r   rF   lenr   r%   default_formatterr(   joinr   )rC   rI   r%   lines       r   rH   zDialogue._format   s    w||S)gllb.@w||T*GLL!Q&Q$$*NN	..IgllC(<<~~""<CLLIDYt||TYY/I
 	
Is   $Dc                    d|  d| S )N[z] r   r   r   s     r   rP   zDialogue.default_formatter   s    7)2dV$$r   c                    | j                         } | sdddS | j                  d      r5d| v r1| j                  d      }| d| }| |dz   d j                         }||dS || dS )zFParse a formatted dialogue line back into speaker and text components.rL   rU   rT   ]rM   N)strip
startswithfind)rR   default_speakerbracket_endr   r   s        r   default_unformatterzDialogue.default_unformatter   sz     zz|!2.. ??3C4K))C.K1[)Ga)*002D&55.==r   c                D   K   t        |      }| j                  |      S w)zQFormat the dialogue in the frontend into a string that's copied to the clipboard.r   )r   rH   )rC   r>   datas      r   formatzDialogue.format   s!      %(||D!!s    c                  K   |j                  dd      }|r|j                         dk(  rg S |j                  | j                        }g }| j                  }|s| j
                  }d}t        | j                  t              r$t        | j                        r| j                  d   }|D ]:  }|j                         }|s |||      }|d   s|d   s*|j                  |       < |S w)zDParse a formatted dialogue string back into dialogue data structure.r   rL   Unknownr   r   )getrX   splitr(   r&   r]   rN   r$   rF   rO   append)	rC   rI   r>   linesdialogue_linesr&   r[   rR   parsed_lines	            r   unformatzDialogue.unformat   s      FB'+IDNN+&&22K#dmmT*s4==/A"mmA.O 	3D::<D%dO<K9%V)<%%k2	3 s   CC$C$c                    |yt        |t              rt        |      S |D cg c]  }t        |d   |d          }}t        |      S c c}w )a  
        Parameters:
            value: Expects a string or a list of dictionaries of dialogue lines, where each dictionary contains 'speaker' and 'text' keys, or a string.
        Returns:
            Returns the dialogue as a `DialogueModel` object for the frontend.
        Nr_   r   r   rU   )rN   r   r   r   )rC   r>   rR   rh   s       r   postprocesszDialogue.postprocess   s`     =eS! e,, RW
IMLitF|D
 
 .11
s   Ac                8    | j                  t        |            S )Nr_   )rJ   r   )rC   r>   s     r   
as_examplezDialogue.as_example   s    }%899r   c                    ddddddgS Nz	Speaker 1zHello, how are you?rU   z	Speaker 2zI'm fine, thank you!r   rC   s    r   example_payloadzDialogue.example_payload       #-BC#-CD
 	
r   c                    ddddddgS rp   r   rq   s    r   example_valuezDialogue.example_value   rs   r   )N)6r>   z&list[dict[str, str]] | Callable | Noner#   zLiteral['list', 'text']r$   list[str] | Noner%   Callable | Noner&   rw   r'   rv   r(   r   r)   zdict[str, str] | Noner*   
str | Noner+   rx   r,   rx   r-   bool | Noner.   boolr/   
int | Noner0   intr1   ry   r2   zbool | Literal['hidden']r3   rx   r4   rz   r5   rz   r6   zlist[str] | str | Noner7   rz   r8   zint | str | Noner9   r{   r:   z%list[Literal['copy'] | Button] | Noner;   zstr | bool | Noner<   z#Literal['dialogue', 'text', 'both'])rI   r   returnzstr | list[dict[str, str]])rI   r   r}   r   )r   r   r   r   r}   r   )rR   r   r[   r   r}   zdict[str, str])r>   zlist[dict] | str)rI   dict)r>   z!list[dict[str, str]] | str | Noner}   zDialogueModel | None)r   r   r   __doc__r   changeinputsubmitEVENTSr   
data_modelrA   rJ   rH   staticmethodrP   r]   r   ra   rj   rl   rn   rr   ru   __classcell__)rD   s   @r   r   r      s4    	F J 9=] )/%)%)'+!%+/&P"&"& #',0"/3 $ $9=(-7==]5] &	]
 #] #] %] ] ] )] ]]  ]  ]  !]" #]$ %]& !']( *)]* +], -]. /]0 -1]2 3]4 5]6 7]8 79]: &;]< 5=]~	%
  % % > > " "
  8262	2(:

r   r   N)
__future__r   collections.abcr   typingr   gradio_client.documentationr   gradio.components.baser   r   gradio.components.buttonr	   gradio.data_classesr
   r   gradio.eventsr   gradio.utilsr   r   r   r   r   r   r   <module>r      sX    " $  0 4 + <   ,; 
#O # 
a
y a
 a
r   