
    LCj                     ~    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        |              nd	|vr|j                  d
| d       n|dk(  rt        |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29+>??#1#7#7#9 /Y-8#{{}MMIhZ01	++d#C
 H$c4(G3,>&xj 19+'  s*	(3VWXf$c4( 0
:_`deh`i_jklSQ$(3F'GG??$MM#H:-W#==/ *PQ MM!( ,R $* %UV "x'7'7'C&DN$SS##%MM:8*Dijkmg6 $!yy|$t+	-*<*<)==VWX_/Yb MS  	MMIhZ/CC5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d4>>*+F?Aq||I'>a?F?!=aQ\\)%<=H=k 	AD*	 KL 	AD*		$T^^$44G
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   