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omForestClassifier
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
# Generate synthetic dataset for Indian crops (focusing on South Indian states)
|
| 9 |
+
def generate_synthetic_dataset(num_samples=5000):
|
| 10 |
+
np.random.seed(42)
|
| 11 |
+
|
| 12 |
+
# Common crops in Andhra Pradesh and Telangana
|
| 13 |
+
crops = [
|
| 14 |
+
'Rice', 'Maize', 'Cotton', 'Groundnut', 'Red Gram (Toor Dal)',
|
| 15 |
+
'Green Gram (Moong Dal)', 'Black Gram (Urad Dal)', 'Sunflower',
|
| 16 |
+
'Sugarcane', 'Turmeric', 'Chilli', 'Tomato', 'Onion', 'Mango',
|
| 17 |
+
'Banana', 'Coconut', 'Soybean', 'Jowar (Sorghum)', 'Bajra (Pearl Millet)'
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
# Soil types common in the region
|
| 21 |
+
soil_types = ['Black Cotton', 'Red Sandy', 'Clayey', 'Loamy', 'Sandy Loam']
|
| 22 |
+
|
| 23 |
+
# Seasons in Indian agriculture
|
| 24 |
+
seasons = ['Kharif (June-Oct)', 'Rabi (Oct-Mar)', 'Zaid (Mar-Jun)', 'Whole Year']
|
| 25 |
+
|
| 26 |
+
# Generate synthetic data
|
| 27 |
+
data = {
|
| 28 |
+
'Temperature (°C)': np.random.uniform(10, 60, num_samples),
|
| 29 |
+
'Rainfall (mm)': np.random.uniform(0, 300, num_samples),
|
| 30 |
+
'Humidity (%)': np.random.uniform(20, 100, num_samples),
|
| 31 |
+
'Soil pH': np.random.uniform(4.5, 9.5, num_samples),
|
| 32 |
+
'Soil Type': np.random.choice(soil_types, num_samples),
|
| 33 |
+
'Nitrogen (N) Level': np.random.uniform(0, 150, num_samples),
|
| 34 |
+
'Phosphorus (P) Level': np.random.uniform(0, 100, num_samples),
|
| 35 |
+
'Potassium (K) Level': np.random.uniform(0, 200, num_samples),
|
| 36 |
+
'Season': np.random.choice(seasons, num_samples),
|
| 37 |
+
'Crop': np.random.choice(crops, num_samples)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Add some logical patterns based on real-world knowledge
|
| 41 |
+
df = pd.DataFrame(data)
|
| 42 |
+
|
| 43 |
+
# Adjust values based on crop preferences
|
| 44 |
+
for idx, row in df.iterrows():
|
| 45 |
+
crop = row['Crop']
|
| 46 |
+
|
| 47 |
+
# Temperature adjustments
|
| 48 |
+
if crop in ['Rice', 'Banana', 'Coconut']:
|
| 49 |
+
df.at[idx, 'Temperature (°C)'] = np.random.uniform(25, 40)
|
| 50 |
+
df.at[idx, 'Humidity (%)'] = np.random.uniform(60, 100)
|
| 51 |
+
elif crop in ['Wheat', 'Barley']:
|
| 52 |
+
df.at[idx, 'Temperature (°C)'] = np.random.uniform(10, 25)
|
| 53 |
+
elif crop in ['Chilli', 'Tomato']:
|
| 54 |
+
df.at[idx, 'Temperature (°C)'] = np.random.uniform(20, 35)
|
| 55 |
+
|
| 56 |
+
# Soil type adjustments
|
| 57 |
+
if crop in ['Cotton', 'Groundnut']:
|
| 58 |
+
df.at[idx, 'Soil Type'] = 'Black Cotton'
|
| 59 |
+
elif crop in ['Rice']:
|
| 60 |
+
df.at[idx, 'Soil Type'] = random.choice(['Clayey', 'Loamy'])
|
| 61 |
+
|
| 62 |
+
# Season adjustments
|
| 63 |
+
if crop in ['Rice', 'Maize', 'Cotton', 'Groundnut']:
|
| 64 |
+
df.at[idx, 'Season'] = 'Kharif (June-Oct)'
|
| 65 |
+
elif crop in ['Wheat', 'Barley', 'Chickpea']:
|
| 66 |
+
df.at[idx, 'Season'] = 'Rabi (Oct-Mar)'
|
| 67 |
+
elif crop in ['Watermelon', 'Cucumber']:
|
| 68 |
+
df.at[idx, 'Season'] = 'Zaid (Mar-Jun)'
|
| 69 |
+
|
| 70 |
+
# Add profit estimates (in INR per acre)
|
| 71 |
+
profit_ranges = {
|
| 72 |
+
'Rice': (25000, 50000),
|
| 73 |
+
'Maize': (20000, 45000),
|
| 74 |
+
'Cotton': (30000, 70000),
|
| 75 |
+
'Groundnut': (25000, 55000),
|
| 76 |
+
'Red Gram (Toor Dal)': (28000, 60000),
|
| 77 |
+
'Green Gram (Moong Dal)': (22000, 50000),
|
| 78 |
+
'Black Gram (Urad Dal)': (24000, 52000),
|
| 79 |
+
'Sunflower': (18000, 40000),
|
| 80 |
+
'Sugarcane': (35000, 75000),
|
| 81 |
+
'Turmeric': (40000, 90000),
|
| 82 |
+
'Chilli': (50000, 120000),
|
| 83 |
+
'Tomato': (30000, 80000),
|
| 84 |
+
'Onion': (25000, 65000),
|
| 85 |
+
'Mango': (60000, 150000),
|
| 86 |
+
'Banana': (50000, 120000),
|
| 87 |
+
'Coconut': (40000, 100000),
|
| 88 |
+
'Soybean': (22000, 48000),
|
| 89 |
+
'Jowar (Sorghum)': (18000, 40000),
|
| 90 |
+
'Bajra (Pearl Millet)': (15000, 35000)
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
df['Profit (INR/acre)'] = df['Crop'].apply(lambda x: random.randint(*profit_ranges[x]))
|
| 94 |
+
|
| 95 |
+
return df
|
| 96 |
+
|
| 97 |
+
# Generate the dataset
|
| 98 |
+
df = generate_synthetic_dataset(10000)
|
| 99 |
+
|
| 100 |
+
# Prepare data for ML model
|
| 101 |
+
X = df.drop(['Crop', 'Profit (INR/acre)'], axis=1)
|
| 102 |
+
X = pd.get_dummies(X) # Convert categorical variables to dummy variables
|
| 103 |
+
y = df['Crop']
|
| 104 |
+
|
| 105 |
+
# Train-test split
|
| 106 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 107 |
+
|
| 108 |
+
# Train Random Forest Classifier
|
| 109 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 110 |
+
model.fit(X_train, y_train)
|
| 111 |
+
|
| 112 |
+
# Crop precautions information
|
| 113 |
+
precautions_db = {
|
| 114 |
+
'Rice': [
|
| 115 |
+
"Maintain proper water level (5-10 cm) in the field",
|
| 116 |
+
"Control weeds through manual weeding or herbicides",
|
| 117 |
+
"Use balanced fertilizers (N:P:K = 100:50:50 kg/ha)",
|
| 118 |
+
"Watch for pests like stem borers and leaf folders"
|
| 119 |
+
],
|
| 120 |
+
'Maize': [
|
| 121 |
+
"Ensure proper spacing (60x20 cm)",
|
| 122 |
+
"Apply fertilizers in split doses",
|
| 123 |
+
"Control weeds during first 30-40 days",
|
| 124 |
+
"Watch for fall armyworm and use pheromone traps"
|
| 125 |
+
],
|
| 126 |
+
'Cotton': [
|
| 127 |
+
"Use drip irrigation for water efficiency",
|
| 128 |
+
"Monitor for pink bollworm regularly",
|
| 129 |
+
"Practice crop rotation to prevent pest buildup",
|
| 130 |
+
"Use recommended spacing (90x60 cm)"
|
| 131 |
+
],
|
| 132 |
+
'Groundnut': [
|
| 133 |
+
"Ensure well-drained soil to prevent fungal diseases",
|
| 134 |
+
"Apply gypsum at flowering stage (500 kg/ha)",
|
| 135 |
+
"Control weeds during first 45 days",
|
| 136 |
+
"Harvest at proper maturity to avoid pod loss"
|
| 137 |
+
],
|
| 138 |
+
'Red Gram (Toor Dal)': [
|
| 139 |
+
"Sow in rows with 45 cm spacing",
|
| 140 |
+
"Treat seeds with rhizobium culture",
|
| 141 |
+
"Provide protective irrigation during flowering",
|
| 142 |
+
"Watch for pod borer and apply neem oil"
|
| 143 |
+
],
|
| 144 |
+
'Tomato': [
|
| 145 |
+
"Use staking for better fruit quality",
|
| 146 |
+
"Practice crop rotation to avoid soil diseases",
|
| 147 |
+
"Monitor for fruit borer and whitefly",
|
| 148 |
+
"Harvest at breaker stage for longer shelf life"
|
| 149 |
+
],
|
| 150 |
+
'Chilli': [
|
| 151 |
+
"Raise seedlings in nursery for 35-40 days",
|
| 152 |
+
"Mulch to conserve soil moisture",
|
| 153 |
+
"Monitor for thrips and mites regularly",
|
| 154 |
+
"Harvest at regular intervals for higher yield"
|
| 155 |
+
],
|
| 156 |
+
# Default precautions for other crops
|
| 157 |
+
'Default': [
|
| 158 |
+
"Use recommended spacing for the crop",
|
| 159 |
+
"Monitor for pests and diseases regularly",
|
| 160 |
+
"Apply balanced fertilizers as per soil test",
|
| 161 |
+
"Ensure proper irrigation based on weather conditions"
|
| 162 |
+
]
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
# Function to get top precautions based on input features
|
| 166 |
+
def get_precautions(crop, temperature, rainfall, humidity, soil_type):
|
| 167 |
+
precautions = precautions_db.get(crop, precautions_db['Default'])
|
| 168 |
+
|
| 169 |
+
# Add weather-specific precautions
|
| 170 |
+
if temperature > 35:
|
| 171 |
+
precautions.append("Provide mulch to reduce soil temperature")
|
| 172 |
+
precautions.append("Increase irrigation frequency during hot days")
|
| 173 |
+
if rainfall < 50:
|
| 174 |
+
precautions.append("Use water conservation techniques like drip irrigation")
|
| 175 |
+
if humidity > 80:
|
| 176 |
+
precautions.append("Watch for fungal diseases and apply preventive sprays")
|
| 177 |
+
|
| 178 |
+
# Add soil-specific precautions
|
| 179 |
+
if soil_type == 'Black Cotton':
|
| 180 |
+
precautions.append("Practice deep ploughing to break soil hardpans")
|
| 181 |
+
elif soil_type == 'Sandy Loam':
|
| 182 |
+
precautions.append("Apply organic manure to improve water retention")
|
| 183 |
+
|
| 184 |
+
return precautions[:5] # Return top 5 precautions
|
| 185 |
+
|
| 186 |
+
# Function to predict crop and details
|
| 187 |
+
def predict_crop(temperature, rainfall, humidity, soil_ph, soil_type, nitrogen, phosphorus, potassium, season):
|
| 188 |
+
# Create input dataframe
|
| 189 |
+
input_data = {
|
| 190 |
+
'Temperature (°C)': [temperature],
|
| 191 |
+
'Rainfall (mm)': [rainfall],
|
| 192 |
+
'Humidity (%)': [humidity],
|
| 193 |
+
'Soil pH': [soil_ph],
|
| 194 |
+
'Nitrogen (N) Level': [nitrogen],
|
| 195 |
+
'Phosphorus (P) Level': [phosphorus],
|
| 196 |
+
'Potassium (K) Level': [potassium],
|
| 197 |
+
'Season': [season]
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
# Add soil type columns (one-hot encoding)
|
| 201 |
+
for st in ['Black Cotton', 'Red Sandy', 'Clayey', 'Loamy', 'Sandy Loam']:
|
| 202 |
+
input_data[f'Soil Type_{st}'] = [1 if soil_type == st else 0]
|
| 203 |
+
|
| 204 |
+
# Add season columns (one-hot encoding)
|
| 205 |
+
for s in ['Kharif (June-Oct)', 'Rabi (Oct-Mar)', 'Zaid (Mar-Jun)', 'Whole Year']:
|
| 206 |
+
input_data[f'Season_{s}'] = [1 if season == s else 0]
|
| 207 |
+
|
| 208 |
+
input_df = pd.DataFrame(input_data)
|
| 209 |
+
|
| 210 |
+
# Ensure columns are in same order as training data
|
| 211 |
+
input_df = input_df.reindex(columns=X.columns, fill_value=0)
|
| 212 |
+
|
| 213 |
+
# Predict crop
|
| 214 |
+
crop = model.predict(input_df)[0]
|
| 215 |
+
|
| 216 |
+
# Get profit range
|
| 217 |
+
profit = df[df['Crop'] == crop]['Profit (INR/acre)'].mean()
|
| 218 |
+
|
| 219 |
+
# Get precautions
|
| 220 |
+
precautions = get_precautions(crop, temperature, rainfall, humidity, soil_type)
|
| 221 |
+
|
| 222 |
+
# Get similar crops (top 3 alternatives)
|
| 223 |
+
probas = model.predict_proba(input_df)[0]
|
| 224 |
+
top3_idx = np.argsort(probas)[-3:][::-1]
|
| 225 |
+
similar_crops = [model.classes_[i] for i in top3_idx if model.classes_[i] != crop][:2]
|
| 226 |
+
|
| 227 |
+
# Prepare output
|
| 228 |
+
output = {
|
| 229 |
+
"Recommended Crop": crop,
|
| 230 |
+
"Expected Profit (INR per acre)": f"₹{int(profit):,}",
|
| 231 |
+
"Top Precautions": precautions,
|
| 232 |
+
"Alternative Crops": similar_crops,
|
| 233 |
+
"Best Season": season
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
return output
|
| 237 |
+
|
| 238 |
+
# Custom CSS for farmer-friendly interface
|
| 239 |
+
custom_css = """
|
| 240 |
+
/* Main container styling */
|
| 241 |
+
.agrismart-container {
|
| 242 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #e4efe9 100%);
|
| 243 |
+
border-radius: 15px;
|
| 244 |
+
padding: 20px;
|
| 245 |
+
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
|
| 246 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
/* Header styling */
|
| 250 |
+
.agrismart-header {
|
| 251 |
+
background: linear-gradient(to right, #4CAF50, #2E8B57);
|
| 252 |
+
color: white;
|
| 253 |
+
padding: 15px 20px;
|
| 254 |
+
border-radius: 10px;
|
| 255 |
+
text-align: center;
|
| 256 |
+
margin-bottom: 20px;
|
| 257 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
/* Input section styling */
|
| 261 |
+
.agrismart-input {
|
| 262 |
+
background-color: rgba(255, 255, 255, 0.9);
|
| 263 |
+
padding: 20px;
|
| 264 |
+
border-radius: 10px;
|
| 265 |
+
margin-bottom: 20px;
|
| 266 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
/* Output section styling */
|
| 270 |
+
.agrismart-output {
|
| 271 |
+
background-color: #ffffff;
|
| 272 |
+
padding: 20px;
|
| 273 |
+
border-radius: 10px;
|
| 274 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 275 |
+
border-left: 5px solid #4CAF50;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
/* Button styling */
|
| 279 |
+
.agrismart-button {
|
| 280 |
+
background: linear-gradient(to right, #4CAF50, #2E8B57) !important;
|
| 281 |
+
color: white !important;
|
| 282 |
+
border: none !important;
|
| 283 |
+
padding: 12px 25px !important;
|
| 284 |
+
border-radius: 8px !important;
|
| 285 |
+
font-size: 16px !important;
|
| 286 |
+
cursor: pointer !important;
|
| 287 |
+
transition: all 0.3s !important;
|
| 288 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
.agrismart-button:hover {
|
| 292 |
+
transform: translateY(-2px) !important;
|
| 293 |
+
box-shadow: 0 6px 8px rgba(0,0,0,0.15) !important;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
/* Slider styling */
|
| 297 |
+
.agrismart-slider .gr-slider {
|
| 298 |
+
background: #e0e0e0 !important;
|
| 299 |
+
height: 10px !important;
|
| 300 |
+
border-radius: 5px !important;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
.agrismart-slider .gr-slider .gr-slider-selection {
|
| 304 |
+
background: linear-gradient(to right, #4CAF50, #2E8B57) !important;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
/* Label styling */
|
| 308 |
+
.agrismart-label {
|
| 309 |
+
font-weight: bold !important;
|
| 310 |
+
color: #2E8B57 !important;
|
| 311 |
+
margin-bottom: 5px !important;
|
| 312 |
+
font-size: 16px !important;
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
/* Dropdown styling */
|
| 316 |
+
.agrismart-dropdown {
|
| 317 |
+
border: 1px solid #ddd !important;
|
| 318 |
+
border-radius: 8px !important;
|
| 319 |
+
padding: 8px 12px !important;
|
| 320 |
+
box-shadow: inset 0 1px 3px rgba(0,0,0,0.1) !important;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/* Result card styling */
|
| 324 |
+
.agrismart-result-card {
|
| 325 |
+
background: #f9f9f9;
|
| 326 |
+
border-radius: 10px;
|
| 327 |
+
padding: 15px;
|
| 328 |
+
margin: 10px 0;
|
| 329 |
+
border-left: 4px solid #4CAF50;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
.agrismart-result-title {
|
| 333 |
+
color: #2E8B57;
|
| 334 |
+
font-weight: bold;
|
| 335 |
+
margin-bottom: 10px;
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
.agrismart-result-value {
|
| 339 |
+
font-size: 18px;
|
| 340 |
+
color: #333;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
/* Precautions list styling */
|
| 344 |
+
.agrismart-precautions {
|
| 345 |
+
list-style-type: none;
|
| 346 |
+
padding-left: 0;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
.agrismart-precautions li {
|
| 350 |
+
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="%234CAF50"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>') no-repeat left center;
|
| 351 |
+
padding-left: 25px;
|
| 352 |
+
margin-bottom: 8px;
|
| 353 |
+
line-height: 1.5;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
/* Responsive design */
|
| 357 |
+
@media (max-width: 768px) {
|
| 358 |
+
.agrismart-container {
|
| 359 |
+
padding: 10px;
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
"""
|
| 363 |
+
|
| 364 |
+
# Function to format outputs
|
| 365 |
+
def format_outputs(output):
|
| 366 |
+
crop_md = f"**Recommended Crop:** {output['Recommended Crop']}"
|
| 367 |
+
profit_md = f"**Expected Profit (INR per acre):** {output['Expected Profit (INR per acre)']}"
|
| 368 |
+
season_md = f"**Best Season:** {output['Best Season']}"
|
| 369 |
+
alt_md = f"**Alternative Crops:** {', '.join(output['Alternative Crops'])}"
|
| 370 |
+
|
| 371 |
+
prec_html = """
|
| 372 |
+
<ul class="agrismart-precautions">
|
| 373 |
+
""" + "\n".join([f"<li>{p}</li>" for p in output['Top Precautions']]) + """
|
| 374 |
+
</ul>
|
| 375 |
+
"""
|
| 376 |
+
|
| 377 |
+
return crop_md, profit_md, prec_html, alt_md, season_md
|
| 378 |
+
|
| 379 |
+
# Create Gradio interface
|
| 380 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 381 |
+
with gr.Column(elem_classes="agrismart-container"):
|
| 382 |
+
with gr.Row(elem_classes="agrismart-header"):
|
| 383 |
+
gr.Markdown("""
|
| 384 |
+
# 🌱 AgriSmart: Smart Crop Advisor for Farmers
|
| 385 |
+
### Get personalized crop recommendations based on your farm conditions
|
| 386 |
+
""")
|
| 387 |
+
|
| 388 |
+
with gr.Row():
|
| 389 |
+
with gr.Column(elem_classes="agrismart-input"):
|
| 390 |
+
gr.Markdown("### 🌦️ Enter Your Farm Conditions", elem_classes="agrismart-label")
|
| 391 |
+
|
| 392 |
+
with gr.Row():
|
| 393 |
+
temperature = gr.Slider(10, 60, label="1. Temperature (How hot is your area?)",
|
| 394 |
+
info="Measure the air temperature in shade (°C)",
|
| 395 |
+
elem_classes="agrismart-slider")
|
| 396 |
+
rainfall = gr.Slider(0, 300, label="2. Rainfall (How much rain your area gets?)",
|
| 397 |
+
info="Annual rainfall in your area (mm)",
|
| 398 |
+
elem_classes="agrismart-slider")
|
| 399 |
+
|
| 400 |
+
with gr.Row():
|
| 401 |
+
humidity = gr.Slider(20, 100, label="3. Humidity (How moist is your air?)",
|
| 402 |
+
info="Relative humidity percentage (%)",
|
| 403 |
+
elem_classes="agrismart-slider")
|
| 404 |
+
soil_ph = gr.Slider(4, 10, label="4. Soil pH (Is your soil acidic or alkaline?)",
|
| 405 |
+
info="7 is neutral, below 7 is acidic, above 7 is alkaline",
|
| 406 |
+
elem_classes="agrismart-slider")
|
| 407 |
+
|
| 408 |
+
with gr.Row():
|
| 409 |
+
soil_type = gr.Dropdown(
|
| 410 |
+
["Black Cotton", "Red Sandy", "Clayey", "Loamy", "Sandy Loam"],
|
| 411 |
+
label="5. Soil Type (What type of soil do you have?)",
|
| 412 |
+
info="Black Cotton is common in Andhra/Telangana",
|
| 413 |
+
elem_classes="agrismart-dropdown"
|
| 414 |
+
)
|
| 415 |
+
season = gr.Dropdown(
|
| 416 |
+
["Kharif (June-Oct)", "Rabi (Oct-Mar)", "Zaid (Mar-Jun)", "Whole Year"],
|
| 417 |
+
label="6. Season (When will you cultivate?)",
|
| 418 |
+
elem_classes="agrismart-dropdown"
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
with gr.Row():
|
| 422 |
+
nitrogen = gr.Slider(0, 150, label="7. Nitrogen Level (N) in soil",
|
| 423 |
+
info="Essential for leaf growth (kg/ha)",
|
| 424 |
+
elem_classes="agrismart-slider")
|
| 425 |
+
phosphorus = gr.Slider(0, 100, label="8. Phosphorus Level (P) in soil",
|
| 426 |
+
info="Important for root development (kg/ha)",
|
| 427 |
+
elem_classes="agrismart-slider")
|
| 428 |
+
potassium = gr.Slider(0, 200, label="9. Potassium Level (K) in soil",
|
| 429 |
+
info="Helps in fruit quality (kg/ha)",
|
| 430 |
+
elem_classes="agrismart-slider")
|
| 431 |
+
|
| 432 |
+
submit_btn = gr.Button("Get Crop Recommendation", elem_classes="agrismart-button")
|
| 433 |
+
|
| 434 |
+
with gr.Column(elem_classes="agrismart-output"):
|
| 435 |
+
gr.Markdown("### 📊 Recommended Crop Details", elem_classes="agrismart-label")
|
| 436 |
+
|
| 437 |
+
with gr.Column(elem_classes="agrismart-result-card"):
|
| 438 |
+
crop = gr.Markdown("**Recommended Crop:** ", elem_classes="agrismart-result-value")
|
| 439 |
+
profit = gr.Markdown("**Expected Profit (INR per acre):** ", elem_classes="agrismart-result-value")
|
| 440 |
+
season_out = gr.Markdown("**Best Season:** ", elem_classes="agrismart-result-value")
|
| 441 |
+
alternatives = gr.Markdown("**Alternative Crops:** ", elem_classes="agrismart-result-value")
|
| 442 |
+
|
| 443 |
+
gr.Markdown("### 🛡️ Top Precautions", elem_classes="agrismart-result-title")
|
| 444 |
+
precautions = gr.HTML("""
|
| 445 |
+
<ul class="agrismart-precautions">
|
| 446 |
+
<li>Enter your farm details and click the button to get recommendations</li>
|
| 447 |
+
</ul>
|
| 448 |
+
""")
|
| 449 |
+
|
| 450 |
+
# Example images (would need actual images in production)
|
| 451 |
+
gr.Markdown("### 🌾 Common Crops in Andhra/Telangana")
|
| 452 |
+
gr.HTML("""
|
| 453 |
+
<div style="display: flex; flex-wrap: wrap; gap: 10px; justify-content: center;">
|
| 454 |
+
<div style="text-align: center;">
|
| 455 |
+
<div style="background: #e3f2fd; padding: 10px; border-radius: 10px; width: 100px;">
|
| 456 |
+
<div style="font-size: 40px;">🌾</div>
|
| 457 |
+
<div>Rice</div>
|
| 458 |
+
</div>
|
| 459 |
+
</div>
|
| 460 |
+
<div style="text-align: center;">
|
| 461 |
+
<div style="background: #e8f5e9; padding: 10px; border-radius: 10px; width: 100px;">
|
| 462 |
+
<div style="font-size: 40px;">🌽</div>
|
| 463 |
+
<div>Maize</div>
|
| 464 |
+
</div>
|
| 465 |
+
</div>
|
| 466 |
+
<div style="text-align: center;">
|
| 467 |
+
<div style="background: #fff3e0; padding: 10px; border-radius: 10px; width: 100px;">
|
| 468 |
+
<div style="font-size: 40px;">🧶</div>
|
| 469 |
+
<div>Cotton</div>
|
| 470 |
+
</div>
|
| 471 |
+
</div>
|
| 472 |
+
<div style="text-align: center;">
|
| 473 |
+
<div style="background: #f3e5f5; padding: 10px; border-radius: 10px; width: 100px;">
|
| 474 |
+
<div style="font-size: 40px;">🥜</div>
|
| 475 |
+
<div>Groundnut</div>
|
| 476 |
+
</div>
|
| 477 |
+
</div>
|
| 478 |
+
</div>
|
| 479 |
+
""")
|
| 480 |
+
|
| 481 |
+
# Define button click action
|
| 482 |
+
submit_btn.click(
|
| 483 |
+
fn=lambda temp, rain, hum, ph, soil, n, p, k, seas: format_outputs(
|
| 484 |
+
predict_crop(temp, rain, hum, ph, soil, n, p, k, seas)
|
| 485 |
+
),
|
| 486 |
+
inputs=[temperature, rainfall, humidity, soil_ph, soil_type, nitrogen, phosphorus, potassium, season],
|
| 487 |
+
outputs=[crop, profit, precautions, alternatives, season_out]
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
# Launch the application
|
| 491 |
+
if __name__ == "__main__":
|
| 492 |
+
demo.launch()
|