Ë
    @h  ã                   óX  — d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ  e j                   e j"                  d¬«        e j$                  e«      Z e«       Z ee«      fd	ed
efd„Zej/                  deddddg¬«       ee«       edd¬«      dœd	eded
efd„«       Zy)é    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              ƒ   óˆ   K  — | j                   r| j                   dk(  r"t        j                  d«       t        dd¬«      ‚| S ­w)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    úHC:\Users\aadil\OneDrive\Desktop\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           	   ƒ   óP  K  — t         j                  d|j                  › �«       |j                  r|j                  j	                  d«      sKt         j                  d|j                  › d|j                  › d�«       t        dd|j                  › d�¬	«      ‚| j                  d
z  d
z  }d}	 |j                  «       ƒ d{  –—† }|s/t         j                  d|j                  › �«       t        dd¬	«      ‚t        |«      }||kD  rTt         j                  d|› d|j                  › d|› d�«       t        ddt        |dz  d«      › d| j                  › d�¬	«      ‚t         j                  d|j                  › dt        |dz  d«      › d�«       	 |j                  «       ƒ d{  –—†  t         j                  d|j                  › d �«       	 t         j                  d!|j                  › d"�«       t!        |«      ƒ d{  –—† }t         j                  d#|j                  › d�«       |j	                  d$«      r2t         j                  d%|j                  › d|› �«       t        d&|¬	«      ‚t#        ||j                  xs d'¬(«      S 7 �ŒÁ# t        $ rE}t         j                  d|j                  › d|› �d¬«       t        ddt        |«      › �¬	«      ‚d}~ww xY w7 �Œ4# |j                  «       ƒ d{  –—†7   t         j                  d|j                  › d �«       w xY w7 �Œ# t$        $ r1 t         j                  d)|j                  › d*�«       t        dd+¬	«      ‚t        $ r}|‚d}~wt        $ r9}t         j                  d,|j                  › d|› �d¬«       t        d-d.¬	«      ‚d}~ww xY w­w)/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Ü�‰Ð,¨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Ü�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¬   ‚B)N&Â,J Â?J Ã CJ ÆN&Æ KÆ!'N&Ç	1L Ç:LÇ;BL É?N&Ê J Ê	KÊA KËKËK ËN&ËLË+K.Ë,)LÌN&ÌL ÌAN#ÍMÍN#Í*4NÎ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   