Upload 4 files
Browse files- README.md +130 -13
- app.py +253 -0
- models.py +133 -0
- requirements.txt +8 -0
README.md
CHANGED
|
@@ -1,13 +1,130 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Plant Disease Treatment Assistant
|
| 2 |
+
|
| 3 |
+
This application helps farmers and gardeners identify plant diseases and get treatment recommendations. It combines:
|
| 4 |
+
|
| 5 |
+
1. **Image-based disease detection**: Upload an image of your plant to identify potential diseases
|
| 6 |
+
2. **Question answering**: Ask questions about plant diseases and treatments
|
| 7 |
+
|
| 8 |
+
## Features
|
| 9 |
+
|
| 10 |
+
- Image analysis for disease detection
|
| 11 |
+
- Text-based question answering about plant diseases and treatments
|
| 12 |
+
- Comprehensive database of crop diseases and their treatments
|
| 13 |
+
- User-friendly interface with examples
|
| 14 |
+
|
| 15 |
+
## Data Sources
|
| 16 |
+
|
| 17 |
+
- Plant disease image data from [PlantDiseaseDatasetpks](https://huggingface.co/datasets/ipranavks/PlantDiseaseDatasetpks)
|
| 18 |
+
- Treatment information from a curated CSV database of crop diseases and treatments
|
| 19 |
+
|
| 20 |
+
## Usage
|
| 21 |
+
|
| 22 |
+
1. Upload an image of a plant to identify diseases
|
| 23 |
+
2. Or ask a question like "How do I treat early blight in tomatoes?"
|
| 24 |
+
3. Review the diagnosis and treatment recommendations
|
| 25 |
+
|
| 26 |
+
## Deployment
|
| 27 |
+
|
| 28 |
+
This app is deployed on Hugging Face Spaces.
|
| 29 |
+
\`\`\`
|
| 30 |
+
|
| 31 |
+
```python file="process_dataset.py"
|
| 32 |
+
import pandas as pd
|
| 33 |
+
import torch
|
| 34 |
+
from datasets import load_dataset
|
| 35 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification, TrainingArguments, Trainer
|
| 36 |
+
from torchvision.transforms import (
|
| 37 |
+
CenterCrop,
|
| 38 |
+
Compose,
|
| 39 |
+
Normalize,
|
| 40 |
+
RandomHorizontalFlip,
|
| 41 |
+
RandomResizedCrop,
|
| 42 |
+
Resize,
|
| 43 |
+
ToTensor,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def train_image_classifier():
|
| 47 |
+
"""
|
| 48 |
+
Script to train a model on the PlantDiseaseDatasetpks.
|
| 49 |
+
This is not used in the main app, but shows how the model would be trained.
|
| 50 |
+
"""
|
| 51 |
+
# Load the dataset
|
| 52 |
+
dataset = load_dataset("ipranavks/PlantDiseaseDatasetpks")
|
| 53 |
+
|
| 54 |
+
# Get the labels from the dataset
|
| 55 |
+
# This assumes the dataset has image-label pairs with proper metadata
|
| 56 |
+
# Since we don't have the actual structure visible, this is a placeholder
|
| 57 |
+
|
| 58 |
+
# Set up image preprocessing
|
| 59 |
+
image_processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
|
| 60 |
+
|
| 61 |
+
def transforms(examples):
|
| 62 |
+
examples["pixel_values"] = [
|
| 63 |
+
image_processor(image.convert("RGB"), return_tensors="pt")["pixel_values"]
|
| 64 |
+
for image in examples["image"]
|
| 65 |
+
]
|
| 66 |
+
return examples
|
| 67 |
+
|
| 68 |
+
# Apply preprocessing
|
| 69 |
+
processed_dataset = dataset.map(transforms, batched=True)
|
| 70 |
+
|
| 71 |
+
# Set up model
|
| 72 |
+
model = AutoModelForImageClassification.from_pretrained(
|
| 73 |
+
"microsoft/resnet-50",
|
| 74 |
+
num_labels=len(dataset["train"].features["label"].names),
|
| 75 |
+
ignore_mismatched_sizes=True,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Set up training arguments
|
| 79 |
+
training_args = TrainingArguments(
|
| 80 |
+
output_dir="./results",
|
| 81 |
+
num_train_epochs=3,
|
| 82 |
+
per_device_train_batch_size=16,
|
| 83 |
+
per_device_eval_batch_size=64,
|
| 84 |
+
warmup_steps=500,
|
| 85 |
+
weight_decay=0.01,
|
| 86 |
+
logging_dir="./logs",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Set up trainer
|
| 90 |
+
trainer = Trainer(
|
| 91 |
+
model=model,
|
| 92 |
+
args=training_args,
|
| 93 |
+
train_dataset=processed_dataset["train"],
|
| 94 |
+
eval_dataset=processed_dataset["test"],
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Train the model
|
| 98 |
+
trainer.train()
|
| 99 |
+
|
| 100 |
+
# Save the model
|
| 101 |
+
model.save_pretrained("./plant-disease-model")
|
| 102 |
+
image_processor.save_pretrained("./plant-disease-model")
|
| 103 |
+
|
| 104 |
+
# This function is to process and prepare the CSV data
|
| 105 |
+
def prepare_csv_data():
|
| 106 |
+
"""
|
| 107 |
+
Function to process the CSV data for easier lookup.
|
| 108 |
+
This is not used in the main app, but shows how the CSV data would be processed.
|
| 109 |
+
"""
|
| 110 |
+
import requests
|
| 111 |
+
import io
|
| 112 |
+
|
| 113 |
+
# Download the CSV
|
| 114 |
+
response = requests.get("https://hebbkx1anhila5yf.public.blob.vercel-storage.com/crop_diseases_treatments-2uScFyZnlnaYo70rR6hIkBBYdnwcG1.csv")
|
| 115 |
+
df = pd.read_csv(io.StringIO(response.text))
|
| 116 |
+
|
| 117 |
+
# Process the data
|
| 118 |
+
df["Crop_lower"] = df["Crop"].str.lower()
|
| 119 |
+
df["Disease_lower"] = df["Disease"].str.lower()
|
| 120 |
+
|
| 121 |
+
# Save processed data
|
| 122 |
+
df.to_csv("processed_treatments.csv", index=False)
|
| 123 |
+
|
| 124 |
+
return df
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
# Example usage
|
| 128 |
+
# prepare_csv_data()
|
| 129 |
+
# train_image_classifier() # This would take significant time and resources
|
| 130 |
+
pass
|
app.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import requests
|
| 7 |
+
from huggingface_hub import HfFolder, Repository
|
| 8 |
+
import torch
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 11 |
+
|
| 12 |
+
# Load the CSV data with treatments
|
| 13 |
+
def load_treatments_data():
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get("https://hebbkx1anhila5yf.public.blob.vercel-storage.com/crop_diseases_treatments-2uScFyZnlnaYo70rR6hIkBBYdnwcG1.csv")
|
| 16 |
+
response.raise_for_status()
|
| 17 |
+
df = pd.read_csv(io.StringIO(response.text))
|
| 18 |
+
return df
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Error loading CSV: {e}")
|
| 21 |
+
# Create a fallback DataFrame if loading fails
|
| 22 |
+
return pd.DataFrame(columns=["Crop", "Disease", "Symptoms", "Treatment", "Medicine/Chemical Control"])
|
| 23 |
+
|
| 24 |
+
# Load and process the disease dataset
|
| 25 |
+
def prepare_disease_classifier():
|
| 26 |
+
# Set up image transformation
|
| 27 |
+
transform = transforms.Compose([
|
| 28 |
+
transforms.Resize((224, 224)),
|
| 29 |
+
transforms.ToTensor(),
|
| 30 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 31 |
+
])
|
| 32 |
+
|
| 33 |
+
# Example class names (to be replaced with actual classes from the dataset)
|
| 34 |
+
# These would come from your trained model
|
| 35 |
+
class_names = [
|
| 36 |
+
"Apple___Apple_scab",
|
| 37 |
+
"Apple___Black_rot",
|
| 38 |
+
"Apple___Cedar_apple_rust",
|
| 39 |
+
"Apple___healthy",
|
| 40 |
+
"Corn_(maize)___Cercospora_leaf_spot",
|
| 41 |
+
"Corn_(maize)___Common_rust",
|
| 42 |
+
"Corn_(maize)___healthy",
|
| 43 |
+
"Tomato___Early_blight",
|
| 44 |
+
"Tomato___Late_blight",
|
| 45 |
+
"Tomato___healthy"
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
return transform, class_names
|
| 49 |
+
|
| 50 |
+
# Function to classify plant disease from image
|
| 51 |
+
def classify_disease(image):
|
| 52 |
+
transform, class_names = prepare_disease_classifier()
|
| 53 |
+
|
| 54 |
+
# Process the image
|
| 55 |
+
if image is not None:
|
| 56 |
+
img_tensor = transform(image).unsqueeze(0)
|
| 57 |
+
|
| 58 |
+
# This is a placeholder for the actual model prediction
|
| 59 |
+
# In a real implementation, you would use your trained model
|
| 60 |
+
# prediction = model(img_tensor)
|
| 61 |
+
|
| 62 |
+
# For demonstration, we'll randomly select a class
|
| 63 |
+
import random
|
| 64 |
+
class_idx = random.randint(0, len(class_names) - 1)
|
| 65 |
+
class_name = class_names[class_idx]
|
| 66 |
+
|
| 67 |
+
# Extract crop and disease from class name
|
| 68 |
+
parts = class_name.split("___")
|
| 69 |
+
crop = parts[0].replace("_", " ")
|
| 70 |
+
disease = parts[1].replace("_", " ") if len(parts) > 1 else "healthy"
|
| 71 |
+
|
| 72 |
+
# Return the result
|
| 73 |
+
return crop, disease
|
| 74 |
+
|
| 75 |
+
return None, None
|
| 76 |
+
|
| 77 |
+
# Function to search for treatments in the CSV
|
| 78 |
+
def find_treatment(crop, disease, df):
|
| 79 |
+
if disease.lower() == "healthy":
|
| 80 |
+
return "The plant appears healthy and does not require treatment."
|
| 81 |
+
|
| 82 |
+
# Search in the DataFrame
|
| 83 |
+
matches = df[(df['Crop'].str.lower() == crop.lower()) &
|
| 84 |
+
(df['Disease'].str.lower().str.contains(disease.lower()))]
|
| 85 |
+
|
| 86 |
+
if not matches.empty:
|
| 87 |
+
# Get the first match
|
| 88 |
+
match = matches.iloc[0]
|
| 89 |
+
return {
|
| 90 |
+
"crop": match['Crop'],
|
| 91 |
+
"disease": match['Disease'],
|
| 92 |
+
"symptoms": match['Symptoms'],
|
| 93 |
+
"treatment": match['Treatment'],
|
| 94 |
+
"medicine": match['Medicine/Chemical Control']
|
| 95 |
+
}
|
| 96 |
+
else:
|
| 97 |
+
# Try to find a partial match
|
| 98 |
+
matches = df[df['Disease'].str.lower().str.contains(disease.lower())]
|
| 99 |
+
if not matches.empty:
|
| 100 |
+
match = matches.iloc[0]
|
| 101 |
+
return {
|
| 102 |
+
"crop": match['Crop'],
|
| 103 |
+
"disease": match['Disease'],
|
| 104 |
+
"symptoms": match['Symptoms'],
|
| 105 |
+
"treatment": match['Treatment'],
|
| 106 |
+
"medicine": match['Medicine/Chemical Control']
|
| 107 |
+
}
|
| 108 |
+
return "No specific treatment information found for this disease."
|
| 109 |
+
|
| 110 |
+
# Function to answer questions about plant diseases
|
| 111 |
+
def answer_question(question, df):
|
| 112 |
+
# Simple keyword-based answering system
|
| 113 |
+
question_lower = question.lower()
|
| 114 |
+
|
| 115 |
+
# Extract potential crop and disease from question
|
| 116 |
+
crops = list(set(df['Crop'].str.lower()))
|
| 117 |
+
diseases = list(set(df['Disease'].str.lower()))
|
| 118 |
+
|
| 119 |
+
identified_crop = None
|
| 120 |
+
for crop in crops:
|
| 121 |
+
if crop.lower() in question_lower:
|
| 122 |
+
identified_crop = crop
|
| 123 |
+
break
|
| 124 |
+
|
| 125 |
+
identified_disease = None
|
| 126 |
+
for disease in diseases:
|
| 127 |
+
if disease.lower() in question_lower:
|
| 128 |
+
identified_disease = disease
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
# Process question based on keywords
|
| 132 |
+
if "treat" in question_lower or "treatment" in question_lower or "how to" in question_lower:
|
| 133 |
+
if identified_crop and identified_disease:
|
| 134 |
+
matches = df[(df['Crop'].str.lower() == identified_crop) &
|
| 135 |
+
(df['Disease'].str.lower() == identified_disease)]
|
| 136 |
+
if not matches.empty:
|
| 137 |
+
match = matches.iloc[0]
|
| 138 |
+
return f"Treatment for {match['Disease']} in {match['Crop']}: {match['Treatment']}\n\nChemical control: {match['Medicine/Chemical Control']}"
|
| 139 |
+
|
| 140 |
+
# Generic treatment advice if specific combination not found
|
| 141 |
+
if identified_disease:
|
| 142 |
+
matches = df[df['Disease'].str.lower() == identified_disease]
|
| 143 |
+
if not matches.empty:
|
| 144 |
+
match = matches.iloc[0]
|
| 145 |
+
return f"Treatment for {match['Disease']}: {match['Treatment']}\n\nChemical control: {match['Medicine/Chemical Control']}"
|
| 146 |
+
|
| 147 |
+
elif "symptom" in question_lower:
|
| 148 |
+
if identified_disease:
|
| 149 |
+
matches = df[df['Disease'].str.lower() == identified_disease]
|
| 150 |
+
if not matches.empty:
|
| 151 |
+
match = matches.iloc[0]
|
| 152 |
+
return f"Symptoms of {match['Disease']}: {match['Symptoms']}"
|
| 153 |
+
|
| 154 |
+
# Generic response
|
| 155 |
+
return "I couldn't find specific information for your question. Please try uploading an image of the plant or asking about a specific crop disease treatment."
|
| 156 |
+
|
| 157 |
+
# Function to process image uploads
|
| 158 |
+
def process_image(image, df):
|
| 159 |
+
if image is None:
|
| 160 |
+
return "Please upload an image to analyze.", None, None, None, None
|
| 161 |
+
|
| 162 |
+
# Identify the crop and disease
|
| 163 |
+
crop, disease = classify_disease(image)
|
| 164 |
+
|
| 165 |
+
if disease == "healthy":
|
| 166 |
+
return f"Good news! This {crop} plant appears to be healthy.", crop, disease, "No treatment needed.", "N/A"
|
| 167 |
+
|
| 168 |
+
# Find treatment information
|
| 169 |
+
treatment_info = find_treatment(crop, disease, df)
|
| 170 |
+
|
| 171 |
+
if isinstance(treatment_info, dict):
|
| 172 |
+
return (
|
| 173 |
+
f"Identified: {treatment_info['disease']} in {treatment_info['crop']}",
|
| 174 |
+
treatment_info['crop'],
|
| 175 |
+
treatment_info['disease'],
|
| 176 |
+
treatment_info['treatment'],
|
| 177 |
+
treatment_info['medicine']
|
| 178 |
+
)
|
| 179 |
+
else:
|
| 180 |
+
return f"Identified: {disease} in {crop}", crop, disease, "No specific treatment found in database.", "N/A"
|
| 181 |
+
|
| 182 |
+
# Function to handle text questions
|
| 183 |
+
def process_question(question, df):
|
| 184 |
+
if not question.strip():
|
| 185 |
+
return "Please enter a question about plant diseases or treatments."
|
| 186 |
+
|
| 187 |
+
# Process the question
|
| 188 |
+
answer = answer_question(question, df)
|
| 189 |
+
return answer
|
| 190 |
+
|
| 191 |
+
# Main function to set up the Gradio interface
|
| 192 |
+
def main():
|
| 193 |
+
# Load the disease treatments data
|
| 194 |
+
df = load_treatments_data()
|
| 195 |
+
|
| 196 |
+
# Set up the Gradio interface with two tabs
|
| 197 |
+
with gr.Blocks(title="Plant Disease Assistant") as app:
|
| 198 |
+
gr.Markdown("# Plant Disease Treatment Assistant")
|
| 199 |
+
gr.Markdown("Upload a plant image to identify diseases or ask questions about treatments.")
|
| 200 |
+
|
| 201 |
+
with gr.Tabs():
|
| 202 |
+
with gr.TabItem("Image Analysis"):
|
| 203 |
+
with gr.Row():
|
| 204 |
+
with gr.Column():
|
| 205 |
+
image_input = gr.Image(type="pil", label="Upload Plant Image")
|
| 206 |
+
image_submit = gr.Button("Analyze Image")
|
| 207 |
+
|
| 208 |
+
with gr.Column():
|
| 209 |
+
diagnosis = gr.Textbox(label="Diagnosis")
|
| 210 |
+
crop = gr.Textbox(label="Crop")
|
| 211 |
+
disease = gr.Textbox(label="Disease")
|
| 212 |
+
treatment = gr.Textbox(label="Recommended Treatment")
|
| 213 |
+
medicine = gr.Textbox(label="Recommended Chemical Control")
|
| 214 |
+
|
| 215 |
+
image_submit.click(
|
| 216 |
+
fn=lambda img: process_image(img, df),
|
| 217 |
+
inputs=[image_input],
|
| 218 |
+
outputs=[diagnosis, crop, disease, treatment, medicine]
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
with gr.TabItem("Ask a Question"):
|
| 222 |
+
question_input = gr.Textbox(
|
| 223 |
+
lines=2,
|
| 224 |
+
placeholder="Ask a question like 'How do I treat early blight in tomatoes?'",
|
| 225 |
+
label="Your Question"
|
| 226 |
+
)
|
| 227 |
+
question_submit = gr.Button("Get Answer")
|
| 228 |
+
answer_output = gr.Textbox(label="Answer")
|
| 229 |
+
|
| 230 |
+
question_submit.click(
|
| 231 |
+
fn=lambda q: process_question(q, df),
|
| 232 |
+
inputs=[question_input],
|
| 233 |
+
outputs=[answer_output]
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
gr.Markdown("### Examples:")
|
| 237 |
+
gr.Examples(
|
| 238 |
+
examples=[
|
| 239 |
+
["How do I treat early blight in tomatoes?"],
|
| 240 |
+
["What are the symptoms of powdery mildew?"],
|
| 241 |
+
["What chemical controls work on apple scab?"]
|
| 242 |
+
],
|
| 243 |
+
inputs=question_input,
|
| 244 |
+
outputs=answer_output,
|
| 245 |
+
fn=lambda q: process_question(q, df)
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
return app
|
| 249 |
+
|
| 250 |
+
# Launch the app
|
| 251 |
+
if __name__ == "__main__":
|
| 252 |
+
app = main()
|
| 253 |
+
app.launch()
|
models.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import models, transforms
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
class PlantDiseaseClassifier:
|
| 6 |
+
"""
|
| 7 |
+
A class to handle plant disease classification using a pre-trained model.
|
| 8 |
+
This is a placeholder for the actual model implementation.
|
| 9 |
+
"""
|
| 10 |
+
def __init__(self, model_path=None, class_names=None):
|
| 11 |
+
# Set up the model architecture
|
| 12 |
+
self.model = models.resnet50(pretrained=True)
|
| 13 |
+
num_ftrs = self.model.fc.in_features
|
| 14 |
+
|
| 15 |
+
# If we have a specific number of classes, replace the final layer
|
| 16 |
+
if class_names:
|
| 17 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(class_names))
|
| 18 |
+
self.class_names = class_names
|
| 19 |
+
else:
|
| 20 |
+
# Default class names (placeholder)
|
| 21 |
+
self.class_names = [
|
| 22 |
+
"Apple___Apple_scab",
|
| 23 |
+
"Apple___Black_rot",
|
| 24 |
+
"Apple___Cedar_apple_rust",
|
| 25 |
+
"Apple___healthy",
|
| 26 |
+
"Corn_(maize)___Cercospora_leaf_spot",
|
| 27 |
+
"Corn_(maize)___Common_rust",
|
| 28 |
+
"Corn_(maize)___healthy",
|
| 29 |
+
"Tomato___Early_blight",
|
| 30 |
+
"Tomato___Late_blight",
|
| 31 |
+
"Tomato___healthy"
|
| 32 |
+
]
|
| 33 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(self.class_names))
|
| 34 |
+
|
| 35 |
+
# Load pretrained weights if provided
|
| 36 |
+
if model_path:
|
| 37 |
+
self.model.load_state_dict(torch.load(model_path))
|
| 38 |
+
|
| 39 |
+
# Set up image transformation
|
| 40 |
+
self.transform = transforms.Compose([
|
| 41 |
+
transforms.Resize((224, 224)),
|
| 42 |
+
transforms.ToTensor(),
|
| 43 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 44 |
+
])
|
| 45 |
+
|
| 46 |
+
# Set model to evaluation mode
|
| 47 |
+
self.model.eval()
|
| 48 |
+
|
| 49 |
+
def predict(self, image):
|
| 50 |
+
"""
|
| 51 |
+
Predict the disease class for a given plant image
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
image: PIL Image of the plant
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
class_name: String representing the predicted disease class
|
| 58 |
+
confidence: Float representing the confidence score
|
| 59 |
+
"""
|
| 60 |
+
# Transform the image
|
| 61 |
+
img_tensor = self.transform(image).unsqueeze(0)
|
| 62 |
+
|
| 63 |
+
# Get prediction
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
outputs = self.model(img_tensor)
|
| 66 |
+
_, predictions = torch.max(outputs, 1)
|
| 67 |
+
confidence = torch.nn.functional.softmax(outputs, dim=1)[0][predictions.item()].item()
|
| 68 |
+
|
| 69 |
+
# Get class name
|
| 70 |
+
class_name = self.class_names[predictions.item()]
|
| 71 |
+
|
| 72 |
+
return class_name, confidence
|
| 73 |
+
|
| 74 |
+
class QuestionAnswerer:
|
| 75 |
+
"""
|
| 76 |
+
A class to handle question answering about plant diseases.
|
| 77 |
+
This is a placeholder for more sophisticated NLP models.
|
| 78 |
+
"""
|
| 79 |
+
def __init__(self, treatments_df):
|
| 80 |
+
self.treatments_df = treatments_df
|
| 81 |
+
|
| 82 |
+
def answer(self, question):
|
| 83 |
+
"""
|
| 84 |
+
Answer a question about plant diseases using the treatments dataframe
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
question: String representing the user's question
|
| 88 |
+
|
| 89 |
+
Returns:
|
| 90 |
+
answer: String representing the answer to the question
|
| 91 |
+
"""
|
| 92 |
+
question_lower = question.lower()
|
| 93 |
+
|
| 94 |
+
# Extract potential crop and disease from question
|
| 95 |
+
crops = list(set(self.treatments_df['Crop'].str.lower()))
|
| 96 |
+
diseases = list(set(self.treatments_df['Disease'].str.lower()))
|
| 97 |
+
|
| 98 |
+
# Find mentions of crops
|
| 99 |
+
mentioned_crops = [crop for crop in crops if crop in question_lower]
|
| 100 |
+
|
| 101 |
+
# Find mentions of diseases
|
| 102 |
+
mentioned_diseases = [disease for disease in diseases if disease in question_lower]
|
| 103 |
+
|
| 104 |
+
# Check question intent
|
| 105 |
+
if any(term in question_lower for term in ["treat", "treatment", "how to", "cure"]):
|
| 106 |
+
# Question about treatment
|
| 107 |
+
if mentioned_crops and mentioned_diseases:
|
| 108 |
+
# Specific crop and disease
|
| 109 |
+
matches = self.treatments_df[
|
| 110 |
+
(self.treatments_df['Crop'].str.lower().isin(mentioned_crops)) &
|
| 111 |
+
(self.treatments_df['Disease'].str.lower().isin(mentioned_diseases))
|
| 112 |
+
]
|
| 113 |
+
if not matches.empty:
|
| 114 |
+
match = matches.iloc[0]
|
| 115 |
+
return f"Treatment for {match['Disease']} in {match['Crop']}: {match['Treatment']}\n\nChemical control: {match['Medicine/Chemical Control']}"
|
| 116 |
+
|
| 117 |
+
# Just disease mentioned
|
| 118 |
+
elif mentioned_diseases:
|
| 119 |
+
matches = self.treatments_df[self.treatments_df['Disease'].str.lower().isin(mentioned_diseases)]
|
| 120 |
+
if not matches.empty:
|
| 121 |
+
match = matches.iloc[0]
|
| 122 |
+
return f"Treatment for {match['Disease']}: {match['Treatment']}\n\nChemical control: {match['Medicine/Chemical Control']}"
|
| 123 |
+
|
| 124 |
+
elif any(term in question_lower for term in ["symptom", "sign", "identify"]):
|
| 125 |
+
# Question about symptoms
|
| 126 |
+
if mentioned_diseases:
|
| 127 |
+
matches = self.treatments_df[self.treatments_df['Disease'].str.lower().isin(mentioned_diseases)]
|
| 128 |
+
if not matches.empty:
|
| 129 |
+
match = matches.iloc[0]
|
| 130 |
+
return f"Symptoms of {match['Disease']}: {match['Symptoms']}"
|
| 131 |
+
|
| 132 |
+
# Generic response
|
| 133 |
+
return "I couldn't find specific information for your question. Please try uploading an image of the plant or asking about a specific crop disease treatment."
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.13.0
|
| 2 |
+
pandas>=2.0.0
|
| 3 |
+
requests>=2.31.0
|
| 4 |
+
torch>=2.0.0
|
| 5 |
+
torchvision>=0.15.0
|
| 6 |
+
transformers>=4.35.0
|
| 7 |
+
Pillow>=10.0.0
|
| 8 |
+
huggingface_hub>=0.19.0
|