§
    \zni¾8  ã                   ó   — d dl Z d dlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ  e¦   «         rd dlZ e	¦   «         rd dlZ e
j        e¦  «        Z G d„ d¦  «        Z eed	¦  «         G d
„ de¦  «        ¦   «         ZdS )é    N)ÚAnyÚDictÚListÚUnioné   )Úadd_end_docstringsÚis_tf_availableÚis_torch_availableÚloggingé   )ÚPIPELINE_INIT_ARGSÚPipelinec                   ó  — e Zd ZdZ	 ddeeeeeef                  f         dej	        fd„Z
d„ Zdeeef         fd„Zdd
edefd„Zdefd„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         ZdS )ÚConversationa  
    Utility class containing a conversation and its history. This class is meant to be used as an input to the
    [`ConversationalPipeline`]. The conversation contains several utility functions to manage the addition of new user
    inputs and generated model responses.

    Arguments:
        messages (Union[str, List[Dict[str, str]]], *optional*):
            The initial messages to start the conversation, either a string, or a list of dicts containing "role" and
            "content" keys. If a string is passed, it is interpreted as a single message with the "user" role.
        conversation_id (`uuid.UUID`, *optional*):
            Unique identifier for the conversation. If not provided, a random UUID4 id will be assigned to the
            conversation.

    Usage:

    ```python
    conversation = Conversation("Going to the movies tonight - any suggestions?")
    conversation.add_message({"role": "assistant", "content": "The Big lebowski."})
    conversation.add_message({"role": "user", "content": "Is it good?"})
    ```NÚmessagesÚconversation_idc                 óª  — |st          j        ¦   «         }|€"|                     dd ¦  «        }|�d|dœg}ng }nt          |t          ¦  «        rd|dœg}d| _        |                     dd ¦  «        }|                     dd ¦  «        }|�|€t          d¦  «        ‚|�¨g }|€g }t          t          t          |¦  «        t          |¦  «        g¦  «        ¦  «        D ]d}|t          |¦  «        k     r| 
                    d||         dœ¦  «         |t          |¦  «        k     r| 
                    d||         dœ¦  «         Œe||z   }|| _         || _        d S )	NÚtextÚuser©ÚroleÚcontentr   Úgenerated_responsesÚpast_user_inputsz>generated_responses cannot be passed without past_user_inputs!Ú	assistant)ÚuuidÚuuid4ÚpopÚ
isinstanceÚstrÚ_num_processed_user_inputsÚ
ValueErrorÚrangeÚmaxÚlenÚappendr   )	Úselfr   r   Údeprecated_kwargsr   r   r   Úlegacy_messagesÚis	            ú…/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/transformers/pipelines/conversational.pyÚ__init__zConversation.__init__(   s   € ð ð 	+Ý"œj™lœlˆOàÐØ$×(Ò(¨°Ñ6Ô6ˆDØÐØ%+¸Ð=Ð=Ð>��à��Ý˜¥#Ñ&Ô&ð 	?Ø!'°HÐ=Ð=Ð>ˆHð +,ˆÔ'Ø/×3Ò3Ð4IÈ4ÑPÔPÐØ,×0Ò0Ð1CÀTÑJÔJÐØÐ*Ð/?Ð/GÝÐ]Ñ^Ô^Ð^ØÐ'Ø ˆOØ"Ð*Ø&(Ð#å�3¥Ð$4Ñ 5Ô 5µsÐ;NÑ7OÔ7OÐPÑQÔQÑRÔRð eð e�Ø•sÐ+Ñ,Ô,Ò,Ð,Ø#×*Ò*°FÐGWÐXYÔGZÐ+[Ð+[Ñ\Ô\Ð\Ø•sÐ.Ñ/Ô/Ò/Ð/Ø#×*Ò*°KÐL_Ð`aÔLbÐ+cÐ+cÑdÔdÐdøØ&¨Ñ1ˆHà#ˆŒ	Ø ˆŒˆˆó    c                 óp   — t          |t          ¦  «        sdS | j        |j        k    p| j        |j        k    S )NF)r   r   r   r   )r'   Úothers     r+   Ú__eq__zConversation.__eq__M   s6   € Ý˜%¥Ñ.Ô.ð 	Ø�5ØŒy˜EœJÒ&ÐI¨$¬-¸5¼>Ò*IÐIr-   Úmessagec                 óØ   — t          |                     ¦   «         ¦  «        ddhk    st          d¦  «        ‚|d         dvrt          d¦  «        ‚| j                             |¦  «         d S )Nr   r   z6Message should contain only 'role' and 'content' keys!)r   r   ÚsystemzBOnly 'user', 'assistant' and 'system' roles are supported for now!)ÚsetÚkeysr"   r   r&   ©r'   r1   s     r+   Úadd_messagezConversation.add_messageR   sl   € Ý�7—<’<‘>”>Ñ"Ô" v¨yÐ&9Ò9Ð9ÝÐUÑVÔVÐVØ�6Œ?Ð"AÐAÐAÝÐaÑbÔbÐbØŒ×Ò˜WÑ%Ô%Ð%Ð%Ð%r-   Fr   Ú	overwritec                 ó`  — t          | ¦  «        dk    r}| d         d         dk    rk|r:t                               d| d         d         › d|› d�¦  «         || d         d<   dS t                               d| d         d         › d	|› d
�¦  «         dS | j                             d|dœ¦  «         dS )a3  
        Add a user input to the conversation for the next round. This is a legacy method that assumes that inputs must
        alternate user/assistant/user/assistant, and so will not add multiple user messages in succession. We recommend
        just using `add_message` with role "user" instead.
        r   éÿÿÿÿr   r   z8User input added while unprocessed input was existing: "r   z" was overwritten with: "z".z" new input ignored: "z>". Set `overwrite` to True to overwrite unprocessed user inputr   N)r%   ÚloggerÚwarningr   r&   )r'   r   r8   s      r+   Úadd_user_inputzConversation.add_user_inputY   s  € õ ˆt‰9Œ9�qŠ=ˆ=˜T "œX fÔ-°Ò7Ð7Øð 
Ý—’ð'ÈtÐTVÌxÐXaÔObð 'ð 'Ø"ð'ð 'ð 'ñô ð ð '+��R”˜Ñ#Ð#Ð#å—’ðfÈtÐTVÌxÐXaÔObð fð fØ!%ðfð fð fñô ð ð ð ð
 ŒM× Ò ¨&¸TÐ!BÐ!BÑCÔCÐCÐCÐCr-   Úresponsec                 ó@   — | j                              d|dœ¦  «         dS )zr
        This is a legacy method. We recommend just using `add_message` with an appropriate role instead.
        r   r   N)r   r&   )r'   r>   s     r+   Úappend_responsezConversation.append_responsen   s(   € ð 	Œ×Ò k¸hÐGÐGÑHÔHÐHÐHÐHr-   c                 ó8   — t          | j        ¦  «        | _        dS )a)  
        This is a legacy method, as the Conversation no longer distinguishes between processed and unprocessed user
        input. We set a counter here to keep behaviour mostly backward-compatible, but in general you should just read
        the messages directly when writing new code.
        N)r%   Ú_user_messagesr!   ©r'   s    r+   Úmark_processedzConversation.mark_processedt   s   € õ +.¨dÔ.AÑ*BÔ*BˆÔ'Ð'Ð'r-   c              #   ó&   K  — | j         D ]}|V — Œd S ©N©r   r6   s     r+   Ú__iter__zConversation.__iter__|   s,   è è € Ø”}ð 	ð 	ˆGØˆMˆMˆMˆMð	ð 	r-   c                 ó   — | j         |         S rF   rG   )r'   Úitems     r+   Ú__getitem__zConversation.__getitem__€   s   € ØŒ}˜TÔ"Ð"r-   c                 ó   — || j         |<   d S rF   rG   )r'   ÚkeyÚvalues      r+   Ú__setitem__zConversation.__setitem__ƒ   s   € Ø"ˆŒ�cÑÐÐr-   c                 ó*   — t          | j        ¦  «        S rF   )r%   r   rC   s    r+   Ú__len__zConversation.__len__†   s   € Ý�4”=Ñ!Ô!Ð!r-   c                 ó^   — d| j         › d�}| j        D ]}||d         › d|d         › d�z  }Œ|S )a  
        Generates a string representation of the conversation.

        Returns:
            `str`:

        Example:
            Conversation id: 7d15686b-dc94-49f2-9c4b-c9eac6a1f114 user: Going to the movies tonight - any suggestions?
            bot: The Big Lebowski
        zConversation id: ú
r   z: r   )r   r   )r'   Úoutputr1   s      r+   Ú__repr__zConversation.__repr__‰   sT   € ð 3 T¤YÐ2Ð2Ð2ˆØ”}ð 	Cð 	CˆGØ˜ œÐBÐB¨G°IÔ,>ÐBÐBÐBÑBˆFˆFØˆr-   c              #   óJ   K  — | j         D ]}|d         dk    |d         fV — Œd S )Nr   r   r   rG   r6   s     r+   Ú
iter_textszConversation.iter_texts™   sI   è è € ð ”}ð 	@ð 	@ˆGØ˜&”/ VÒ+¨W°YÔ-?Ð?Ð?Ð?Ð?Ð?ð	@ð 	@r-   c                 ó$   — d„ | j         D ¦   «         S )Nc                 ó6   — g | ]}|d          dk    ¯|d         ‘ŒS )r   r   r   © ©Ú.0r1   s     r+   ú
<listcomp>z/Conversation._user_messages.<locals>.<listcomp>£   s,   € Ð]Ð]Ð] wÀ7È6Ä?ÐV\ÒC\ÐC\�˜	Ô"ÐC\ÐC\ÐC\r-   rG   rC   s    r+   rB   zConversation._user_messagesŸ   s   € ð ^Ð]°$´-Ð]Ñ]Ô]Ð]r-   c                 ó¨   — | j         sg S | j        d         d         dk    s| j        t          | j         ¦  «        k    r| j         S | j         d d…         S )Nr:   r   r   )rB   r   r!   r%   rC   s    r+   r   zConversation.past_user_inputs¥   sb   € ð
 Ô"ð 	ØˆIð
 Œ=˜Ô˜VÔ$¨Ò.Ð.°$Ô2QÕUXÐY]ÔYlÑUmÔUmÒ2mÐ2mØÔ&Ð&àÔ" 3 B 3Ô'Ð'r-   c                 ó$   — d„ | j         D ¦   «         S )Nc                 ó6   — g | ]}|d          dk    ¯|d         ‘ŒS )r   r   r   rZ   r[   s     r+   r]   z4Conversation.generated_responses.<locals>.<listcomp>¹   s,   € ÐbÐbÐb wÀ7È6Ä?ÐVaÒCaÐCa�˜	Ô"ÐCaÐCaÐCar-   rG   rC   s    r+   r   z Conversation.generated_responsesµ   s   € ð cÐb°$´-ÐbÑbÔbÐbr-   c                 ó   — | j         d         S )Nr:   )rB   rC   s    r+   Únew_user_inputzConversation.new_user_input»   s   € ð Ô" 2Ô&Ð&r-   )NN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   r   r   ÚUUIDr,   r0   r7   Úboolr=   r@   rD   rH   rK   rO   rQ   rU   rW   ÚpropertyrB   r   r   rb   rZ   r-   r+   r   r      sÍ  € € € € € ðð ð, _cð#!ð #!Ø˜c 4¨¨S°#¨X¬Ô#7Ð7Ô8ð#!ØRVÔR[ð#!ð #!ð #!ð #!ðJJð Jð Jð
& 4¨¨S¨¤>ð &ð &ð &ð &ðDð D 3ð D°4ð Dð Dð Dð Dð*I¨ð Ið Ið Ið IðCð Cð Cðð ð ð#ð #ð #ð#ð #ð #ð"ð "ð "ðð ð ð @ð @ð @ð ð^ð ^ñ „Xð^ð
 ð(ð (ñ „Xð(ð ðcð cñ „Xðcð
 ð'ð 'ñ „Xð'ð 'ð 'r-   r   a  
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    c                   óš   ‡ — e Zd ZdZˆ fd„Z	 dd„Zddeee         e	ee	         f         fˆ fd„Z
dd	e	d
eeef         fd„Zdd„Zdd„Zˆ xZS )ÚConversationalPipelinea‘  
    Multi-turn conversational pipeline.

    Example:

    ```python
    >>> from transformers import pipeline, Conversation

    >>> chatbot = pipeline(model="microsoft/DialoGPT-medium")
    >>> conversation = Conversation("Going to the movies tonight - any suggestions?")
    >>> conversation = chatbot(conversation)
    >>> conversation.generated_responses[-1]
    'The Big Lebowski'

    >>> conversation.add_user_input("Is it an action movie?")
    >>> conversation = chatbot(conversation)
    >>> conversation.generated_responses[-1]
    "It's a comedy."
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    This conversational pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"conversational"`.

    The models that this pipeline can use are models that have been fine-tuned on a multi-turn conversational task,
    currently: *'microsoft/DialoGPT-small'*, *'microsoft/DialoGPT-medium'*, *'microsoft/DialoGPT-large'*. See the
    up-to-date list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=conversational).
    c                 ó‚   •—  t          ¦   «         j        |i |¤Ž | j        j        €| j        j        | j        _        d S d S rF   )Úsuperr,   Ú	tokenizerÚpad_token_idÚ	eos_tokenÚ	pad_token)r'   ÚargsÚkwargsÚ	__class__s      €r+   r,   zConversationalPipeline.__init__ë   sG   ø€ Ø�‰ŒÔ˜$Ð) &Ð)Ð)Ð)ØŒ>Ô&Ð.Ø'+¤~Ô'?ˆDŒNÔ$Ð$Ð$ð /Ð.r-   Nc                 óŽ   — i }i }i }|�||d<   |�||d<   d|v r|d         |d<   |�||d<   |r|                      |¦  «         |||fS )NÚmin_length_for_responseÚminimum_tokensÚ
max_lengthÚclean_up_tokenization_spaces)Úupdate)r'   rv   rw   ry   Úgenerate_kwargsÚpreprocess_paramsÚforward_paramsÚpostprocess_paramss           r+   Ú_sanitize_parametersz+ConversationalPipeline._sanitize_parametersð   sŽ   € ð ÐØˆØÐà"Ð.Ø;RÐÐ7Ñ8ØÐ%Ø/=ˆNÐ+Ñ,à˜?Ð*Ð*Ø+:¸<Ô+HˆN˜<Ñ(à'Ð3ØA]ÐÐ=Ñ>àð 	3Ø×!Ò! /Ñ2Ô2Ð2Ø  .Ð2DÐDÐDr-   r   Úconversationsc                 ó˜  •— t          |t          ¦  «        r+t          |d         t          ¦  «        rt          |¦  «        }n<t          |t          ¦  «        r't          |d         t          ¦  «        rd„ |D ¦   «         } t	          ¦   «         j        |fd|i|¤Ž}t          |t          ¦  «        rt          |¦  «        dk    r|d         S |S )a/  
        Generate responses for the conversation(s) given as inputs.

        Args:
            conversations (a [`Conversation`] or a list of [`Conversation`]):
                Conversation to generate responses for. Inputs can also be passed as a list of dictionaries with `role`
                and `content` keys - in this case, they will be converted to `Conversation` objects automatically.
                Multiple conversations in either format may be passed as a list.
            clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
                Whether or not to clean up the potential extra spaces in the text output.
            generate_kwargs:
                Additional keyword arguments to pass along to the generate method of the model (see the generate method
                corresponding to your framework [here](./model#generative-models)).

        Returns:
            [`Conversation`] or a list of [`Conversation`]: Conversation(s) with updated generated responses for those
            containing a new user input.
        r   c                 ó,   — g | ]}t          |¦  «        ‘ŒS rZ   )r   )r\   Úconvs     r+   r]   z3ConversationalPipeline.__call__.<locals>.<listcomp>   s    € ÐJÐJÐJ°D�\¨$Ñ/Ô/ÐJÐJÐJr-   Únum_workersr   )r   ÚlistÚdictr   rm   Ú__call__r%   )r'   r€   r„   rs   Úoutputsrt   s        €r+   r‡   zConversationalPipeline.__call__  sÏ   ø€ õ. �m¥TÑ*Ô*ð 	K­z¸-ÈÔ:JÍDÑ/QÔ/Qð 	KÝ(¨Ñ7Ô7ˆMˆMÝ˜¥tÑ,Ô,ð 	Kµ¸MÈ!Ô<LÍdÑ1SÔ1Sð 	KØJÐJ¸MÐJÑJÔJˆMØ"•%‘'”'Ô" =ÐTÐT¸kÐTÈVÐTÐTˆÝ�g�tÑ$Ô$ð 	­¨W©¬¸Ò):Ð):Ø˜1”:ÐØˆr-   é    ÚconversationÚreturnc                 óÆ   — | j                              |d¬¦  «        }| j        dk    rt          j        |g¦  «        }n | j        dk    rt          j        |g¦  «        }||dœS )NT)Úadd_generation_promptÚptÚtf)Ú	input_idsrŠ   )rn   Úapply_chat_templateÚ	frameworkÚtorchÚ
LongTensorr�   Úconstant)r'   rŠ   rv   r�   s       r+   Ú
preprocessz!ConversationalPipeline.preprocess&  sj   € Ø”N×6Ò6°|Ð[_Ð6Ñ`Ô`ˆ	àŒ>˜TÒ!Ð!ÝÔ(¨)¨Ñ5Ô5ˆIˆIØŒ^˜tÒ#Ð#Ýœ Y KÑ0Ô0ˆIØ&¸ÐEÐEÐEr-   é
   c                 óâ   — |d         j         d         }|                     d¦  «        }d|vr	d|vrd|d<    | j        j        di |¤|¤Ž}| j        j        j        rd}n|}|d d …|d …f         |dœS )	Nr�   r   rŠ   rx   Úmax_new_tokensé   )Ú
output_idsrŠ   rZ   )Úshaper   ÚmodelÚgenerateÚconfigÚis_encoder_decoder)r'   Úmodel_inputsrw   r{   ÚnrŠ   r›   Ústart_positions           r+   Ú_forwardzConversationalPipeline._forward/  s£   € Ø˜Ô%Ô+¨AÔ.ˆØ#×'Ò'¨Ñ7Ô7ˆØ˜Ð.Ð.Ð3CÈ?Ð3ZÐ3ZØ03ˆOÐ,Ñ-Ø(�T”ZÔ(ÐKÐK¨<ÐK¸?ÐKÐKˆ
ØŒ:ÔÔ/ð 	ØˆNˆNàˆNØ(¨¨¨¨N¨O¨OÐ);Ô<ÈlÐ[Ð[Ð[r-   Tc                 óœ   — |d         }| j                              |d         d|¬¦  «        }|d         }|                     d|dœ¦  «         |S )Nr›   r   T)Úskip_special_tokensry   rŠ   r   r   )rn   Údecoder7   )r'   Úmodel_outputsry   r›   ÚanswerrŠ   s         r+   Úpostprocessz"ConversationalPipeline.postprocess;  sc   € Ø" <Ô0ˆ
Ø”×&Ò&Ø�qŒMØ $Ø)Eð 'ñ 
ô 
ˆð
 % ^Ô4ˆØ× Ò ¨+À&Ð!IÐ!IÑJÔJÐJØÐr-   )NNN)r   )r‰   )r—   )T)rc   rd   re   rf   r,   r   r   r   r   r   r‡   r    r   r–   r¤   rª   Ú__classcell__)rt   s   @r+   rk   rk   Â   s  ø€ € € € € ðð ð>@ð @ð @ð @ð @ð _cðEð Eð Eð Eð,ð  e¨D°¬J¸ÀdÈ<ÔFXÐ,XÔ&Yð ð ð ð ð ð ð@Fð F |ð FÐTXÐY\Ð^aÐYaÔTbð Fð Fð Fð Fð
\ð 
\ð 
\ð 
\ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	r-   rk   )r   Útypingr   r   r   r   Úutilsr   r	   r
   r   Úbaser   r   Ú
tensorflowr�   r“   Ú
get_loggerrc   r;   r   rk   rZ   r-   r+   ú<module>r±      sM  ðØ €€€Ø )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )à TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TØ .Ð .Ð .Ð .Ð .Ð .Ð .Ð .ð €?ÑÔð ØÐÐÐàÐÑÔð Ø€L€L€Lð 
ˆÔ	˜HÑ	%Ô	%€ðm'ð m'ð m'ð m'ð m'ñ m'ô m'ð m'ð` ÐØðñô ðyð yð yð yð y˜Xñ yô yñô ðyð yð yr-   