
    RIQh                        d dl mZ d dlZd dlmZ d dlZd dlZd dlm	Z	 d dl
Z
d dlmZ d dlmZ d dlZd dlZd dlZ G d de      Zy)	    )annotationsN)Any)Image)ClipEmbedding)ObjectsDetectedc                  :     e Zd Zd fdZd Zd ZddZd Z xZS )ONNXClipEmbeddingc                (    t         |   ||       y )N)pluginnativeId)super__init__)selfr   r   	__class__s      H/server/volume/plugins/@scrypted/onnx/zip/unzipped/ort/clip_embedding.pyr   zONNXClipEmbedding.__init__   s    :    c                
    ddgS )N	text.onnxvision.onnx )r   s    r   getFileszONNXClipEmbedding.getFiles   s    
 	
r   c                x   |D cg c]$  }|j                         j                  d      s#|& }}|st        d      |d   }|D cg c]$  }|j                         j                  d      s#|& }}|st        d      |d   }g i | j                  j                  }|D ]  }t        j                         }g }t        j                  dk(  r|j                  d       dt        j                  v r7t        j                         dk(  r t        |      }|j                  d	d
|if       |j                  d       t        j                  |||      }	t        j                  |||      }
j                  |	|
f        fd}t        j                  j                  |t!              d      }|fS c c}w c c}w )Nr   z3No onnx model file found in the provided files listr   r   darwinCoreMLExecutionProviderlinuxx86_64CUDAExecutionProvider	device_idCPUExecutionProvider)sess_options	providersc                     t        j                         j                  } j                         }|| <   t	        dj                  |              y )Nz Runtime initialized on thread {})	threadingcurrent_threadnamepopprintformat)thread_nameinterpretercompiled_modelscompiled_models_arrays     r   executor_initializerz9ONNXClipEmbedding.loadModel.<locals>.executor_initializerB   sC    #22499K/335K+6OK(4;;KHIr   custom)initializermax_workersthread_name_prefix)lowerendswith
ValueErrorr   	deviceIdsonnxruntimeSessionOptionssysplatformappendmachineintInferenceSession
concurrentfuturesThreadPoolExecutorlen)r   filesf	text_onnxvision_onnxr5   deviceIdr    r!   
text_modelvision_modelr-   executorr+   r,   s                @@r   	loadModelzONNXClipEmbedding.loadModel   s    %I1););K)HQI	IRSSaL	"'MQ1779+=+=m+LqMMRSS!!n !#KK))	! 	EH&557L#%I||x'  !:;#,,&8+;+;+=+Ix=  "9K;R!ST34$55	J '77,)L "((*l)CD'	E*	J %%88,12' 9 
 ((a J
 Ns   $F2F2$F7-F7c                    K    j                   \  } fd}t        j                         j                  ||       d {   }|S 7 w)Nc                 |   
j                  	ddd      } t        j                         j                     }|\  }}|j	                  d |j                         d   j                  | j                  d   i      }|d   }t        |j                  t        j                        j                               }d|igd}|S )	Nnp
max_lengthT)imagesreturn_tensorspadding
truncationr   pixel_values	embedding)
detectionsinputDimensions)	processorr#   r$   r%   run
get_inputsdata	bytearrayastyperM   float32tobytes)inputscompiled_model_vision_sessionvision_predictionsimage_embedsrT   retr+   inputr   src_sizes           r   predictz.ONNXClipEmbedding.detect_once.<locals>.predictR   s    ^^5|hl^mF,Y-E-E-G-L-LMN .A~!/!3!3D>;T;T;VWX;Y;^;^`f`k`klz`{:|!}-a0L!,"5"5bjj"A"I"I"KLI $Y
 $,$C Jr   modelasyncioget_event_looprun_in_executor)	r   rf   settingsrg   cvssrI   rh   objsr+   s	   `` `    @r   detect_oncezONNXClipEmbedding.detect_onceP   sK     $(JJ!	$ ++-==g
 
 
s   ?AAAc                    K    j                   \  } fd}t        j                         j                  ||       d {   }|S 7 w)Nc                    	j                  ddd      } t        j                         j                     }|\  }}|j	                         d   j                  | d   |j	                         d   j                  | d   i}|j                  d |      }|d   }t        |j                  t        j                        j                               S )	NrM   rN   T)textrP   rQ   rR   r   	input_ids   attention_mask)rW   r#   r$   r%   rY   rX   r[   r\   rM   r]   r^   )
r_   r`   text_sessionra   text_inputstext_predictionstext_embedsr+   rf   r   s
          r   rh   z3ONNXClipEmbedding.getTextEmbedding.<locals>.predictk   s    ^^t\fj^kF,Y-E-E-G-L-LMN,OL!'')!,116+3F'')!,116:J3KK  ,//kB*1-K[//

;CCEFFr   ri   )r   rf   rI   rh   rp   r+   s   ``   @r   getTextEmbeddingz"ONNXClipEmbedding.getTextEmbeddingi   sL     $(JJ!
	G ++-==g
 
 
s   >AA
A)r   str)rf   zImage.Imagern   r   )	__name__
__module____qualname__r   r   rJ   rq   r|   __classcell__)r   s   @r   r	   r	      s    ;
2)h2r   r	   )
__future__r   rk   typingr   numpyrM   r6   PILr   r#   predict.clipr   scrypted_sdkr   concurrent.futuresr>   r8   r9   r	   r   r   r   <module>r      s7    "       & (  
 h hr   