
    	]j?u                        d Z ddlZddl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mZmZmZ ddlmZ dd	lmZ  ed
d      ZddddddgZddddgddddgddddddgddddgddgZd Zej0                  j3                   e        d      ej0                  j5                  dee eddddgddddgd !       eddddgddddgd !      gedd"d#fee eddddgddddgd !       eddddgddddgd !      gedd"d fg      ej0                  j6                  d$                      Zej0                  j3                   e        d      ej0                  j5                  d%d& ed
d'd()      d"ddiddigddgd*d+d,d,dgidgdd'd-d*d+d,d,dgidgdd'd-gd.dddf
d/ ed
d'd0)      d"ddiddigddgd*d+d,d,dgidgdd"d-d*d+d,d,dgidgdd"d-gddddf
d1 ed
d"d0)      d"ddiddigddgd*d+d,d,dgidgdd"d-d*d+d,d,dgidgdd'd-gddddf
d2 ed
d'd3)      d"ddiddigddgd*d+d,d,dgidgdd"d-d*d+d,d,dgidgdd'd-gddddf
d4 ed
d"d3)      d"ddiddigddgddgd.dddf
d5 ed
d"d6)      d"ddiddigddgd*d+d,d,dgidgdd"d-dgddddf
d7 ed
d"d8)      d"ddiddigdd*d+d,d,dgidgd d!gd*d+d,d,dgidgdd"d-dgddddf
d9 ed
d"d8)      d"ddiddigddgd*d+d,d,dgidgdd'd-dgd.dddf
d: ed
d'd;)      d"ddiddigddgd*d+d,d,dgidgdd'd-dgd.dddf
d< ed
d'd=)      d"ddiddiddigg d>d*d+d,d,dgidgdd'd-d*d+d,d,dgidgdd"d-dgd.dddf
g
      ej0                  j6                  d?                      Zej0                  j6                  d@        Zej0                  j6                  dA        Zej0                  j6                  dB        Z ej0                  j6                  dC        Z!y)DzThis 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)redis_healthcheck)	MLBackend)Project)
AnnotationAnnotationDraft
PredictionTask)User   )make_projectTestaB  
        <View>
          <Text name="meta_info" value="$meta_info"></Text>
          <Text name="text" value="$text"></Text>
          <Choices name="text_class" toName="text" choice="single">
            <Choice value="class_A"></Choice>
            <Choice value="class_B"></Choice>
          </Choices>
        </View>)titlelabel_configzmeta info Aztext A)	meta_infotextzmeta info Bztext B
text_classr   labelsclass_A)r   startend	from_nameto_nametypevalueffffff?resultscoreclass_BgzG?c                      G d d      }t         j                  j                  |j                        j                  d      j	                         }| j                  d|j                   d      S )Nc                       e Zd Zd Zy)%run_task_predictions.<locals>.TestJobc                     || _         y )Nid)selfjob_ids     M/root/env/lib/python3.12/site-packages/label_studio/tests/test_predictions.py__init__z.run_task_predictions.<locals>.TestJob.__init__=   s	    DG    N)__name__
__module____qualname__r*    r+   r)   TestJobr#   <   s    	r+   r0   )projecthttp://localhost:8999url/api/ml//predict)r   objectsfilterr&   firstpost)clientr1   mockerr0   ms        r)   run_task_predictionsr>   ;   s_      	   4;;@W;X^^`A;;!$$x011r+   z2Starting predictions requires Redis server enabled)reasonzpproject_config, tasks, annotations, prediction_results, log_messages, model_version_in_request, use_ground_truthT)r   ground_truth12345Fc	           	         t        || j                        }	t        ||      D ]J  \  }
}t        j                  j                  |
|	      }|s*t        j                  j
                  dd|i| L t        j                         5 }|j                  dt        j                  d|i             |j                  dt        j                  |d d |d             t        | |	|      }|j                  d	k(  sJ |j                  sJ 	 d d d        t        j                  j!                         }t"        j                  j%                  |	j&                  
      }	t(        j                  j%                  d      }|j+                         t-        |      k(  sJ t        ||      D ]N  \  }}|j.                  |d   d   k(  sJ |j0                  |d   d   k(  sJ |j2                  |j2                  k(  rNJ  y # 1 sw Y   xY w)N)datar1   taskhttp://localhost:8999/setupmodel_versionr   http://localhost:8999/predictr   resultsrF      r%   r2   r3   r   r   r   r/   )r   userzipr	   r7   creater   requests_mockMockerr:   jsondumpsr>   status_codecalledr   allr   getr&   r   countlenr   r   rF   )business_clientproject_configtasksannotationsprediction_resultslog_messagesmodel_version_in_requestuse_ground_truthr<   r1   rD   
annotationtr=   rpredictions
ml_backendactual_predictionexpected_prediction_results                      r)   test_predictionsrh   D   s   @ >?+?+?@G{3 <jLLT7;%%;1;
;< 
			 1	,4::Ph>i3jk	+(:2A(>Qijk 	 	
 !'6B}}###xxx $$((*Koo!!WZZ!0G""&&+B&CJ#e*,,,9<[J\9] K55 ''+=a+@+JJJJ &&*<Q*?*HHHH''+<+J+JJJJK# s   BG22G;ztest_name, project_config, setup_returns_model_version, tasks, annotations, input_predictions, prediction_call_count, num_project_stats, num_ground_truth_in_stats, num_ground_truth_fit_predictionszCAll predictions are outdated, project.model_version is outdated too	12345_olda5  
                <View>
                  <Text name="txt" value="$text"></Text>
                  <Choices name="cls" toName="txt" choice="single">
                    <Choice value="class_A"></Choice>
                    <Choice value="class_B"></Choice>
                  </Choices>
                </View>)r   rF   r   clstxtchoices)r   r   rF      zAll predictions are up-to-datez
        <View>
          <Text name="txt" value="$text"></Text>
          <Choices name="cls" toName="txt" choice="single">
            <Choice value="class_A"></Choice>
            <Choice value="class_B"></Choice>
          </Choices>
        </View>zXSome predictions are outdated, other are up-to-date. project.model_version is up-to-datezVSome predictions are outdated, other are up-to-date. project.model_version is outdatedz
<View>
  <Text name="txt" value="$text"></Text>
  <Choices name="cls" toName="txt" choice="single">
    <Choice value="class_A"></Choice>
    <Choice value="class_B"></Choice>
  </Choices>
</View>zAll tasks has no predictionsz4Some tasks has no predictions, others are up-to-datea'  
                <View>
                <Text name="txt" value="$text"></Text>
                <Choices name="cls" toName="txt" choice="single">
                <Choice value="class_A"></Choice>
                <Choice value="class_B"></Choice>
                </Choices>
                </View>zXSome tasks has no predictions, others are up-to-date, labeled task contains ground_truthz
        <View>
        <Text name="txt" value="$text"></Text>
        <Choices name="cls" toName="txt" choice="single">
        <Choice value="class_A"></Choice>
        <Choice value="class_B"></Choice>
        </Choices>
        </View>z2Some tasks has no predictions, others are outdatedzUSome tasks has no predictions, others are outdated, project.model_version is outdatedz
    <View>
    <Text name="txt" value="$text"></Text>
    <Choices name="cls" toName="txt" choice="single">
    <Choice value="class_A"></Choice>
    <Choice value="class_B"></Choice>
    </Choices>
    </View>zISome tasks has no predictions, others are outdated, others are up-to-datez
<View>
<Text name="txt" value="$text"></Text>
<Choices name="cls" toName="txt" choice="single">
<Choice value="class_A"></Choice>
<Choice value="class_B"></Choice>
</Choices>
</View>)NNNc                 N   t        || j                        }t        j                  j	                  d      }|d   |_        |j                          t        |||      D ]y  \  }}}t        j                  j                  ||      }|!t        j                  j                  dd|i| |Nt        j                  j                  d||j                  d| { t        j                         5 }|j                  ddt!        j"                  d|i      	       |j                  dd
t!        j"                  dddddgidgddg|d      	       t%        | ||      }|j&                  dk(  sJ t)        t+        t-        d |j.                                    |k(  sJ t        j                  j-                  |j0                  |      j3                         t)        |      k(  sJ t        j                  j	                  d      j
                  |k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nr2   r3   rF   )r1   rC   rD   )rD   r1   POSTrE   rG   rH   rj   rk   rl   r   r   r   r   rI   rK   c                 8    | j                   j                  d      S )Npredict)r4   endswith)hs    r)   <lambda>zAtest_predictions_with_partially_predicted_tasks.<locals>.<lambda>  s    	)B r+   )r1   rF   r/   )r   rL   r   r7   rV   rF   saverM   r	   rN   r   r   r1   rO   rP   register_urirQ   rR   r>   rS   rX   listr8   request_historyr&   rW   )rY   	test_namesetup_returns_model_versionrZ   r[   r\   input_predictionsprediction_call_countnum_project_statsnum_ground_truth_in_stats num_ground_truth_fit_predictionsr<   r1   re   rD   ra   
predictiontask_objr=   rc   s                       r)   /test_predictions_with_partially_predicted_tasksr      s7   J >?+?+?@G""&&+B&CJ-o>JOO(+E;@Q(R ]$j*<<&&wT&B!%%B8BzB!%%\8X=M=M\Q[\] 
			 "o1	1

OUpCq8r 	 	
 	
+ 27/4,5.7)-E	!"' &'
  &A 	 	
. !'6B}}###4BADUDUVWX\qqqq!!((Kf(gmmosvt
 
 	
 
   $$)@$AOOSnnnnE"o "o "os   .D#HH$c           	      .   |j                   j                         }d|_        |j                          |j                  j                         }t        j                  d      5 }|j                  d|j                   ddddigid       | j                  d	|j                   d
t        j                  |j                  ddid      d      }d|_        |j                         }d|v sJ d|d   v sJ |d   d   dk(  sJ 	 d d d        y # 1 sw Y   y xY w)NT	real_httpro   r6   rJ   xrK   rQ   rS   r5   /interactive-annotatingyrD   contextapplication/jsonrC   content_typerC   )ml_backendsr9   is_interactiveru   r[   rO   rP   rv   r4   r:   pkrQ   rR   r&   rS   )rY   configured_projectre   rD   r=   rc   r   s          r)   test_interactive_annotatingr     s%    $//557J $JOO##))+D				- *	v*..!1:)sTWj\AZhkl  z}}o%<= GG #Sz , ! 	
 fVn$$$f~c"c)))'* * *s   "BDDc           	         |j                   j                         }d|_        |j                          |j                  j                         }| j                  d|j                   dt        j                  |j                  ddid      d      }d|_
        |j                         }d	|v sJ t        j                  d
      5 }|j                  d|j                   dddgggid       | j                  d|j                   dt        j                  |j                  ddid      d      }d|_
        |j                         }d d d        d	|v sJ y # 1 sw Y   xY w)NTr5   r   r   r   r   r   rK   errorsr   ro   r6   kebabeatr   )r   r9   r   ru   r[   r:   r   rQ   rR   r&   rS   rO   rP   rv   r4   )rY   r   re   rD   rc   r   r=   s          r)   #test_interactive_annotating_failingr     sm    $//557J $JOO##))+D 	
:==/!89ZZ:
 ( 	 		A AMVVXFv 
			- 	v*..!1:'eWI;AWehi  z}}o%<= GG #Sz , ! 	
 " v# s   BEEc           	         |j                   j                         }d|_        |j                          t	        t
        j                  j                               }|j                  j                         }t        j                  j                  ||d   i d       t        j                  j                  ||d   i d       t        j                  d      5 }|j                  d|j                   dd	d
d
igid       | j                  d|j                    dt#        j$                  |j&                  ddid      d      }d|_        |j#                         }d|v sJ d
|d   v sJ |d   d
   d
k(  sJ |j*                  D cg c]  }d|j,                  v s| c}d   }	|	j.                  sJ t#        j0                  |	j.                        }
t3        |
d   d   d         dk(  sJ 	 ddd       yc c}w # 1 sw Y   yxY w)zy
    Test interactive annotating with drafts
    :param business_client:
    :param configured_project:
    :return:
    Tr   r   )rD   rL   r   	lead_timerm   r   ro   r6   rJ   r   rK   r   r5   r   r   r   r   r   rC   rq   r[   draftsN)r   r9   r   ru   rw   r
   r7   rU   r[   r   rN   rO   rP   rv   r4   r:   r   rQ   rR   r&   rS   rx   pathr   loadsrX   )rY   r   re   usersrD   r=   rc   r   reqhistoryjss              r)   'test_interactive_annotating_with_draftsr     s    $//557J $JOO!!#$E##))+D""58BRS"T""58BRS"T				- 2	v*..!1:)sTWj\AZhkl  z}}o%<= GG #Sz , ! 	
 fVn$$$f~c"c)))"#"3"3M3yCHH7L3MaP|||ZZ%2g;q>(+,11152 2* N+2 2s&   B-G5G0G0AG50G55G>c           
         ddl m}m}m} |j                  j                         }|j                  j                  ||j                  ddddddgidgid	d
      }|j                  j                  ||j                  dd
      }|j                  j                  |      }|j                  j                  |j                        }	|	j                  J |	j                  J |j                  j                  |      }|j                  j                  |j                        }	|	j                  J |	j                  J t        j                   t"              5  |j                  j                  ||       d d d        t        j                   t"              5  |j                  j                          d d d        y # 1 sw Y   FxY w# 1 sw Y   y xY w)Nr   )FailedPredictionr   PredictionMetar   r   r   rl   r   r   r   rA   )rD   r1   r   r   rF   error)rD   r1   messagerF   )r   r%   )failed_prediction)r   r   )tasks.modelsr   r   r   r[   r9   r7   rN   r1   rV   r&   inference_timer   
total_costr   pytestraises	Exception)
rY   r   r   r   r   rD   r   r   pmetas
             r)   test_predictions_metar   7  s   II##))+D ##***vy\ehqgr[st

  + 
J )0077	 8  	%%%<A!!%%%.D&&&!!))) 	%%8I%JA!!%%%.D??"""??""" 
y	! b%%O`%ab 
y	! (%%'( (	b b( (s   F;G;GG)"__doc__rQ   r   rO   
core.redisr   	ml.modelsr   projects.modelsr   r   r   r   r   r	   users.modelsr
   utilsr   dict*_project_for_text_choices_onto_A_B_classes_2_tasks_with_textA_and_textB%_2_prediction_results_for_textA_textBr>   markskipifparametrize	django_dbrh   r   r   r   r   r   r/   r+   r)   <module>r      s      (  # F F  -1
. *  22!  *! %.K!AF	
 
 *! %.K!AF	
 
) %42 )++4hiv 7)  *6'-$,1:aPQ%R	 "&
  *6'-$,1:aPQ%R	 "&
2 2I%	
P 7)  *6'-$,1:aPQ%R	 "&
  *6'-$,1:aPQ%R	 "&
2 2I%	
OMPb )K cP jf)KX )++4hi' R) h&(!344L
 ',y\ehqgr[st "%0 ',y\ehqgr[st "%0" Y-	
` -) h&(!344L
 ',y\ehqgr[st "%, ',y\ehqgr[st "%," Y-	
` g% h&(!344L
 ',y\ehqgr[st "%, ',y\ehqgr[st "%0" Y-	
` e) h&(!344L
 ',y\ehqgr[st "%, ',y\ehqgr[st "%0" Y-	
` +% h&(!344L4L;	
B C% h&(!344L
 ',y\ehqgr[st "%, 	 M'	
T g% h&(!34  ',y\ehqgr[st %)	 ',y\ehqgr[st "%, 	 ]/	
d A% h&(!344L
 ',y\ehqgr[st "%0 	 M'	
T d) h&(!344L
 ',y\ehqgr[st "%0 	 M'	
T X) h&(!3fh5GH
 ',y\ehqgr[st "%0 ',y\ehqgr[st "%, $ [.	
Ao	tj <o kt jn<o~ * *< + +\ ,2 ,2^ -( -(r+   