ó
     :hŠ  ã            	       óN  • S SK r S SKJrJrJrJrJr  S SKJr  S SK	J
r
  S SKJrJr  S SKJr  \ R                   " \ R"                  SS9  \ R$                  " \5      r\" 5       r\" \5      4S	\S
\4S jjr\R/                  S\SSSS/S9\" \5      \" SSS9S.S	\S\S
\4S jj5       rg)é    N)Ú	APIRouterÚDependsÚ
UploadFileÚFileÚHTTPException)ÚUnidentifiedImageError)Úget_disease_prediction)Úget_settingsÚSettings)ÚDiseaseResponsez4%(asctime)s - %(name)s - %(levelname)s - %(message)s)ÚlevelÚformatÚsettingsÚreturnc              ƒ   óŽ   #   • U R                   (       a  U R                   S:X  a  [        R                  S5        [        SSS9eU $ 7f)z;Dependency to get settings and validate necessary API keys.ÚYOUR_GEMINI_API_KEY_NOT_SETz-Gemini API Key is not configured in settings.é÷  z0AI Service (Gemini) is not configured correctly.©Ústatus_codeÚdetail)ÚGEMINI_API_KEYÚloggerÚerrorr   )r   s    ÚG/home/sneha/Desktop/shweta/404/404/backend/app/api/endpoints/disease.pyÚget_validated_settingsr      s>   é € ð ×"×" h×&=Ñ&=ÐA^Ó&^Ü�‰ÐDÔEä¨Ð4fÑgÐgØ€Oùs   ‚AAz/detectéÈ   zDetect Crop Disease from ImagezUpload a plant image to get an AI-based analysis of potential diseases or pests. Supported formats include JPEG, PNG, WEBP etc.zDisease Detection)Úresponse_modelr   ÚsummaryÚdescriptionÚtags.zImage file of the plant.)r   )r   Úfiler!   c           	   ƒ   óN  #   • [         R                  SUR                   35        UR                  (       a   UR                  R	                  S5      (       dH  [         R                  SUR                   SUR                   S35        [        SSUR                   S3S	9eU R                  S
-  S
-  nSn UR                  5       I Sh  v•N nU(       d,  [         R                  SUR                   35        [        SSS	9e[        U5      nXB:”  aQ  [         R                  SU SUR                   SU S35        [        SS[        US-  S5       SU R                   S3S	9e[         R                  SUR                   S[        US-  S5       S35         UR                  5       I Sh  v•N   [         R                  SUR                   S 35         [         R                  S!UR                   S"35        [!        U5      I Sh  v•N n[         R                  S#UR                   S35        UR	                  S$5      (       a/  [         R                  S%UR                   SU 35        [        S&US	9e[#        UUR                  =(       d    S'S(9$  GNÄ! [         a?  n[         R                  SUR                   SU 3SS9  [        SS[        U5       3S	9eSnAff = f GN3! UR                  5       I Sh  v•N    [         R                  SUR                   S 35        f = f GN! [$         a.    [         R                  S)UR                   S*35        [        SS+S	9e[         a  nUeSnAf[         a3  n[         R                  S,UR                   SU 3SS9  [        S-S.S	9eSnAff = f7f)/a£  
Handles plant image uploads, validates them, sends for AI analysis,
and returns the disease prediction results.

Args:
    settings: Injected application settings.
    file: The uploaded image file.

Returns:
    A DiseaseResponse object containing the analysis and filename.

Raises:
    HTTPException: For various errors like invalid file type, size,
                   AI service errors, or internal server errors.
z-Received request to detect disease for file: zimage/zInvalid file type 'z' for file 'z'.i�  zInvalid file type: 'zA'. Please upload a standard image format (JPEG, PNG, WEBP, etc.).r   i   NzEmpty file uploaded: z:Received an empty image file. Please upload a valid image.zFile too large: z bytes for file 'z'. Limit is z bytes.i�  zFile is too large (i   é   z MB). Maximum size allowed is zMB.zFile 'z' size validated (z MB).zError reading uploaded file 'z': T)Úexc_infozCould not read uploaded file: z	' closed.zSending image 'z' to AI service...z'Received AI analysis successfully for 'zError:z*AI Service returned an error message for 'r   Úunknown_filename)ÚanalysisÚfilenamez'Cannot identify image format for file 'z&'. It might be corrupt or unsupported.zTCould not identify image format. Please ensure it's a standard, non-corrupted image.z)Unexpected error during AI analysis for 'iô  zTAn internal server error occurred while analyzing the image. Please try again later.)r   Úinfor'   Úcontent_typeÚ
startswithÚwarningr   ÚMAX_FILE_SIZE_MBÚreadÚlenÚroundÚ	Exceptionr   ÚstrÚcloseÚdebugr	   r   r   )r   r!   Úmax_size_bytesÚimage_bytesÚ	file_sizeÚeÚprediction_resultÚhttp_excs           r   Údetect_crop_diseaser:   !   s’  é € ô: ‡K�KÐ?ÀÇÁ¸ÐOÔPð ×× D×$5Ñ$5×$@Ñ$@À×$JÑ$JÜ�‰Ð,¨T×->Ñ->Ð,?¸|ÈDÏMÉMÈ?ÐZ\Ð]Ô^ÜØØ)¨$×*;Ñ*;Ð)<Ð<}Ð~ñ
ð 	
ð ×.Ñ.°Ñ5¸Ñ<€NØ€Kð8ð !ŸI™I›K×'ˆæÜ�N‰NÐ2°4·=±=°/ÐBÔCÜ¨CÐ8tÑuÐuä˜Ó$ˆ	ØÓ%Ü�N‰NÐ-¨i¨[Ð8IÈ$Ï-É-ÈÐXdÐesÐdtÐt{Ð|Ô}ÜØØ,¬U°9¸iÑ3HÈ!Ó-LÐ,MÐMkÐlt÷  mFñ  mFð  lGð  GJð  Kñð ô 	�‰�f˜TŸ]™]˜OÐ+=¼eÀIÈyÑDYÐ[\Ó>]Ð=^Ð^cÐdÕeð �j‰j‹l×ÐÜ�‰�v˜dŸm™m˜_¨IÐ6Ô7ð!
Ü�‰�o d§m¡m _Ð4FÐGÔHä"8¸Ó"E×EÐÜ�‰Ð=¸d¿m¹m¸_ÈBÐOÔPð ×'Ñ'¨×1Ñ1Ü�L‰LÐEÀdÇmÁmÀ_ÐTWÐXiÐWjÐkÔlä¨CÐ8IÑJÐJô Ø&Ø—]‘]×8Ð&8ñ
ð 	
òM (øô ó _ä�‰Ð4°T·]±]°OÀ3ÀqÀcÐJÐUYˆÑZÜ¨Ð6TÔUXÐYZÓU[ÐT\Ð4]Ñ^Ð^ûð_úò 	øˆd�j‰j‹l×ÑÜ�‰�v˜dŸm™m˜_¨IÐ6Õ7úò Føô "ó Lä�‰Ð@ÀÇÁÀÐOuÐvÔwÜ¨ð  5Kñ  Lð  	LÜó àˆûÜó 
ô 	�‰Ð@ÀÇÁÀÈsÐSTÐRUÐVÐaeˆÑfäØØiñ
ð 	
ûð
üs«   ‚B0N%Â3J ÃJ
ÃCJ ÆN%Æ&KÆ''N%Ç1L  È LÈBL  Ê	N%Ê
J Ê
KÊ:KËKËK ËN%ËLË0K3Ë1)LÌN%ÌL  Ì A N"Í M"Í"N"Í/.NÎN"Î"N%)ÚloggingÚfastapir   r   r   r   r   ÚPILr   Úapp.core.ai_servicesr	   Úapp.core.configr
   r   Úapp.models.disease_modelr   ÚbasicConfigÚINFOÚ	getLoggerÚ__name__r   Úrouterr   Úpostr:   © ó    r   Ú<module>rI      sÝ   ðó ß GÕ GÝ &õ 8ß 2å 4ð × Ò ˜'Ÿ,™,Ð/eÒ fØ	×	Ò	˜8Ó	$€ñ 
‹€ñ 7>¸lÓ6Kñ ¨8ð ÐPXõ ð ‡�ØØ"ØØ,ðAà
Ð	ð ð ñ !Ð!7Ó8Ù˜CÐ-GÑHò_
àð_
ð ð_
ð ô	_
óñ_
rH   