
    	]j                         d 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  G d dej                        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.
    )validate_upload_url)settings)extend_schema_field)	MLBackendMLBackendAuth)serializersc                        e Zd ZdZ ej
                         Z ej                  dddd      Z ej
                         Z	 e
e      d        Zd Zd Zd Zd	 Zd
 Zd Z fdZ G d d      Z xZS )MLBackendSerializerz)
    Serializer for MLBackend model.
    TF)
write_onlyrequired
allow_nullallow_blankc                 ,    t        |j                        S N)boolbasic_auth_passselfobjs     E/root/env/lib/python3.12/site-packages/label_studio/ml/serializers.pyget_basic_auth_pass_is_setz.MLBackendSerializer.get_basic_auth_pass_is_set   s    C''((    c                 "    |j                         S r   )get_state_displayr   s     r   get_readable_statez&MLBackendSerializer.get_readable_state   s    $$&&r   c                     |sA| j                   j                  st        j                  d      | j                   j                  S |S )Nz=Authentication password is required for Basic Authentication.)instancer   r   ValidationErrorr   values     r   validate_basic_auth_passz,MLBackendSerializer.validate_basic_auth_pass   s;    ==00!112qrr }}444r   c                 <    t        |t        j                         |S )N)block_local_urls)r   r   ML_BLOCK_LOCAL_IPr   s     r   validate_urlz MLBackendSerializer.validate_url$   s    EH4N4NOr   c                     j                  d      t        j                  k(  r.ddg}t        fd|D              rt	        j
                  d      y y )Nauth_methodbasic_auth_userr   c              3   &   K   | ]  }|v 
 y wr    ).0fieldattrss     r   	<genexpr>z?MLBackendSerializer._validate_authentication.<locals>.<genexpr>-   s     C%5%Cs   zJAuthentication username and password is required for Basic Authentication.)getr   
BASIC_AUTHanyr   r   )r   r-   required_fieldss    ` r   _validate_authenticationz,MLBackendSerializer._validate_authentication)   sU    99]#}'?'??02CDOC?CC!11`  D @r   c                     t        j                  di |}|j                  r<|j                  dk(  rd}nd|d    d|j                   }t        j                  |      y )Ni  zfAble to connect to ML Server, but authentication parameters were either not provided or are incorrect.zCan't connect to ML backend urlz, health check failed. Make sure it is up and your firewall is properly configured. <a href="https://labelstud.io/guide/ml.html">Learn more</a> about how to set up an ML backend. Additional info: r*   )r   healthcheck_is_errorstatus_codeerror_messager   r   )r   r-   healthcheck_responsemessages       r   _validate_healthcheckz)MLBackendSerializer._validate_healthcheck2   s~    (55>>((#//36<  35<. AK L`KmKmJnp  --g66 )r   c                     t        j                  di |}|j                  r+d|d    d|j                   d}t	        j
                  |      y )NzSuccessfully connected to r5   z6 but it doesn't look like a valid ML backend. Reason: z.
Check the ML backend server console logs to check the status.There might be something wrong with your model or it might be incompatible with the current labeling configuration.r*   )r   setup_r7   r9   r   r   )r   r-   setup_responser;   s       r   _validate_setupz#MLBackendSerializer._validate_setupE   sc    "))2E2"",U5\N ;)778 9FF  --g66 #r   c                     t         |   |      }| j                  |       | j                  |       | j	                  |       |S r   )supervalidater3   r<   r@   )r   r-   	__class__s     r   rC   zMLBackendSerializer.validateR   sA     '%%e,""5)U#r   c                       e Zd ZeZg dZy)MLBackendSerializer.Meta)idstatereadable_stateis_interactiver5   r9   titler'   r(   r   basic_auth_pass_is_setdescriptionextra_paramsmodel_versiontimeout
created_at
updated_atauto_updateprojectN)__name__
__module____qualname__r   modelfieldsr*   r   r   MetarF   [   s    
r   rZ   )rU   rV   rW   __doc__r   SerializerMethodFieldrI   	CharFieldr   rL   r   r   r   r   r!   r%   r3   r<   r@   rC   rZ   __classcell__)rD   s   @r   r
   r
   
   s     7[668N+k++teX\jnoO>[>>@) )'
7&7
 
r   r
   c                   b    e Zd ZdZ ej
                  dd      Z ej                  ddd      Zy)MLInteractiveAnnotatingRequestz;
    Serializer for ML interactive annotating request.
    zID of task to annotateT)	help_textr   zContext for ML modelN)ra   r   default)	rU   rV   rW   r[   r   IntegerFieldtask	JSONFieldcontextr*   r   r   r`   r`   t   s8     $;##.FQUVD#k##.DQU_cdGr   r`   N)r[   core.utils.ior   django.confr   drf_spectacular.utilsr   	ml.modelsr   r   rest_frameworkr   ModelSerializerr
   
Serializerr`   r*   r   r   <module>rn      sB    -   5 . &g
+55 g
Te[%;%; er   