
    %	&h(                         d dl mZmZmZ ddl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mZ  G d ded	
      Z G d de	d	
      Z G d de
d	
      Z G d de      ZdgZy)    )ListOptionalUnion   )BatchFeature)
ImageInput)ImagesKwargsProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInputc                       e Zd ZU eed<   y)Emu3TextKwargsreturn_for_image_generationN)__name__
__module____qualname__bool__annotations__     ~/var/www/pru.catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/transformers/models/emu3/processing_emu3.pyr   r      s    !%%r   r   F)totalc                   "    e Zd ZU eed<   eed<   y)Emu3ImagesKwargsratio
image_areaN)r   r   r   strr   intr   r   r   r   r      s    JOr   r   c                   6    e Zd ZU eed<   eed<   ddiddddZy	)
Emu3ProcessorKwargstext_kwargsimages_kwargsr   Fz1:1i  )r   r   )r$   r%   N)r   r   r   r   r   r   	_defaultsr   r   r   r#   r#   "   s.    ## *5
  
	Ir   r#   c                        e Zd ZdZddgZdgZdZdZ	 d fd	Z	 	 	 	 dde	e
   d	e	eeeee   ee   f      d
ee   defdZd Zde
fdZd Zd Zed        Z xZS )Emu3Processora  
    Constructs a Emu3 processor which wraps a Emu3 image processor and a GPT2 tokenizer into a single
    processor.

    [`Emu3Processor`] offers all the functionalities of [`Emu3ImageProcessor`] and [`GPT2TokenizerFast`].
    See the [`~Emu3Processor.__call__`] and [`~Emu3Processor.decode`] for more information.

    Args:
        image_processor ([`Emu3ImageProcessor`]):
            The image processor is a required input.
        tokenizer ([`Emu3TokenizerFast`]):
            The tokenizer is a required input.
        chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
            in a chat into a tokenizable string.
    image_processor	tokenizerchat_template)GPT2TokenizerGPT2TokenizerFastEmu3ImageProcessorc                    |j                   | _         |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        d| _	        t        | -  |||       y )N   )r+   )image_token	boi_tokenimage_start_token	eoi_tokenimage_end_tokenimage_wrapper_tokenfake_token_around_image	eof_token	bos_tokendownsample_ratiosuper__init__)selfr)   r*   r+   kwargs	__class__s        r   r<   zEmu3Processor.__init__F   ss     %00!*!4!4(22'0'D'D$",,",, !)=Qr   imagestextr>   returnc                    t        |t              r|g}n.t        |t              st        |d   t              st        d       | j                  t
        fd| j                  j                  i|}|d   j                  dd      }|d   j                  dd	      }|d   j                  d
d	      }	|r|t        d      |s||t        d      i }
| j                   }| j                   | j                   }|s| | j                  |fi |d   }
t        |
j                        }g }|D ]  }| j                   |v rt#        |      }|\  }}|| j$                  z  }|| j$                  z  }||dz   z  }| | d| | j&                   d|z   | }|j)                  | j                   |d      }| j*                   | }| j                   |v r|j-                  |        |D cg c]  }|j)                  d| j                           }}nj|rh| j/                  ||	| j$                        \  }}| | d| | j&                   }|D cg c]  }| j*                   | |  }}||ggt1        |      z  |
d<    | j                  |fi |d   } |j2                  di |
 t5        ||d   j                  dd	            S c c}w c c}w )a  
        Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
        and `kwargs` arguments to Emu3TokenizerFast's [`~Emu3TokenizerFast.__call__`] if `text` is not `None` to encode
        the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
        CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
        of the above two methods for more information.

        Args:
            images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
                The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
                tensor. Both channels-first and channels-last formats are supported.
            text (`str`, `List[str]`, `List[List[str]]`):
                The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
                (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
                `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors of a particular framework. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return NumPy `np.ndarray` objects.
                - `'jax'`: Return JAX `jnp.ndarray` objects.

        Returns:
            [`BatchFeature`]: A [`BatchFeature`] with the following fields:

            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
        r   zAInvalid input text. Please provide a string, or a list of stringstokenizer_init_kwargsr$   r   Fr%   r   Nr   zGYou should not provide `images` when `return_for_image_generation=True`zOYou must provide either text or images when `return_for_image_generation=False`   *z<placeholder>image_sizescommon_kwargsreturn_tensors)datatensor_typer   )
isinstancer    list	TypeError_merge_kwargsr#   r*   init_kwargspop
ValueErrorr3   r8   r5   r)   iterrG   r1   nextr:   r7   replacer9   appendcalculate_generate_sizelenupdater   )r=   r@   rA   audiovideosr>   output_kwargsr   r   r   image_featuresimage_start_tokensimage_end_tokensrG   prompt_stringssample
image_sizeheightwidthimage_seq_lengthimage_placeholderimage_promptrJ   s                          r   __call__zEmu3Processor.__call__V   sC   T dC 6DD$'
47C0H_``***
"&.."<"<
 

 '4M&B&F&FGdfk&l#o.227DA"?377dK
&6+=fgg*t|noo $ 6 67"nn-d.B.B-CD +v/A1T11&[M/<Z[N~99:KN .&&&0!%k!2J$.MFE#t'<'<<F!T%:%::E'-';$+=*>vhawtOkOkNlm|  @P  nP  mQ  Rb  Qc  )d%#^^D,<,<>OQRSF $/x8F &&&0 %%f-. Ucc&FNN?D4D4DEcDc ) 88
DLaLabMFE01&5'$B^B^A_`LLPQ&t~~&vh|n=QDQ.4e_,=D	,IN=) t~~dCmM&BC%n%=3Q3U3UVfhl3mnn d Rs   ##J;K c                     t        t        |j                  d            \  }}||z  }||z  dz  }t        t        ||z  |z              }t        t        ||z  |z              }	||	fS )N:g      ?)mapr!   splitround)
r=   r   r   spatial_factorrd   rc   current_areatarget_ratiotoken_heighttoken_widths
             r   rW   z%Emu3Processor.calculate_generate_size   sp    CS!12vv~"\1c95,!6!GHI% 4~ EFG[((r   c                 <     | j                   j                  |fi |S N)r)   postprocess)r=   r@   r>   s      r   ru   zEmu3Processor.postprocess   s     /t##//A&AAr   c                 :     | j                   j                  |i |S )z
        This method forwards all its arguments to Emu3TokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r*   batch_decoder=   argsr>   s      r   rw   zEmu3Processor.batch_decode   s     
 +t~~**D;F;;r   c                 :     | j                   j                  |i |S )z
        This method forwards all its arguments to Emu3TokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r*   decoderx   s      r   r{   zEmu3Processor.decode   s     
 %t~~$$d5f55r   c                     | j                   j                  }| j                  j                  }t        t        j                  ||z               S rt   )r*   model_input_namesr)   rM   dictfromkeys)r=   tokenizer_input_namesimage_processor_input_namess      r   r}   zEmu3Processor.model_input_names   s?     $ @ @&*&:&:&L&L#DMM"7:U"UVWWr   rt   )NNNN)r   r   r   __doc__
attributesvalid_kwargstokenizer_classimage_processor_classr<   r   r   r   r   r   r   r   r#   r   rh   rW   ru   rw   r{   propertyr}   __classcell__)r?   s   @r   r(   r(   0   s      $[1J#$L<O0 	R$ (,hlao$ao uY(94	?DQbLccdeao ,-ao 
aoF)B* B<6 X Xr   r(   N)typingr   r   r   image_processing_utilsr   image_utilsr   processing_utilsr	   r
   r   r   r   tokenization_utils_baser   r   r   r   r#   r(   __all__r   r   r   <module>r      sb   " ) ( 2 % b b C&Zu &|5 
*% gXN gXT 
r   