
    	]j#                     h   d Z ddlZddl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 dd
lmZmZmZmZ  ej.                  e      Z G d dej4                        Zd Z G d dej:                        Z G d dej:                        Z G d de      Z  G d dej:                        Z!y)zThis file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.    N)settings)models)Q)gettext_lazy)ModelProviderConnectionModelProviders)Project)ValidationError)
AnnotationFailedPrediction
PredictionPredictionMetac                   4    e Zd Zd ed      fZd ed      fZy)
SkillNamesTextClassificationNamedEntityRecognitionN)__name__
__module____qualname___TEXT_CLASSIFICATIONNAMED_ENTITY_RECOGNITION     G/root/env/lib/python3.12/site-packages/label_studio/ml_models/models.pyr   r      s$    .2F0GG7;S9TTr   r   c                     | st        d      t        | t              st        d      t        d | D              st        d      y )Nzlist should not be emptyzValue must be a listc              3   <   K   | ]  }t        |t                y wN)
isinstancestr).0items     r   	<genexpr>z'validate_string_list.<locals>.<genexpr>   s     7z$$7s   z%All items in the list must be strings)r
   r   listall)values    r   validate_string_listr'      sF    899eT"455777EFF 8r   c                   :   e Zd Z ej                   ed      dddd      Z ej                   ed      ddd	      Z ej                  e
j                  d
ej                  d      Z ej                   ed      d      Z ej                   ed      d      Z ej                  dej"                  dd      Z ej                  dej(                  d      Z ej,                  eeg      Z ej,                  eeg      Z ej6                  dd      Zd Zy)ModelInterfacetitle  F
Model name
max_lengthnullblank	help_textdescriptionTzModel descriptionr/   r0   r1   created_modelsrelated_name	on_deleter/   
created atauto_now_add
updated atauto_noworganizations.Organizationmodel_interfacesr7   r6   r/      )r.   choicesr/   )default
validatorszprojects.Project)r0   c                 4    |j                   | j                  k(  S r   active_organizationorganizationselfusers     r   has_permissionzModelInterface.has_permission9       ''4+<+<<<r   N)r   r   r   r   	CharFieldr   r*   	TextFieldr2   
ForeignKeyr   AUTH_USER_MODELSET_NULL
created_byDateTimeField
created_at
updated_atCASCADErH   r   rB   
skill_name	JSONFieldr$   r'   input_fieldsoutput_classesManyToManyFieldassociated_projectsrL   r   r   r   r)   r)       s#   FQwZCe5\hiE"&""1]#3$dVijK"""  /?6??aeJ &%%aoDIJ%%%aoEJ$6$$$M_fjL "!!S*:L:LSWXJ#6##D>R=STL%V%%d@T?UVN0&001C4P=r   r)   c                   2    e Zd Z G d d      Z ej
                   ed      dddd      Z ej                  e	dej                  	      Z ej                   ed
      ddd      Z ej                  edej                  d      Zed        Z fdZ xZS )ModelVersionc                       e Zd ZdZy)ModelVersion.MetaTN)r   r   r   abstractr   r   r   Metara   >   s    r   rc   r*   r+   Fr,   r-   model_versions)r6   r7   promptzPrompt to executer3   Tr5   c                 L    | j                   j                   d| j                   S )N__)parent_modelr*   rJ   s    r   
full_titlezModelVersion.full_titleK   s$    ##))*"TZZL99r   c                     t         j                  j                  | j                        }|D ]  }|j	                           t        |   |i | y)zB
        Deletes Predictions associated with ModelVersion
        )model_versionN)ModelRunobjectsfilteriddelete_predictionssuperdelete)rJ   argskwargs
model_runs	model_run	__class__s        r   rs   zModelVersion.deleteO   sN     %%,,477,C
# 	+I((*	+''r   )r   r   r   rc   r   rN   r   r*   rP   r)   rW   rh   rO   re   r   rR   model_provider_connectionpropertyrj   rs   __classcell__rx   s   @r   r_   r_   =   s      FQwZCe5\hiE$6$$^BR^d^l^lmLVakUNabF 1 1 1.>&//`d! : :( (r   r_   c                      e Zd Z ej                  dej                  ej                  d      Z ej                  dddd      Z	 ej                  ej                  dej                  d	      Z ej                   ed
      d      Z ej                   ed      d      Z ej                  dej&                  dd      Zed        Zd Zy)ThirdPartyModelVersionrA   z%The model provider to use e.g. OpenAI)r.   rB   rC   r1   Fz;The model ID to use within the given provider, e.g. gpt-3.5)r.   r0   r/   r1   "created_third_party_model_versionsTr5   r8   r9   r;   r<   r>   third_party_model_versionsr@   c                     | j                   rH| j                   j                  j                         r$| j                   j                  j                         S y r   )rh   r]   existsfirstri   s    r   projectzThirdPartyModelVersion.projectw   sB     !2!2!F!F!M!M!O$$88>>@@r   c                 4    |j                   | j                  k(  S r   rF   rI   s     r   rL   z%ThirdPartyModelVersion.has_permission~   rM   r   N)r   r   r   r   rN   r   rB   OPENAIproviderprovider_model_idrP   r   rQ   rR   rS   rT   r   rU   rV   rW   rH   rz   r   rL   r   r   r   r~   r~   Y   s    v&&%%9	H )((O	 #""  9//	J &%%aoDIJ%%%aoEJ$6$$$MiptL  =r   r~   c                       e Zd Z G d dej                        Z G d dej                        Z G d dej                        Z ej                  dej                  dd	
      Z
 ej                  eej                  d      Z ej                  eej                  d      Z ej                  ej                   dej"                  d	      Z ej&                  dej(                  ej*                        Z ej&                  dej(                  ej.                        Z ej&                  dd	d	dd      Z ej4                   ed      d      Z ej4                   ed      d      Z ej4                   ed      d      Z ej>                   ed      d	      Z  ej>                   ed      d	d      Z! ej>                   ed      d	d      Z" ej>                   ed      d	d      Z#d Z$d Z% fdZ& xZ'S ) rm   c                   H    e Zd Zd ed      fZd ed      fZd ed      fZy)ModelRun.ProjectSubsetAllHasGTSampleN)r   r   r   r   ALLHASGTSAMPLEr   r   r   ProjectSubsetr      s+    QuXo7#1X;&r   r   c                   4    e Zd Zd ed      fZd ed      fZy)ModelRun.FileTypeInputOutputN)r   r   r   r   INPUTOUTPUTr   r   r   FileTyper      s    7#1X;&r   r   c                   @    e Zd Zd ed      fZd ed      fZdZdZdZy)ModelRun.ModelRunStatusPending
InProgress)	Completedr   )Failedr   )Canceledr   N)	r   r   r   r   PENDING
INPROGRESS	COMPLETEDFAILEDCANCELEDr   r   r   ModelRunStatusr      s-    Qy\)!1\?2
.	%+r   r   r>   rv   Tr@   )r7   r6   r5   rA   )r.   rB   rC   Nz9Job ID for inference job for a ModelRun e.g. Adala job ID)r.   r/   r0   rC   r1   ztotal predictionsr   )rC   ztotal correct predictionsztotal tasksr8   r9   ztriggered at)r/   rC   zpredictions updated atzcompleted atc                 4    |j                   | j                  k(  S r   rF   rI   s     r   rL   zModelRun.has_permission   rM   r   c                    t         j                  j                  | j                        }|D cg c]  }|j                   }}t        j                  j                  |      j                  d       	 ddlm}m} |j                  j                  |      }|j                          |j                  j                  t        |      t        |	      z        }|j                          t        j                  j                  | j                        }	|	D cg c]  }|j                   }
}t        j                  j                  |      j                          t        j                  j                  |
      j                          |j!                  |j"                         |	j!                  |	j"                         yc c}w # t        $ r#}t        j                  d
|        Y d}~d}~ww xY wc c}w )a  
        Deletes any predictions that have originated from a ModelRun

        Executing a raw SQL query here for speed. This ignores any foreign key relationships
        so if another model has a Prediction fk and set to on_delete=CASCADE for example,
        it will not take affect. The only relationship like this that currently exists
        is in Annotation.parent_prediction, which we are handling here.
        )rw   )parent_prediction__inN)parent_predictionr   )PredictionPairStatsPredictionStats)prediction_to__in)prediction_to_id__in)prediction_from_id__inzHPredictionStats or PredictionPairStats model does not exist , exception:)prediction__in)failed_prediction__in)r   rn   ro   rp   r   updatestats.modelsr   r   rs   r   	Exceptionloggerinfor   r   _raw_deletedb)rJ   predictionspprediction_idsr   r   prediction_stats_to_be_deleted#prediction_pair_stats_to_be_deletedefailed_predictionsfailed_predictions_idss              r   rq   zModelRun.delete_predictions   s    !((//$''/B(341!$$44!!!GNNaeNf
	hI-<-D-D-K-K^l-K-m**1132E2M2M2T2T~6R`9aa3/ 0668
 .55<<tww<O0B!C1!$$!C!C 	%%^%DKKM%%<R%SZZ\ 	/&&'9'<'<=3 5  	hKKbcdbefgg	h
 "Ds$   G5A6G G5	G2G--G2c                 D    | j                          t        |   |i | y)z>
        Deletes Predictions associated with ModelRun
        N)rq   rr   rs   )rJ   rt   ru   rx   s      r   rs   zModelRun.delete   s"     	!''r   )(r   r   r   r   TextChoicesr   r   r   rP   rW   rH   r	   r   r~   rl   r   rQ   rR   rS   rN   rB   r   project_subsetr   statusjob_idIntegerFieldr   total_predictionstotal_correct_predictionstotal_tasksrT   rU   triggered_atpredictions_updated_atcompleted_atrL   rq   rs   r{   r|   s   @r   rm   rm      s   '** '
'6%% ',++ , %6$$$\`dL  f6>>P\]G%F%%&<eqrM"""  !//	J &V%%m>S>S]j]p]pqNVn6L6LVdVlVlmFVMF ,++A.A,BAN 3 3 3A6Q4R\] ^%&%%a&6BK%%%aoDIJ'6''.(9dSL1V11!4L2MTXbfg'6''.(9dSL=#>J( (r   rm   )"__doc__loggingdjango.confr   	django.dbr   django.db.modelsr   django.utils.translationr   r   ml_model_providers.modelsr   r   projects.modelsr	   rest_framework.exceptionsr
   tasks.modelsr   r   r   r   	getLoggerr   r   r   r   r'   Modelr)   r_   r~   rm   r   r   r   <module>r      s    l      6 M # 5 Q Q			8	$U## U
G=V\\ =:(6<< (8&=\ &=Rg(v|| g(r   