o
    Pi]5                     @   s  d dl 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
mZmZ d dlmZ d dlZd dlZd dlmZ d dlmZ ejejd	 eeZd
ZdZejedd ejer|eedZ e!e Z"W d   n1 svw   Y  ni Z"dd e"# D Z"ej$Z%eddZ&G dd deZ'G dd deZ(e&j)de	e( ddde'fddZe&j)de	e( ddde'fdd Z*dS )!    )FastAPI)	BaseModelN)List)ThreadPoolExecutoras_completed)Image)BytesIO)predict_all)leveltemp_imageszimage_hashes.jsonT)exist_okrc                 C   s   i | ]
\}}t ||qS  )	imagehashhex_to_hash.0kvr   r   '/var/www/html/ai-image-ml/api_filter.py
<dictcomp>"   s    r   zImage Filter API)titlec                   @   s"   e Zd ZU ee ed< eed< dS )ImageBatchRequest
image_urlsactivityN)__name__
__module____qualname__r   str__annotations__r   r   r   r   r   )   s   
 r   c                   @   s*   e Zd ZU eed< eed< ee ed< dS )FilterResponse	image_urlacceptedreasonsN)r   r   r   r   r   boolr   r   r   r   r   r    .   s   
 r    z/filterzFilter multiple images by URL)response_modelsummaryrequestc                    s   g }| j  t  dtdtf fddtdd"fdd| jD }t|D ]	}||  q)W d    |S 1 s>w   Y  |S )	Nr!   returnc              
      s4  zt d|   tj| dd}|jdkrt| ddgdW S | dd	 d
d  }|dvr9t| ddgdW S t	  d| }t
jt|}t|d}||j W d    n1 s_w   Y  zPtt|j}t|}|tv rt| }t
| t| dd| gdW W S | t|< ttd}tjdd t D |dd W d    n1 sw   Y  W n ty }	 zt d|  d|	  W Y d }	~	nd }	~	ww t| }
t
| t| |
dd|
ddgdW S  ty }	 z t d|  dt|	  t| ddt|	 gdW  Y d }	~	S d }	~	ww )NzReceived image URL: 
   )timeout   Fz"Failed to download image from URL.r!   r"   r#   .?r   )jpgjpegpngzUnsupported image format.wbz>Duplicate image detected. Matches previously processed image: wc                 S   s   i | ]	\}}t ||qS r   )r   r   r   r   r   r   c   s    z6filter_images.<locals>.process_url.<locals>.<dictcomp>   )indentzHashing failed for z: r"   r#   zImage rejected.zError processing image zError: )loggerinforequestsgetstatus_coder    splitloweruuiduuid4ospathjoinTEMP_DIRopenwritecontentr   r   	hash_func	hash_dictremove	HASH_FILEjsondumpitems	Exceptionwarningr	   errorr   )r!   responseextfilenamefilepathfimgimg_hashoriginal_urleresult)r   r   r   process_url:   sr   



 "


z"filter_images.<locals>.process_url   )max_workersc                    s   g | ]}  |qS r   )submit)r   url)executorr[   r   r   
<listcomp>|   s    z!filter_images.<locals>.<listcomp>)	r   printr   r    r   r   r   appendrZ   )r'   resultsfuturesfuturer   )r   r`   r[   r   filter_images4   s   A
rg   z/filter_devz,Filter multiple images by URL (bypass logic)c                 C   s8   g }| j D ]}td|  |t|dg d q|S )Nz"Bypass filter received image URL: Tr,   )r   r7   r8   rc   r    )r'   rd   r!   r   r   r   filter_images_bypass   s   

rh   )+fastapir   pydanticr   r9   r>   r@   loggingtypingr   concurrent.futuresr   r   PILr   r   rK   ior   rg   r	   basicConfigINFO	getLoggerr   r7   rC   rJ   makedirsrA   existsrD   rU   loadrH   rM   phashrG   appr   r    postrh   r   r   r   r   <module>   sB    

N