Ë
    LµCj  ã                   ó~   — d Z ddlZddlZddlZddlmZ ddlZdddddddœZdedee	   fd	„Z
dd
„Zedk(  r e«        yy)a±  
Validates a trained model bundle directory before it's deployed to the API.

This deliberately mirrors the validation logic in
app/models_loader/registry.py so you catch a broken model bundle here,
during training/CI, rather than discovering it when the production API
refuses to start (or worse, if that fail-fast check ever has a gap).

Usage:
    python -m ml_pipeline.evaluation.validate_model_bundle --model-dir data/models/v1
é    N)ÚPathÚdictÚbundle)zsarimax_climate.pklzxgb_feasibility.pklzxgb_yield.pklzdt_trend.pklzrf_recommendation.pklzsarimax_market.pklÚ	model_dirÚreturnc           	      óZ  — g }| j                  «       sd| › �gS t        j                  «       D �]À  \  }}| |z  }|j                  «       s|j                  d|› �«       Œ1	 t	        j
                  |«      }|dk(  rOt        |t        «      rd|vr!|j                  d|› dt        |«      › �«       n±d	|vr­|j                  d
|› d�«       n—|dk(  r’t        |t        «      s!|j                  d|› dt        |«      › �«       nat        |«      dk(  rS| |› d�z  }|j                  «       r#|j                  d
|› d|j                  › d�«       n|j                  d|› d|› d�«       | |j                  dd«      › d�z  }|j                  «       s|j                  d|› d�«       �Œqt        |d¬«      5 }	t        j
                  |	«      }
d d d «       d
vs�Œ¢|j                  d
|j                  › d�«       �ŒÃ |S # t        $ r"}|j                  d|› d|› �«       Y d }~�Œìd }~ww xY w# 1 sw Y   ŒaxY w)Nz Model directory does not exist: z	MISSING: z	CORRUPT: u    failed to load â€” r   ÚmodelzINVALID FORMAT: z% expected dict with 'model' key, got Úfeature_colsz	WARNING: z# has no 'feature_cols' key recordedr   z% expected dict of fitted models, got r   z.empty_acknowledgedz* is intentionally empty (acknowledged via ui   ) â€” predictions using this model will be degraded for every query until it is retrained with real data.zEMPTY: z˜ contains zero trained sub-models. If this is a deliberate placeholder for a known data gap (see docs/data_sourcing.md), create an empty marker file at zm to acknowledge this explicitly and downgrade this to a warning. Do not deploy an unacknowledged empty model.z.pklÚ z_metadata.jsonzWARNING: no metadata file for u%    â€” cannot audit training provenancezutf-8)ÚencodingÚtrained_at_utcz missing 'trained_at_utc')ÚexistsÚREQUIRED_FILESÚitemsÚappendÚjoblibÚloadÚ	ExceptionÚ
isinstancer   ÚtypeÚlenÚnameÚreplaceÚopenÚjson)r   ÚissuesÚfilenameÚexpected_kindÚpathÚobjÚexcÚack_pathÚmetadata_pathÚfÚmetas              úYC:\Crop_Prediction\Backend\crop-ai-system\ml_pipeline\evaluation\validate_model_bundle.pyÚvalidater'      se  € Ø€Fà×ÑÔØ2°9°+Ð>Ð?Ð?ä#1×#7Ñ#7Ó#9ó /YÑˆ�-Ø˜8Ñ#ˆØ�{‰{Œ}Ø�M‰M˜I h ZÐ0Ô1Øð	Ü—+‘+˜dÓ#ˆCð
 ˜HÒ$Ü˜c¤4Ô(¨G¸3Ñ,>Ø—‘Ø& x jð 1Ü ›9˜+ð'õð   sÑ*Ø—‘ 	¨(¨Ð3VÐWÕXØ˜fÒ$Ü˜c¤4Ô(Ø—‘Ð 0°°
Ð:_Ô`dÐehÓ`iÐ_jÐkÕlÜ�S“˜Q’Ø$¨(¨Ð3FÐ'GÑG�Ø—?‘?Ô$Ø—M‘MØ# H :Ð-WØ#Ÿ=™=˜/ð *PðQõð —M‘MØ! ( ð ,Rð $˜*ð %UðVôð " x×'7Ñ'7¸ÀÓ'CÐ&DÀNÐ$SÑSˆØ×#Ñ#Ô%Ø�M‰MÐ:¸8¸*ÐDiÐjÖkä�m¨gÔ6ð $¸!Ü—y‘y “|�÷$à tÓ+Ø—‘ 	¨-×*<Ñ*<Ð)=Ð=VÐWÖXð_/Yðb €MøôS ò 	Ø�M‰M˜I h ZÐ/CÀCÀ5ÐIÔJÝûð	ú÷H$ð $ús$   ÁG3Æ+H!Ç3	HÇ<HÈHÈ!H*	c                  ó.  — t        j                  d¬«      } | j                  dd¬«       | j                  «       }t	        t        |j                  «      «      }|D �cg c]  }|j                  d«      rŒ|‘Œ }}|D �cg c]  }|j                  d«      sŒ|‘Œ }}|r t        d«       |D ]  }t        d|› �«       Œ |r5t        d	«       |D ]  }t        d|› �«       Œ t        j                  d
«       t        d|j                  › d�«       y c c}w c c}w )Nz)Validate a trained model bundle directory)Údescriptionz--model-dirT)ÚrequiredÚWARNINGz	WARNINGS:z  - u:   
VALIDATION FAILED â€” model bundle is NOT safe to deploy:é   u   
VALIDATION PASSED â€” z is ready to deploy)ÚargparseÚArgumentParserÚadd_argumentÚ
parse_argsr'   r   r   Ú
startswithÚprintÚsysÚexit)ÚparserÚargsr   ÚiÚerrorsÚwarningsÚwÚes           r&   Úmainr<   X   s  € Ü×$Ñ$Ð1\Ô]€FØ
×Ñ˜°ÐÔ5Ø×ÑÓ€Dä”d˜4Ÿ>™>Ó*Ó+€FàÖ?�A q§|¡|°IÕ'>ŠaÐ?€FÐ?Ø!Ö=�a Q§\¡\°)Õ%<’Ð=€HÐ=áÜˆkÔØò 	ˆAÜ�D˜˜�*Õð	ñ ÜÐKÔLØò 	ˆAÜ�D˜˜�*Õð	ä�‰�Œä	Ð$ T§^¡^Ð$4Ð4GÐ
HÕIùò @ùÚ=s   ÁDÁ3DÁ=DÂDÚ__main__)r   N)Ú__doc__r-   r   r3   Úpathlibr   r   r   ÚlistÚstrr'   r<   Ú__name__© ó    r&   ú<module>rE      sh   ðñ
ó Û Û 
Ý ã ð "Ø#ØØØ%Ø ñ€ð7˜ð 7  c¡ó 7ótJð0 ˆzÒÙ…Fð rD   