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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# Synthetic Dataset Creation
|
| 10 |
+
def create_synthetic_dataset():
|
| 11 |
+
# Districts of Andhra Pradesh
|
| 12 |
+
districts = [
|
| 13 |
+
'Anantapur', 'Chittoor', 'East Godavari', 'Guntur', 'Krishna',
|
| 14 |
+
'Kurnool', 'Nellore', 'Prakasam', 'Srikakulam', 'Visakhapatnam',
|
| 15 |
+
'Vizianagaram', 'West Godavari', 'YSR Kadapa'
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Common crops in Andhra Pradesh
|
| 19 |
+
crops = [
|
| 20 |
+
'Rice', 'Maize', 'Cotton', 'Groundnut', 'Red Gram (Toor Dal)',
|
| 21 |
+
'Green Gram (Moong Dal)', 'Black Gram (Urad Dal)', 'Sugarcane',
|
| 22 |
+
'Chilli', 'Turmeric', 'Tobacco', 'Mango', 'Banana', 'Coconut',
|
| 23 |
+
'Cashew', 'Soybean', 'Sunflower', 'Jowar (Sorghum)', 'Bajra (Pearl Millet)'
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Months
|
| 27 |
+
months = ['January', 'February', 'March', 'April', 'May', 'June',
|
| 28 |
+
'July', 'August', 'September', 'October', 'November', 'December']
|
| 29 |
+
|
| 30 |
+
# Create synthetic data
|
| 31 |
+
np.random.seed(42)
|
| 32 |
+
num_samples = 5000
|
| 33 |
+
|
| 34 |
+
data = {
|
| 35 |
+
'District': np.random.choice(districts, num_samples),
|
| 36 |
+
'Month': np.random.choice(months, num_samples),
|
| 37 |
+
'Temperature': np.random.uniform(20, 40, num_samples),
|
| 38 |
+
'Rainfall': np.random.uniform(0, 300, num_samples),
|
| 39 |
+
'Soil_Type': np.random.choice(['Black', 'Red', 'Alluvial', 'Laterite'], num_samples),
|
| 40 |
+
'Crop': np.random.choice(crops, num_samples),
|
| 41 |
+
'Suitability': np.random.choice([0, 1], num_samples, p=[0.3, 0.7])
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Add some logical patterns based on real-world knowledge
|
| 45 |
+
for i in range(num_samples):
|
| 46 |
+
district = data['District'][i]
|
| 47 |
+
month = data['Month'][i]
|
| 48 |
+
|
| 49 |
+
# Adjust temperature based on month
|
| 50 |
+
if month in ['December', 'January', 'February']:
|
| 51 |
+
data['Temperature'][i] = np.random.uniform(15, 28)
|
| 52 |
+
elif month in ['March', 'April', 'May']:
|
| 53 |
+
data['Temperature'][i] = np.random.uniform(28, 42)
|
| 54 |
+
else:
|
| 55 |
+
data['Temperature'][i] = np.random.uniform(25, 35)
|
| 56 |
+
|
| 57 |
+
# Adjust rainfall based on district and month
|
| 58 |
+
if district in ['Visakhapatnam', 'Srikakulam', 'Vizianagaram']:
|
| 59 |
+
if month in ['July', 'August', 'September']:
|
| 60 |
+
data['Rainfall'][i] = np.random.uniform(150, 300)
|
| 61 |
+
else:
|
| 62 |
+
data['Rainfall'][i] = np.random.uniform(50, 150)
|
| 63 |
+
elif district in ['Anantapur', 'Kurnool', 'YSR Kadapa']:
|
| 64 |
+
data['Rainfall'][i] = np.random.uniform(0, 100)
|
| 65 |
+
else:
|
| 66 |
+
if month in ['July', 'August', 'September']:
|
| 67 |
+
data['Rainfall'][i] = np.random.uniform(100, 250)
|
| 68 |
+
else:
|
| 69 |
+
data['Rainfall'][i] = np.random.uniform(20, 100)
|
| 70 |
+
|
| 71 |
+
# Adjust suitability based on some logical conditions
|
| 72 |
+
crop = data['Crop'][i]
|
| 73 |
+
|
| 74 |
+
# Rice needs more water
|
| 75 |
+
if crop == 'Rice' and data['Rainfall'][i] < 100:
|
| 76 |
+
data['Suitability'][i] = 0
|
| 77 |
+
|
| 78 |
+
# Groundnut grows well in Anantapur
|
| 79 |
+
if crop == 'Groundnut' and district == 'Anantapur':
|
| 80 |
+
data['Suitability'][i] = 1
|
| 81 |
+
|
| 82 |
+
# Coconut grows well in coastal areas
|
| 83 |
+
if crop == 'Coconut' and district in ['East Godavari', 'West Godavari', 'Visakhapatnam']:
|
| 84 |
+
data['Suitability'][i] = 1
|
| 85 |
+
|
| 86 |
+
# Chilli grows well in Guntur
|
| 87 |
+
if crop == 'Chilli' and district == 'Guntur':
|
| 88 |
+
data['Suitability'][i] = 1
|
| 89 |
+
|
| 90 |
+
df = pd.DataFrame(data)
|
| 91 |
+
return df, crops, districts, months
|
| 92 |
+
|
| 93 |
+
# Create dataset
|
| 94 |
+
df, crops, districts, months = create_synthetic_dataset()
|
| 95 |
+
|
| 96 |
+
# Train machine learning model
|
| 97 |
+
def train_model(df):
|
| 98 |
+
# Convert categorical variables to numerical
|
| 99 |
+
df_encoded = pd.get_dummies(df, columns=['District', 'Month', 'Soil_Type', 'Crop'])
|
| 100 |
+
|
| 101 |
+
X = df_encoded.drop('Suitability', axis=1)
|
| 102 |
+
y = df_encoded['Suitability']
|
| 103 |
+
|
| 104 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 105 |
+
model.fit(X, y)
|
| 106 |
+
|
| 107 |
+
return model
|
| 108 |
+
|
| 109 |
+
model = train_model(df)
|
| 110 |
+
|
| 111 |
+
# Crop information and precautions
|
| 112 |
+
crop_info = {
|
| 113 |
+
'Rice': {
|
| 114 |
+
'description': 'Staple food crop requiring abundant water',
|
| 115 |
+
'precautions': [
|
| 116 |
+
'Ensure proper water management (5-10 cm standing water)',
|
| 117 |
+
'Use certified seeds for better yield',
|
| 118 |
+
'Control weeds in early stages',
|
| 119 |
+
'Monitor for pests like stem borers and leaf folders'
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
'Maize': {
|
| 123 |
+
'description': 'Versatile cereal crop grown in diverse conditions',
|
| 124 |
+
'precautions': [
|
| 125 |
+
'Plant in well-drained soil',
|
| 126 |
+
'Maintain proper spacing (60x20 cm)',
|
| 127 |
+
'Apply nitrogen in split doses',
|
| 128 |
+
'Watch for fall armyworm infestation'
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
'Cotton': {
|
| 132 |
+
'description': 'Important cash crop known as "white gold"',
|
| 133 |
+
'precautions': [
|
| 134 |
+
'Use Bt cotton seeds for pest resistance',
|
| 135 |
+
'Monitor for pink bollworm',
|
| 136 |
+
'Practice crop rotation to prevent soil depletion',
|
| 137 |
+
'Avoid waterlogging in fields'
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
'Groundnut': {
|
| 141 |
+
'description': 'Oilseed crop important for protein and oil',
|
| 142 |
+
'precautions': [
|
| 143 |
+
'Plant in well-drained sandy loam soil',
|
| 144 |
+
'Apply gypsum at flowering stage',
|
| 145 |
+
'Harvest at proper maturity to avoid aflatoxin',
|
| 146 |
+
'Store in dry conditions'
|
| 147 |
+
]
|
| 148 |
+
},
|
| 149 |
+
'Red Gram (Toor Dal)': {
|
| 150 |
+
'description': 'Important pulse crop rich in protein',
|
| 151 |
+
'precautions': [
|
| 152 |
+
'Drought resistant but needs irrigation at flowering',
|
| 153 |
+
'Treat seeds with rhizobium culture',
|
| 154 |
+
'Control pod borer with recommended pesticides',
|
| 155 |
+
'Harvest when 80% pods are mature'
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
'Green Gram (Moong Dal)': {
|
| 159 |
+
'description': 'Short duration pulse crop',
|
| 160 |
+
'precautions': [
|
| 161 |
+
'Grows well in well-drained soils',
|
| 162 |
+
'Short duration (60-70 days)',
|
| 163 |
+
'Susceptible to yellow mosaic virus - use resistant varieties',
|
| 164 |
+
'Harvest when 80% pods are mature'
|
| 165 |
+
]
|
| 166 |
+
},
|
| 167 |
+
'Black Gram (Urad Dal)': {
|
| 168 |
+
'description': 'Important pulse crop for protein',
|
| 169 |
+
'precautions': [
|
| 170 |
+
'Grows well in black cotton soils',
|
| 171 |
+
'Treat seeds with rhizobium culture',
|
| 172 |
+
'Control leaf spot diseases',
|
| 173 |
+
'Harvest when pods turn black'
|
| 174 |
+
]
|
| 175 |
+
},
|
| 176 |
+
'Sugarcane': {
|
| 177 |
+
'description': 'Important cash crop for sugar production',
|
| 178 |
+
'precautions': [
|
| 179 |
+
'Requires heavy irrigation',
|
| 180 |
+
'Use disease-free setts for planting',
|
| 181 |
+
'Control early shoot borer',
|
| 182 |
+
'Harvest at proper maturity (10-12 months)'
|
| 183 |
+
]
|
| 184 |
+
},
|
| 185 |
+
'Chilli': {
|
| 186 |
+
'description': 'Important spice crop with high value',
|
| 187 |
+
'precautions': [
|
| 188 |
+
'Requires well-drained fertile soil',
|
| 189 |
+
'Irrigate carefully to avoid flower drop',
|
| 190 |
+
'Control fruit borer and mites',
|
| 191 |
+
'Harvest at color break stage'
|
| 192 |
+
]
|
| 193 |
+
},
|
| 194 |
+
'Turmeric': {
|
| 195 |
+
'description': 'Important spice crop with medicinal value',
|
| 196 |
+
'precautions': [
|
| 197 |
+
'Plant in well-drained fertile soil',
|
| 198 |
+
'Treat seed rhizomes with fungicide',
|
| 199 |
+
'Control leaf spot diseases',
|
| 200 |
+
'Harvest after 8-9 months when leaves dry'
|
| 201 |
+
]
|
| 202 |
+
},
|
| 203 |
+
'Tobacco': {
|
| 204 |
+
'description': 'Commercial crop mainly for export',
|
| 205 |
+
'precautions': [
|
| 206 |
+
'Requires well-drained sandy loam soils',
|
| 207 |
+
'Needs careful curing after harvest',
|
| 208 |
+
'Follow government regulations',
|
| 209 |
+
'Practice crop rotation'
|
| 210 |
+
]
|
| 211 |
+
},
|
| 212 |
+
'Mango': {
|
| 213 |
+
'description': 'Important fruit crop of Andhra Pradesh',
|
| 214 |
+
'precautions': [
|
| 215 |
+
'Plant in well-drained deep soils',
|
| 216 |
+
'Prune for proper canopy management',
|
| 217 |
+
'Control mango hopper and fruit fly',
|
| 218 |
+
'Harvest at proper maturity'
|
| 219 |
+
]
|
| 220 |
+
},
|
| 221 |
+
'Banana': {
|
| 222 |
+
'description': 'Important fruit crop with high yield',
|
| 223 |
+
'precautions': [
|
| 224 |
+
'Requires heavy irrigation and fertilization',
|
| 225 |
+
'Plant disease-free tissue culture plants',
|
| 226 |
+
'Control sigatoka leaf spot disease',
|
| 227 |
+
'Support plants during fruiting'
|
| 228 |
+
]
|
| 229 |
+
},
|
| 230 |
+
'Coconut': {
|
| 231 |
+
'description': 'Important plantation crop of coastal areas',
|
| 232 |
+
'precautions': [
|
| 233 |
+
'Plant in coastal sandy soils',
|
| 234 |
+
'Apply balanced fertilizers regularly',
|
| 235 |
+
'Control rhinoceros beetle',
|
| 236 |
+
'Intercrop with cocoa or pepper'
|
| 237 |
+
]
|
| 238 |
+
},
|
| 239 |
+
'Cashew': {
|
| 240 |
+
'description': 'Important plantation crop for export',
|
| 241 |
+
'precautions': [
|
| 242 |
+
'Plant in well-drained sandy soils',
|
| 243 |
+
'Prune for proper shape',
|
| 244 |
+
'Control tea mosquito bug',
|
| 245 |
+
'Harvest nuts when apple turns pink'
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
'Soybean': {
|
| 249 |
+
'description': 'Oilseed crop rich in protein',
|
| 250 |
+
'precautions': [
|
| 251 |
+
'Plant in well-drained soils',
|
| 252 |
+
'Inoculate seeds with rhizobium',
|
| 253 |
+
'Control yellow mosaic virus',
|
| 254 |
+
'Harvest when leaves yellow and drop'
|
| 255 |
+
]
|
| 256 |
+
},
|
| 257 |
+
'Sunflower': {
|
| 258 |
+
'description': 'Important oilseed crop',
|
| 259 |
+
'precautions': [
|
| 260 |
+
'Plant in well-drained soils',
|
| 261 |
+
'Provide support if needed',
|
| 262 |
+
'Control head borer',
|
| 263 |
+
'Harvest when back of head turns yellow'
|
| 264 |
+
]
|
| 265 |
+
},
|
| 266 |
+
'Jowar (Sorghum)': {
|
| 267 |
+
'description': 'Traditional millet crop',
|
| 268 |
+
'precautions': [
|
| 269 |
+
'Drought resistant crop',
|
| 270 |
+
'Control shoot fly in early stages',
|
| 271 |
+
'Harvest when grains are hard'
|
| 272 |
+
]
|
| 273 |
+
},
|
| 274 |
+
'Bajra (Pearl Millet)': {
|
| 275 |
+
'description': 'Traditional drought-resistant crop',
|
| 276 |
+
'precautions': [
|
| 277 |
+
'Grows well in poor soils',
|
| 278 |
+
'Control downy mildew',
|
| 279 |
+
'Harvest when grains are hard'
|
| 280 |
+
]
|
| 281 |
+
}
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
# District-wise climate information
|
| 285 |
+
district_climate = {
|
| 286 |
+
'Anantapur': {
|
| 287 |
+
'description': 'Hot and dry climate with low rainfall',
|
| 288 |
+
'soil': 'Red sandy loam soils',
|
| 289 |
+
'avg_temp': '28-40°C',
|
| 290 |
+
'avg_rainfall': '500-600 mm'
|
| 291 |
+
},
|
| 292 |
+
'Chittoor': {
|
| 293 |
+
'description': 'Moderate climate with some hilly areas',
|
| 294 |
+
'soil': 'Red soils and black cotton soils',
|
| 295 |
+
'avg_temp': '22-38°C',
|
| 296 |
+
'avg_rainfall': '900-1000 mm'
|
| 297 |
+
},
|
| 298 |
+
'East Godavari': {
|
| 299 |
+
'description': 'Coastal district with high humidity',
|
| 300 |
+
'soil': 'Alluvial and deltaic soils',
|
| 301 |
+
'avg_temp': '24-36°C',
|
| 302 |
+
'avg_rainfall': '1000-1100 mm'
|
| 303 |
+
},
|
| 304 |
+
'Guntur': {
|
| 305 |
+
'description': 'Coastal plains with hot climate',
|
| 306 |
+
'soil': 'Black cotton soils',
|
| 307 |
+
'avg_temp': '25-38°C',
|
| 308 |
+
'avg_rainfall': '800-900 mm'
|
| 309 |
+
},
|
| 310 |
+
'Krishna': {
|
| 311 |
+
'description': 'Coastal district with fertile delta',
|
| 312 |
+
'soil': 'Alluvial and black soils',
|
| 313 |
+
'avg_temp': '24-36°C',
|
| 314 |
+
'avg_rainfall': '900-1000 mm'
|
| 315 |
+
},
|
| 316 |
+
'Kurnool': {
|
| 317 |
+
'description': 'Semi-arid climate with low rainfall',
|
| 318 |
+
'soil': 'Red soils and black soils',
|
| 319 |
+
'avg_temp': '26-40°C',
|
| 320 |
+
'avg_rainfall': '600-700 mm'
|
| 321 |
+
},
|
| 322 |
+
'Nellore': {
|
| 323 |
+
'description': 'Coastal district with moderate rainfall',
|
| 324 |
+
'soil': 'Red soils and sandy loams',
|
| 325 |
+
'avg_temp': '24-36°C',
|
| 326 |
+
'avg_rainfall': '1000-1100 mm'
|
| 327 |
+
},
|
| 328 |
+
'Prakasam': {
|
| 329 |
+
'description': 'Mixed coastal and dry climate',
|
| 330 |
+
'soil': 'Red soils and sandy loams',
|
| 331 |
+
'avg_temp': '25-38°C',
|
| 332 |
+
'avg_rainfall': '800-900 mm'
|
| 333 |
+
},
|
| 334 |
+
'Srikakulam': {
|
| 335 |
+
'description': 'Northern coastal district with good rainfall',
|
| 336 |
+
'soil': 'Red and alluvial soils',
|
| 337 |
+
'avg_temp': '22-34°C',
|
| 338 |
+
'avg_rainfall': '1100-1200 mm'
|
| 339 |
+
},
|
| 340 |
+
'Visakhapatnam': {
|
| 341 |
+
'description': 'Coastal district with hilly terrain',
|
| 342 |
+
'soil': 'Red and laterite soils',
|
| 343 |
+
'avg_temp': '22-33°C',
|
| 344 |
+
'avg_rainfall': '1000-1100 mm'
|
| 345 |
+
},
|
| 346 |
+
'Vizianagaram': {
|
| 347 |
+
'description': 'Coastal district with moderate climate',
|
| 348 |
+
'soil': 'Red and alluvial soils',
|
| 349 |
+
'avg_temp': '23-35°C',
|
| 350 |
+
'avg_rainfall': '1000-1100 mm'
|
| 351 |
+
},
|
| 352 |
+
'West Godavari': {
|
| 353 |
+
'description': 'Fertile delta region with high humidity',
|
| 354 |
+
'soil': 'Alluvial and black soils',
|
| 355 |
+
'avg_temp': '24-36°C',
|
| 356 |
+
'avg_rainfall': '1000-1100 mm'
|
| 357 |
+
},
|
| 358 |
+
'YSR Kadapa': {
|
| 359 |
+
'description': 'Hot and dry climate with low rainfall',
|
| 360 |
+
'soil': 'Red soils and black soils',
|
| 361 |
+
'avg_temp': '27-40°C',
|
| 362 |
+
'avg_rainfall': '600-700 mm'
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
# Prediction function
|
| 367 |
+
def predict_crop(district, month, crop_choice=None):
|
| 368 |
+
# Get current temperature and rainfall based on district and month
|
| 369 |
+
temp = df[(df['District'] == district) & (df['Month'] == month)]['Temperature'].mean()
|
| 370 |
+
rainfall = df[(df['District'] == district) & (df['Month'] == month)]['Rainfall'].mean()
|
| 371 |
+
soil_type = df[df['District'] == district]['Soil_Type'].mode()[0]
|
| 372 |
+
|
| 373 |
+
# Prepare input for model
|
| 374 |
+
input_data = {
|
| 375 |
+
'District': district,
|
| 376 |
+
'Month': month,
|
| 377 |
+
'Temperature': temp,
|
| 378 |
+
'Rainfall': rainfall,
|
| 379 |
+
'Soil_Type': soil_type
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
# If user has selected a crop
|
| 383 |
+
if crop_choice and crop_choice != "I don't know":
|
| 384 |
+
input_data['Crop'] = crop_choice
|
| 385 |
+
input_df = pd.DataFrame([input_data])
|
| 386 |
+
input_encoded = pd.get_dummies(input_df, columns=['District', 'Month', 'Soil_Type', 'Crop'])
|
| 387 |
+
|
| 388 |
+
# Ensure all columns are present (add missing with 0)
|
| 389 |
+
train_columns = pd.get_dummies(df, columns=['District', 'Month', 'Soil_Type', 'Crop']).columns.drop('Suitability')
|
| 390 |
+
for col in train_columns:
|
| 391 |
+
if col not in input_encoded.columns:
|
| 392 |
+
input_encoded[col] = 0
|
| 393 |
+
|
| 394 |
+
input_encoded = input_encoded[train_columns]
|
| 395 |
+
|
| 396 |
+
prediction = model.predict(input_encoded)[0]
|
| 397 |
+
|
| 398 |
+
if prediction == 1:
|
| 399 |
+
result = f"✅ {crop_choice} is suitable to grow in {district} during {month}."
|
| 400 |
+
precautions = crop_info[crop_choice]['precautions']
|
| 401 |
+
precautions_text = "\n".join([f"• {precaution}" for precaution in precautions])
|
| 402 |
+
output = f"{result}\n\n📌 Precautions:\n{precautions_text}"
|
| 403 |
+
else:
|
| 404 |
+
alternatives = get_alternative_crops(district, month)
|
| 405 |
+
alt_text = "\n".join([f"• {crop}" for crop in alternatives[:3]])
|
| 406 |
+
output = f"❌ {crop_choice} is not recommended for {district} in {month}.\n\n🌱 Better alternatives:\n{alt_text}"
|
| 407 |
+
else:
|
| 408 |
+
# Recommend best crops
|
| 409 |
+
recommended_crops = get_alternative_crops(district, month)
|
| 410 |
+
rec_text = "\n".join([f"• {crop}" for crop in recommended_crops[:5]])
|
| 411 |
+
|
| 412 |
+
# Get climate info
|
| 413 |
+
climate = district_climate[district]
|
| 414 |
+
climate_text = (
|
| 415 |
+
f"🌡️ Avg Temperature: {climate['avg_temp']}\n"
|
| 416 |
+
f"🌧️ Avg Rainfall: {climate['avg_rainfall']}\n"
|
| 417 |
+
f"🌱 Soil Type: {climate['soil']}"
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
output = (
|
| 421 |
+
f"🌾 Recommended crops for {district} in {month}:\n\n{rec_text}\n\n"
|
| 422 |
+
f"📌 District Climate Info:\n{climate_text}"
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
return output
|
| 426 |
+
|
| 427 |
+
def get_alternative_crops(district, month):
|
| 428 |
+
# Get current temperature and rainfall based on district and month
|
| 429 |
+
temp = df[(df['District'] == district) & (df['Month'] == month)]['Temperature'].mean()
|
| 430 |
+
rainfall = df[(df['District'] == district) & (df['Month'] == month)]['Rainfall'].mean()
|
| 431 |
+
soil_type = df[df['District'] == district]['Soil_Type'].mode()[0]
|
| 432 |
+
|
| 433 |
+
# Test all crops and get suitability scores
|
| 434 |
+
crop_scores = []
|
| 435 |
+
for crop in crops:
|
| 436 |
+
input_data = {
|
| 437 |
+
'District': district,
|
| 438 |
+
'Month': month,
|
| 439 |
+
'Temperature': temp,
|
| 440 |
+
'Rainfall': rainfall,
|
| 441 |
+
'Soil_Type': soil_type,
|
| 442 |
+
'Crop': crop
|
| 443 |
+
}
|
| 444 |
+
input_df = pd.DataFrame([input_data])
|
| 445 |
+
input_encoded = pd.get_dummies(input_df, columns=['District', 'Month', 'Soil_Type', 'Crop'])
|
| 446 |
+
|
| 447 |
+
# Ensure all columns are present (add missing with 0)
|
| 448 |
+
train_columns = pd.get_dummies(df, columns=['District', 'Month', 'Soil_Type', 'Crop']).columns.drop('Suitability')
|
| 449 |
+
for col in train_columns:
|
| 450 |
+
if col not in input_encoded.columns:
|
| 451 |
+
input_encoded[col] = 0
|
| 452 |
+
|
| 453 |
+
input_encoded = input_encoded[train_columns]
|
| 454 |
+
|
| 455 |
+
# Get probability instead of binary prediction
|
| 456 |
+
proba = model.predict_proba(input_encoded)[0][1]
|
| 457 |
+
crop_scores.append((crop, proba))
|
| 458 |
+
|
| 459 |
+
# Sort by probability
|
| 460 |
+
crop_scores.sort(key=lambda x: x[1], reverse=True)
|
| 461 |
+
return [crop for crop, score in crop_scores if score > 0.7]
|
| 462 |
+
|
| 463 |
+
# Custom CSS for styling
|
| 464 |
+
css = """
|
| 465 |
+
.gradio-container {
|
| 466 |
+
font-family: 'Poppins', sans-serif;
|
| 467 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
.title {
|
| 471 |
+
text-align: center;
|
| 472 |
+
color: #2c3e50;
|
| 473 |
+
font-size: 28px;
|
| 474 |
+
font-weight: 600;
|
| 475 |
+
margin-bottom: 20px;
|
| 476 |
+
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 477 |
+
-webkit-background-clip: text;
|
| 478 |
+
-webkit-text-fill-color: transparent;
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
.description {
|
| 482 |
+
text-align: center;
|
| 483 |
+
color: #4a5568;
|
| 484 |
+
margin-bottom: 30px;
|
| 485 |
+
font-size: 16px;
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
.input-section {
|
| 489 |
+
background: white;
|
| 490 |
+
padding: 20px;
|
| 491 |
+
border-radius: 10px;
|
| 492 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 493 |
+
margin-bottom: 20px;
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
.input-label {
|
| 497 |
+
font-weight: 500;
|
| 498 |
+
color: #2d3748;
|
| 499 |
+
margin-bottom: 8px;
|
| 500 |
+
display: block;
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
.output-section {
|
| 504 |
+
background: white;
|
| 505 |
+
padding: 25px;
|
| 506 |
+
border-radius: 10px;
|
| 507 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 508 |
+
min-height: 200px;
|
| 509 |
+
font-size: 16px;
|
| 510 |
+
line-height: 1.6;
|
| 511 |
+
white-space: pre-wrap;
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
.output-title {
|
| 515 |
+
color: #2c3e50;
|
| 516 |
+
font-weight: 600;
|
| 517 |
+
margin-bottom: 15px;
|
| 518 |
+
font-size: 20px;
|
| 519 |
+
border-bottom: 2px solid #e2e8f0;
|
| 520 |
+
padding-bottom: 8px;
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
.btn-primary {
|
| 524 |
+
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 525 |
+
border: none;
|
| 526 |
+
color: white;
|
| 527 |
+
padding: 12px 24px;
|
| 528 |
+
border-radius: 8px;
|
| 529 |
+
font-weight: 500;
|
| 530 |
+
cursor: pointer;
|
| 531 |
+
transition: all 0.3s ease;
|
| 532 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
.btn-primary:hover {
|
| 536 |
+
transform: translateY(-2px);
|
| 537 |
+
box-shadow: 0 7px 14px rgba(0, 0, 0, 0.1);
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
.select-dropdown, .text-input {
|
| 541 |
+
width: 100%;
|
| 542 |
+
padding: 12px;
|
| 543 |
+
border: 1px solid #e2e8f0;
|
| 544 |
+
border-radius: 8px;
|
| 545 |
+
font-size: 16px;
|
| 546 |
+
transition: all 0.3s ease;
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
+
.select-dropdown:focus, .text-input:focus {
|
| 550 |
+
border-color: #4b6cb7;
|
| 551 |
+
box-shadow: 0 0 0 3px rgba(75, 108, 183, 0.2);
|
| 552 |
+
outline: none;
|
| 553 |
+
}
|
| 554 |
+
|
| 555 |
+
.footer {
|
| 556 |
+
text-align: center;
|
| 557 |
+
margin-top: 30px;
|
| 558 |
+
color: #718096;
|
| 559 |
+
font-size: 14px;
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
.success {
|
| 563 |
+
color: #2e7d32;
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
.warning {
|
| 567 |
+
color: #d32f2f;
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
.recommendation {
|
| 571 |
+
background: #f0f4f8;
|
| 572 |
+
padding: 15px;
|
| 573 |
+
border-radius: 8px;
|
| 574 |
+
margin-top: 15px;
|
| 575 |
+
border-left: 4px solid #4b6cb7;
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
.crop-image {
|
| 579 |
+
max-width: 100%;
|
| 580 |
+
border-radius: 8px;
|
| 581 |
+
margin-top: 15px;
|
| 582 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 583 |
+
}
|
| 584 |
+
"""
|
| 585 |
+
|
| 586 |
+
# Gradio Interface
|
| 587 |
+
with gr.Blocks(css=css) as demo:
|
| 588 |
+
gr.Markdown("""
|
| 589 |
+
<div class="title">🌱 Crop Vision: Andhra Pradesh Farmer Decision Support System</div>
|
| 590 |
+
<div class="description">
|
| 591 |
+
Helping farmers in Andhra Pradesh make informed decisions about crop selection based on location and season
|
| 592 |
+
</div>
|
| 593 |
+
""")
|
| 594 |
+
|
| 595 |
+
with gr.Row():
|
| 596 |
+
with gr.Column():
|
| 597 |
+
with gr.Group(visible=True) as input_section:
|
| 598 |
+
gr.Markdown("### 📍 Enter Your Farming Details")
|
| 599 |
+
district = gr.Dropdown(
|
| 600 |
+
label="Select Your District",
|
| 601 |
+
choices=districts,
|
| 602 |
+
value="Anantapur",
|
| 603 |
+
interactive=True,
|
| 604 |
+
elem_classes="select-dropdown"
|
| 605 |
+
)
|
| 606 |
+
month = gr.Dropdown(
|
| 607 |
+
label="Select Planting Month",
|
| 608 |
+
choices=months,
|
| 609 |
+
value=datetime.now().strftime("%B"),
|
| 610 |
+
interactive=True,
|
| 611 |
+
elem_classes="select-dropdown"
|
| 612 |
+
)
|
| 613 |
+
crop_choice = gr.Dropdown(
|
| 614 |
+
label="Do you have a specific crop in mind? (Select 'I don't know' for recommendations)",
|
| 615 |
+
choices=["I don't know"] + sorted(crops),
|
| 616 |
+
value="I don't know",
|
| 617 |
+
interactive=True,
|
| 618 |
+
elem_classes="select-dropdown"
|
| 619 |
+
)
|
| 620 |
+
submit_btn = gr.Button("Get Recommendation", variant="primary", elem_classes="btn-primary")
|
| 621 |
+
|
| 622 |
+
with gr.Column():
|
| 623 |
+
with gr.Group(visible=True) as output_section:
|
| 624 |
+
gr.Markdown("### 🌾 Recommendation")
|
| 625 |
+
output = gr.Textbox(
|
| 626 |
+
label="",
|
| 627 |
+
interactive=False,
|
| 628 |
+
lines=15,
|
| 629 |
+
elem_classes="output-section"
|
| 630 |
+
)
|
| 631 |
+
|
| 632 |
+
gr.Markdown("""
|
| 633 |
+
<div class="footer">
|
| 634 |
+
Note: This system provides recommendations based on historical data and machine learning predictions.
|
| 635 |
+
Always consult with local agricultural experts before making final decisions.
|
| 636 |
+
</div>
|
| 637 |
+
""")
|
| 638 |
+
|
| 639 |
+
submit_btn.click(
|
| 640 |
+
fn=predict_crop,
|
| 641 |
+
inputs=[district, month, crop_choice],
|
| 642 |
+
outputs=output
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
# Launch the app
|
| 646 |
+
if __name__ == "__main__":
|
| 647 |
+
demo.launch()
|