Ë
    `Uhž$  ã                   ó¶  — d dl mZ d dlmZ d dlZd dlmZ ddlm	Z	 erd dl
mZ  e	«       Z	 ej                  rQej                  dk7  rB ej                  ej                  ¬«        ej                  d	«      Z ej                  d	«      Zn ed
«       dZdZdedefd„Zdddefd„Zddededefd„Zdedefd„Zy# e$ rZ ede› �«       dZdZY dZ[Œ=dZ[ww xY w)é    N)ÚImage)ÚTYPE_CHECKINGé   )Úget_settings)Ú
YieldInputÚYOUR_GEMINI_API_KEY_NOT_SET)Úapi_keyzgemini-1.5-flashzJAI_SERVICES: Gemini API key not configured. Gemini features will not work.z'AI_SERVICES: Error configuring Gemini: Úimage_bytesÚreturnc              ƒ   ó  K  — t         sy	 t        j                  t        j                  | «      «      }d}t         j                  ||g«      }|j                  S # t        $ r&}t        d|› �«       dt        |«      › �cY d}~S d}~ww xY w­w)zN
    Analyzes an image using Gemini Vision model to detect crop diseases.
    z-Error: Gemini Vision model is not configured.aÞ  Analyze the attached image of a plant leaf from the Baramati, Maharashtra region.
        Identify potential diseases or pests. Describe the visible symptoms clearly.
        Suggest 1-2 brief, practical, and sustainable management/treatment options suitable for a local farmer.
        If the image is unclear or not a plant, state that clearly.
        Format the response clearly, perhaps using bullet points for symptoms and suggestions.
        Respond in English.
        z$Error in Gemini disease prediction: z#Error analyzing image with Gemini: N)
Úgemini_vision_modelr   ÚopenÚioÚBytesIOÚgenerate_contentÚtextÚ	ExceptionÚprintÚstr)r
   ÚimgÚpromptÚresponseÚes        úCC:\Users\aadil\OneDrive\Desktop\404\backend\app\core\ai_services.pyÚget_disease_predictionr   #   sƒ   è ø€ õ Ø>ð>Ü�j‰jœŸ™ KÓ0Ó1ˆð	ˆô '×7Ñ7¸À¸ÓFˆð �}‰}Ðøäò >ÜÐ4°Q°CÐ8Ô9à4´S¸³V°HÐ=Õ=ûð>üs5   ‚B
‹AA ÁB
Á	BÁ!BÁ<BÁ=B
ÂBÂB
Úyield_inputr   c              ƒ   óh  K  — t         sy	 d}d|› d| j                  › d| j                  › d| j                  › d|› d| j                  xs d	› d
| j
                  xs d	› d�}t         j                  |«      }|j                  S # t        $ r&}t        d|› �«       dt        |«      › �cY d}~S d}~ww xY w­w)zJ
    Generates a yield estimate using Gemini based on farmer's input.
    ú+Error: Gemini text model is not configured.zBaramati, Maharashtra, Indiaz1Act as an agricultural assistant for a farmer in z9.
        Based on the following inputs:
        - Crop: z
        - Area: z
        - Region Details: z	 (within z)
        - Soil Type: zNot specifiedz$
        - Recent/Expected Weather: as  

        Provide a realistic estimated yield range (e.g., in quintals per acre or tonnes per hectare, specify the unit clearly).
        Briefly explain the key factors (like weather, soil, crop type in this region) influencing this estimate in 2-3 short bullet points.
        Keep the explanation simple and practical for a farmer.
        Respond in English.
        z"Error in Gemini yield prediction: z-Error generating yield estimate with Gemini: N)Úgemini_text_modelÚ	crop_typeÚareaÚregionÚsoilÚweatherÚgenerate_textr   r   r   r   )r   Úlocation_contextr   r   r   s        r   Úget_yield_estimater'   H   sé   è ø€ õ Ø<ðHà9ÐØFÐGWÐFXð Yà×&Ñ&Ð'ð (Ø×!Ñ!Ð"ð #Ø&×-Ñ-Ð.¨iÐ8HÐ7Ið JØ!×&Ñ&Ò9¨/Ð:ð ;$Ø$/×$7Ñ$7Ò$J¸?Ð#Kð L	ðˆô  %×2Ñ2°6Ó:ˆð �}‰}Ðøäò HÜÐ2°1°#Ð6Ô7à>¼sÀ1»v¸hÐGÕGûðHüs5   ‚B2‹A4B  Á?B2Â 	B/Â	B*Â$B/Â%B2Â*B/Â/B2Ú
transcriptÚlanguagec           	   ƒ   ó  K  — t         syddddœ}|j                  |d«      }d}	 d|› d| › d	|› d
|› d�	}t         j                  |«      }|j                  S # t        $ r&}t        d|› �«       dt        |«      › �cY d}~S d}~ww xY w­w)za
    Processes a voice transcript using Gemini to understand intent and generate a response.
    r   ÚEnglishÚHindiÚMarathi)ÚenÚhiÚmra]  
    The FarmGenius app can:
    1. Analyze an uploaded image of a plant to detect diseases (triggered by asking about the 'last image' or 'this picture').
    2. Predict crop yield based on inputs like crop type, area, region, soil, weather.
    3. Provide mock information about local market prices for crops like Wheat and Onion in Baramati.
    z‰You are the voice interface for the FarmGenius agricultural app, assisting a farmer in Baramati, Maharashtra.
        The user, speaking z	, said: "z%"

        App Capabilities:
        a¨  

        Your tasks:
        1. Understand the user's intent based on their statement. Does the user want to:
            - Get analysis of the last uploaded image?
            - Ask for a yield prediction (they might mention crop, area etc.)?
            - Ask about market prices (they might mention crop names)?
            - Something else (greet, ask for help)?
        2. Generate a concise and helpful response **in a÷  **.
        3. If the intent is clear and relates to an app capability:
            - For image analysis: Respond like "Okay, analyzing the last uploaded image..." or ask for the image if needed. (The actual analysis happens separately).
            - For yield prediction: If they provided details, acknowledge them. If not, ask for necessary details like crop type, area, etc.
            - For market prices: Provide the mock data if they ask for Wheat/Onion prices in Baramati, otherwise state which prices are available.
        4. If the intent is unclear or unrelated to app capabilities, politely state what the app can do or ask for clarification.
        5. Keep the response relatively short and easy to understand for a voice interaction.
        z"Error in Gemini voice processing: z,Error processing voice command with Gemini: N)r   Úgetr%   r   r   r   r   )r(   r)   Úlang_mapÚlanguage_nameÚapp_capabilitiesr   r   r   s           r   Úprocess_voice_command_air5   q   sÂ   è ø€ õ Ø<ð   w°iÑ@€HØ—L‘L ¨9Ó5€MðÐð#GðØ)˜?¨)°J°<ð @	ð 
Ðð 9ð :G¸ð H	ðˆô. %×2Ñ2°6Ó:ˆð �}‰}Ðøäò GÜÐ2°1°#Ð6Ô7à=¼cÀ!»f¸XÐFÕFûðGüs4   ‚"B¥/A ÁBÁ	BÁA?Á9BÁ:BÁ?BÂBÚmarket_datac              ƒ   ó>  K  — t         sy| sy	 dj                  | D �cg c]  }d|d   › d|d   › d|d	   › �‘Œ c}«      }d
|› d�}t         j                  |«      }|j                  S c c}w # t        $ r&}t        d|› �«       dt        |«      › �cY d}~S d}~ww xY w­w)z@
    Generates a brief summary of market data using Gemini.
    r   z&No market data available to summarize.Ú
z- Úcropz: Úprice_per_quintalz INR/quintal at Úlocationz=Here is some recent market data from Baramati Mandi:
        z£

        Provide a very brief (1-2 sentence) summary highlighting any notable price points or trends based ONLY on this data.
        Respond in English.
        z!Error generating market summary: z-Error generating market summary with Gemini: N)r   Újoinr%   r   r   r   r   )r6   ÚitemÚdata_stringr   r   r   s         r   Úget_market_summary_air?   «   så   è ø€ õ Ø=ÙØ7ðHØ—i‘iñ  ALó  !Mñ  ALÐx| 2 d¨6¡l ^°2°dÐ;NÑ6OÐ5PÐP`ÐaeÐfpÑaqÐ`rÒ!sð  ALñ  !Mó  Nˆð	Ø	ˆð 	ðˆô %×2Ñ2°6Ó:ˆð �}‰}Ðùò# !Møô& ò HÜÐ1°!°Ð5Ô6à>¼sÀ1»v¸hÐGÕGûðHüsD   ‚BŽA+ �A&¸-A+ Á%BÁ&A+ Á+	BÁ4BÂBÂBÂBÂB)r.   )Úgoogle.generativeaiÚgenerativeaiÚgenaiÚPILr   r   Útypingr   Úconfigr   Úapp.models.yield_modelr   ÚsettingsÚGEMINI_API_KEYÚ	configureÚGenerativeModelr   r   r   r   r   Úbytesr   r   r'   r5   Úlistr?   © ó    r   Ú<module>rO      s#  ðÝ #Ý Û 	Ý  õ !ñ Ý1ñ ‹>€ðØ×Ò 8×#:Ñ#:Ð>[Ò#[Øˆ�‰ × 7Ñ 7Õ8à3˜e×3Ñ3Ð4FÓGÐà1˜E×1Ñ1Ð2DÓEÑáÐZÔ[Ø"ÐØ Ðð!>¨eð !>¸ó !>ðJ%H¨,ð %H¸3ó %HñR6G¨sð 6G¸cð 6GÈSó 6Gðt H¨Tð  H°cô  Høð_ ò Ù	Ð3°A°3Ð
7Ô8ØÐØÕûðús   ­A)B: Â:CÂ?CÃC