Ë
    KµCj=  ã                   óT  — d 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
 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mZm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# ddl$m%Z% ddl&m'Z'  e«       Z( e«         ed«      Z)edefd„«       Z* ee(jV                  e(jX                  de*e(jZ                  sdnde(jZ                  sdnd¬«      Z.e.j_                  ee(j`                  dddgdg¬ «       e.j_                  e«       e(jb                  r(e.j_                  e«       e.je                  d! e
«       «       e.jg                  ee«       e.jg                  ee«       e.jg                  e4e«       e.jk                  ee(jl                  ¬"«       e.jo                  d#«      d$e8fd%„«       Z9y)&uó  
Application entry point.

`uvicorn app.main:app` (dev) or `gunicorn -k uvicorn.workers.UvicornWorker
app.main:app` (production, see deployment/systemd/) both load this file.

The lifespan context manager is where the production-fail-fast principle
is enforced: if reference data, the feature builder, or any model fails
to load, the application refuses to start at all rather than starting
in a broken state. This is intentional and correct â€” see
app/models_loader/registry.py for the reasoning.
é    )Úasynccontextmanager)ÚFastAPI)ÚRequestValidationError)ÚCORSMiddleware)Úmake_asgi_app)Ú
api_router)Úget_settings)ÚAppException)Ú
get_loggerÚsetup_logging)Údispose_engine)Úapp_exception_handlerÚunhandled_exception_handlerÚvalidation_exception_handler)ÚPrometheusMiddleware)ÚRequestContextMiddleware)Úmodel_registry)Úcache_service)Úfeature_builder)Úllm_service)Úreference_datazapp.mainÚappc           	     óV  K  — t         j                  dt        j                  › dt        j                  › dt        j
                  › d�«       t        j                  rt        j                  st        d«      ‚t        j                  «        t        j                  «        t        j                  «        t        j                  «        t!        j                  «       ƒ d {  –—†  t         j                  d«       d ­–— t         j                  d«       t#        «       ƒ d {  –—†  y 7 ŒF7 Œ­w)Nz	Starting z vz (ú)z¯ANTHROPIC_API_KEY is not set. Refusing to start in production without it. Set the key or explicitly accept fallback-only mode by changing ENVIRONMENT for a controlled rollout.u7   Startup complete â€” service is ready to accept trafficu.   Shutting down â€” closing database connections)ÚloggerÚinfoÚsettingsÚAPP_NAMEÚAPP_VERSIONÚENVIRONMENTÚis_productionÚANTHROPIC_API_KEYÚRuntimeErrorr   Úloadr   r   Úload_allr   Úinitr   r   )r   s    ú5C:\Crop_Prediction\Backend\crop-ai-system\app\main.pyÚlifespanr(   -   sÛ   è ø€ ä
‡K�K�)œH×-Ñ-Ð.¨b´×1EÑ1EÐ0FÀbÌ×I]ÑI]ÐH^Ð^_Ð`Ôaä×Ò¤h×&@Ò&@ô ð@ó
ð 	
ô ×ÑÔÜ×ÑÔÜ×ÑÔÜ×ÑÔÜ
×
Ñ
Ó
×Ðä
‡K�KÐIÔJä	ä
‡K�KÐ@ÔAÜ
Ó
×Ñð øð ús%   ‚CD)ÃD%ÃA D)ÄD'Ä D)Ä'D)z¥Production API for the AI Crop Decision Support System. Evaluates crop suitability for a given location and month using 6 ML models, with LLM-generated explanations.ú/docsNz/redoc)ÚtitleÚversionÚdescriptionr(   Údocs_urlÚ	redoc_urlTÚGETÚPOSTÚ*)Úallow_originsÚallow_credentialsÚallow_methodsÚallow_headersz/metrics)Úprefixú/Úreturnc               ƒ   óx   K  — t         j                  t         j                  dt         j                  sddœS ddœS ­w)NÚrunningr)   zdisabled in production)Úservicer+   ÚstatusÚdocs)r   r   r   r!   © ó    r'   Úrootr@   p   sB   è ø€ ô ×$Ñ$Ü×'Ñ'ØÜ'×5Ò5�ñ	ð ð <Tñ	ð ùs   ‚8:):Ú__doc__Ú
contextlibr   Úfastapir   Úfastapi.exceptionsr   Úfastapi.middleware.corsr   Úprometheus_clientr   Úapp.api.v1.routerr   Úapp.core.configr	   Úapp.core.exceptionsr
   Úapp.core.logging_configr   r   Úapp.db.sessionr   Ú!app.middleware.exception_handlersr   r   r   Úapp.middleware.metricsr   Úapp.middleware.request_contextr   Úapp.models_loader.registryr   Úapp.services.cache_servicer   Úapp.services.feature_builderr   Úapp.services.llm_servicer   Úapp.services.reference_datar   r   r   r(   r   r   r!   r   Úadd_middlewareÚcors_origins_listÚENABLE_METRICSÚmountÚadd_exception_handlerÚ	ExceptionÚinclude_routerÚAPI_V1_PREFIXÚgetÚdictr@   r>   r?   r'   ú<module>r^      s—  ðñõ +å Ý 5Ý 2Ý +å (Ý (Ý ,ß =Ý )÷ñ õ
 8Ý CÝ 5Ý 4Ý 8Ý 0Ý 6á‹>€á „Ù	�JÓ	€ð ð˜ò ó ðñ< Ø
×
Ñ
Ø× Ñ ð	8ð Ø$×2Ò2‰W¸Ø&×4Ò4‰h¸$ô€ð" × Ñ ØØ×,Ñ,ØØ˜&�/Ø�%ð ô ð × Ñ Ð+Ô ,Ø×ÒØ×ÑÐ+Ô,Ø‡I�Iˆj™-›/Ô*à × Ñ ˜,Ð(=Ô >Ø × Ñ Ð0Ð2NÔ OØ × Ñ ˜)Ð%@Ô Aà × Ñ �: h×&<Ñ&<Ð Ô =ð ‡�ˆƒð�Dò ó ñr?   