U
    9%e	                     @   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 rJd 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ed	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.Zdocument_qaimagetextc                    s    t  stdt j|| d S )NzBPillow must be installed to use the DocumentQuestionAnsweringTool.)r   
ValueErrorsuper__init__)selfargskwargs	__class__ m/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/tools/document_question_answering.pyr   +   s    z&DocumentQuestionAnsweringTool.__init__r   )document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)Zadd_special_tokensreturn_tensors)r   )decoder_input_idspixel_values)replacepre_processor	tokenizerZ	input_idsr   )r   r   r   Z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_lengthZearly_stoppingpad_token_ideos_token_idZ	use_cacheZ	num_beamsZbad_words_idsZreturn_dict_in_generate)modelgeneratetoZdevicedecoderconfigZmax_position_embeddingsr   r   r"   r#   Z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   )countZanswer)
r   Zbatch_decoder   r   Z	eos_tokenZ	pad_tokenresubstripZ
token2json)r   outputssequencer   r   r   decodeI   s    z$DocumentQuestionAnsweringTool.decode)__name__
__module____qualname__Zdefault_checkpointdescriptionnamer   Zpre_processor_classr   Zmodel_classr*   r1   r   strr    r+   r3   __classcell__r   r   r   r   r	      s   
r	   )r.   Zmodels.autor   Zmodels.vision_encoder_decoderr   utilsr   baser   ZPILr   r	   r   r   r   r   <module>   s   