""" Plant Disease Detection System - Upload or capture image - Predict disease using Hugging Face model - Get remedies using Groq (Llama 3.3 70B) via LangChain """ import os import io import base64 from PIL import Image import gradio as gr from transformers import pipeline from langchain_groq import ChatGroq from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableSequence from dotenv import load_dotenv # ============================================================================ # CONFIGURATION # ============================================================================ # Load environment variables from .env file load_dotenv() # Set your Groq API Key here or via environment variable GROQ_API_KEY = os.getenv("GROQ_API_KEY", "") # Hugging Face model for plant disease classification # Using a popular plant disease detection model HF_MODEL = "linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification" # ============================================================================ # MODEL INITIALIZATION # ============================================================================ print("Loading Hugging Face model...") try: # Initialize the image classification pipeline disease_classifier = pipeline( "image-classification", model=HF_MODEL, top_k=3 # Get top 3 predictions ) print("โ Hugging Face model loaded successfully") except Exception as e: print(f"โ Error loading Hugging Face model: {e}") disease_classifier = None print("Initializing LangChain with Groq...") try: # Initialize Groq with Llama 3.3 70B (free and powerful) llm = ChatGroq( model="llama-3.3-70b-versatile", # You can also use: "mixtral-8x7b-32768", "llama-3.1-70b-versatile" groq_api_key=GROQ_API_KEY, temperature=0.7, max_tokens=1024 ) # Create prompt template for disease remedies remedy_prompt = PromptTemplate( input_variables=["disease_name", "confidence"], template="""You are an expert agricultural consultant specializing in plant diseases. A plant disease has been detected with the following information: - Disease Name: {disease_name} - Confidence Level: {confidence}% Please provide a comprehensive response in MARKDOWN format with the following sections: ## ๐ Disease Overview Brief description of this plant disease (2-3 sentences) ## ๐ Symptoms Key symptoms to look for (use bullet points with - ) ## ๐ฆ Causes What causes this disease (use bullet points with - ) ## ๐ Treatment Recommendations ### โก Immediate Actions - List immediate steps to take ### ๐ฑ Organic/Natural Remedies - List organic and natural treatment options ### ๐งช Chemical Treatments (if necessary) - List chemical treatment options ### ๐ก๏ธ Preventive Measures - List preventive measures to avoid future infections ## โฑ๏ธ Recovery Timeline Expected recovery timeline and what to expect ## ๐ก Additional Tips Any other helpful advice for managing this disease IMPORTANT: Use proper Markdown formatting: - Use ## for main sections - Use ### for subsections - Use **bold** for emphasis - Use bullet points with - for lists - Keep it well-structured and easy to read """ ) # Create chain using LCEL (LangChain Expression Language) remedy_chain = remedy_prompt | llm print("โ LangChain initialized successfully") except Exception as e: print(f"โ Error initializing LangChain: {e}") llm = None remedy_chain = None # ============================================================================ # CORE FUNCTIONS # ============================================================================ def predict_disease(image): """ Predict plant disease from image using Hugging Face model Args: image: PIL Image or numpy array Returns: tuple: (predictions_list, top_disease, top_confidence) """ if disease_classifier is None: return None, "Model not loaded", 0.0 try: # Run prediction predictions = disease_classifier(image) # Extract top prediction top_prediction = predictions[0] disease_name = top_prediction['label'] confidence = top_prediction['score'] * 100 # Format all predictions for display predictions_text = "\n".join([ f"{i+1}. {pred['label']}: {pred['score']*100:.2f}%" for i, pred in enumerate(predictions) ]) return predictions_text, disease_name, confidence except Exception as e: return None, f"Error during prediction: {str(e)}", 0.0 def get_remedies(disease_name, confidence): """ Get treatment remedies using Groq via LangChain Args: disease_name: Name of the detected disease confidence: Confidence score of the prediction Returns: str: Formatted remedies and treatment information """ if remedy_chain is None: return "โ ๏ธ LangChain not initialized. Please set your GROQ_API_KEY environment variable." try: # Generate remedies using LangChain with LCEL response = remedy_chain.invoke({ "disease_name": disease_name, "confidence": f"{confidence:.2f}" }) # Extract content from AIMessage return response.content except Exception as e: return f"โ ๏ธ Error generating remedies: {str(e)}\n\nPlease check your Groq API key." def process_image(image): """ Main processing function: predict disease and get remedies Args: image: Input image from Gradio Returns: tuple: (predictions_text, remedies_text) """ if image is None: return "โ ๏ธ Please upload or capture an image first.", "" # Step 1: Predict disease predictions_text, disease_name, confidence = predict_disease(image) if predictions_text is None: return disease_name, "" # disease_name contains error message # Format prediction results with enhanced Markdown prediction_output = f""" # ๐ Disease Detection Results --- ### ๐ Top Predictions: {predictions_text} --- ### ๐ฏ Primary Diagnosis **Disease:** {disease_name} **Confidence Level:** {confidence:.2f}% --- """ # Step 2: Get remedies from LLM remedies_output = f""" # ๐ฟ Treatment & Remedies ## ๐ Detected Disease: **{disease_name}** --- """ remedies_output += get_remedies(disease_name, confidence) return prediction_output, remedies_output # ============================================================================ # GRADIO INTERFACE # ============================================================================ def create_interface(): """Create and configure Gradio interface""" with gr.Blocks(title="Plant Disease Detection System") as demo: # Header with embedded CSS gr.HTML("""
Upload or capture a plant image to detect diseases and get AI-powered treatment recommendations