
    <j                        d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZ  e       Zh d
Zh dZdedefdZej9                  ddge	e   dd       ed      fdede	e   fd       Zy)    N)Path)	APIRouter
UploadFileFilestatus)ResponseWrapper)HTTPException)settings)OCRResponseImageOCRSchema)
ChatOpenAI)ChatPromptTemplateHumanMessagePromptTemplateSystemMessagePromptTemplate>   .bmp.gif.jpg.png.tif.jpeg.tiff.webp>   	image/bmp	image/gif	image/png	image/tif
image/jpeg
image/tiff
image/webpbase64returnc                    K   dd| idg}t        j                  t        j                  d      t	        j                  |      g      S w)N	image_urlurl)typer#   zYou are a helpful assistant that can extract information from images or documents. Try to avoid short form filling and try to fill full form if possible else fill the short form)r   from_messagesr   from_templater   )r    input_contents     0/var/www/html/ls-doc-apis/app/controllers/ocr.pyget_chat_promptr*      s^       6	
M ++#11  3f  	g"00?-  s   AAz/ocrOCRzOCR document.   )tagsresponse_modelsummarystatus_code.filec                   K   t        | j                  xs d      j                  j                         }|t        vrt        t        j                  d      | j                  t        vrt        t        j                  d      | j                          d {   }|st        t        j                  d      d| j                   dt        j                  |      j                  d       }t        t        j                   d	
      }|j#                  t$              }t'        |       d {   |z  }|j)                  i        d {   }t+        ddt-        |j.                  |j0                  |j2                  |j4                  |j6                  |j8                  |j:                  |j<                  |j>                  |j@                  |jB                  |jD                              S 7 P7 7 w)N zQOnly image files (.jpg, .jpeg, .png, .webp, .bmp, .gif, .tiff, .tif) are allowed.)r0   detailz/Invalid file type. Please upload a valid image.zUploaded file is empty.zdata:z;base64,zutf-8zgpt-5.4)api_keymodelTSUCCESS)	firstNamelastNameemailphoneNumberaddresscitystatezipCodecountrydocumentTypedocumentNumberdateOfBirth)r   messagedata)#r   filenamesuffixlowerALLOWED_EXTENSIONSr	   r   HTTP_400_BAD_REQUESTcontent_typeALLOWED_CONTENT_TYPESreadr    	b64encodedecoder   r
   OPENAI_API_KEYwith_structured_outputr   r*   ainvoker   r   
first_name	last_namer:   phoner<   r=   r>   zipr@   document_typedocument_numberdob)r1   	extensionimage_bytesbase64_imagellmstructured_llmchainresults           r)   ocrra   ,   s     T]](b)00668I**33f
 	
  5533D
 	
 		#K33,
 	
 !!"(K(//8
9	; 
 X44I
FC//?N!,//.@E#(==#44F''%%,,NN,,JJNN--!11


 - $$ 04s8   BG.G'BG.+G*,G.G,B G.*G.,G.)r    pathlibr   fastapir   r   r   r   app.schemasr   app.exceptionsr	   app.configs.settingsr
   app.schemas.ocrr   r   !langchain_openai.chat_models.baser   langchain_core.promptsr   r   r   routerrI   rL   strr*   postra        r)   <module>ro      s      7 7 ' ( ) 7 9 n n	X  # *<   

";/   "&c 9J 9{/K 99rn   