|
|
"""
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
|
|
|
|
|
|
|
|
|
|
|
HF_MODEL = "linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading Hugging Face model...")
|
|
|
try:
|
|
|
|
|
|
disease_classifier = pipeline(
|
|
|
"image-classification",
|
|
|
model=HF_MODEL,
|
|
|
top_k=3
|
|
|
)
|
|
|
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:
|
|
|
|
|
|
llm = ChatGroq(
|
|
|
model="llama-3.3-70b-versatile",
|
|
|
groq_api_key=GROQ_API_KEY,
|
|
|
temperature=0.7,
|
|
|
max_tokens=1024
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
)
|
|
|
|
|
|
|
|
|
remedy_chain = remedy_prompt | llm
|
|
|
print("β LangChain initialized successfully")
|
|
|
except Exception as e:
|
|
|
print(f"β Error initializing LangChain: {e}")
|
|
|
llm = None
|
|
|
remedy_chain = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
predictions = disease_classifier(image)
|
|
|
|
|
|
|
|
|
top_prediction = predictions[0]
|
|
|
disease_name = top_prediction['label']
|
|
|
confidence = top_prediction['score'] * 100
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
response = remedy_chain.invoke({
|
|
|
"disease_name": disease_name,
|
|
|
"confidence": f"{confidence:.2f}"
|
|
|
})
|
|
|
|
|
|
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.", ""
|
|
|
|
|
|
|
|
|
predictions_text, disease_name, confidence = predict_disease(image)
|
|
|
|
|
|
if predictions_text is None:
|
|
|
return disease_name, ""
|
|
|
|
|
|
|
|
|
prediction_output = f"""
|
|
|
# π Disease Detection Results
|
|
|
|
|
|
---
|
|
|
|
|
|
### π Top Predictions:
|
|
|
|
|
|
{predictions_text}
|
|
|
|
|
|
---
|
|
|
|
|
|
### π― Primary Diagnosis
|
|
|
|
|
|
**Disease:** {disease_name}
|
|
|
**Confidence Level:** {confidence:.2f}%
|
|
|
|
|
|
---
|
|
|
"""
|
|
|
|
|
|
|
|
|
remedies_output = f"""
|
|
|
# πΏ Treatment & Remedies
|
|
|
|
|
|
## π Detected Disease: **{disease_name}**
|
|
|
|
|
|
---
|
|
|
|
|
|
"""
|
|
|
remedies_output += get_remedies(disease_name, confidence)
|
|
|
|
|
|
return prediction_output, remedies_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_interface():
|
|
|
"""Create and configure Gradio interface"""
|
|
|
|
|
|
with gr.Blocks(title="Plant Disease Detection System") as demo:
|
|
|
|
|
|
gr.HTML("""
|
|
|
<style>
|
|
|
.markdown-text h1 {
|
|
|
color: #2c3e50;
|
|
|
border-bottom: 3px solid #667eea;
|
|
|
padding-bottom: 10px;
|
|
|
margin-top: 20px;
|
|
|
}
|
|
|
.markdown-text h2 {
|
|
|
color: #34495e;
|
|
|
border-bottom: 2px solid #95a5a6;
|
|
|
padding-bottom: 8px;
|
|
|
margin-top: 18px;
|
|
|
}
|
|
|
.markdown-text h3 {
|
|
|
color: #7f8c8d;
|
|
|
margin-top: 15px;
|
|
|
}
|
|
|
.markdown-text ul {
|
|
|
line-height: 1.8;
|
|
|
}
|
|
|
.markdown-text li {
|
|
|
margin-bottom: 8px;
|
|
|
}
|
|
|
.markdown-text strong {
|
|
|
color: #2c3e50;
|
|
|
}
|
|
|
.markdown-text hr {
|
|
|
border: none;
|
|
|
border-top: 2px solid #ecf0f1;
|
|
|
margin: 20px 0;
|
|
|
}
|
|
|
</style>
|
|
|
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
|
|
<h1>π± Plant Disease Detection & Treatment System</h1>
|
|
|
<p>Upload or capture a plant image to detect diseases and get AI-powered treatment recommendations</p>
|
|
|
</div>
|
|
|
""")
|
|
|
|
|
|
with gr.Row():
|
|
|
with gr.Column(scale=1):
|
|
|
gr.Markdown("### πΈ Upload or Capture Image")
|
|
|
|
|
|
|
|
|
image_input = gr.Image(
|
|
|
label="Plant Image",
|
|
|
type="pil",
|
|
|
sources=["upload", "webcam"],
|
|
|
height=400
|
|
|
)
|
|
|
|
|
|
|
|
|
process_btn = gr.Button(
|
|
|
"π¬ Analyze Disease",
|
|
|
variant="primary",
|
|
|
size="lg"
|
|
|
)
|
|
|
|
|
|
|
|
|
gr.Markdown("""
|
|
|
**Instructions:**
|
|
|
1. Upload an image or use your camera
|
|
|
2. Click 'Analyze Disease' button
|
|
|
3. View predictions and AI-generated remedies
|
|
|
|
|
|
**Tips for best results:**
|
|
|
- Ensure good lighting
|
|
|
- Focus on affected areas
|
|
|
- Use clear, high-quality images
|
|
|
""")
|
|
|
|
|
|
with gr.Column(scale=1):
|
|
|
gr.Markdown("### π Results")
|
|
|
|
|
|
|
|
|
predictions_output = gr.Markdown(
|
|
|
label="Disease Predictions",
|
|
|
value="*Upload an image to see predictions...*"
|
|
|
)
|
|
|
|
|
|
|
|
|
remedies_output = gr.Markdown(
|
|
|
label="Treatment & Remedies (AI-Generated)",
|
|
|
value=""
|
|
|
)
|
|
|
|
|
|
|
|
|
gr.Markdown("""
|
|
|
---
|
|
|
**Powered by:**
|
|
|
- π€ Hugging Face (Disease Detection)
|
|
|
- π Groq + Llama 3.3 70B (Treatment Recommendations via LangChain)
|
|
|
|
|
|
*Note: This system provides suggestions only. Always consult with agricultural experts for serious plant health issues.*
|
|
|
""")
|
|
|
|
|
|
|
|
|
process_btn.click(
|
|
|
fn=process_image,
|
|
|
inputs=[image_input],
|
|
|
outputs=[predictions_output, remedies_output]
|
|
|
)
|
|
|
|
|
|
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
if not GROQ_API_KEY:
|
|
|
print("\n" + "="*70)
|
|
|
print("β οΈ WARNING: GROQ_API_KEY not found!")
|
|
|
print("="*70)
|
|
|
print("\nTo use the LLM features, please set your Groq API key:")
|
|
|
print(" - Windows: set GROQ_API_KEY=your_key_here")
|
|
|
print(" - Linux/Mac: export GROQ_API_KEY=your_key_here")
|
|
|
print("\nOr add it to a .env file in the project directory.")
|
|
|
print("\nThe app will still run, but remedy generation will not work.")
|
|
|
print("="*70 + "\n")
|
|
|
|
|
|
|
|
|
print("\nπ Launching Plant Disease Detection System...")
|
|
|
demo = create_interface()
|
|
|
demo.launch(
|
|
|
server_name="0.0.0.0",
|
|
|
server_port=7860,
|
|
|
share=False,
|
|
|
show_error=True
|
|
|
)
|
|
|
|