
    Ng                     z    d dl Z d dlmZ d dlmZmZ d dlZd dlmZ	 d dl
mZmZmZ d dlmZ  G d de          ZdS )	    N)Path)OptionalUnion)Image)DonutProcessorVisionEncoderDecoderConfigVisionEncoderDecoderModel)UnstructuredModelc                        e Zd ZdZdej        f fdZddddej        	                                rdndfde
eeef         d	e
eeef         d
ee
eeef                  dee         dee         f
dZdej        fdZ xZS )UnstructuredDonutModelz7Unstructured model wrapper for Donut image transformer.xc                 p    t                                          |           |                     |          S )z!Make prediction using donut model)superpredictrun_prediction)selfr   	__class__s     _/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured_inference/models/donut.pyr   zUnstructuredDonutModel.predict   s-    ""1%%%    Nz<s>cudacpumodel	processorconfigtask_promptdevicec                    || _         || _        	 t          |t                    st	          j        |          }t          j        d           t          j        |          | _	        t          j        ||          | _
        nE# t          $ r8 t          j        d           t          j        d           t          d          w xY w| j
                            |           dS )z4Loads the donut model using the specified parametersz(Loading the Donut model and processor...)r   zFailed to initialize the model.zHEnsure that the Donut parameters config, model and processor are correctz@Review the parameters to initialize a UnstructuredDonutModel objN)r   r   
isinstancer	   r   from_pretrainedlogginginfor   r   r   EnvironmentErrorcriticalImportErrorto)r   r   r   r   r   r   s         r   
initializez!UnstructuredDonutModel.initialize   s     '	bf&?@@ L3CFKKLCDDD+;IFFDN2B5QWXXXDJJ 	b 	b 	b>???Z   `aaa	b 	
fs   A1B ACc                 8   |                      |d          j        }| j                             | j        dd          j        }| j                            |                    | j                  |                    | j                  | j        j	        j
        j        d| j         j        j        | j         j        j        dd| j         j        j        ggd
  
        }| j                             |j                  d         }| j                             |          }|S )	zInternal prediction method.pt)return_tensorsF)add_special_tokensr)   T   )	decoder_input_ids
max_lengthearly_stoppingpad_token_ideos_token_id	use_cache	num_beamsbad_words_idsreturn_dict_in_generater   )r   pixel_values	tokenizerr   	input_idsr   generater%   r   decoderr   max_position_embeddingsr/   r0   unk_token_idbatch_decode	sequences
token2json)r   r   r5   r,   outputs
predictions         r   r   z%UnstructuredDonutModel.run_prediction5   s	   ~~a~==J N44$ 5 
 
 	 	
 *%%OODK((/224;??z)0H1>1> N4ABC$( & 
 
 ^001BCCAF

 ^..z::
r   )__name__
__module____qualname____doc__PILImager   r   torchr   is_availabler   strr   r	   r   r   r   r&   r   __classcell__)r   s   @r   r   r      s       AA& & & & & & & >B6:IM%**/**A*A*C*C N S$ 99: dN23 sD*DDEF	
 c]    :        r   r   )r    pathlibr   typingr   r   rF   PILr   rE   transformersr   r   r	   /unstructured_inference.models.unstructuredmodelr
   r    r   r   <module>rP      s           " " " " " " " "  ! ! ! ! ! !          N M M M M M? ? ? ? ?. ? ? ? ? ?r   