
    Mh	                     n   d dl mZmZmZmZ d dlmZ d dlZd dlZ	d dl
Z
d dlZd dlmZ d dlZd dlZd dlZ ej                            ej"                         Z e       Zi ZdZdedefdZd	edefd
Zd Zej;                  d       ed      fdefd       Zedk(  rd dl Z  e jB                  edd       yy)    )FastAPIFile
UploadFileHTTPException)JSONResponseN)visioniX  image_bytesreturnc                 H    t        j                  |       j                         S )z%Generate SHA-256 hash of image bytes.)hashlibsha256	hexdigest)r	   s    C/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/pydep/OCR.pyget_image_hashr      s    >>+&0022    	timestampc                 >    t        j                          | z
  t        k  S )z1Check if cache entry is still valid (within TTL).)time	CACHE_TTL)r   s    r   is_cache_validr      s    IIK)#y00r   c                      t        j                          } t        j                         D cg c]  \  }}t        |d         r| }}}|D ]  }t        j	                  |d        yc c}}w )zRemove expired cache entries.r   N)r   cacheitemsr   pop)current_timekeyvalueexpired_keyss       r   clear_expired_cacher      sZ    99;L*/++-bJC~eT_N`?aCbLb 		#t cs   A(A(z/ocr.imagec                   K   t        dt        j                                	 | st        dd      | j                          d {   }t	        |      }t                |t        v r,t        t        |   d         rt        d       t        |   d   S t        j                  |      } t        j                  | 	      }|j                  }g }|D ]  }|j                  d
|j                  i       ! t        d|d      }|t        j                         dt        |<   |S 7 # t         $ r}t        dt#        |            d }~ww xY ww)Nz
ocr calledi  zNo image file provided)status_codedetailr   zServing from cacheresult)content)r    textT)successresults)r$   r   i  )printr   r   readr   r   r   r   r   Imageclienttext_detectiontext_annotationsappenddescriptionr   	Exceptionstr)	r    r	   
image_hashresponsetextstext_resultsr&   response_dataes	            r   ocrr9   %   sD    	,		$*<C8PQQ "JJL( $K0
 	>%
2CK2P#Q&'$X.. [1((u(5)) 	D((! 	
 %#&
  $
j
 C )F  <CF;;<sH   E"D  DAD  EBD  ED   	E)D??EE__main__z0.0.0.0iq  )hostport)"fastapir   r   r   r   fastapi.responsesr   easyocrnumpynpcv2r   google.cloudr   dotenvr   load_dotenvImageAnnotatorClientr,   appr   r   bytesr2   r   floatboolr   r   postr9   __name__uvicornrun r   r   <module>rP      s    < < *   
         	$	$	$	&i 		3 3# 31e 1 1 &"&s) ,<Z ,< ,<\ zGKK)$/ r   