o
    h	                     @   s^   d dl Z ddlmZ ddlmZ ddlmZ ddlmZ e r%d dl	m
Z
 G d	d
 d
eZdS )    N   )AutoProcessor)VisionEncoderDecoderModel)is_vision_available   )PipelineTool)Imagec                       s`   e Zd ZdZdZdZeZeZ	ddgZ
dgZ fddZdd	d
efddZdd Zdd Z  ZS )DocumentQuestionAnsweringToolz*naver-clova-ix/donut-base-finetuned-docvqaa  This is a tool that answers a question about an document (pdf). It takes an input named `document` which should be the document containing the information, as well as a `question` that is the question about the document. It returns a text that contains the answer to the question.document_qaimagetextc                    s$   t  stdt j|i | d S )NzBPillow must be installed to use the DocumentQuestionAnsweringTool.)r   
ValueErrorsuper__init__)selfargskwargs	__class__ d/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/tools/document_question_answering.pyr   +   s   z&DocumentQuestionAnsweringTool.__init__documentr   questionc                 C   s>   d}| d|}| jj|dddj}| j|ddj}||dS )Nz9<s_docvqa><s_question>{user_input}</s_question><s_answer>z{user_input}Fpt)add_special_tokensreturn_tensors)r   )decoder_input_idspixel_values)replacepre_processor	tokenizer	input_idsr   )r   r   r   task_promptpromptr   r   r   r   r   encode1   s   
z$DocumentQuestionAnsweringTool.encodec                 C   sX   | j j|d | j|d | j| j jjjd| jjj	| jjj
dd| jjjggdd
jS )Nr   r   Tr   )	r   
max_lengthearly_stoppingpad_token_ideos_token_id	use_cache	num_beamsbad_words_idsreturn_dict_in_generate)modelgeneratetodevicedecoderconfigmax_position_embeddingsr   r    r'   r(   unk_token_id	sequences)r   inputsr   r   r   forward;   s   
z%DocumentQuestionAnsweringTool.forwardc                 C   s^   | j |d }|| j jjd}|| j jjd}tjdd|dd }| j 	|}|d S )Nr    z<.*?>r   )countanswer)
r   batch_decoder   r    	eos_token	pad_tokenresubstrip
token2json)r   outputssequencer   r   r   decodeI   s   z$DocumentQuestionAnsweringTool.decode)__name__
__module____qualname__default_checkpointdescriptionnamer   pre_processor_classr   model_classr6   rB   r   strr$   r7   rD   __classcell__r   r   r   r   r	      s    
r	   )r>   models.autor   models.vision_encoder_decoderr   utilsr   baser   PILr   r	   r   r   r   r   <module>   s   